(ns auto-ap.views.components.date-range-filter
  (:require
   [clojure.spec.alpha :as s]
   [auto-ap.entities.invoice :as invoice]
   [auto-ap.views.utils :refer [bind-field date-picker date->str local-now standard]]
   [cljs-time.core :as t]
   [re-frame.core :as re-frame]))

(defn dispatch-change [on-change-event start end]
  (fn [_]
    (re-frame/dispatch (into on-change-event [[:start] start]) )
    (re-frame/dispatch (into on-change-event [[:end] end]))))

(defn date-range-filter [{:keys [value on-change-event]}]
  [:div
   [:div.field.has-addons
    [:p.control [:a.button.is-small {:on-click
                                     (dispatch-change on-change-event
                                                      (date->str (t/minus (local-now) (t/period :days 7)) standard)
                                                      (date->str (local-now) standard))}
                 "Week" ]]
    [:p.control [:a.button.is-small {:on-click
                                     (dispatch-change on-change-event
                                                      (date->str (t/minus (local-now) (t/period :months 1)) standard)
                                                      (date->str (local-now) standard))}
                 "Month" ]]
    [:p.control [:a.button.is-small {:on-click
                                     (dispatch-change on-change-event
                                                      (date->str (t/minus (local-now) (t/period :years 1)) standard)
                                                      (date->str (local-now) standard))}
                 "Year"]]
    [:p.control [:a.button.is-small {:on-click
                                     (dispatch-change on-change-event
                                                      nil
                                                      nil)}
                 "All"]]]
   [:div.field.has-addons
    [:div.control 
     [bind-field
      [date-picker {:class-name "input is-fullwidth"
                    :class "input"
                    :format-week-number (fn [] "")
                    :previous-month-button-label ""
                    :placeholder-text "Start"
                    :next-month-button-label ""
                    :next-month-label ""
                    :event on-change-event
                    :type "date"
                    :field [:start]
                    :subscription value}]]]
    [:div.control
     [bind-field
      [date-picker {:class-name "input is-fullwidth"
                    :class "input"
                    :format-week-number (fn [] "")
                    :previous-month-button-label ""
                    :placeholder-text "End"
                    :next-month-button-label ""
                    :event on-change-event
                    :next-month-label ""
                    :type "date"
                    :field [:end]
                    :subscription value}]]]]])
