import BorderBottomItem from "@/components/borderBottomItem";
import ButtonTab from "@/components/buttonTab";
import ImageUser from "@/components/imageNew";
import InputSearch from "@/components/inputSearch";
import LabelStatus from "@/components/labelStatus";
import Styles from "@/constants/Styles";
import { apiGetDiscussion, apiGetDivisionOneFeature } from "@/lib/api";
import { useAuthSession } from "@/providers/AuthProvider";
import { AntDesign, Feather, Ionicons } from "@expo/vector-icons";
import { router, useLocalSearchParams } from "expo-router";
import { useEffect, useState } from "react";
import { SafeAreaView, ScrollView, Text, View } from "react-native";
import { useSelector } from "react-redux";


type Props = {
   id: string,
   title: string,
   desc: string,
   status: number,
   user_name: string,
   img: string,
   total_komentar: number,
   createdAt: string,
   isActive: boolean
}


export default function DiscussionDivision() {
   const { id, active } = useLocalSearchParams<{ id: string, active?: string }>()
   const [data, setData] = useState<Props[]>([])
   const { token, decryptToken } = useAuthSession()
   const [search, setSearch] = useState('')
   const update = useSelector((state: any) => state.discussionUpdate);

   async function handleLoad() {
      try {
         const hasil = await decryptToken(String(token?.current))
         const response = await apiGetDiscussion({ user: hasil, search, division: id, active })
         setData(response.data)
      } catch (error) {
         console.error(error)
      }
   }


   useEffect(() => {
      handleLoad()
   }, [active, search, update.data])

   return (
      <SafeAreaView>
         <ScrollView>
            <View style={[Styles.p15, Styles.mb100]}>
               <View style={[Styles.wrapBtnTab]}>
                  <ButtonTab
                     active={active == "false" ? "false" : "true"}
                     value="true"
                     onPress={() => { router.replace('./discussion?active=true') }}
                     label="Aktif"
                     icon={<Feather name="check-circle" color={active == "false" ? 'black' : 'white'} size={20} />}
                     n={2} />
                  <ButtonTab
                     active={active == "false" ? "false" : "true"}
                     value="false"
                     onPress={() => { router.replace('./discussion?active=false') }}
                     label="Arsip"
                     icon={<AntDesign name="closecircleo" color={active == "true" ? 'black' : 'white'} size={20} />}
                     n={2} />
               </View>
               <InputSearch onChange={setSearch} />
               <View>
                  {data.length > 0 ?
                     data.map((item, index) => (
                        <BorderBottomItem
                           key={index}
                           width={55}
                           onPress={() => { router.push(`./discussion/${item.id}`) }}
                           borderType="bottom"
                           icon={
                              <ImageUser src={`https://wibu-storage.wibudev.com/api/files/${item.img}`} size="sm" />
                           }
                           title={item.user_name}
                           subtitle={
                              active == "true" ? item.status == 1 ? <LabelStatus category='success' text='BUKA' size="small" /> : <LabelStatus category='error' text='TUTUP' size="small" /> : <></>
                           }
                           rightTopInfo={item.createdAt}
                           desc={item.desc}
                           leftBottomInfo={
                              <View style={[Styles.rowItemsCenter]}>
                                 <Ionicons name="chatbox-ellipses-outline" size={18} color="grey" style={Styles.mr05} />
                                 <Text style={[Styles.textInformation, Styles.cGray, Styles.mb05]}>Diskusikan</Text>
                              </View>
                           }
                           rightBottomInfo={item.total_komentar + ' Komentar'}
                        />
                     ))
                     :
                     (
                        <Text style={[Styles.textDefault, Styles.cGray, Styles.mv10, { textAlign: "center" }]}>Tidak ada diskusi</Text>
                     )}
               </View>
            </View>
         </ScrollView>
      </SafeAreaView>
   );
}