(ns auto-ap.views.pages.pos.sales-orders
  (:require
   [auto-ap.forms :as forms]
   [auto-ap.subs :as subs]
   [auto-ap.views.components.layouts
    :refer [appearing-side-bar side-bar-layout]]
   [auto-ap.views.pages.data-page :as data-page]
   [auto-ap.views.pages.pos.form :as form]
   [auto-ap.views.pages.pos.side-bar :as side-bar]
   [auto-ap.views.pages.pos.table :as table]
   [auto-ap.views.utils :refer [with-user standard date->str]]
   [cljs-time.core :as time]
   [clojure.set :as set]
   [re-frame.core :as re-frame]
   [reagent.core :as reagent]
   [vimsical.re-frame.fx.track :as track]))

(re-frame/reg-event-fx
 ::params-change
 [with-user]
 (fn [{:keys [user]}[_ params]]
   {:graphql {:token user
              :owns-state {:single [::data-page/page ::page]}
              :query-obj {:venia/queries [[:sales_order_page
                                           {:start (:start params 0)
                                            :sort (:sort params)
                                            :per-page (:per-page params)
                                            :type-name (:type-name params)
                                            :category (:category params)
                                            :total-gte (:amount-gte (:total-range params))
                                            :total-lte (:amount-lte (:total-range params))
                                            :date-range (:date-range params)
                                            :processor (some-> (:processor params) keyword)
                                            :client-id (:id @(re-frame/subscribe [::subs/client]))}
                                           [[:sales-orders [:id :source :total :tax :tip :reference-link :discount :service-charge :returns :date
                                                            [:charges [:type-name :note :reference-link :total :processor :id [:expected-deposit [:id]] ]]
                                                            [:line-items [:item-name :total :category]]
                                                            [:client [:name :id]]]]
                                            :total
                                            :start
                                            :end
                                            :sales_order_total
                                            :sales_order_tax]]]} 
              :on-success (fn [result]
                            (let [result (set/rename-keys (:sales-order-page result)
                                                          {:sales-orders :data})]
                              [::data-page/received ::page result]))}}))


(re-frame/reg-event-fx
 ::unmounted
 (fn [_ _]
   {:dispatch [::data-page/dispose ::page]
    ::track/dispose {:id ::params}}))

(re-frame/reg-event-fx
 ::mounted
 (fn [{:keys [db]} _]
   {:db (assoc-in db [::data-page/settled-filters ::page :date-range] {:start (date->str (time/plus (time/now) (time/days -3))
                                                                                         standard)})
    ::track/register {:id ::params
                      :subscription [::data-page/params ::page]
                      :event-fn (fn [params]
                                  [::params-change params])}}))

(defn content []
  [:div
   [:h1.title "Sales Orders"]
   [table/table {:id :sales-orders
                 :data-page ::page}]])

(defn sales-orders-page []
  (reagent/create-class
   {:display-name "sales-orders-page"
    :component-will-unmount #(re-frame/dispatch-sync [::unmounted])
    :component-did-mount #(re-frame/dispatch [::mounted])
    :reagent-render
    (fn []
      (let [{form-active? :active?} @(re-frame/subscribe [::forms/form ::form/form])]
        [side-bar-layout {:side-bar [side-bar/side-bar {:data-page ::page}]
                          :main [content]
                          :right-side-bar [appearing-side-bar {:visible? form-active?}
                                           [form/form {}]]}]))}))

