"use client";
import { useCallback, useEffect, useState } from "react";
import CustomInputForm from "../CustomInputForm";
import { useRouter, useSearchParams } from "next/navigation";
import { useTranslation } from "react-i18next";
import {
  FieldErrors,
  FormProvider,
  SubmitErrorHandler,
  SubmitHandler,
  useForm,
} from "react-hook-form";
import { CreateStaffInputs, saveStaff } from "@/app/api/staff/actions";
import { Button, Stack, Typography } from "@mui/material";
// import CreateStaffForm from "../CreateStaffForm";
import { comboProp } from "@/app/api/companys/actions";
// import StaffInfo from "./StaffInfo";
import { Check, Close, RestartAlt } from "@mui/icons-material";
import StaffInfo from "./StaffInfo";
import { IndividualStaff, SalaryEffectiveInfo } from "@/app/api/staff";
import dayjs from "dayjs";
// import { useGridApiContext } from '@mui/x-data-grid';

export interface comboItem {
  company: comboProp[];
  team: comboProp[];
  department: comboProp[];
  position: comboProp[];
  grade: comboProp[];
  skill: comboProp[];
  salary: comboProp[];
}

interface formProps {
  Staff: IndividualStaff
  combos: comboItem;
  SalaryEffectiveInfo: SalaryEffectiveInfo[];
}


