import { useEffect } from "react"
import { usePage } from "@inertiajs/react"
import useFormBuilder from "@/utils/useFormBuilder"
import FormBuilder from "@/Components/FormBuilder"
import { slugify, useSearchParams } from "@/utils/format"

function MainForm({ isEdit = false, formOptions = {}, BackButton = null }) {
  const formConfig = { isEdit, getProperties, formOptions }
  const builderProps = useFormBuilder(formConfig)
  const { data, setData } = builderProps.form

  return <FormBuilder {...builderProps} BackButton={BackButton} />
}

const getProperties = (state) => {
  const { props, data, setData } = state
  const { params } = useSearchParams(props.location)

  const properties = [
    [
      {
        form: "select",
        props: {
          name: "company_id",
          label: "Perusahaan",
          options: props.companies,
          isReactSelect: true,
          isVirtualized: true,
          handleChange: (e) => setData("company_id", e?.value),
        },
      },
      {
        form: "input",
        props: {
          name: "code",
          label: "Kode",
          type: "text",
        },
      },
    ],
    [
      {
        form: "select",
        props: {
          name: "user_id",
          label: "Pengguna",
          options: props.users,
          isReactSelect: true,
          isVirtualized: true,
          handleChange: (e) => setData("user_id", e?.value),
        },
      },
      {
        form: "select",
        props: {
          name: "department_id",
          label: "Departemen",
          options: props.departments,
          isReactSelect: true,
          isVirtualized: true,
          handleChange: (e) => setData("department_id", e?.value),
        },
      },
    ],
    [
      {
        form: "input",
        props: {
          name: "debit_amount",
          label: "Debit",
          type: "number",
        },
      },
      {
        form: "input",
        props: {
          name: "credit_amount",
          label: "Kredit",
          type: "number",
        },
      },
    ],
    [
      {
        form: "input",
        props: {
          name: "year",
          label: "Tahun",
          type: "number",
        },
      },
      {
        form: "input",
        props: {
          name: "month",
          label: "Bulan",
          type: "number",
        },
      },
    ],
  ]

  return properties
}

const PropertiesProvider = ({ children }) => {
  return children(getProperties)
}

MainForm.PropertiesProvider = PropertiesProvider

const BalanceMainForm = MainForm
export default BalanceMainForm
