(ns auto-ap.views.components.sorter
  (:require [re-frame.core :as re-frame]
            [auto-ap.subs :as subs]
            [auto-ap.views.utils :refer [date->str]]
            [reagent.core :as reagent]
            [clojure.string :as str]
            [cljs-time.format :as format]))
(defn toggle-sort-by [params key]

  (-> params
      (assoc :sort-by key)
      (update :asc not)))

(defn sort-icon [which sort-by asc]
  (cond
    (and (= sort-by which) asc)
    [:span.icon
     [:i.fa.fa-sort-up]]

    (and (= sort-by which) (not asc))
    [:span.icon
     [:i.fa.fa-sort-down]]

    :else
    [:span.icon
     [:i.fa.fa-sort]]))

(defn sorted-column [{:keys [on-sort sort-key sort-by asc style]} & rest]
  
  [:th {:on-click (fn [e] (on-sort (toggle-sort-by {:asc asc} sort-key)))
        :style style}
   rest 
   (sort-icon sort-key sort-by asc)])
