(ns auto-ap.views.pages.ledger.external-ledger-table
  (:require
   [auto-ap.subs :as subs]
   [auto-ap.views.components.grid :as grid]
   [auto-ap.views.pages.data-page :as data-page]
   [auto-ap.views.utils :refer [date->str nf]]
   [re-frame.core :as re-frame]))

(defn external-ledger-row [{{:keys [client vendor alternate-description date amount id source external-id line-items] :as i} :row
                   :keys [selected-client bank-accounts-by-id]}]
  [:<>
   [grid/row {:class (:class i) :id id :checkable? true :entity i}
    (when-not selected-client
      [grid/cell {} (:name client)])
    [grid/cell {} source ]
    [grid/cell {} external-id ]
    [grid/cell {} (if vendor
                    (:name vendor)
                    [:i.has-text-grey (or (when alternate-description (str "Bank Description: " alternate-description))
                                          "Unknown Merchant")])]
    [grid/cell {} (date->str date) ]
    [grid/cell {} ]
    [grid/cell {:class "has-text-right"} (nf amount )]
    [grid/cell {:class "has-text-right"} (nf amount )]]
   [:<>
    (for [{:keys [debit credit location account id running-balance]} line-items
          :let [account (or (bank-accounts-by-id (:id account))
                            account)]]
      ^{:key id}
      [grid/row {:checkable? false}
       (when-not selected-client
         [grid/cell {} ])
       [grid/cell {} ]
       [grid/cell {} ]
       [grid/cell {} ]
       [grid/cell {} ]
       [grid/cell {}
        (if (:name account)
                       [:span.has-tooltip-arrow.has-tooltip-right {:data-tooltip (str "Balance as of this entry: " (nf running-balance ))}
                        (str location ": " (:name account)) ]
                       [:i "unknown"])]
       [grid/cell {:class "has-text-right"} (when debit (nf debit ))]
       [grid/cell {:class "has-text-right"} (when credit (nf credit ))]])]])

(defn table [{:keys [data-page action-buttons]}]
  (let [{:keys [data params]} @(re-frame/subscribe [::data-page/page data-page])
        selected-client @(re-frame/subscribe [::subs/client])
        bank-accounts-by-id @(re-frame/subscribe [::subs/bank-accounts-by-id])]
    [grid/grid {:data-page data-page
                :check-boxes? true
                :column-count (if selected-client 7 8)}
     [grid/controls (assoc data :action-buttons action-buttons)]
     [grid/table {:fullwidth true :class ["wrappable"]}
      [grid/header
       [grid/row {:id  "header"
                  :entity params}
        (when-not selected-client
          [grid/sortable-header-cell {:sort-key "client" :sort-name "Client"} "Client"])
        [grid/sortable-header-cell {:sort-key "source" :sort-name "Source" :style {:width "10em"}} "Source"]
        [grid/sortable-header-cell {:sort-key "external-id" :sort-name "External Id"} "External Id"]
        [grid/sortable-header-cell {:sort-key "vendor" :sort-name "Vendor"} "Vendor"]
        [grid/sortable-header-cell {:sort-key "date" :sort-name "Date" :style {:width "8em"}} "Date"]
        [grid/header-cell {} "Account"]
        [grid/sortable-header-cell {:sort-key "amount" :sort-name "Amount" :class "has-text-right" :style {:width "7em"}} "Debit"]
        [grid/sortable-header-cell {:sort-key "amount" :sort-name "Amount" :class "has-text-right" :style {:width "7em"}} "Credit"]]]
      [grid/body
       (for [{:keys [id] :as i} (:data data)]
         ^{:key id}
         [external-ledger-row {:row i
                               :selected-client selected-client
                               :bank-accounts-by-id bank-accounts-by-id}])]]]))

