(ns auto-ap.views.pages.admin.plaid.table
  (:require
   [auto-ap.status :as status]
   [auto-ap.subs :as subs]
   [auto-ap.views.components.buttons :as buttons]
   [auto-ap.views.components.grid :as grid]
   [auto-ap.views.components.modal :as modal]
   [auto-ap.views.pages.data-page :as data-page]
   [auto-ap.views.utils
    :refer [->$ action-cell-width date->str dispatch-event with-user]]
   [re-frame.core :as re-frame]))

(re-frame/reg-event-fx
 ::plaid-item-deleted
 (fn [_ _]
   {:dispatch [::modal/modal-closed ]}))

(re-frame/reg-event-fx
 ::delete-plaid-item
 [with-user ]
 (fn [{:keys [user]} [_ id ]]
   {:graphql {:token user
              :owns-state {:single ::delete-plaid-item}
              :query-obj
              {:venia/operation {:operation/type :mutation
                                 :operation/name "DeletePlaidItem"}
               :venia/queries [{:query/data
                                [:delete-plaid-item
                                 {:id id}
                                 [:message]]}]}
              :on-success [::plaid-item-deleted]}}))

(re-frame/reg-event-fx
 ::delete-requested
 [with-user]
 (fn [_ [_ id]]
   {:dispatch 
    [::modal/modal-requested {:title "Delete Provider account "
                              :body [:div "Are you sure you want to delete " id "?"]
                              :confirm {:value "Delete plaid accounts"
                                        :status-from [::status/single ::delete-plaid-item]
                                        :class "is-danger"
                                        :on-click (dispatch-event [::delete-plaid-item id])
                                        :close-event [::status/completed ::delete-plaid-item]}
                              :cancel? true}]}))

(re-frame/reg-sub
 ::params
 (fn [db]
   (-> db ::params)))

(defn table [{:keys [data-page]}]
  (let [{:keys [data]} @(re-frame/subscribe [::data-page/page data-page])
        is-admin? @(re-frame/subscribe [::subs/is-admin?])]
    [grid/grid {:data-page data-page
                :column-count 5}
     [grid/controls data]
     [grid/table {:fullwidth true}
      [grid/header
       [grid/row {}
        [grid/header-cell {:style {:width "18em"}} "Provider Account"]
        [grid/header-cell {:style {:width "12em"}} "Status"]
        [grid/header-cell {:style {:width "12em"}} "Last Updated"]
        [grid/header-cell {} "Accounts"]
        [grid/header-cell {:style {:width (action-cell-width 1)}} ]]]
      [grid/body
       (for [{:keys [id name accounts status last-updated] :as c} (:data data)]
         ^{:key (str name "-" id )}
         [grid/row {:class (:class c) :id id}
          [grid/cell {} id]
          [grid/cell {} status]
          [grid/cell {} (date->str last-updated)]
          [grid/cell {}
           [:ul
            (for [a accounts]
              ^{:key (:id a)}
              [:li (:name a) [:div.tag (->$ (:balance a))]])]]
          [grid/cell {}
           [:div.buttons 
            (when is-admin?
              [buttons/fa-icon {:event [::delete-requested (:id c)]
                                :icon  "fa-times"}])]]])]]]))
