import ButtonBackHeader from "@/components/buttonBackHeader";
import ItemDetailMember from "@/components/itemDetailMember";
import HeaderRightMemberDetail from "@/components/member/headerMemberDetail";
import { valueRoleUser } from "@/constants/RoleUser";
import Styles from "@/constants/Styles";
import { apiGetProfile } from "@/lib/api";
import { router, Stack, useLocalSearchParams } from "expo-router";
import { useEffect, useState } from "react";
import { Image, SafeAreaView, ScrollView, Text, View } from "react-native";
import { useSelector } from "react-redux";

type Props = {
   id: string,
   name: string,
   nik: string,
   email: string,
   phone: string,
   gender: string,
   position: string,
   group: string,
   img: string,
   isActive: boolean,
   role: string
}

export default function MemberDetail() {
   const { id } = useLocalSearchParams<{ id: string }>();
   const [data, setData] = useState<Props>()
   const [error, setError] = useState(false)
   const entityUser = useSelector((state: any) => state.user)
   const [isEdit, setEdit] = useState(false)

   async function handleLoad() {
      try {
         const response = await apiGetProfile({ id: id })
         setData(response.data)
         setEdit(valueRoleUser.filter((v) => v.login == entityUser.role)[0]?.data.some((i: any) => i.id == response.data.idUserRole))
      } catch (error) {
         console.error(error)
      }
   }

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

   return (
      <SafeAreaView>
         <Stack.Screen
            options={{
               headerLeft: () => <ButtonBackHeader onPress={() => { router.back() }} />,
               headerTitle: 'Anggota',
               headerTitleAlign: 'center',
               headerRight: () => (entityUser.role != "user") && isEdit && <HeaderRightMemberDetail active={data?.isActive} id={id} />,
               headerShadowVisible: false
            }}
         />
         <ScrollView>
            <View style={{ flexDirection: 'column' }}>
               <View style={[Styles.wrapHeadViewMember]}>
                  <Image
                     source={error ? require("../../../assets/images/user.jpg") : { uri: `https://wibu-storage.wibudev.com/api/files/${data?.img}` }}
                     onError={() => { setError(true) }}
                     style={[Styles.userProfileBig]}
                  />
                  <Text style={[Styles.textSubtitle, Styles.cWhite, Styles.mt10]}>{data?.name}</Text>
                  <Text style={[Styles.textMediumNormal, Styles.cWhite]}>{data?.role}</Text>
               </View>
               <View style={[Styles.p15]}>
                  <View style={[Styles.rowSpaceBetween]}>
                     <Text style={[Styles.textDefaultSemiBold]}>Informasi</Text>
                  </View>
                  <ItemDetailMember category="nik" value={data?.nik} />
                  <ItemDetailMember category="group" value={data?.group} />
                  <ItemDetailMember category="position" value={data?.position} />
                  <ItemDetailMember category="phone" value={`+62${data?.phone}`} />
                  <ItemDetailMember category="email" value={data?.email} />
                  <ItemDetailMember category="gender" value={data?.gender == "F" ? "Perempuan" : "Laki-Laki"} />
               </View>
            </View>
         </ScrollView>
      </SafeAreaView>
   )
}