<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton } from 'element-plus'

const { t } = useI18n()

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  }
})

const formSchema = reactive<FormSchema[]>([
  {
    field: 'name',
    label: '指标名称',
    component: 'Input'
  },
  {
    field: 'id',
    label: '指标编码',
    component: 'Input'
  },
  {
    field: 'ruleStrength',
    label: '规则强度',
    component: 'Select',
    componentProps: {
      style: {
        width: '100%'
      },
      options: [
        {
          label: '硬性',
          value: '1'
        },
        {
          label: '软性',
          value: '2'
        }
      ]
    }
  },
  {
    field: 'status',
    label: '规则状态',
    component: 'Select',
    value: '1',
    componentProps: {
      style: {
        width: '100%'
      },
      options: [
        {
          label: '启用',
          value: '1'
        },
        {
          label: '停用',
          value: '2'
        }
      ]
    }
  },
  {
    field: 'id1',
    label: '业务类型',
    component: 'Input'
  },
  {
    field: 'id1',
    label: '对照编码',
    component: 'Input'
  },
  {
    field: 'check1',
    label: '保存前校验',
    component: 'Switch',
    colProps: {
      span: 8
    },
    componentProps: {
      activeText: '是',
      inactiveText: '否'
    },
    value: true
  },
  {
    field: 'check2',
    label: '导入前校验',
    component: 'Switch',
    colProps: {
      span: 8
    },
    componentProps: {
      activeText: '是',
      inactiveText: '否'
    },
    value: true
  },
  {
    field: 'check3',
    label: '入库前校验',
    component: 'Switch',
    colProps: {
      span: 8
    },
    componentProps: {
      activeText: '是',
      inactiveText: '否'
    },
    value: true
  },
  {
    field: 'id1',
    label: '非空函数',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      slots: {
        append: () => {
          return <ElButton>编辑函数</ElButton>
        }
      }
    }
  },
  {
    field: 'id1',
    label: '过滤条件',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      type: 'textarea'
    }
  },
  {
    field: 'id1',
    label: '规则说明',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      type: 'textarea'
    }
  }
])

const rules = reactive({
  name: [required()],
  id: [required()]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose, setSchema } = 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,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

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