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

const { required } = useValidator()

const actionType = ref('add')

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

const formSchema = reactive<FormSchema[]>([
  {
    field: 'jId',
    label: '作业ID',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  },
  {
    field: 'jName',
    label: '任务名称',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  },
  {
    field: 'jType',
    label: '任务类型',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  },
  {
    field: 'jSystem',
    label: '系统',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  },
  {
    field: 'createUser',
    label: '创建人',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  },
  {
    field: 'createTime',
    label: '创建时间',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  },
  {
    field: 'updateTime',
    label: '更新时间',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  },
  {
    field: 'createOrgan',
    label: '创建机构',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  },
  {
    field: 'jPath',
    label: '下载文件路径',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  },
  {
    field: 'jRely',
    label: '依赖关系',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  },
  {
    field: 'paramJson',
    label: '参数json字符串',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  },
  {
    field: 'repCode',
    label: '相关报表编号',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  },
  {
    field: 'jJobexec',
    label: '执行器,V2',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  },
  {
    field: 'jDescription',
    label: '描述',
    component: 'Input',
    componentProps: {
      disabled: actionType.value === 'update'
    },
    hidden: actionType.value === 'add' || actionType.value === 'update'
  }
])

const rules = reactive({
  component: [required()],
  path: [required()],
  jId: [required()],
  jName: [required()],
  jType: [required()],
  jSystem: [required()],
  createUser: [required()],
  createTime: [required()],
  updateTime: [required()],
  createOrgan: [required()],
  jPath: [required()],
  jRely: [required()],
  paramJson: [required()],
  repCode: [required()],
  jJobexec: [required()],
  jDescription: [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,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

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