<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 { getWorkFlowVisiableButton } from '@/api/csck/CsckFlow';

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  },
  actionType: {
    type: String,
    default: 'submit'
  }
})
const formSchema = reactive<FormSchema[]>([
  {
    field: 'systemDesc',
    label: '审批意见：',
    component: 'Input',
    colProps: { span: 24},
    componentProps: {
      type: 'textarea',
      rows: 6
    },
    hidden:true,
    value:'审核'
  },
  {
    field: 'systemEdit',
    label: '操作：',
    component: 'RadioGroup',
    optionApi: async () => {
      return props.actionType === 'reject' ? [
        {
          label: '打回',
          value: '0'
        }
      ] : [
        {
          label: '通过',
          value: '1'
        }
      ]
    },
    value: props.actionType === 'reject' ? '0' : '1'
  }
])

const rules = reactive({
  systemEdit: [required()],
  systemDesc: [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>

