import Styles from "@/constants/Styles"
import { apiGetDivisionOneFeature } from "@/lib/api"
import { useAuthSession } from "@/providers/AuthProvider"
import { useTheme } from "@/providers/ThemeProvider"
import { AntDesign } from "@expo/vector-icons"
import { router, useLocalSearchParams } from "expo-router"
import { useEffect, useState } from "react"
import { View } from "react-native"
import { useSelector } from "react-redux"
import ButtonMenuHeader from "../buttonMenuHeader"
import DrawerBottom from "../drawerBottom"
import MenuItemRow from "../menuItemRow"
import ModalFilter from "../modalFilter"

export default function HeaderRightTaskList() {
   const { colors } = useTheme()
   const [isVisible, setVisible] = useState(false)
   const [isAdminDivision, setIsAdminDivision] = useState(false);
   const { token, decryptToken } = useAuthSession()
   const { id } = useLocalSearchParams<{ id: string }>();
   const entityUser = useSelector((state: any) => state.user);
   const [isFilter, setFilter] = useState(false)


   async function handleCheckAdmin() {
      try {
         const hasil = await decryptToken(String(token?.current));
         const response = await apiGetDivisionOneFeature({
            id,
            user: hasil,
            cat: "check-admin",
         });
         setIsAdminDivision(response.data);
      } catch (error) {
         console.error(error);
      }
   }

   useEffect(() => {
      handleCheckAdmin()
   }, [])


   return (
      <>
         {/* {
            (entityUser.role != "user" && entityUser.role != "coadmin") || isAdminDivision
               ? <ButtonMenuHeader onPress={() => { setVisible(true) }} /> : <></>
         } */}
         <ButtonMenuHeader onPress={() => { setVisible(true) }} />
         <DrawerBottom animation="slide" isVisible={isVisible} setVisible={setVisible} title="Menu">
            <View style={Styles.rowItemsCenter}>
               {
                  ((entityUser.role != "user" && entityUser.role != "coadmin") || isAdminDivision)
                  &&
                  <MenuItemRow
                     icon={<AntDesign name="pluscircleo" color={colors.text} size={25} />}
                     title="Tambah Tugas Divisi"
                     onPress={() => {
                        setVisible(false)
                        router.push('./task/create')
                     }}
                  />
               }
               <MenuItemRow
                  icon={<AntDesign name="filter" color={colors.text} size={25} />}
                  title="Filter"
                  onPress={() => {
                     setVisible(false)
                     setTimeout(() => {
                        setFilter(true)
                     }, 600)
                  }}
               />
            </View>
         </DrawerBottom>
         <ModalFilter
            close={() => { setFilter(false) }}
            open={isFilter}
            page="division/task"
            category={"year-only"}
            dataPassing={id}
         />
      </>
   )
}