(ns auto-ap.views.components.date-range-filter
  (:require
   [auto-ap.views.utils :refer [date-picker date->str local-now standard]]
   [cljs-time.core :as t]
   [re-frame.core :as re-frame]
   [auto-ap.forms.builder :as form-builder]))


(defn set-value [on-change-event v]
  (re-frame/dispatch (conj on-change-event v)))

(defn date-range-filter [{:keys [value on-change-event]}]
  [form-builder/virtual-builder {:value     (or value {})
                                 :on-change (fn [v]
                                              (set-value on-change-event v))}
   
   [:div
    [:div.field.has-addons
     [:p.control [:a.button.is-small {:on-click
                                      #(set-value on-change-event
                                                 {:start (date->str (t/minus (local-now) (t/period :days 7)) standard)
                                                  :end (date->str (local-now) standard)})}
                  "Week" ]]
     [:p.control [:a.button.is-small {:on-click
                                      #(set-value on-change-event
                                                  {:start (date->str (t/minus (local-now) (t/period :months 1)) standard)
                                                   :end   (date->str (local-now) standard)})}
                  "Month" ]]
     [:p.control [:a.button.is-small {:on-click

                                      #(set-value on-change-event 
                                                  {:start (date->str (t/minus (local-now) (t/period :years 1)) standard)
                                                   :end (date->str (local-now) standard)})}
                  "Year"]]
     [:p.control [:a.button.is-small {:on-click
                                      #(set-value on-change-event nil)}
                  "All"]]]
    [:div.field.has-addons
     [:div.control 

      [form-builder/raw-field-v2 {:field :start}
       [date-picker
        {:placeholder  "Start"
         :class        "is-small"
         :output       :text}]]]
     [:div.control
      [form-builder/raw-field-v2 {:field :end}
       [date-picker
        {:class        "is-small"
         :placeholder  "End"
         :output       :text}]]]]]])
