(ns auto-ap.views.pages.admin.accounts.table
  (:require
   [auto-ap.views.components.buttons :as buttons]
   [auto-ap.views.components.grid :as grid]
   [auto-ap.views.pages.admin.accounts.form :as account-form]
   [auto-ap.views.pages.data-page :as data-page]
   [auto-ap.views.utils :refer [action-cell-width]]
   [re-frame.core :as re-frame]))

(defn accounts-table [{:keys [data-page]}]
  (let [{:keys [data]} @(re-frame/subscribe [::data-page/page data-page])]

    [:div
     [grid/grid {:data-page data-page
                 :column-count 5}
      [grid/controls data]
      [grid/table {:fullwidth true}
       [grid/header
        [grid/row {}
         [grid/header-cell {} "Code"]
         [grid/header-cell {} "Name"]
         [grid/header-cell {} "Type"]
         [grid/header-cell {} "Location"]
         [grid/header-cell {:style {:width (action-cell-width 1)}} ]]]
       [grid/body
        (for [{:keys [id numeric-code name type location] :as account} (:data data)]
          ^{:key id}
          [grid/row {:class (:class account) :id id}
           [grid/cell {} numeric-code]
           [grid/cell {} name]
           [grid/cell {} type]
           [grid/cell {} location]
           [grid/cell {}
            [buttons/fa-icon {:event [::account-form/editing account [::edit-completed]]
                              :icon "fa-pencil"}]]])]]]]))
