import ButtonBackHeader from "@/components/buttonBackHeader";
import ButtonSaveHeader from "@/components/buttonSaveHeader";
import { InputForm } from "@/components/inputForm";
import Styles from "@/constants/Styles";
import { apiEditDiscussion, apiGetDiscussionOne } from "@/lib/api";
import { setUpdateDiscussion } from "@/lib/discussionUpdate";
import { useAuthSession } from "@/providers/AuthProvider";
import { router, Stack, useLocalSearchParams } from "expo-router";
import { useEffect, useState } from "react";
import { SafeAreaView, ScrollView, View } from "react-native";
import Toast from "react-native-toast-message";
import { useDispatch, useSelector } from "react-redux";

export default function DiscussionDivisionEdit() {
   const { id, detail } = useLocalSearchParams<{ id: string; detail: string }>();
   const { token, decryptToken } = useAuthSession();
   const [data, setData] = useState("");
   const update = useSelector((state: any) => state.discussionUpdate);
   const dispatch = useDispatch();

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

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

   async function handleUpdate() {
      try {
         const hasil = await decryptToken(String(token?.current));
         const response = await apiEditDiscussion({
            data: { user: hasil, desc: data },
            id: detail,
         });
         if (response.success) {
            Toast.show({ type: 'small', text1: 'Berhasil mengubah data', })
            dispatch(setUpdateDiscussion({ ...update, data: !update.data }));
            router.back();
         }
      } catch (error) {
         console.error(error);
      }
   }

   return (
      <SafeAreaView>
         <Stack.Screen
            options={{
               headerLeft: () => (
                  <ButtonBackHeader
                     onPress={() => {
                        router.back();
                     }}
                  />
               ),
               headerTitle: "Edit Diskusi",
               headerTitleAlign: "center",
               headerRight: () => (
                  <ButtonSaveHeader
                     disable={data == ""}
                     category="update"
                     onPress={() => {
                        handleUpdate();
                     }}
                  />
               ),
            }}
         />
         <ScrollView>
            <View style={[Styles.p15]}>
               <InputForm
                  label="Diskusi"
                  type="default"
                  placeholder="Hal yang didiskusikan"
                  required
                  value={data}
                  onChange={setData}
               />
            </View>
         </ScrollView>
      </SafeAreaView>
   );
}
