import React, { useEffect } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import {
  useCreateExpenseType,
  useUpdateExpenseType,
} from "../../hooks/masterHook/useMaster";

const ExpnseSchema = z.object({
  name: z.string().min(1, { message: "Name is required" }),
  noOfPersonsRequired: z.boolean().default(false),
  description: z.string().min(1, { message: "Description is required" }),
});

const ManageExpenseType = ({ data = null, onClose }) => {
  const {
    register,
    handleSubmit,
    reset,
    formState: { errors },
  } = useForm({
    resolver: zodResolver(ExpnseSchema),
    defaultValues: { name: "", noOfPersonsRequired: false, description: "" },
  });
  const { mutate: UpdateExpenseType, isPending:isPendingUpdate } = useUpdateExpenseType(
    () => onClose?.()
  );
  const { mutate: CreateExpenseType, isPending } = useCreateExpenseType(() =>
    onClose?.()
  );

  const onSubmit = (payload) => {
    if (data) {
      UpdateExpenseType({
        id: data.id,
        payload: { ...payload, id: data.id },
      });
    } else {
      CreateExpenseType(payload);
    }
  };

  useEffect(() => {
    if (data) {
      reset({
        name: data.name ?? "",
        noOfPersonsRequired: data.noOfPersonsRequired ?? false,
        description: data.description ?? "",
      });
    }
  }, [data]);
  return (
    <form className="row g-2" onSubmit={handleSubmit(onSubmit)}>
      <div className="col-12 col-md-12">
        <label className="form-label">Expesne Type Name</label>
        <input
          type="text"
          {...register("name")}
          className={`form-control ${errors.name ? "is-invalids" : ""}`}
        />
        {errors.name && <p className="danger-text">{errors.name.message}</p>}
      </div>
      <div className="col-12 col-md-12">
        <label className="form-label" htmlFor="description">
          Description
        </label>
        <textarea
          rows="3"
          {...register("description")}
          className={`form-control ${errors.description ? "is-invalids" : ""}`}
        ></textarea>

        {errors.description && (
          <p className="danger-text">{errors.description.message}</p>
        )}
      </div>
      <div className="col-12 col-md-12 d-flex align-items-center">
        <label className="from-label">No. of Persons Required </label>{" "}
        <input
          type="checkbox"
          className="form-check-input  ms-2"
          {...register("noOfPersonsRequired")}
        />
      </div>
      <div className="col-12 text-center">
        <button
          type="submit"
          className="btn btn-sm btn-primary me-3"
          disabled={isPending || isPendingUpdate}
        >
          {isPending || isPendingUpdate
            ? "Please Wait..."
            : data
            ? "Update"
            : "Submit"}
        </button>
        <button
          type="reset"
          className="btn btn-sm btn-label-secondary "
          data-bs-dismiss="modal"
          aria-label="Close"
          disabled={isPending || isPendingUpdate}
          onClick={()=>onClose()}
        >
          Cancel
        </button>
      </div>
    </form>
  );
};

export default ManageExpenseType;
