<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, watch, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { cloneDeep } from 'lodash-es'

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  },
  menuList: {
    type: Array,
    default: () => [{ label: '顶级', value: 0 }]
  }
})

const formSchema = ref<FormSchema[]>([
  {
    field: 'title',
    label: '名称',
    value: '',
    component: 'Input',
    componentProps: {
      placeholder: '请输入名称'
    }
  },
  {
    field: 'eng_title',
    label: '英文名',
    value: '',
    component: 'Input',
    componentProps: {
      placeholder: '请输入英文名'
    }
  },
  {
    field: 'pid',
    label: '父级菜单',
    component: 'TreeSelect',
    value: 0,
    componentProps: {
      nodeKey: 'id',
      props: {
        label: 'title',
        value: 'id',
        children: 'children'
      },
      highlightCurrent: true,
      expandOnClickNode: false,
      checkStrictly: true,
      checkOnClickNode: true,
      clearable: true
    },
    optionApi: async () => {
      let ar: any[] = [{ title: '顶级', id: 0 }]
      if (props.menuList.length > 1) {
        ar = ar.concat(...props.menuList)
      }
      return ar
    }
  },
  {
    field: 'icon',
    label: '图标',
    value: '',
    component: 'IconPicker',
    componentProps: {
      placeholder: '请选择图标'
    }
  },

  {
    field: 'info',
    label: '描述',
    value: '',
    component: 'Input',
    componentProps: {
      placeholder: '请输入描述内容'
    }
  },
  {
    field: 'sort',
    label: '排序',
    value: '',
    component: 'Input',
    componentProps: {
      placeholder: '请输入排序',
      type: 'number'
    }
  },
  {
    field: 'is_store',
    label: '类型',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '默认',
          value: ''
        },
        {
          label: '非门店',
          value: 0
        },
        {
          label: '门店',
          value: 1
        }
      ]
    }
  },
  {
    field: 'status',
    label: '状态',
    component: 'Switch',
    value: true
  },
  {
    field: 'id',
    label: 'id',
    hidden: true,
    component: 'Input',
    componentProps: {
      type: 'number'
    }
  }
])

const rules = ref({
  eng_title: [required('请填写英文名')],
  title: [required('请填写名称')]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    return formData
  }
}

watch(
  () => props.currentRow,
  async (value) => {
    if (!value) return
    const currentRow = cloneDeep(value)
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="formSchema" />
</template>
