<template>
  <BasicModal v-bind="$attrs" @register="registerModal" okText="提交" :title="title" :closeFunc="handleCloseFunc" @ok="handleSubmit" width="50%">
    <div class="pt-4" :style="{ height: isDetail ? 'auto' : '400px' }"><BasicForm @register="registerForm" /></div>
    <div v-show="isDetail && !isHistory">
      <a-divider orientation="left">历史记录</a-divider>
      <a-table :columns="historyColumns" :data-source="czwcHisList" :pagination="false" bordered size="middle">
        <!--操作栏-->
        <template #action="{ record }">
          <a-button type="link" @click="toDetail(record)">查看</a-button>
        </template>
      </a-table>
    </div>
  </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 { updateZyjhCzwc, findSzdzg, findJsdzg, queryCzwc } from '../api';
  import { useUserStore } from '/@/store/modules/user';
  import { historyColumns } from '../data';
  // Emits声明

  const userStore: any = useUserStore();
  const jszgId = ref<any>({ realname: '' });
  const emit = defineEmits(['register', 'success']);
  const bm = ref();

  const AdviceFormSchema: FormSchema[] = [
    { label: '污染地块ID', field: 'id', component: 'Input', show: false },
    { label: '污染地块ID', field: 'wrdkid', component: 'Input', show: false },
    {
      label: '地块所在地主管部门',
      field: 'szdzgbm',
      required: true,
      component: 'Input',
      componentProps: {
        disabled: true,
      },
    },
    {
      label: '接收地主管部门',
      field: 'jsdzgbm',
      required: true,
      component: 'ApiSelect',
      componentProps: () => {
        if (userStore.userInfo.userType == 'S') bm.value = '';
        return {
          api: findJsdzg,
          params: { wrdkbm: bm.value },
          labelField: 'realname',
          valueField: 'id',
        };
      },
    },
    {
      label: '处置时间',
      field: 'czsj',
      required: true,
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      label: '处置总量',
      field: 'czzl',
      required: true,
      component: 'InputNumber',
    },
    {
      label: '处置措施',
      field: 'czcs',
      required: true,
      component: 'CheckboxGroup',
      componentProps: {
        //options里面由一个一个的radio组成,支持disabled
        options: [
          { label: '水泥窑', value: '水泥窑' },
          { label: '集中处置场所', value: '集中处置场所' },
          { label: '填埋', value: '填埋' },
          { label: '其他', value: '其他' },
        ],
      },
    },
    {
      label: '与转运计划是否有偏移',
      field: 'yzyjhsfypy',
      required: true,
      component: 'Input',
    },
    {
      label: '偏移情况说明',
      field: 'ypyqksm',
      component: 'Input',
    },
  ];
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    labelWidth: 150,
    schemas: AdviceFormSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 12 },
  });
  //设置标题
  const title = ref('处置完成填报');
  const isDetail = ref(false);
  const isHistory = ref(false);

  const res1 =  ref({})
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单

    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
    if (data?.isDetail) {
      isDetail.value = data?.isDetail;
    }

    bm.value = data.record.wrdkbm;
    if (userStore.userInfo.userType == 'S') bm.value = '';
    jszgId.value = await findSzdzg({ wrdkbm: bm.value });
    const res = { szdzgbm: jszgId?.value?.realname };
    res1.value = await queryCzwc({ id: data.record.id });
    czwcHisList.value = res1.value.czwcHisList;
    //表单赋值
    if (res1.value) {
      await setFieldsValue({
        ...res1.value,
        ...res,
      });
    } else {
      await setFieldsValue({
        ...data.record,
        ...res,
      });
    }
    console.log('是否是详情', !data?.showFooter);
    if (!data?.showFooter) {
      let fieldArr = ['jsdzgbm', 'czsj', 'czzl', 'czcs', 'yzyjhsfypy', 'ypyqksm'];
      fieldArr.forEach((item) => {
        updateSchema({
          field: item,
          componentProps: {
            disabled: true,
          },
        });
      });
    }
  });

  //表单提交事件
  async function handleSubmit() {
    try {
      let values = await validate();
      values.szdzgbm = jszgId.value.id;
      values.flag = 1;
      setModalProps({ confirmLoading: true });
      //提交表单

      await updateZyjhCzwc(values);
      //关闭弹窗
      closeModal();
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
  function handleCloseFunc() {
    if (isDetail.value && isHistory.value) {
      isHistory.value = false;
      title.value = '处置完成填报';
      setFieldsValue(res1.value);
      return false;
    }
    return true;
  }
  const czwcHisList = ref([]);
  const toDetail = (record) => {
    isHistory.value = true;
    title.value = '详情';
    setFieldsValue(JSON.parse(record.his));
  };
</script>

<style lang="less" scoped>
  :deep(.ant-input-number) {
    width: 100%;
  }
</style>
