(ns auto-ap.views.pages.pos.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 date total tax tip charges line-items id]} sales-order]
    [grid/row {:class (:class sales-order) :id id}
     (when-not selected-client
       [grid/cell {} (:name client)])
     [grid/cell {} (date->str date)]
     [grid/cell {:class "has-text-right"} (nf total)]
     [grid/cell {:class "has-text-right"} (nf tax)]
     [grid/cell {:class "has-text-right"} (nf tip)]
     [grid/cell {}
      [:div.level
       [:div.level-left 
        (for [charge charges]

          (with-meta
            (condp = (:processor charge)
              :grubhub
              [:img.level-item {:src "/img/grubhub.png" :style {:width "24px" :height "24px"}}]

              :doordash
              [:img.level-item {:src "/img/doordash.png" :style {:width "24px" :height "24px"}}]

              :uber-eats
              [:img.level-item {:src "/img/ubereats.png" :style {:width "24px" :height "24px"}}]

              (condp = (:type-name charge)
                "CASH"
                [:span.icon.level-item {:style {:font-size "24px"}} [:span  {:class "icon-accounting-bill" :style {:font-weight "400"}}]]

                "CARD"
                [:span.icon.level-item {:style {:font-size "24px"}} [:span  {:class "icon-credit-card-1" :style {:font-weight "400"}}]]

                "SQUARE_GIFT_CARD"
                [:span.icon.level-item {:style {:font-size "24px"}} [:span  {:class "icon-gift-box" :style {:font-weight "400"}}]]

                [:span.level-item "Other (" (:type-name charge) ")"]))
                
            {:key (:id charge)}))]]]
      
      
     [grid/cell {} (str/join ", " (map :item-name line-items))]
     [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
      [:div.level-item 
       [:div.tag "Total: " (nf (:sales-order-total data))] 
         ]
      [:div.level-item
       [:div.tag " Tax: " (nf (:sales-order-tax 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 "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 "tax" :sort-name "Tax" :class "has-text-right" :style {:width "7em"}} "Tax"]
        [grid/sortable-header-cell {:sort-key "tip" :sort-name "Tip" :class "has-text-right" :style {:width "7em"}} "Tip"]
        [grid/header-cell  {} "Payment Methods"]
        [grid/header-cell  {} "Line Items"]
        [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}])]]]))
