<template>
  <!-- 
    整页表单的分片表单
    v 1.0
    2021-05-12
    Lufthafen
  -->
  <a-form-model
    :label-col="labelCol"
    :model="form"
    :rules="rules"
    :wrapper-col="wrapperCol"
    ref="form"
  >
    <a-spin :spinning="loading">
      <a-icon slot="indicator" spin type="loading" />
      <!-- 表单控件 -->
      <!-- ... -->
      <a-row :gutter="16" type="flex">
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="立项文件" prop="anEntryDocument">
            <a-upload
              :custom-request="onFileUpload"
              :file-list="form.anEntryDocument"
              :showUploadList=" { showRemoveIcon: true, showDownloadIcon: true }"
              @change="(data) => onFileChange(data, 'anEntryDocument')"
              @download="onFileDownload"
            >
              <a-button icon="upload" type="dashed">上传立项文件</a-button>
            </a-upload>
          </a-form-model-item>
        </a-col>

        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="规划许可" prop="planningPermission">
            <a-upload
              :custom-request="onFileUpload"
              :file-list="form.planningPermission"
              :showUploadList=" { showRemoveIcon: true, showDownloadIcon: true }"
              @change="(data) => onFileChange(data, 'planningPermission')"
              @download="onFileDownload"
            >
              <a-button icon="upload" type="dashed">上传规划许可</a-button>
            </a-upload>
          </a-form-model-item>
        </a-col>

        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="竣工验收备案" prop="completionRecord">
            <a-upload
              :custom-request="onFileUpload"
              :file-list="form.completionRecord"
              :showUploadList=" { showRemoveIcon: true, showDownloadIcon: true }"
              @change="(data) => onFileChange(data, 'completionRecord')"
              @download="onFileDownload"
            >
              <a-button icon="upload" type="dashed">上传竣工验收备案</a-button>
            </a-upload>
          </a-form-model-item>
        </a-col>

        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="监理文件" prop="monitor">
            <a-upload
              :custom-request="onFileUpload"
              :file-list="form.monitor"
              :showUploadList=" { showRemoveIcon: true, showDownloadIcon: true }"
              @change="(data) => onFileChange(data, 'monitor')"
              @download="onFileDownload"
            >
              <a-button icon="upload" type="dashed">上传监理文件</a-button>
            </a-upload>
          </a-form-model-item>
        </a-col>

        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="鉴定报告" prop="identificationReport">
            <a-upload
              :custom-request="onFileUpload"
              :file-list="form.identificationReport"
              :showUploadList=" { showRemoveIcon: true, showDownloadIcon: true }"
              @change="(data) => onFileChange(data, 'identificationReport')"
              @download="onFileDownload"
            >
              <a-button icon="upload" type="dashed">上传鉴定报告</a-button>
            </a-upload>
          </a-form-model-item>
        </a-col>

        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="其他附件" prop="otherDocument">
            <a-upload
              :custom-request="onFileUpload"
              :file-list="form.otherDocument"
              :showUploadList=" { showRemoveIcon: true, showDownloadIcon: true }"
              @change="(data) => onFileChange(data, 'otherDocument')"
              @download="onFileDownload"
            >
              <a-button icon="upload" type="dashed">上传其他附件</a-button>
            </a-upload>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-spin>
  </a-form-model>
</template>
<script>
import { GetFileName } from '@/util/file';

/* 表单内容默认值 */
const defaultForm = {
  /* ... */
  anEntryDocument: [],
  planningPermission: [],
  completionRecord: [],
  monitor: [],
  identificationReport: [],
  otherDocument: [],
};

export default {
  props: ['param'],

  data() {
    return {
      labelCol: { flex: '150px' },
      wrapperCol: { flex: '1' },

      /** 表单数据 */
      form: {},
      /** 验证格式 */
      rules: {
        /* ... */
      },

      /** 加载异步数据状态 */
      loading: false,

      /** 其他成员属性 */
      /* ... */
    };
  },

  async created() {
    await this.onInit();
    this.onFillData();
  },

  methods: {
    /**
     * 必要的方法
     * 在打开编辑页时允许填充数据
     */
    onFillData() {
      /** 将默认数据覆盖到form */
      const record = this.param && this.param.record;

      this.form = this.$_.cloneDeep({
        ...defaultForm,
        ...record,
        /** 在此处添加其他默认数据转换 */
        /* ... */
      });
    },

    /**
     * 必要方法
     * 验证表单并获取表单数据
     */
    onGetData() {
      return new Promise((reslove, reject) => {
        this.$refs.form.validate((valid) => {
          if (valid) {
            const record = this.$_.cloneDeep(this.form);

            /** 验证通过后可以对数据进行转换得到想要提交的格式 */
            /* ... */

            reslove(record);
          } else {
            reject();
          }
        });
      });
    },

    /**
     * 必要的方法
     * 在外部窗口进行保存时调用表单验证
     */
    onValidate(callback) {
      this.$refs.form.validate(callback);
    },

    /**
     * 必要的方法
     * 在外部窗口关闭或重置时对表单验证进行初始化
     */
    onResetFields() {
      setTimeout(() => {
        this.$refs.form.resetFields();

        /** 在这里可以初始化当前组件中其他属性 */
        /* ... */
      }, 300);
    },

    /**
     * 必要方法
     * 加载当前表单中所需要的异步数据
     */
    async onInit() {
      this.loading = true;
      /** 可以在这里await获取一些异步数据 */
      /* ... */
      this.loading = false;
    },

    /** 当前组件的其他方法 */
    /* ... */

    /* 上传下载文件相关 Begin */
    async onFileUpload({ file, onProgress, onSuccess, onError }) {
      onProgress();
      const fd = new FormData();
      fd.append('file', file);
      try {
        const { data: fileId } = await this.$api.sysFileInfoUpload(fd);
        onSuccess(fileId);
      } catch {
        onError();
      }
    },
    onFileChange({ fileList }, key) {
      this.form[key] = fileList;
    },
    onFileDownload(file) {
      this.$api.sysFileInfoDownload({ id: file.response }).then((res) => {
        const url = window.URL.createObjectURL(res.data);
        const fileName = GetFileName(res.headers['content-disposition']);
        const a = document.createElement('a');
        a.href = url;
        a.download = fileName;
        a.click();
        window.URL.revokeObjectURL(url);
        a.remove();
      });
    },
    /* 上传图片相关 End */
  },
};
</script>