<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="50%">
    <BasicForm @register="registerForm"> </BasicForm>
  </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 { submiSlyj } from '../api';
  // Emits声明

  const emit = defineEmits(['register', 'success']);

  const isUpdate = ref(true);
  const AdviceFormSchema: FormSchema[] = [
    { label: '污染地块ID', field: 'wrdkid', required: true, component: 'Input', show: false },
    { label: '所处阶段编码', field: 'scjdbm', required: true, component: 'Input', show: false },
    { label: 'pgtype', field: 'pgtype', required: true, component: 'Input', show: false },
    { label: '填报阶段编码', field: 'jdnzt', required: true, component: 'Input', show: false },

    {
      label: '评审方式',
      required: true,
      field: 'psfs',
      component: 'RadioGroup',
      componentProps: ({ formModel, formActionType }) => {
        return {
          options: [
            { label: '不评审', value: 1 },
            { label: '提交', value: 2 },
            { label: '市级评审', value: 3 },
            { label: '省级评审', value: 4 },
          ],
          onChange: (v) => {
            const { updateSchema } = formActionType;
            if (formModel.psfs == 1) {
              updateSchema({
                field: 'clyj',
                show: true,
              });
            } else {
              updateSchema({
                field: 'clyj',
                show: false,
              });
            }
          },
        };
      },
    },
    {
      label: '报告结论',
      required: true,
      field: 'clyj',
      show: ({ values }) => {
        return values.psfs == 1 ? true : false;
      },
      component: 'Select',
      componentProps: {
        options: [{ label: '达到土壤污染风险评估报告确定的风险管控、修复目标目可以安全利用', value: 1 }],
      },
    },
  ];
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 220,
    schemas: AdviceFormSchema,
    showActionButtonGroup: false,
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    if (unref(isUpdate)) {
      //表单赋值
      await setFieldsValue({
        ...data.record,
      });
    }
  });
  //设置标题
  const title = ref('请选择评审方式');
  //表单提交事件
  async function handleSubmit() {
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      await submiSlyj(values);
      //关闭弹窗
      closeModal();
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

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