<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="treatmentProposal">
            <a-checkbox-group v-model="form.treatmentProposal">
              <a-checkbox
                :key="item.code"
                :value="item.code"
                class="mb-xs"
                v-for="item in codes.ortherWallType"
              >{{ item.value }}</a-checkbox>
            </a-checkbox-group>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item class="ant-row-flex" label="面积" prop="unitHouseholds">
            <a-row type="flex">
              <a-col flex="1">
                <a-input-number
                  :min="0"
                  :step="1"
                  class="w-100-p"
                  placeholder="请输入面积"
                  v-model="form.unitHouseholds"
                />
              </a-col>
              <a-col>
                <span class="yo-addon">m²</span>
              </a-col>
            </a-row>
          </a-form-model-item>
        </a-col>
        <a-col :span="12"></a-col>
        <a-col :span="12">
          <a-form-model-item class="ant-row-flex" label="幕墙施工图审核单位" prop="unitHouseholds">
            <a-input />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item class="ant-row-flex" label="幕墙四性检测单位" prop="unitHouseholds">
            <a-input />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item class="ant-row-flex" label="幕墙维护单位" prop="unitHouseholds">
            <a-input />
          </a-form-model-item>
        </a-col>
        <a-col :span="12"></a-col>
        <a-col :span="12">
          <a-form-model-item class="ant-row-flex" label="负责人" prop="unitHouseholds">
            <a-input />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item class="ant-row-flex" label="负责人电话" prop="unitHouseholds">
            <a-input />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item class="ant-row-flex" label="联系人" prop="unitHouseholds">
            <a-input />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item class="ant-row-flex" label="联系人电话" prop="unitHouseholds">
            <a-input />
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="幕墙鉴定情况" prop="treatmentProposal">
            <a-radio-group button-style="solid" v-model="form.treatmentProposal">
              <a-radio-button
                :key="item.code"
                :value="item.code"
                v-for="item in codes.wallIdentification"
              >{{ item.value }}</a-radio-button>
            </a-radio-group>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="幕墙解危处置情况" prop="unitHouseholds">
            <a-textarea auto-size />
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="幕墙使用维护说明书" prop="unitHouseholds">
            <a-row :gutter="16" type="flex">
              <a-col>
                <a-switch />
              </a-col>
              <a-col flex="1">
                <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-col>
            </a-row>
          </a-form-model-item>
        </a-col>
        <a-col :span="24">
          <a-form-model-item class="ant-row-flex" label="幕墙安全使用管理制度" prop="unitHouseholds">
            <a-row :gutter="16" type="flex">
              <a-col>
                <a-switch />
              </a-col>
              <a-col flex="1">
                <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-col>
            </a-row>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-spin>
  </a-form-model>
</template>
<script>
/* 表单内容默认值 */
const defaultForm = {
  /* ... */
};

export default {
  props: ['param'],

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

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

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

      /** 其他成员属性 */
      /* ... */
      codes: {
        ortherWallType: [],
        wallIdentification: [],
      },
    };
  },

  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获取一些异步数据 */
      /* ... */
      await this.onLoadCodes();
      this.loading = false;
    },

    /** 当前组件的其他方法 */
    /* ... */
    onLoadCodes() {
      return this.$api
        .sysDictTypeDropDowns({
          code: ['dic_house_orther_wall_type', 'dic_house_wall_identification'],
        })
        .then(({ data: { dic_house_orther_wall_type, dic_house_wall_identification } }) => {
          this.codes = {
            ortherWallType: dic_house_orther_wall_type,
            wallIdentification: dic_house_wall_identification,
          };
        });
    },

    /* 上传下载文件相关 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>