<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="50%">
    <BasicForm @register="registerForm"> </BasicForm>
    <a-table :dataSource="dataSource" :columns="columns" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, unref } from 'vue';
  import { FormSchema } from '/@/components/Table';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { submiDqr, fhHis } from '@/api/common/api';

  const emit = defineEmits(['register', 'success']);
  const ishow = ref(false);
  const isUpdate = ref(true);
  const AdviceFormSchema: FormSchema[] = [
    { label: '污染地块ID', field: 'wrdkid', required: true, component: 'Input', show: false },
    { label: '所处阶段编码', field: 'scjdbm', component: 'Input', show: false },
    { label: 'pgtype', field: 'pgtype', component: 'Input', show: false },
    { label: '填报阶段编码', field: 'jdnzt', component: 'Input', show: false },
    {
      label: '复核报告是否通过 ',
      field: 'nodeType',
      component: 'Select',
      required: true,
      defaultValue: 1,
      componentProps: {
        options: [
          { label: '复核通过', value: 1 },
          { label: '复核需继续修改', value: 2 },
        ],
        onChange: (v) => {
          if (v == 2) {
            updateSchema({
              label: '退回意见',
              required: true,
              field: 'clyj',
              component: 'InputTextArea',
              show: true,
            });
          } else {
            updateSchema({
              label: '退回意见',
              required: false,
              field: 'clyj',
              component: 'InputTextArea',
              show: false,
            });
          }
        },
      },
    },
    {
      label: '退回意见',
      field: 'clyj',
      component: 'InputTextArea',
      show: false,
    },
  ];
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    labelWidth: 220,
    schemas: AdviceFormSchema,
    showActionButtonGroup: false,
  });
  const datas = ref();
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    //表单赋值
    datas.value = data.record;
    getfhHis({ wrdkid: datas.value.wrdkid, scjdbm: datas.value.scjdbm, jdnzt: datas.value.jdnzt, pgtype: datas.value.pgtype });

    await setFieldsValue({
      ...data.record,
    });
  });
  //设置标题
  const title = ref('复核评审信息');
  const dataSource = ref<any>();
  const columns = [
    {
      title: '记录生成时间',
      dataIndex: 'clsj',
      key: 'clsj',
    },

    {
      title: '复核是否通过',
      dataIndex: 'nodeType',
      key: 'nodeType',
      customRender: ({ record }: any) => {
        if (record.nodeType == '1') {
          return '是';
        } else if (record.nodeType == '2') return '否';
      },
    },
    {
      title: '退回意见',
      dataIndex: 'clyj',
      key: 'clyj',
    },
  ];
  //  获取历史评审
  async function getfhHis(v) {
    console.log(v, 'v');
    dataSource.value = await fhHis(v);
  }
  //表单提交事件
  async function handleSubmit() {
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });
      if (values.nodeType == 1) {
        values.clyj = '复核通过';
      }
      //提交表单
      await submiDqr(values);
      //关闭弹窗
      closeModal();
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style lang="less" scoped></style>
