<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="风险管控措施落实情况" @ok="handleSubmit" width="40%" :minHeight="360">
    <template #footer></template>
    <BasicForm
      @register="registerForm"
      v-if="isUpdate"
    />
    <div class="btnBOx" v-if="isUpdate">
      <a-button @click="closeModal">取消</a-button>
      <a-button type="primary" @click="handleSubmit">确认</a-button>
    </div>
    <div v-show="auditlistData.length > 0" >
      <!-- <label style="margin-bottom: 10px; display: block">风险管控措施落实情况历史</label> -->
      <div class="hiistory">
        <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.lsnf }}</div>
          </div>
          <div class="list">
            <div class="name">备注</div>
            <div class="value">{{ item.bz }}</div>
          </div>
          <div class="list">
            <div class="name">风险管控措施落实材料</div>
            <div class="value"><a-button type="primary" v-if="item.lscl" @click="downFile(item.lscl)">下载</a-button> </div>
          </div>
        </div>
      </div>
    </div>
    <div class="empty" style="text-align: center; line-height:200px;font-size:40px;" v-if="auditlistData.length == 0">
      暂无数据
    </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 { BasicColumn, FormSchema } from '/@/components/Table';
  import { downloadFile, getZbkfly, addZbkfly } from '/@/api/common/api';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  const lsqkformSchema: FormSchema[] = [
  // TODO 主键隐藏字段，目前写死为ID
  { label: '', field: 'id', component: 'Input', show: false },
  { label: '', field: 'wrdkbm', component: 'Input', show: false },
  { label: '', field: 'zbkflydk', component: 'Input', show: false },
  {
    label: '风险管控措施落实年份',
    field: 'lsnf',
    component: 'DatePicker',
    componentProps: {
      picker: 'year',
      style: { width: '100%' },
      format:'YYYY',
      valueFormat:'YYYY',
    },
    required: true,
  },
  {
    label: '风险管控措施落实材料',
    field: 'lscl',
    component: 'JUpload',
    required: true,
    componentProps: {
      showUploadList: false,
      fileName: '风险管控措施落实材料',
      accept: '.pdf,.zip',
      beforeUpload: (file) => {
        let isshow = false;
        const isFile = file.name.split('.')[file.name.split('.').length - 1];
        console.log(file, 'file');
        if (isFile == 'pdf' || isFile == 'zip' || isFile == 'PDF') {
          isshow = true;
        } else {
          createMessage.error('只能上传.pdf/.zip格式的文件');
          isshow = false;
        }
        return isshow;
      },
    },
  },
  {
    label: '备注',
    field: 'bz',
    component: 'InputTextArea',
    required: true,
    componentProps: {
      allowClear: true,
      placeholder: '说明风险管控措施实施的时间、管控措施、主要成效等内容',
      autoSize: {
        //最小显示行数
        minRows: 4,
      },
    },
  },
];
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 150,
    schemas: lsqkformSchema,
    showActionButtonGroup: false,
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    //表单赋值
    
    await setFieldsValue({
      ...data.record,
    });
    // }
    getAuditlist(data.record.wrdkbm);
  });

  const auditlistData = ref([]);
  const getAuditlist = (wrdkbm) => {
    auditlistData.value = [];
    getZbkfly({ wrdkbm: wrdkbm }).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 addZbkfly(values);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success', { isUpdate: isUpdate.value, values });
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style lang="less" scoped>
.hiistory {
  max-height: 400px;
  overflow-y: scroll;
}
.btnBOx {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: 20px;
  .ant-btn {
    margin: 0 10px;
  }
}
  .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>
