<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="自然资源部门审核" @ok="handleSubmit" width="40%">
    <BasicForm
      @register="registerForm"
      v-if="isUpdate"
    />
    <div>
      <label for="" style="margin-bottom: 10px; display: block">审核历史</label>
      <div class="hiistory" v-show="auditlistData.length > 0">
        <div class="historyListBox" v-for="(item, index) in auditlistData" :key="index + 'shls'">
          <!-- 在这里添加审核历史的展示内容 -->
          <div class="list">
            <div class="name">处理时间</div>
            <div class="value">{{ item.createTime }}</div>
          </div>
          <div class="list">
            <div class="name">审核情况</div>
            <div class="value">{{ item.audits }}</div>
          </div>
          <div class="list">
            <div class="name">意见</div>
            <div class="value">{{ item.remark }}</div>
          </div>
          <div class="list">
            <div class="name">文件</div>
            <div class="value"><a-button type="primary" v-if="item.wjlj" @click="downFile(item.wjlj)">下载</a-button> </div>
          </div>
        </div>
      </div>
    </div>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { ZgshformSchema } from '../data';
  import { saveCityAudit, findZrzyjList } from '../api';
  import { downloadFile } from '/@/api/common/api';
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 150,
    schemas: ZgshformSchema,
    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)) {
    //表单赋值
    const formData = {
      code: data.record.cityCode,
      jd: data.record.jd,
    };
    await setFieldsValue({
      ...formData,
    });
    // }
    getAuditlist(data.record.jd);
  });

  const auditlistData = ref([]);
  const getAuditlist = (jd) => {
    findZrzyjList({ jd: jd }).then((res) => {
      auditlistData.value = res;
    });
  };

  const downFile = (text) => {
    downloadFile('/sys/common/static' + text, text.split('/')[text.split('/').length - 1]);
  };

  //表单提交事件
  async function handleSubmit(v) {
    if (!isUpdate.value) {
      emit('success', { isUpdate: isUpdate.value });
      closeModal();
      return;
    }
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      await saveCityAudit(values);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success', { isUpdate: isUpdate.value, values });
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style lang="less" scoped>
.hiistory {
  max-height: 400px;
  overflow-y: scroll;
}
  .historyListBox {
    .list {
      display: flex;
      align-items: center;
      border: 1px solid #999999;
      .name {
        width: 180px;
        font-weight: bolder;
        text-align: center;
        line-height: 32px;
        border-right: 1px solid #999999;
      }
      .value {
        flex: 1;
        padding-left: 10px;
      }
    }
    .list:first-child {
      background-color: #c7c2c2;
    }
  }
</style>
