<template>
  <BasicModal v-bind="$attrs" title="编辑/查看" @register="registerModal" :width="800" destroyOnClose @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm, FormSchema } from '/@/components/Form/index';
  import { saveOrUpdate } from '/@/views/newlib/components/api/AuditTaskReport.api';
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  const row = ref({});
  const formSchemas: FormSchema[] = [
    {
      label: '分组',
      field: 'cgrpPath',
      component: 'Input',
      componentProps: {
        disabled: true,
      },
    },
    {
      label: '检查项',
      field: 'content',
      component: 'Input',
      componentProps: {
        disabled: true,
      },
    },
    {
      label: '合规条目',
      field: 'item',
      component: 'Input',
      componentProps: {
        disabled: true,
      },
    },
    {
      label: '检查分类',
      field: 'tp',
      component: 'Input',
      componentProps: {
        disabled: true,
      },
    },
    {
      label: '检查方法',
      field: 'tp',
      component: 'InputTextArea',
      componentProps: {
        disabled: true,
      },
    },
    {
      label: '当前现状',
      field: 'situation',
      component: 'InputTextArea',
    },
    {
      label: '存在问题',
      field: 'issues',
      component: 'InputTextArea',
    },
    {
      label: '',
      field: 'pid',
      component: 'Input',
      show:false
    },
    {
      label: '',
      field: 'checkItemId',
      component: 'Input',
      show:false
    },
    {
      label: '',
      field: 'checkItemTp',
      component: 'Input',
      show:false
    },
    {
      label: '',
      field: 'checkid',
      component: 'Input',
      show:false
    },
    {
      label: '',
      field: 'projectid',
      component: 'Input',
      show:false
    },
    {
      label: 'id',
      field: 'id',
      component: 'Input',
      show: false,
    },
  ];
  //表单配置
  const [registerForm, { setProps, resetFields, setFieldsValue, validate }] = useForm({
    //labelWidth: 150,
    schemas: formSchemas,
    showActionButtonGroup: false,
    baseColProps: { span: 24 },
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {

    row.value = data.row;
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false });
    await setFieldsValue({
      ...data.row,
    });
    setProps({ disabled: false });
  });
  //表单提交事件
  async function handleSubmit() {
    try {
      let values = await validate();
      values['id'] = row.value.id;
      setModalProps({ confirmLoading: true });
      //提交表单
      isUpdate.value = !!values["id"]?true:false
      const retData = await saveOrUpdate(values, isUpdate.value);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success', retData,row.value);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
