<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" width="50%">
    <template #footer>
      <div class="w-full text-center py-5">
        <a-space wrap>
          <a-button type="primary" class="mr-15" @click="handleSubmit('1')" v-if="mgxxzt != '1'">提交</a-button>
          <a-button type="primary" class="mr-15" @click="handleSubmit('0')" v-if="mgxxzt != '1'">保存</a-button>
          <a-button @click="closeModal()">取消</a-button>
        </a-space>
      </div>
    </template>
    <BasicForm @register="registerForm"> </BasicForm>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';

  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { FormSchema } from '/@/components/Table';
  import { updateZbmgxx } from '/@/api/dkgl.api';

  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  const mgxxzt = ref('');
  const schema: FormSchema[] = [
    { label: '污染地块ID', field: 'wrdkid', component: 'Input', show: false },
    {
      label: '地块名称',
      field: 'wrdkmc',

      component: 'Input',
      componentProps: {
        disabled: true,
      },
    },
    {
      label: '所属区',
      field: 'countryName',
      component: 'Input',
      componentProps: {
        disabled: true,
      },
    },
    {
      label: '具体位置',
      field: 'wrdkdz',
      component: 'Input',
      componentProps: {
        disabled: true,
      },
    },
    {
      label: '地块面积(m²)',
      field: 'zdmj',
      component: 'Input',
      componentProps: {
        disabled: true,
      },
    },
    {
      label: '排查信息',
      field: 'zbmgxx',
      required: true,
      component: 'InputTextArea',
      componentProps: {
        rows: 8,
      },
    },
  ];
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 220,
    schemas: schema,
    // autoAdvancedCol: 2,
    // baseColProps: { span: 12 },
    showActionButtonGroup: false,
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
    mgxxzt.value = data.record.mgxxzt;
    //表单赋值
    await setFieldsValue({
      ...data.record,
    });
  });
  //设置标题
  const title = ref('周边敏感信息');
  //表单提交事件
  async function handleSubmit(v) {
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });
      delete values.wrdkmc;
      delete values.countryName;
      delete values.wrdkdz;
      delete values.zdmj;
      values.mgxxzt = v;
      //提交表单
      await updateZbmgxx(values);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success', { isUpdate: isUpdate.value, values });
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

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