(ns auto-ap.views.components.vendor-dialog
  (:require [re-frame.core :as re-frame]
            [auto-ap.views.utils :refer [dispatch-event horizontal-field bind-field]]
           [auto-ap.views.components.modal :refer [action-modal]]
           [auto-ap.views.components.address :refer [address-field]]
           [auto-ap.views.components.typeahead :refer [typeahead]]

           [auto-ap.expense-accounts :refer [chooseable-expense-accounts]]
           [clojure.spec.alpha :as s]
           [auto-ap.entities.vendors :as entity]
           [auto-ap.entities.contact :as contact]
           [auto-ap.subs :as subs]))

(defn vendor-dialog [{:keys [vendor save-event change-event id] {:keys [name]} :vendor}]
  (println (s/explain ::entity/vendor vendor) )
  (let [clients-by-id @(re-frame/subscribe [::subs/clients-by-id])]
    [action-modal {:id id
                   :title [:span (if (:id vendor)
                                   (str "Edit " (or name "<vendor>"))
                                   (str "Add " (or name "<new vendor>")))
                           (when (:error vendor)
                             [:span.icon.has-text-danger
                              [:i.fa.fa-exclamation-triangle]])]
                   :action-text "Save"
                   :save-event save-event
                   :can-submit? (s/valid? ::entity/vendor vendor)}

     
     
     [horizontal-field
      [:label.label "Name"]
      [:div.control
       [bind-field
        [:input.input {:type "text"
                       :auto-focus true
                       :field :name
                       :spec ::entity/name
                       :event change-event
                       :subscription vendor}]]]]

     [horizontal-field
      [:label.label "Print Checks As"]
      [:div.control
       [bind-field
        [:input.input {:type "text"
                       :field :print-as
                       :spec ::entity/print-as
                       :event change-event
                       :subscription vendor}]]]]
     [:h2.subtitle "Expense Accounts"]
     [horizontal-field
      [:label.label "Default"]
      [bind-field
       [typeahead {:matches (map (fn [[k v]] [k (:name v)]) chooseable-expense-accounts)
                   :type "typeahead"
                   :field [:default-expense-account]
                   :spec ::entity/default-expense-account
                   :event change-event
                   :subscription vendor}]]]     
     #_[horizontal-field
      [:label.label "Code"]
      [:div.control

       [bind-field
        [:input.input.is-expanded {:type "text"
                                   :field :code
                                   :spec ::entity/code
                                   :event change-event
                                   :subscription vendor}]]
       [:p.help "The vendor code is used for invoice parsing. Only one vendor at a time can use a code"]]] 

     [:h2.subtitle "Address"]
     [address-field {:field [:address]
                     :event change-event
                     :subscription vendor}]
     
     
     
     [:h2.subtitle "Contact"]
     [horizontal-field
      [:label.label "Primary"]
      [:div.control.has-icons-left
       [bind-field
        [:input.input.is-expanded {:type "text"
                                   :field [:primary-contact :name]
                                   :spec ::contact/name
                                   :event change-event
                                   :subscription vendor}]]
       [:span.icon.is-small.is-left
        [:i.fa.fa-user]]]

      [:div.control.has-icons-left
       [:span.icon.is-small.is-left
        [:i.fa.fa-envelope]]
       [bind-field
        [:input.input {:type "email"
                       :field [:primary-contact :email]
                       :spec ::contact/email
                       :event change-event
                       :subscription vendor}]]]

      [:div.control.has-icons-left
       [bind-field
        [:input.input {:type "phone"
                       :field [:primary-contact :phone]
                       :spec ::contact/phone
                       :event change-event
                       :subscription vendor}]]
       [:span.icon.is-small.is-left
        [:i.fa.fa-phone]]]]
     
     [horizontal-field
      [:label.label "Secondary"]
      [:div.control.has-icons-left
       [bind-field
        [:input.input.is-expanded {:type "text"
                                   :field [:secondary-contact :name]
                                   :spec ::contact/name
                                   :event change-event
                                   :subscription vendor}]]
       [:span.icon.is-small.is-left
        [:i.fa.fa-user]]]
      [:div.control.has-icons-left
       [:span.icon.is-small.is-left
        [:i.fa.fa-envelope]]
       [bind-field
        [:input.input {:type "email"
                       :field [:secondary-contact :email]
                       :spec ::contact/email
                       :event change-event
                       :subscription vendor}]]]
      [:div.control.has-icons-left
       [bind-field
        [:input.input {:type "phone"
                       :field [:secondary-contact :phone]
                       :spec ::contact/phone
                       :event change-event
                       :subscription vendor}]]
       [:span.icon.is-small.is-left
        [:i.fa.fa-phone]]]]
     
     [horizontal-field
      [:label.label "Invoice Reminders"]
      [:div.control
       [:label.radio 
        [bind-field
         [:input {:type "radio"
                  :name "schedule"
                  :value "Weekly"
                  :field :invoice-reminder-schedule
                  :spec ::entity/invoice-reminder-schedule
                  :event change-event
                  :subscription vendor}]]
        " Send weekly"]

       [:label.radio 
        [bind-field
         [:input {:type "radio"
                  :name "schedule"
                  :value "Never"
                  :field :invoice-reminder-schedule
                  :spec ::entity/invoice-reminder-schedule
                  :event change-event
                  :subscription vendor}]]
        " Never"]]]

     

     
      
     
     
     (when (:saving? vendor) [:div.is-overlay {:style {"backgroundColor" "rgba(150,150,150, 0.5)"}}])]))
