(ns auto-ap.views.pages.ledger.side-bar
  (:require
   [auto-ap.routes :as routes]
   [auto-ap.subs :as subs]
   [auto-ap.views.components.bank-account-filter
    :refer [bank-account-filter]]
   [auto-ap.views.components.date-range-filter :refer [date-range-filter]]
   [auto-ap.views.components.number-filter :refer [number-filter]]
   [auto-ap.views.components.typeahead.vendor
    :refer [search-backed-typeahead]]
   [auto-ap.views.pages.data-page :as data-page]
   [auto-ap.views.utils :refer [active-when dispatch-value-change]]
   [bidi.bidi :as bidi]
   [re-frame.core :as re-frame]))

(defn ledger-side-bar [{:keys [data-page]}]
  (let [ap @(re-frame/subscribe [::subs/active-page])
        user @(re-frame/subscribe [::subs/user])
        client @(re-frame/subscribe [::subs/client])]
    [:div
     [:ul.menu-list
      [:li.menu-item
       [:a.item {:href (bidi/path-for routes/routes :ledger)
                 :class [(active-when ap = :ledger)]}
        [:span {:class "icon icon-receipt" :style {:font-size "25px"}}]
        [:span {:class "name"} "Register"]]]
      [:li.menu-item
       [:a.item {:href (bidi/path-for routes/routes :profit-and-loss)
                 :class [(active-when ap = :profit-and-loss)]}

        [:span {:class "icon icon-performance-increase-1" :style {:font-size "25px"}}]
        [:span {:class "name"} "Profit & Loss"]]]
      [:li.menu-item
       [:a.item {:href  (bidi/path-for routes/routes :profit-and-loss-detail)
                 :class [(active-when ap = :profit-and-loss-detail)]}

        [:span {:class "icon  icon-performance-graph-calculator" :style {:font-size "25px"}}]
        [:span {:class "name"} "Profit & Loss Detail"]]]
      [:li.menu-item
       [:a.item {:href  (bidi/path-for routes/routes :cash-flows)
                 :class [(active-when ap = :cash-flows)]}

        [:span {:class "icon icon-money-wallet-open" :style {:font-size "25px"}}]
        [:span {:class "name"} "Cash Flows"]]]
      [:li.menu-item
       [:a.item {:href (bidi/path-for routes/routes :balance-sheet)
                 :class [(active-when ap = :balance-sheet)]}

        [:span {:class "icon icon-accounting-abacus" :style {:font-size "25px"}}]
        [:span {:class "name"} "Balance Sheet"]]]
      (when (= "admin" (:user/role user))
        [:li.menu-item
         [:a.item {:href (bidi/path-for routes/routes :external-ledger)
                   :class [(active-when ap = :external-ledger)]}
          [:span {:class "icon icon-receipt" :style {:font-size "25px"}}]
          [:span {:class "name"} "External Ledger"]]])
      (when (= "admin" (:user/role user))
        [:li.menu-item
         [:a.item {:href (bidi/path-for routes/routes :external-import-ledger)
                   :class [(active-when ap = :external-import-ledger)]}
          [:span.icon [:i {:class "fa fa-download"}]]
          [:span {:class "name"} "External Ledger Import"]]])]
     (when (#{ :ledger :external-ledger} ap)
        [:div
         (when client 
           [:<> 
            [:p.menu-label "Bank Account"]
            [bank-account-filter {:value           @(re-frame/subscribe [::data-page/filter data-page :bank-account])
                                  :bank-accounts   @(re-frame/subscribe [::subs/bank-accounts])
                                  :on-change-event [::data-page/filter-changed data-page :bank-account]}]])

         

         [:p.menu-label "Financial Account"]
         [:div
          [search-backed-typeahead {:search-query (fn [i]
                                                    [:search_account
                                                     {:query i
                                                      :client-id (:id client)}
                                                     [:name :id]])
                                    :entity->text   (fn [x ] (str (:numeric-code x) " - " (:name x)))
                                    :type           "typeahead-v3"
                                    :on-change      #(re-frame/dispatch [::data-page/filter-changed data-page :account (some-> % (select-keys [:name :id :numeric-code]))])
                                    :value          @(re-frame/subscribe [::data-page/filter data-page :account])}]]

         [:p.menu-label "Vendor"]
         [:div
          [search-backed-typeahead {:search-query (fn [i]
                                                    [:search_vendor
                                                     {:query i}
                                                     [:name :id]])
                                    :type         "typeahead-v3"
                                    :on-change    #(re-frame/dispatch [::data-page/filter-changed data-page :vendor %])
                                    :value        @(re-frame/subscribe [::data-page/filter data-page :vendor])}]]

         [:p.menu-label "Date Range"]
         [:div
          [date-range-filter
           {:on-change-event [::data-page/filter-changed data-page :date-range]
            :value @(re-frame/subscribe [::data-page/filter data-page :date-range])}]]
         [:p.menu-label "Amount"]
         [:div
          [number-filter
           {:on-change-event [::data-page/filter-changed data-page :amount-range]
            :value @(re-frame/subscribe [::data-page/filter data-page :amount-range])}]]

         [:p.menu-label "Location"]
         [:div.field
          [:div.control [:input.input {:placeholder "SC"
                                       :style {:width "3em"}
                                       :value @(re-frame/subscribe [::data-page/filter data-page :location])
                                       :on-change (dispatch-value-change [::data-page/filter-changed data-page :location ])} ]]]

         (when (= :external-ledger ap)
           [:<> 
            [:p.menu-label "Source"]
            [:div
             [:div.field
              [:div.control [:input.input {:placeholder "Source"
                                           :value       @(re-frame/subscribe [::data-page/filter data-page :source])
                                           :on-change   (dispatch-value-change [::data-page/filter-changed data-page :source])} ]]]]

            [:p.menu-label "External ID"]
            [:div
             [:div.field
              [:div.control [:input.input {:placeholder "External id"
                                           :value @(re-frame/subscribe [::data-page/filter data-page :external-id])
                                           :on-change (dispatch-value-change [::data-page/filter-changed data-page :external-id])} ]]]]])])]))
