
(ns auto-ap.views.pages.pos.expected-deposits.table
  (:require [auto-ap.subs :as subs]
            [auto-ap.views.components.buttons :as buttons]            [auto-ap.views.components.grid :as grid]
            [auto-ap.views.pages.data-page :as data-page]
            [auto-ap.views.pages.pos.form :as form]
            [auto-ap.views.utils :refer [date->str nf]]
            [clojure.string :as str]
            [re-frame.core :as re-frame]))

(defn row [{sales-order :sales-order
            selected-client :selected-client}]
  (let [{:keys [client location date total fee id]} sales-order]
    [grid/row {:class (:class sales-order) :id id}
     (when-not selected-client
       [grid/cell {} (:name client)])
     [grid/cell  location ]
     [grid/cell {} (date->str date) ]
     [grid/cell {:class "has-text-right"} (nf total )]
     [grid/cell {:class "has-text-right"} (nf fee )]
     
[grid/button-cell {}
      [:div.buttons 
       [buttons/fa-icon {:event [::form/editing sales-order] :icon "fa-pencil"}]]]]))

(defn table [{:keys [data-page]}]
  (let [selected-client @(re-frame/subscribe [::subs/client])
        {:keys [data status]} @(re-frame/subscribe [::data-page/page data-page])]
    [grid/grid {:data-page data-page
                :column-count (if selected-client 7 8)}
     [grid/controls data]
     [grid/table {:fullwidth true}
      [grid/header {}
       [grid/row {}
        (when-not selected-client
          [grid/sortable-header-cell {:sort-key "client" :sort-name "Client"} "Client"])
        [grid/sortable-header-cell {:sort-key "location" :sort-name "Location" :style {:width "7em"}} "Location"]
        [grid/sortable-header-cell {:sort-key "date" :sort-name "Date" :style {:width "8em"}} "Date"]
        [grid/sortable-header-cell {:sort-key "total" :sort-name "Total" :class "has-text-right" :style {:width "8em"}} "Total"]
        [grid/sortable-header-cell {:sort-key "fee" :sort-name "Fee" :class "has-text-right" :style {:width "7em"}} "Fee"]
        
        [grid/header-cell {:style {:width "4em"}}]]]
      [grid/body
       (for [sales-order (:data data)]
         ^{:key (:id sales-order)}
         [row {:sales-order sales-order
               :selected-client selected-client}])]]]))
