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: "input",
        props: {
          name: "name",
          label: "Nama",
          type: "text",
        },
      },
      {
        form: "input",
        props: {
          name: "company_name",
          label: "Nama Perusahaan",
          type: "text",
        },
      },
    ],
    [
      {
        form: "input",
        props: {
          name: "phone",
          label: "Telepon",
          type: "text",
        },
      },
      {
        form: "input",
        props: {
          name: "city",
          label: "Kota",
          type: "text",
        },
      },
    ],
    [
      {
        form: "select",
        props: {
          name: "status",
          label: "Status",
          options: props.status,
          prefix: "vendor.status",
        },
      },
    ],
    [
      {
        form: "textarea",
        props: {
          name: "address",
          label: "Alamat",
        },
      },
    ],
  ]

  return properties
}

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

MainForm.PropertiesProvider = PropertiesProvider

const VendorMainForm = MainForm
export default VendorMainForm
