(ns auto-ap.views.pages.pos.form
  (:require 
            [auto-ap.events :as events]
            [auto-ap.forms :as forms]
            [auto-ap.subs :as subs]
            [auto-ap.utils :refer [dollars=]]
            [auto-ap.views.components.dropdown :refer [drop-down]]
            [auto-ap.views.components.expense-accounts-field :as expense-accounts-field :refer [expense-accounts-field recalculate-amounts]]
            [auto-ap.views.components.layouts :as layouts]
            [auto-ap.views.components.money-field :refer [money-field]]
            [auto-ap.views.components.typeahead :refer [typeahead-v3]]
            [auto-ap.status :as status]
            [auto-ap.views.utils :refer [date->str date-picker dispatch-event standard with-user]]
            [cljs-time.core :as c]
            [clojure.spec.alpha :as s]
            [clojure.string :as str]
            [re-frame.core :as re-frame]))
(re-frame/reg-sub
 ::can-submit
 :<- [::forms/form ::form]
 (fn [{:keys [data status]} _]
   false))

(re-frame/reg-event-db
 ::editing
 (fn [db [_ which]]
   (let [which (update which :date #(date->str % standard))]
     (forms/start-form db ::form which))))

(def sales-order-form (forms/vertical-form {:can-submit [::can-submit]
                                        :change-event [::forms/changed]
                                        :submit-event [::saving ]
                                        :id ::form}))

(defn form [{:keys [can-change-amount?] :as params}]
  [layouts/side-bar {:on-close   (dispatch-event [::forms/form-closing ::form ])}
   (let [{:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form])
         {:keys [form-inline field raw-field error-notification submit-button ]} sales-order-form]
     (with-meta 
       (form-inline (assoc params :title "Sales order") 
                    [:<>
                     (when-not @(re-frame/subscribe [::subs/client])
                       (field [:span "Client"
                               [:span.has-text-danger " *"]]
                              [typeahead-v3 {:entities @(re-frame/subscribe [::subs/clients])
                                             :entity->text :name
                                             :type "typeahead-v3"
                                             :field [:client]
                                             :disabled true}]))


                     (field "Date"
                            [date-picker {:class-name "input"
                                          :class "input"
                                          :type "date"
                                          :disabled true
                                          :field [:date]}])
                     (field "Total"
                            [money-field {:type "money"
                                          :field [:total]
                                          :disabled true}])
                     (field "Tax"
                            [money-field {:type "money"
                                          :field [:tax]
                                          :disabled true}])
                     (field "Discount"
                            [money-field {:type "money"
                                          :field [:discount]
                                          :disabled true}])

                     (field "Returns"
                            [money-field {:type "money"
                                          :field [:returns]
                                          :disabled true}])

                     (field "Service Charge"
                            [money-field {:type "money"
                                          :field [:service-charge]
                                          :disabled true}])

                     (field "Tip"
                            [money-field {:type "money"
                                          :field [:tip]
                                          :disabled true}])

                     [:h1.subtitle.is-4 "Charges"]
                     [:ul 
                      (for [charge (:charges data)]
                        [:li (:type-name charge) ": " (:total charge)])]

                     [:h1.subtitle.is-4 "Line Items"]
                     [:ul 
                      (for [line-item (:line-items data)]
                        [:li (:item-name line-item) ": " (:total line-item) [:span.tag (:category line-item)]])]])
       {:key (:id data)}))])

