(ns auto-ap.views.pages.admin.vendors
  (:require-macros [cljs.core.async.macros :refer [go]])
  (:require [re-frame.core :as re-frame]
            [reagent.core :as reagent]
            [clojure.string :as str]
            [auto-ap.subs :as subs]
            [auto-ap.events.admin.vendors :as events]
            [auto-ap.entities.vendors :as entity]
            [auto-ap.views.components.address :refer [address-field]]
            [auto-ap.views.components.vendor-dialog :refer [vendor-dialog]]
            [auto-ap.views.components.modal :refer [modal]]
            [auto-ap.views.components.admin.side-bar :refer [admin-side-bar]]
            [auto-ap.views.components.layouts :refer [side-bar-layout]]
            [clojure.spec.alpha :as s]
            [auto-ap.views.utils :refer [login-url dispatch-value-change dispatch-event bind-field horizontal-field]]

            [auto-ap.views.components.typeahead :refer [typeahead]]
            [auto-ap.expense-accounts :refer [expense-accounts]]
            [cljs.reader :as edn]
            [auto-ap.routes :as routes]
            [bidi.bidi :as bidi]))

(defn invalid-fields [spec v]
  (set (mapcat :in (::s/problems (s/explain-data spec v)))))

(defn vendors-table []
  (let [vendors (re-frame/subscribe [::subs/sorted-vendors])
        editing-vendor (:editing @(re-frame/subscribe [::subs/admin]))]
    
    [:table {:class "table", :style {:width "100%"}}
     [:thead  
      [:tr  
       [:th "Name"]
       [:th "Email"]
       [:th "Invoice Reminders"]
       [:th]]]
     [:tbody (for [v @vendors]
               ^{:key (str (:id v))}
               [:tr {:on-click (dispatch-event [::events/edit (:id v)])
                     :style {"cursor" "pointer"}}
                [:td (:name v)]
                [:td (:primary-email v)]
                [:td (:invoice-reminder-schedule v)]
                [:td
                 (when (:primary-email v)
                   [:button.button.is-primary.is-outlined

                    {:on-click (dispatch-event [::events/remind (:id v)])
                     :href "#"}
                    [:span.icon [:i.fa.fa-share-square]] [:span "Send Reminder"]])]])]]))

(defn danger-for [[dom {:keys [field subscription class] :as keys} & rest]]
  (let [keys (assoc keys :class (str class
                                     (when (not (s/valid? field (field subscription)))
                                       " is-danger")))
        keys (dissoc keys :field :subscription)]
    (vec (concat [dom keys] rest))))


(defn admin-vendors-content []
  [(with-meta
     (fn []
       [:div.inbox-messages
        (when-let [banner (:banner @(re-frame/subscribe [::subs/admin]))]
          [:div.notification banner])
        (let [vendors (re-frame/subscribe [::subs/vendors])
              editing-vendor (:vendor @(re-frame/subscribe [::subs/admin]))]
          
          
          [:div
           [:h1.title "Vendors"]
           [vendors-table]

           [:div.is-pulled-right
            [:a.button.is-primary.is-large {:on-click (dispatch-event [::events/new])} "New vendor"]]

           [vendor-dialog {:vendor editing-vendor
                           :save-event [::events/save]
                           :change-event ::events/change
                           :id ::admin-vendor}]])])
     {:component-did-mount (fn []
                             (re-frame/dispatch [::events/mounted]))})])

(defn admin-vendors-page []
  [side-bar-layout {:side-bar [admin-side-bar {}]
                    :main [admin-vendors-content]}])
