(ns auto-ap.ssr.components.user-dropdown
  (:require
   [auto-ap.client-routes :as client-routes2]
   [auto-ap.datomic :refer [conn pull-attr]]
   [auto-ap.ssr-routes :as ssr-routes]
   [bidi.bidi :as bidi]
   [datomic.api :as dc]
   [hiccup2.core :as hiccup]))

(defn dropdown [{:keys [identity]}]
  [:div {:class "flex items-center ml-3 mr-10"}
   [:div
    [:button#user-menu-button {:type "button", :class "flex text-sm bg-gray-800 rounded-full focus:ring-4 focus:ring-gray-300 dark:focus:ring-gray-600", :aria-expanded "false"
                               "@click" "$tooltip($refs.tooltip, {content: ()=>$refs.tooltip.innerHTML, theme: $store.darkMode.on ? 'dark' : 'light', allowHTML: true, interactive:true})"
                               }
     [:span {:class "sr-only"} "Open user menu"]
     [:img {:class "w-8 h-8 rounded-full", :src (pull-attr (dc/db conn) :user/profile-image-url (:db/id identity)) :alt "user photo" :referrerpolicy "no-referrer"}]]]
   [:template {:class ""
                    :x-ref "tooltip"}
    [:div {:class "px-4 py-3", :role "none"}
     [:p {:class "text-sm text-gray-900 dark:text-white", :role "none"} (:user/name identity)]
     [:p {:class "text-sm font-medium text-gray-900 truncate dark:text-gray-300", :role "none"} (pull-attr (dc/db conn) :user/email (:db/id identity))] ]
    [:ul {:class "py-1", :role "none"}
     [:li
      [:a {:href (bidi/path-for ssr-routes/only-routes :company), :class "block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-600 dark:hover:text-white", :role "menuitem"} "My Company"]]
     (when (= "admin" (:user/role identity))
       [:a {:href (bidi/path-for ssr-routes/only-routes :auto-ap.routes.admin/page),
            :class "block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-600 dark:hover:text-white", :role "menuitem"} "Admin"])
     [:li
      [:a {:href "#", :class "block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-600 dark:hover:text-white", :role "menuitem"
           "@click.prevent" "$store.darkMode.toggle()" }
       "Night Mode"]]
     [:li
      [:a {:href "/logout", :class "block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-600 dark:hover:text-white", :role "menuitem"} "Sign out"]]]] ])
