(ns auto-ap.views.components.invoices.side-bar
  (:require
   [auto-ap.routes :as routes]
   [auto-ap.subs :as subs]
   [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 invoice-number-filter [{:keys [data-page]}]
  [:div.field
   [:div.control [:input.input {:placeholder "AP-123"
                                :value @(re-frame/subscribe [::data-page/filter data-page :invoice-number-like])
                                :on-change (dispatch-value-change [::data-page/filter-changed data-page :invoice-number-like ])} ]]])

(defn invoices-side-bar [{:keys [data-page] :as params}]
  (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 :invoices)
                 :class [(active-when ap = :invoices)]}
        [:span {:class "icon icon-accounting-invoice-mail" :style {:font-size "25px"}}]
        [:span {:class "name"} "All Invoices"]]]
      [:li.menu-item
       [:a.item {:href (bidi/path-for routes/routes :unpaid-invoices)
                 :class [(active-when ap = :unpaid-invoices)]}
        [:span {:class "icon icon-accounting-invoice-mail" :style {:font-size "25px"}}]
        [:span {:class "name"} "Unpaid Invoices"]]]
      [:li.menu-item
       [:a.item {:href (bidi/path-for routes/routes :paid-invoices)
                 :class [(active-when ap = :paid-invoices)]}

        [:span {:class "icon icon-check-payment-give" :style {:font-size "25px"}}]
        
        [:span {:class "name"} "Paid Invoices"]]]
      [:li.menu-item
       [:a.item {:href (bidi/path-for routes/routes :voided-invoices)
                 :class [(active-when ap = :voided-invoices)]}

        [:span {:class "icon icon-bin-2" :style {:font-size "25px"}}]
        
        [:span {:class "name"} "Voided Invoices"]]]
      (when (or (= "admin" (:user/role user))
                (= "power-user" (:user/role user)))
        [:li.menu-item
         [:a.item {:href (bidi/path-for routes/routes :import-invoices)
                   :class [(active-when ap = :import-invoices)]}

          [:span {:class "icon icon-accounting-document" :style {:font-size "25px"}}]
          
          [:span {:class "name"} "Import Invoices"]]])]]
     (when (not= ap :import-invoices)
       [:div
        [: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 "Due Range"]
        [:div
         [date-range-filter
          {:on-change-event [::data-page/filter-changed data-page :due-range]
           :value @(re-frame/subscribe [::data-page/filter data-page :due-range])}]]

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

                                     
                                     )
                        :label "Scheduled payments"
                        :type "checkbox"}]]

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

        [:p.menu-label "Invoice #"]
        [:div 
         [invoice-number-filter params]]

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

        (when-let [exact-match-id @(re-frame/subscribe [::data-page/filter data-page :exact-match-id])]
          [:div
           [:p.menu-label "Specific Invoice"]
           [: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"}]]]
          )
        ])]))
