import Content from "./Content";
import DateBirthday from "./DateBirthday";
import Field from "./Field";
import {
  gender,
  blood_group,
  marital_status,
  education,
  job,
} from "./listSelector";
import AutoSelector from "@/app/component/element/AutoSelector";

function Form({ data, userInfo, insurance, changeData, prevData }) {
  const findVal = (list, name) =>
    data && list && list.length && list.find((g) => g.id === data[name]);

  return (
    <div className="grid grid-cols-1 lg:grid-cols-2 gap-[24px]">
      <Content title="شماره موبایل" isConfirmed={true}>
        <Field
          isTransparent={true}
          iconGray={true}
          type="number"
          changeData={changeData}
          data={userInfo?.username}
          disable={true}
          name="work_phone"
        />
      </Content>
      <Content title="کد ملی" isConfirmed={false}>
        <Field
          isTransparent={true}
          iconGray={true}
          type="number"
          changeData={changeData}
          data={data?.national_code}
          name="national_code"
        />
      </Content>
      <Content title="نام و نام خانوادگی" isConfirmed={false}>
        <Field
          isTransparent={true}
          iconGray={true}
          changeData={changeData}
          data={data?.name}
          name="name"
        />
      </Content>
      <Content title="جنسیت">
        <AutoSelector
          updateData={(name, data) =>
            changeData(data === "زن" ? "female" : "male", "value", name)
          }
          value={findVal(gender, "gender")}
          label="جنسیت"
          name="gender"
          list={gender}
        />
      </Content>
      <Content title="تاریخ تولد">
        <DateBirthday
          data={data?.birthday}
          isConfirmed={false}
          changeData={changeData}
        />
      </Content>
      <Content title="نام پدر" isConfirmed={false}>
        <Field
          isTransparent={true}
          iconGray={true}
          changeData={changeData}
          data={data?.fathers_name}
          name="fathers_name"
        />
      </Content>
      <Content title="نوع بیمه">
        <AutoSelector
          updateData={(name, val) => {
            changeData([Number(val.id)], "value", name);
          }}
          value={findVal(insurance, "basic_insurance")}
          label="نوع بیمه"
          name="basic_insurance"
          list={insurance}
        />
      </Content>
      <Content title="شماره بیمه" isConfirmed={false}>
        <Field
          isTransparent={true}
          iconGray={true}
          type="number"
          changeData={changeData}
          data={data?.supplementary_insurance}
          name="supplementary_insurance"
        />
      </Content>
      <Content title="گروه خونی">
        <AutoSelector
          updateData={(name, value) =>
            changeData(
              blood_group.find((g) => g.label === value).id,
              "value",
              name
            )
          }
          value={findVal(blood_group, "blood_type")}
          label="گروه خونی"
          name="blood_type"
          list={blood_group}
        />
      </Content>
      <Content title="وضعیت تاهل">
        <AutoSelector
          updateData={(name, value) =>
            changeData(value === "متاهل" ? "married" : "single", "value", name)
          }
          value={findVal(marital_status, "marital_status")}
          label="وضعیت تاهل"
          name="marital_status"
          list={marital_status}
        />
      </Content>
      <Content title="تلفن منزل" isConfirmed={false}>
        <Field
          isTransparent={true}
          iconGray={true}
          type="number"
          changeData={changeData}
          data={data?.home_phone}
          name="home_phone"
        />
      </Content>
      <Content title="تحصیلات">
        <AutoSelector
          updateData={(name, value) =>
            changeData(
              education.find((g) => g.label === value).id,
              "value",
              name
            )
          }
          value={findVal(education, "education")}
          label="تحصیلات"
          name="education"
          list={education}
        />
      </Content>
      <Content title="شغل">
        <AutoSelector
          updateData={(name, value) =>
            changeData(value === "آزاد" ? "azad" : "dolati", "value", name)
          }
          value={findVal(job, "job")}
          label="شغل"
          name="job"
          list={job}
        />
      </Content>
      <Content title="تلفن محل کار" isConfirmed={false}>
        <Field
          isTransparent={true}
          type="number"
          iconGray={true}
          changeData={changeData}
          data={data?.work_phone}
          name="work_phone"
        />
      </Content>
    </div>
  );
}

export default Form;
