(ns auto-ap.views.pages.paid-invoices
  (:require [re-frame.core :as re-frame]
            [auto-ap.entities.clients :as client]
            [auto-ap.entities.vendors :as vendor]
            [auto-ap.events :as events]
            [auto-ap.views.utils :refer [dispatch-event]]
            [auto-ap.utils :refer [by replace-if]]
            [auto-ap.views.components.layouts :refer [side-bar-layout]]
            [auto-ap.views.components.invoices.side-bar :refer [invoices-side-bar]]
            [auto-ap.views.pages.check :as check]
            [auto-ap.views.components.invoice-table :refer [invoice-table] :as invoice-table]
            [auto-ap.views.components.expense-accounts-dialog :as expense-accounts-dialog]
            [auto-ap.views.components.vendor-dialog :refer [vendor-dialog]]
            [auto-ap.subs :as subs]))


(re-frame/reg-sub
 ::invoice-page
 (fn [db]
   (-> db ::invoice-page)))

(re-frame/reg-sub
 ::params
 (fn [db]
   (-> db (::params {}))))

(re-frame/reg-event-fx
 ::params-change
 (fn [cofx [_ params]]
   {:db (-> (:db cofx)
            (assoc-in [:status :loading] true)
            (assoc-in [::params] params))
    :graphql {:token (-> cofx :db :user)
              :query-obj (invoice-table/query (assoc params :imported true :status "paid")) 
              :on-success [::received]}}))

(re-frame/reg-event-db
 ::received
 (fn [db [_ data]]
   (-> db
       (assoc ::invoice-page (first (:invoice-page data)))
       (assoc-in [:status :loading] false))))

(re-frame/reg-event-fx
 ::invalidated
 (fn [cofx [_ params]]
   {:dispatch [::params-change @(re-frame/subscribe [::params])]}))

(re-frame/reg-event-fx
 ::expense-accounts-updated
 (fn [{:keys [db]} [_ data]]
   (let [updated (:edit-expense-accounts data)]
     {:dispatch [::events/modal-completed ::expense-accounts-dialog/change-expense-accounts]
      :db (-> db
              (update-in [::invoice-page :invoices]
                         
                         (fn [is]
                           
                           (replace-if #(= (:id %1) (:id %2)) updated is)))
              (dissoc ::change-expense-accounts))})))

(def paid-invoices-content
  (with-meta
    (fn []
      (let [current-client @(re-frame/subscribe [::subs/client])]
        [:div 
         [:h1.title "Paid invoices"]
         [invoice-table {:id :paid
                         :params (re-frame/subscribe [::params])
                         :invoice-page (re-frame/subscribe [::invoice-page])
                         :status (re-frame/subscribe [::subs/status])
                         :expense-event [::expense-accounts-dialog/change-expense-accounts]
                         :on-params-change (fn [params]
                                             (re-frame/dispatch [::params-change params]))
                         :check-boxes false}]
         [expense-accounts-dialog/change-expense-accounts-modal
          {:updated-event [::expense-accounts-updated]}]]))
    {:component-will-mount #(do (println "HERE2") (re-frame/dispatch-sync [::params-change {}])) }))

(defn paid-invoices-page []
  [side-bar-layout {:side-bar [invoices-side-bar]
                    :main [paid-invoices-content]
                    :bottom [vendor-dialog {:vendor @(re-frame/subscribe [::subs/user-editing-vendor])
                                            :save-event [::events/save-vendor]
                                            :change-event [::events/change-nested-form-state [:user-editing-vendor]] :id :auto-ap.views.main/user-editing-vendor}]}])
