import Styles from "@/constants/Styles";
import { apiGetProjectOne } from "@/lib/api";
import { useAuthSession } from "@/providers/AuthProvider";
import { MaterialCommunityIcons } from "@expo/vector-icons";
import { router, useLocalSearchParams } from "expo-router";
import { useEffect, useState } from "react";
import { Text, ToastAndroid, View } from "react-native";
import AlertKonfirmasi from "../alertKonfirmasi";
import BorderBottomItem from "../borderBottomItem";
import DrawerBottom from "../drawerBottom";
import ImageUser from "../imageNew";
import MenuItemRow from "../menuItemRow";

type Props = {
   id: string;
   idUser: string;
   name: string;
   email: string;
   img: string;
   position: string;
};

export default function SectionMember() {
   const [isModal, setModal] = useState(false);
   const { token, decryptToken } = useAuthSession();
   const { id } = useLocalSearchParams<{ id: string }>();
   const [data, setData] = useState<Props[]>([]);

   async function handleLoad() {
      try {
         const hasil = await decryptToken(String(token?.current));
         const response = await apiGetProjectOne({
            user: hasil,
            cat: "member",
            id: id,
         });
         setData(response.data);
      } catch (error) {
         console.error(error);
      }
   }

   useEffect(() => {
      handleLoad();
   }, []);

   return (
      <>
         <View style={[Styles.mb15]}>
            <View style={[Styles.rowSpaceBetween, Styles.mv05]}>
               <Text style={[Styles.textDefaultSemiBold]}>Anggota</Text>
               <Text style={[Styles.textDefault]}>Total {data.length} Anggota</Text>
            </View>

            <View style={[Styles.wrapPaper]}>
               {
                  data.length > 0
                     ?
                     data.map((item, index) => {
                        return (
                           <BorderBottomItem
                              key={index}
                              borderType="bottom"
                              icon={<ImageUser src={`https://wibu-storage.wibudev.com/api/files/${item.img}`} />}
                              title={item.name}
                              subtitle={item.position}
                              rightTopInfo="Anggota"
                              onPress={() => {
                                 setModal(true);
                              }}
                           />
                        );
                     })
                     :
                     <Text style={[Styles.textDefault, Styles.cGray, { textAlign: 'center' }]}>Tidak ada anggota</Text>
               }
            </View>
         </View>

         <DrawerBottom
            animation="slide"
            isVisible={isModal}
            setVisible={setModal}
            title="Menu"
         >
            <View style={Styles.rowItemsCenter}>
               <MenuItemRow
                  icon={
                     <MaterialCommunityIcons
                        name="account-eye"
                        color="black"
                        size={25}
                     />
                  }
                  title="Lihat Profil"
                  onPress={() => {
                     setModal(false);
                     router.push("/member/123");
                  }}
               />

               <MenuItemRow
                  icon={
                     <MaterialCommunityIcons
                        name="account-remove"
                        color="black"
                        size={25}
                     />
                  }
                  title="Keluarkan"
                  onPress={() => {
                     AlertKonfirmasi({
                        title: "Konfirmasi",
                        desc: "Apakah Anda yakin ingin mengeluarkan anggota?",
                        onPress: () => {
                           setModal(false);
                           ToastAndroid.show(
                              "Berhasil mengeluarkan anggota",
                              ToastAndroid.SHORT
                           );
                        },
                     });
                  }}
               />
            </View>
         </DrawerBottom>
      </>
   );
}
