(ns auto-ap.views.pages.transactions.side-bar
  (:require
   [auto-ap.routes :as routes]
   [auto-ap.ssr-routes :as ssr-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.switch-field :refer [switch-field]]
   [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-event dispatch-value-change]]
   [bidi.bidi :as bidi]
   [re-frame.core :as re-frame]))

(defn 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
     [:div [:p.menu-label "Type"]
      [:ul.menu-list
       [:li.menu-item
        [:a.item {:href (bidi/path-for routes/routes :transactions)
                  :class [(active-when ap = :transactions)]}
         [:span {:class "icon" :style {:font-size "25px"}}]
         [:span {:class "name"} "All"]]]
       [:li.menu-item
        [:a.item {:href (bidi/path-for routes/routes :unapproved-transactions)
                  :class [(active-when ap = :unapproved-transactions)]}
         [:span {:class "icon icon-task-list-text-1" :style {:font-size "25px"}}]
         [:span {:class "name"} "Unapproved"]]]
       [:li.menu-item
        [:a.item {:href (bidi/path-for routes/routes :requires-feedback-transactions)
                  :class [(active-when ap = :requires-feedback-transactions)]}

         [:span {:class "icon icon-task-list-question" :style {:font-size "25px"}}]

         [:span {:class "name"} "Client Review"]]]
       [:li.menu-item
        [:a.item {:href (bidi/path-for routes/routes :approved-transactions)
                  :class [(active-when ap = :approved-transactions)]}

         [:span {:class "icon icon-task-list-check-1" :style {:font-size "25px"}}]

         [:span {:class "name"} "Approved"]]]
       [:li.menu-item
        [:a.item {:href (bidi/path-for routes/routes :excluded-transactions)
                  :class [(active-when ap = :excluded-transactions)]}

         [:span {:class "icon icon-task-list-disable" :style {:font-size "25px"}}]

         [:span {:class "name"} "Excluded"]]]
       (when @(re-frame/subscribe [::subs/is-admin?])
         [:li.menu-item
          [:a.item {:href  (bidi/path-for ssr-routes/only-routes :transaction-insights)
                    :class [(active-when ap = :transaction-insights)]}

           [:span {:class "icon icon-task-list-disable" :style {:font-size "25px"}}]

           [:span {:class "name"} "Insights"]]])]]
     (when client
       [:<>
        [:p.menu-label "Bank Account"]
        [:div
         [bank-account-filter
          {:on-change-event [::data-page/filter-changed data-page :bank-account]
           :value           @(re-frame/subscribe [::data-page/filter data-page :bank-account])
           :bank-accounts   @(re-frame/subscribe [::subs/bank-accounts])}]]])

     [: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 "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 "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 "Linking"]
     [:div.field.has-addons
      [:p.control [:a.button.is-small {:on-click
                                       #(re-frame/dispatch [::data-page/filter-changed data-page :linked-to nil])}
                   "All" ]]
      [:p.control [:a.button.is-small {:on-click
                                       #(re-frame/dispatch [::data-page/filter-changed data-page :linked-to :none])}
                   "None" ]]
      [:p.control [:a.button.is-small {:on-click
                                       #(re-frame/dispatch [::data-page/filter-changed data-page :linked-to :invoice])}
                   "Invoice" ]]
      [:p.control [:a.button.is-small {:on-click
                                       #(re-frame/dispatch [::data-page/filter-changed data-page :linked-to :expected-deposit])}
                   "Expected Deposit"]]
      [:p.control [:a.button.is-small {:on-click
                                       #(re-frame/dispatch [::data-page/filter-changed data-page :linked-to :payment])}
                   "Payment"]]]


     [: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])}]]]

     [:p.menu-label "Description"]
     [:div
      [:div.field
       [:div.control [:input.input {:placeholder "CHECK 123 ABC"
                                    :value @(re-frame/subscribe [::data-page/filter data-page :description])
                                    :on-change (dispatch-value-change [::data-page/filter-changed data-page :description])}]]]]
     (when-let [exact-match-id @(re-frame/subscribe [::data-page/filter data-page :exact-match-id])]
       [:div
        [:p.menu-label "Specific Transaction"]
        [:span.tag.is-medium exact-match-id " "
         [:button.delete.is-small {:on-click
                                   (dispatch-event [::data-page/filter-changed data-page :exact-match-id nil])}]]])

     (when (= "admin" (:user/role user))
       [:<>

        [:p.menu-label "Admin only"]
        [:div
         [switch-field {:id "unresolved-only"
                        :checked (boolean @(re-frame/subscribe [::data-page/filter data-page :unresolved]))
                        :on-change (fn [e]
                                     (re-frame/dispatch [::data-page/filter-changed data-page :unresolved (.-checked (.-target e))]))
                        :label "Unresolved only"
                        :type "checkbox"}]]

        (when-let [import-batch-id @(re-frame/subscribe [::data-page/filter data-page :import-batch-id])]
          [:div
           [:p.menu-label "Import Batch"]
           [:span.tag.is-medium import-batch-id " "
            [:button.delete.is-small {:on-click
                                      (dispatch-event [::data-page/filter-changed data-page :import-batch-id nil])}]]])

        [:div
         [switch-field {:id "potentially-duplicate"
                        :checked (boolean @(re-frame/subscribe [::data-page/filter data-page :potential-duplicates]))
                        :on-change (fn [e]
                                     (re-frame/dispatch [::data-page/filter-changed data-page :potential-duplicates (.-checked (.-target e))]))
                        :label "Same Amount + Date"
                        :type "checkbox"}]]])]))

