;; Orders app — declarative page definitions
;; All data fetching via (service ...) IO primitives, no Python helpers.

;; ---------------------------------------------------------------------------
;; Orders list
;; ---------------------------------------------------------------------------

(defpage orders-list
  :path "/"
  :auth :public
  :data (service "orders-page" "list-page-data"
          :search (or (request-arg "search") "")
          :page (or (request-arg "page" "1") "1"))
  :layout (:orders
    :list-url (str (route-prefix) (url-for "defpage_orders_list")))
  :filter (~order-list-header
            :search-mobile (~search-mobile
              :current-local-href "/"
              :search (or search "")
              :search-count (or search-count "")
              :hx-select "#main-panel"
              :search-headers-mobile "{\"X-Origin\":\"search-mobile\",\"X-Search\":\"true\"}"))
  :aside (~search-desktop
           :current-local-href "/"
           :search (or search "")
           :search-count (or search-count "")
           :hx-select "#main-panel"
           :search-headers-desktop "{\"X-Origin\":\"search-desktop\",\"X-Search\":\"true\"}")
  :content (let* ((pfx (route-prefix))
                  (detail-url-raw (str pfx (url-for "defpage_order_detail" :order-id 0)))
                  (detail-prefix (slice detail-url-raw 0 (- (len detail-url-raw) 2)))
                  (rows-url (str pfx (url-for "orders.orders_rows"))))
             (~orders-list-content
               :orders orders
               :page page
               :total-pages total-pages
               :rows-url rows-url
               :detail-url-prefix detail-prefix)))

;; ---------------------------------------------------------------------------
;; Order detail
;; ---------------------------------------------------------------------------

(defpage order-detail
  :path "/<int:order_id>/"
  :auth :public
  :data (service "orders-page" "detail-page-data" :order-id order-id)
  :layout (:order-detail
    :list-url (str (route-prefix) (url-for "defpage_orders_list"))
    :detail-url (str (route-prefix) (url-for "defpage_order_detail" :order-id order-id)))
  :filter (let* ((pfx (route-prefix)))
            (~order-detail-filter-content
              :order order
              :list-url (str pfx (url-for "defpage_orders_list"))
              :recheck-url (str pfx (url-for "orders.order.order_recheck" :order-id order-id))
              :pay-url (str pfx (url-for "orders.order.order_pay" :order-id order-id))
              :csrf (csrf-token)))
  :content (~order-detail-content
              :order order
              :calendar-entries calendar-entries))
