/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { toast } from "react-toastify";
import { api } from "../../../../Api/Api";

export const addModalInitialValuesHandler = (
  items: any,
  setAddModalInitialValues: any
) => {
  setAddModalInitialValues({
    id: items?.id,
    first_name: items?.firstName || "",
    last_name: items?.lastName || "",
    email: items?.email || "",
    phone: items?.phone || "",
    profile_pic: items?.profilePicture || null,
    employee_id: items?.employeeId || "",
    first_name_ar: items?.firstNameAr || "",
    last_name_ar: items?.lastNameAr || "",
    user_name: items?.userName || "",
    block: items?.blockId || null,
    road: items?.roadId || null,
    building: items?.buildingId || null,
    nationality: items?.nationalityId || null,
    password: "", // Add empty password for edit mode
    c_password: "", // Add empty confirm password for edit mode
  });
};
// Example type for your form values (customize based on actual form fields)
interface StaffFormValues {
  employee_id: string;
  first_name: string;
  last_name: string;
  first_name_ar: string;
  last_name_ar: string;
  name: string;
  email: string;
  phone: string;
  user_name: string;
  password: string;
  c_password: string;
  profile_pic: File | null;

  // Make these fields optional if they are not always required
  address_line_one?: string;
  address_line_two?: string;

  block: any; // Add actual types if possible
  road: any; // Add actual types if possible
  building: any; // Add actual types if possible
  nationality: any; // Add actual types if possible
}

interface AddModalInitialValues extends Partial<StaffFormValues> {
  id?: number;
}

export const SubmitHandler = (
  values: StaffFormValues,
  actions: any,
  toggle: () => void,
  addModalInitialValues: AddModalInitialValues
) => {
  const {
    name,
    address_line_one,
    address_line_two,
    c_password,
    block,
    road,
    building,
    nationality,
    ...rest
  } = values
  const payload = {
    ...rest,
    block_id: block || null,
    road_id: road || null,
    building_id: building || null,
    nationality_id: nationality || null
  }
  
  if (addModalInitialValues?.id) {
    api
      .put(
        `/customer/corporate/staff/update/${addModalInitialValues?.id}`,
        payload,
        true
      )
      .then(async function ([success, response]: any) {
        if (success) {
          toast.success("Saved");
          actions?.setSubmitting(false);
          toggle();
          // refetch();
        } else toast.error(response?.message);
      })
      .catch((err) => {
        actions?.setSubmitting(false);
        if (err?.response?.data?.message) {
          toast.error(err?.response?.data?.message);
        } else {
          //   toast.error(backend_error);
        }
      });
  } else {
    api
      .post("/customer/corporate/staff/create", payload, true)
      .then(async function ([success, response]: any) {
        if (success) {
          toast.success("Saved");
          actions?.setSubmitting(false);
          toggle();
          // refetch();
        } else toast.error(response?.message);
      })
      .catch((err) => {
        actions?.setSubmitting(false);
        if (err?.response?.data?.message) {
          toast.error(err?.response?.data?.message);
        } else {
          //   toast.error(backend_error);
        }
      });
  }
};