const EditStaff: React.FC<formProps> = ({ Staff, combos, SalaryEffectiveInfo }) => {
  // console.log(Staff.joinDate)
  const defaultSkillset = Staff.skillset.map((s: any) => s.skill.id)
  const { t } = useTranslation();
  const searchParams = useSearchParams()
  const id = parseInt(searchParams.get("id") || "0");
  const formProps = useForm<CreateStaffInputs & { salaryEffectiveInfo: SalaryEffectiveInfo[] } & { delSalaryEffectiveInfo: number[] }>({
    defaultValues: {
      staffId: Staff.staffId,
      name: Staff.name,
      companyId: Staff.company.id,
      teamId: Staff.team?.id,
      departmentId: Staff.department?.id,
      gradeId: Staff.grade?.id,
      skillSetId: defaultSkillset,
      // removeSkillSetId: [],
      currentPositionId: Staff.currentPosition?.id,
      salaryId: Staff.salary.salaryPoint,
      employType: Staff.employType,
      email: Staff.email,
      phone1: Staff.phone1,
      phone2: Staff.phone2,
      emergContactName: Staff.emergContactName,
      emergContactPhone: Staff.emergContactPhone,
      joinDate: Staff.joinDate ? dayjs(Staff.joinDate as string).format("YYYY-MM-DD") : null,
      joinPositionId: Staff.joinPosition?.id || null,
      departDate: Staff.departDate ? dayjs(Staff.departDate as string).format("YYYY-MM-DD") : null,
      departReason: Staff.departReason,
      remark: Staff.remark,
      salaryEffectiveInfo: SalaryEffectiveInfo.map(item => {
        return ({
        id: item.id,
        salaryPoint: combos.salary.filter(sal => sal.id === item.salaryPoint)[0].label,
        date: dayjs(item.date).toDate(),
      })}),
      delSalaryEffectiveInfo: []
  }});
  const [serverError, setServerError] = useState("");
  const router = useRouter();
  // const [tabIndex, setTabIndex] = useState(0);

  const errors = formProps.formState.errors;

  const checkDuplicates = (str1: string, str2: string, str3: string) => {
    return str1 === str2 || str1 === str3 || str2 === str3;
  }

  const onSubmit = useCallback<SubmitHandler<CreateStaffInputs & { salaryEffectiveInfo: SalaryEffectiveInfo[] } >>(
    async (data) => {
      try {
        console.log(data);
        let haveError = false;
        let regex_email = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
        let regex_phone = /^\d{8}$/

        if (!regex_email.test(data.email)) {
          haveError = true
          formProps.setError("email", { message: t("Please Enter Correct Email."), type: "required" })
        }
        if (!regex_phone.test(data.phone1)) {
          haveError = true
          formProps.setError("phone1", { message: t("Please Enter Correct Phone No.."), type: "required" })
        }
        if (data.emergContactPhone && !regex_phone.test(data.emergContactPhone)) {
          haveError = true
          formProps.setError("emergContactPhone", { message: t("Please Enter Correct Phone No.."), type: "required" })
        }
        if (data.phone2 && data.phone2?.length > 0) {
          if(!regex_phone.test(data.phone2)) {
            haveError = true
            formProps.setError("phone2", { message: t("Please Enter Correct Phone No.."), type: "required" })
          }
        }
        if (data.phone1 === data.phone2 || data.phone1 === data.emergContactPhone || data.phone2 && data.phone2 === data.emergContactPhone && data.phone2.length > 0) {
          haveError = true
          formProps.setError("phone1", { message: t("Please Enter Different Phone No.."), type: "required" })
          if (data.phone2!.length > 0) {
            formProps.setError("phone2", { message: t("Please Enter Different Phone No.."), type: "required" })
          }
          formProps.setError("emergContactPhone", { message: t("Please Enter Different Phone No.."), type: "required" })
        }
        if (!regex_email.test(data.email)) {
          haveError = true
          formProps.setError("email", { message: t("Please Enter Correct Email."), type: "required" })
        }
        if (!data.companyId) {
          haveError = true
          formProps.setError("companyId", { message: t("Please Enter Company."), type: "required" })
        }
        if (!data.employType) {
          haveError = true
          formProps.setError("employType", { message: t("Please Enter Employ Type."), type: "required" })
        }
        if (!data.salaryId) {
          haveError = true
          formProps.setError("salaryId", { message: t("Please Enter Salary."), type: "required" })
        }
        // if (data.joinDate && data.departDate && new Date(data.departDate) <= new Date(data.joinDate)) {
        //   haveError = true
        //   formProps.setError("departDate", { message: t("Depart Date cannot be earlier than Join Date."), type: "required" })
        // }
        if (haveError) {
          return
        }
        console.log("passed")
        const postData: CreateStaffInputs = {
          id: id,
          ...data,
          salaryEffectiveInfo: data.salaryEffectiveInfo.map((item: SalaryEffectiveInfo) => ({
            id: item.id,
            salaryPoint: chopSalaryPoints(item.salaryPoint),
            date: dayjs(item.date).format('YYYY-MM-DD').toString()
          }))
        }
        if (postData.joinDate) {
          postData.joinDate = dayjs(postData.joinDate).format("YYYY-MM-DD")
        }
        if (postData.departDate) {
          postData.departDate = dayjs(postData.departDate).format("YYYY-MM-DD")
        }
        console.log(postData)
        await saveStaff(postData)
        router.replace("/settings/staff")
      } catch (e: any) {
        console.log(e);
        formProps.setError("staffId", { message: t("Please Enter Employ Type."), type: "required" })
        let msg = ""
        if (e.message === "Duplicated StaffId Found") {
          msg = t("Duplicated StaffId Found")
        }
        setServerError(`${t("An error has occurred. Please try again later.")} ${msg} `);
      }
    },
    [router]
  );
  
  const handleCancel = () => {
    router.back();
  };

  function chopSalaryPoints(input: string | number): number | null {
    if (typeof input === 'string') {
      const match = input.match(/(\d+) \((\d+) - (\d+)\)/);
      if (match) {
        return parseInt(match[1], 10);
      }
    } else if (typeof input === 'number') {
      return input;
    }
    return null;
  }

  // const resetStaff = useCallback(() => {
    // window.location.reload()
    // console.log(dayjs(Staff.joinDate).format(INPUT_DATE_FORMAT))
    // console.log(formProps.getValues("joinDate"))
    // formProps.setValue('salaryEffectiveInfo', SalaryEffectiveInfo.map(item => {
    //     return ({
    //     id: item.id,
    //     salaryPoint: combos.salary.filter(sal => sal.id === item.salaryPoint)[0].label,
    //     date: dayjs(item.date).toDate(),
    //   })}))

    // formProps.reset({
    //   staffId: Staff.staffId,
    //   name: Staff.name,
    //   companyId: Staff.company.id,
    //   teamId: Staff.team?.id,
    //   departmentId: Staff.department?.id,
    //   gradeId: Staff.grade?.id,
    //   skillSetId: defaultSkillset,
    //   currentPositionId: Staff.currentPosition?.id,
    //   salaryId: Staff.salary.salaryPoint,
    //   employType: Staff.employType,
    //   email: Staff.email,
    //   phone1: Staff.phone1,
    //   phone2: Staff.phone2,
    //   emergContactName: Staff.emergContactName,
    //   emergContactPhone: Staff.emergContactPhone,
    //   joinDate: Staff.joinDate ? dayjs(Staff.joinDate).format(INPUT_DATE_FORMAT) : "",
    //   joinPositionId: Staff.joinPosition?.id,
    //   departDate: !Staff.departDate ? "" : dayjs(Staff.departDate).format(INPUT_DATE_FORMAT),
    //   departReason: Staff.departReason,
    //   remark: Staff.remark,
    //   salaryEffectiveInfo: SalaryEffectiveInfo.map(item => {
    //     return ({
    //     id: item.id,
    //     salaryPoint: combos.salary.filter(sal => sal.id === item.salaryPoint)[0].label,
    //     date: dayjs(item.date).toDate(),
    //   })})
    // });
  // }, []);
  
  // useEffect(() => {
  //   formProps.setValue('salaryEffectiveInfo', SalaryEffectiveInfo.map(item => {
  //     return ({
  //     id: item.id,
  //     salaryPoint: combos.salary.filter(sal => sal.id === item.salaryPoint)[0].label,
  //     date: dayjs(item.date).toDate(),
  //   })})
  // )
  // }, [formProps]);
  
  // useEffect(() => {
  //   resetStaff()
  // }, [Staff, formProps, combos]);

  return (
    <>
      <FormProvider {...formProps}>
      <Stack
          spacing={2}
          component="form"
          onSubmit={formProps.handleSubmit(onSubmit)}
        >
          {serverError && (
            <Typography variant="body2" color="error" alignSelf="flex-end">
              {serverError}
            </Typography>
          )}
          {Staff && <StaffInfo combos={combos}/>}
          <Stack direction="row" justifyContent="flex-end" gap={1}>
          <Button
              variant="text"
              startIcon={<RestartAlt />}
              onClick={()=> window.location.reload()}
            >
              {t("Reset")}
            </Button>
            <Button
              variant="outlined"
              startIcon={<Close />}
              onClick={handleCancel}
            >
              {t("Cancel")}
            </Button>
            <Button
              variant="contained"
              startIcon={<Check />}
              type="submit"
              // disabled={Boolean(formProps.watch("isGridEditing"))}
            >
              {t("Confirm")}
            </Button>
          </Stack>
        </Stack>
      </FormProvider>
    </>
  );
};

export default EditStaff;