import Styles from "@/constants/Styles";
import { apiGetTaskOne } from "@/lib/api";
import { useAuthSession } from "@/providers/AuthProvider";
import { useTheme } from "@/providers/ThemeProvider";
import { MaterialCommunityIcons } from "@expo/vector-icons";
import { useLocalSearchParams } from "expo-router";
import { useEffect, useState } from "react";
import { View } from "react-native";
import { useSelector } from "react-redux";
import Text from "../Text";
import TextExpandable from "../textExpandable";

export default function SectionReportTask({ refreshing }: { refreshing: boolean }) {
   const { colors } = useTheme()
   const update = useSelector((state: any) => state.taskUpdate)
   const { token, decryptToken } = useAuthSession()
   const { id, detail } = useLocalSearchParams<{ id: string, detail: string }>();
   const [data, setData] = useState('')

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

   useEffect(() => { handleLoad() }, [update.report])
   useEffect(() => { if (refreshing) handleLoad() }, [refreshing])

   if (!data || data === "") return null;

   return (
      <View style={[
         Styles.wrapPaper,
         Styles.mb15,
         Styles.sectionCard,
         { backgroundColor: colors.card, borderColor: colors.icon + '18' },
      ]}>
         <View style={Styles.sectionHeader}>
            <View style={[Styles.sectionIconBox, Styles.mr10, { backgroundColor: colors.tabActive + '18' }]}>
               <MaterialCommunityIcons name="text-box-outline" size={18} color={colors.tabActive} />
            </View>
            <Text style={[Styles.textDefaultSemiBold, { color: colors.text }]}>
               Laporan Kegiatan
            </Text>
         </View>

         <View style={[Styles.reportContent, { borderLeftColor: colors.tabActive + '50' }]}>
            <TextExpandable content={data} maxLines={2} />
         </View>
      </View>
   )
}
