(ns auto-ap.views.pages.ledger
  (:require [auto-ap.events :as events]
            [auto-ap.forms :as forms]
            [auto-ap.subs :as subs]
            [auto-ap.views.components.bank-account-filter :refer [bank-account-filter]]
            [auto-ap.views.components.layouts :refer [appearing-side-bar side-bar-layout]]
            [auto-ap.routes :as routes]
            [bidi.bidi :as bidi]
            [auto-ap.views.components.paginator :refer [paginator]]
            [auto-ap.views.components.sorter :refer [sorted-column]]
            [auto-ap.views.pages.ledger.table :as table]
            [auto-ap.views.pages.ledger.side-bar :refer [ledger-side-bar] :as side-bar]
            [auto-ap.views.pages.transactions.common :refer [transaction-read]]

            [auto-ap.utils :refer [replace-by]]
            [auto-ap.views.pages.transactions.manual :as manual]
            [auto-ap.views.utils :refer [bind-field date->str dispatch-event nf active-when with-user]]
            [goog.string :as gstring]
            [re-frame.core :as re-frame]
            [reagent.core :as reagent]
            [auto-ap.status :as status]
            [vimsical.re-frame.fx.track :as track]
            [auto-ap.views.pages.data-page :as data-page]
            [clojure.set :as set]))
(defn data-params->query-params [params]
  {:start (:start params 0)
   :sort (:sort params)
   :per-page (:per-page params)
   :client-id (:id @(re-frame/subscribe [::subs/client]))
   :vendor-id (:id (:vendor params))
   :date-range (:date-range params)
   :account-id (:id (:account params))
   :location (:location params)
   :bank-account-id (:id (:bank-account params))
   :amount-gte (:amount-gte (:amount-range params))
   :amount-lte (:amount-lte (:amount-range params))})


(re-frame/reg-event-fx
 ::params-change
 [with-user]
 (fn [{:keys [user db]} [_ params]]
   {:graphql {:token user
              :owns-state {:single [::data-page/page ::page]}
              :query-obj {:venia/queries [[:ledger-page
                                           {:filters (data-params->query-params params)}
                                           [[:journal-entries [:id
                                                               :source
                                                               :original-entity
                                                               :amount
                                                               :note
                                                               :cleared-against
                                                               :alternate-description
                                                               [:vendor
                                                                [:name :id]]
                                                               [:client
                                                                [:name :id]]
                                                               [:line-items
                                                                [:id :debit :credit :location :running-balance
                                                                 [:account [:id]]]]
                                                               :date]]
                                            :total
                                            :start
                                            :end]]]} 
              :on-success (fn [result]
                            [::data-page/received ::page (set/rename-keys (:ledger-page result)
                                                                          {:journal-entries :data})])}}))

(re-frame/reg-event-fx
 ::unmounted
 (fn [{:keys [db]} _]
   {:dispatch [::data-page/dispose ::page]
    ::track/dispose {:id ::params}}))

(re-frame/reg-event-fx
 ::mounted
 (fn [{:keys [db]} _]
   {::track/register {:id ::params
                      :subscription [::data-page/params ::page]
                      :event-fn (fn [params] [::params-change params])}}))



(defn ledger-content []
  (let [current-client @(re-frame/subscribe [::subs/client])]
    [:div 
     [:h1.title "Ledger"]
     [table/table {:id :ledger
                   :data-page ::page}]]))


(defn ledger-page []
  (let [user (re-frame/subscribe [::subs/user])]
    (reagent/create-class
     {:display-name "ledger-page"
      :component-did-mount #(re-frame/dispatch [::mounted])
      :component-will-unmount #(re-frame/dispatch [::unmounted])
      :reagent-render
      (fn []
        (if (not= "manager" (:user/role @user))
          [side-bar-layout
           {:side-bar [ledger-side-bar {:data-page ::page}]
            :main [ledger-content]}]
          [:div "Not authorized"]))})))

