<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 { JOB_TYPE_LIST,JOB_BUSSINESS_TYPE_LIST } from '../constants'

const { required,lengthRange } = useValidator()

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

const formSchema = reactive<FormSchema[]>([
  {
    field: 'jPid',
    label: '系统编号',
    component: 'Input',
    value:props.systemCode,
    componentProps:{
      disabled:props.actionType != 'add' || props.systemCode
    }
  },
  {
    field: 'jId',
    label: '作业编号',
    component: 'Input',
    componentProps:{
      disabled:props.actionType != 'add'
    }
  },
  {
    field: 'jName',
    label: '任务名称',
    component: 'Input',
  },
  {
    field: 'jType',
    label: '任务类型',
    component: 'Select',
    componentProps: {
      options: JOB_TYPE_LIST
    }
  },
  {
    field: 'jBusinessType',
    label: '业务分类',
    component: 'Select',
    componentProps: {
      options: JOB_BUSSINESS_TYPE_LIST
    }
  },
  
  {
    field: 'jobGroup',
    label: '作业分组',
    component: 'Input'
  },
  // {
  //   field: 'jJobexec',
  //   label: '执行器',
  //   component: 'Input'
  // },
  {
    field: 'jBeforeJob',
    label: '前置作业名称',
    component: 'Input',
    colProps: {
      span: 12
    },
    componentProps:{
      placeholder:"例:|ORDBA|ORDBB|",
    }
  },
  {
    field: '',
    label: '',
    component:"JsonEditor",
    value:'多个任务使用|分隔(例:|ORDBA|ORDBB|)',
    colProps: {
      span: 12
    },
    componentProps:{
      showLineNumber:false,
      itemHeight:32,
      editable:false,
      renderNodeValue:()=>{
        return '多个任务使用|分隔(例:|ORDBA|ORDBB|)'
      }
    }
  },
  {
    field: 'jobRetryTimes',
    label: '失败重试次数',
    component: 'Input',
    value:"3",
    colProps: {
      span: 16
    },
    componentProps:{
      type:"number"
    }
  },
  {
    field: 'jobRetryPeriod',
    label: '失败重试间隔',
    component: 'Input',
    value:"3",
    colProps: {
      span: 16
    },
    componentProps:{
      type:"number"
    }
  },
  {
    field: '',
    label: '(单位：分钟)',
    colProps: {
      span: 7
    },
  },
  {
    field: 'jobPriority',
    label: '作业优先级',
    colProps: {
      span: 24
    },
    component: 'Input'
  },
  {
    field: 'paramJson',
    label: '报文',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      type: 'textarea',
      rows: 4
    }
  },
  
])

const rules = reactive({
  jPid: [required(),lengthRange({min:0,max:50,message:'最大长度为50个字符'})],
  jId: [required(),lengthRange({min:0,max:50,message:'最大长度为50个字符'})],
  jName: [required(),lengthRange({min:0,max:30,message:'最大长度为30个字符'})],
  jType: [required()],
  paramJson:[required(),lengthRange({min:0,max:4000,message:'最大长度为4000个字符'})],
  jobGroup:[lengthRange({min:0,max:20,message:'最大长度为20个字符'})],
  jBeforeJob:[lengthRange({min:0,max:500,message:'最大长度为500个字符'})],
  jobPriority:[lengthRange({min:0,max:50,message:'最大长度为50个字符'})],
})

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>
