;; Events payments components

(defcomp ~payments/panel (&key update-url csrf merchant-code placeholder input-cls sumup-configured checkout-prefix)
  (section :class "p-4 max-w-lg mx-auto"
    (~shared:misc/sumup-settings-form :update-url update-url :csrf csrf :merchant-code merchant-code
      :placeholder placeholder :input-cls input-cls :sumup-configured sumup-configured
      :checkout-prefix checkout-prefix :sx-select "#payments-panel")))

(defcomp ~payments/markets-create-form (&key create-url csrf)
  (<>
    (div :id "market-create-errors" :class "mt-2 text-sm text-red-600")
    (form :class "mt-4 flex gap-2 items-end" :sx-post create-url
          :sx-target "#markets-list" :sx-select "#markets-list" :sx-swap "outerHTML"
          :sx-on:beforeRequest "document.querySelector('#market-create-errors').textContent='';"
          :sx-on:responseError "document.querySelector('#market-create-errors').textContent='Error'; if(event.detail.response){event.detail.response.clone().text().then(function(t){event.target.closest('form').querySelector('[id$=errors]').innerHTML=t})}"
      (input :type "hidden" :name "csrf_token" :value csrf)
      (div :class "flex-1"
        (label :class "block text-sm text-gray-600" "Name")
        (input :name "name" :type "text" :required true :class "w-full border rounded px-3 py-2"
               :placeholder "e.g. Farm Shop, Bakery"))
      (button :type "submit" :class "border rounded px-3 py-2" "Add market"))))

(defcomp ~payments/markets-panel (&key form list)
  (section :class "p-4"
    form
    (div :id "markets-list" :class "mt-6" list)))

(defcomp ~payments/markets-empty ()
  (p :class "text-gray-500 mt-4" "No markets yet. Create one above."))

(defcomp ~payments/markets-item (&key href market-name market-slug del-url csrf-hdr)
  (div :class "mt-6 border rounded-lg p-4"
    (div :class "flex items-center justify-between gap-3"
      (a :class "flex items-baseline gap-3" :href href
        (h3 :class "font-semibold" market-name)
        (h4 :class "text-gray-500" (str "/" market-slug "/")))
      (button :class "text-sm border rounded px-3 py-1 hover:bg-red-50 hover:border-red-400"
              :data-confirm true :data-confirm-title "Delete market?"
              :data-confirm-text "Products will be hidden (soft delete)"
              :data-confirm-icon "warning" :data-confirm-confirm-text "Yes, delete it"
              :data-confirm-cancel-text "Cancel" :data-confirm-event "confirmed"
              :sx-delete del-url :sx-trigger "confirmed"
              :sx-target "#markets-list" :sx-select "#markets-list" :sx-swap "outerHTML"
              :sx-headers csrf-hdr
        (i :class "fa-solid fa-trash")))))
