(ns auto-ap.views.components.bank-account-filter
  (:require
   [auto-ap.views.utils :refer [->$]]
   [auto-ap.subs :as subs]
   [re-frame.core :as re-frame]))

(defn bank-account-filter [{:keys [on-change-event value bank-accounts]}]
  (let [is-power-user? (or @(re-frame/subscribe [::subs/is-power-user?])
                           @(re-frame/subscribe [::subs/is-admin?]))]
    [:nav.panel
     (for [ba bank-accounts
           :when (not= :cash (:type ba))]
       ^{:key (:id ba)}
       [:a.panel-block {:class (when (= (:id value)
                                        (:id ba))
                                 "is-active")
                        :on-click (fn []
                                    (if (= (:id value)
                                           (:id ba))
                                      (re-frame/dispatch (conj on-change-event nil ))
                                      (re-frame/dispatch (conj on-change-event (select-keys ba #{:name :id})))))}
        [:span.panel-icon
         (cond 
           (#{:check ":check"} (:type ba)) [:span.icon-check-payment-sign]

           (#{:credit ":credit"} (:type ba))
           [:span.icon-credit-card-1]

           :else
           [:i.fa.fa-icon-check])
         ]
        (:name ba) (when (and (:current-balance ba)
                              is-power-user?)
                     (str ": " (->$ (:current-balance ba))))

        (when (and (:yodlee-balance-old ba)
                   is-power-user?)
          [:<> 
           [:div.tag [:img {:src "/img/yodlee.png" :style {:width "16px" :height "16px"}}]
            (->$ (:yodlee-balance-old ba 0.00))]])])]))
