<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'

const { required } = useValidator()

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

const formSchema = reactive<FormSchema[]>([
  {
    field: 'jName',
    label: '作业名称',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps:{
      disabled:true
    }
  },
  {
    field: 'OrganId',
    label: '机构号',
    component: 'Input',
    colProps: {
      span: 24
    }
  },
  {
    field: 'V_DATE',
    label: '报表日期',
    component: 'DatePicker',
    componentProps: {
      type: 'date',
      valueFormat: 'YYYYMMDD',
      style: {
        width: '100%'
      }
    },
    colProps: {
      span: 24
    }
  },
  {
    field: 'jModuleState',
    label: '是否执行子作业',
    component: 'Switch',
    value: false,
    componentProps:{
      style:{"--el-switch-on-color": "#13ce66", "--el-switch-off-color": "#ff4949"},
      on:{
        change(value){
          formSchema[4].componentProps.renderNodeValue = ()=>{return value?"执行所有子作业":"不执行子作业"};
          console.log(formSchema,"jModuleState");
        }
      }
    },
    colProps: {
      xs:12,
      sm:10,
      md:8,
      lg:6,
      xl:5
    },
  },
  {
    field: '',
    label: '',//默认是不带依赖的情况
    component:"JsonEditor",
    value:'不执行子作业',
    colProps: {
      span: 12
    },
    componentProps:{
      showLineNumber:false,
      itemHeight:32,
      editable:false,
      renderNodeValue:()=>{
        return '不执行子作业'
      }
    }
  }
])

const rules = reactive({
  jName:[required()],
  OrganId: [required()],
  V_DATE: [required()],
  jModuleState:[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>
