<template>
  <a-modal v-model="visible" :title="$t('global-btn.upload.test.report')" @ok="save" @cancel="closeModal" :width="600" :maskClosable="false">
    <a-row :gutter="24">
      <a-form layout="vertical">
        <!-- 试验报告编号 -->
        <a-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
          <a-form-item :label="$t('verity.dvp.test.report.number')" required>
            <a-input
              allowClear
              :maxLength="50"
              v-model="formData.reportNo"
              :placeholder="$t('global-btn.please-enter') + $t('verity.dvp.test.report.number')"
            />
          </a-form-item>
        </a-col>
        <!-- 上传材料报告 -->
        <a-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
          <a-form-item :label="$t('global-btn.upload') + $t('dvp.full.vehicle.dvp.test-report')" required>
            <a-upload-dragger
              name="file"
              :multiple="false"
              action=""
              :customRequest='editUploadFile'
              :file-list="fileList"
              :remove='editRemoveFile'
            >
              <p class="ant-upload-drag-icon">
                <a-icon type="inbox"/>
              </p>
              <p class="ant-upload-text">
                {{ $t('dvp.full.vehicle.dvp.click-drag-file-area') }}
              </p>
              <p class="ant-upload-hint">
                {{ $t('dvp.full.vehicle.dvp.only-support-single-upload') }}
              </p>
            </a-upload-dragger>
          </a-form-item>
        </a-col>
        <!-- 零部件编号 -->
        <a-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
          <a-form-item :label="$t('verity.dvp.part.number')" required>
            <a-select
              allowClear
              mode="multiple"
              v-model="formData.partNo"
              :placeholder="$t('global-btn.please-select') + $t('verity.dvp.part.number')"
              :options="partNoList"
              showSearch
              @change="partNoChange"
            >
              <a-icon slot="suffixIcon" type="search"/>
            </a-select>
          </a-form-item>
        </a-col>
        <!-- 零部件名称 -->
        <a-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
          <a-form-item :label="$t('verity.dvp.part.name')" required>
            <a-select
              mode="multiple"
              :disabled="true"
              v-model="formData.partName"
              :options="partNameList"/>
          </a-form-item>
        </a-col>
        <!-- 试验项目 -->
        <!-- <a-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24">
          <a-form-item :label="$t('plan.dvp.test-project')" required>
            <a-select
              allowClear
              v-model="formData.testProject"
              :placeholder="$t('global-btn.please-select') + $t('plan.dvp.test-project')"
              showSearch
              @change="partProjectChange"
            >
              <a-icon slot="suffixIcon" type="search"/>
              <a-select-option
                v-for="item in partProjectList"
                :key="item.id"
                :value="item.value"
              >
                {{ item.value }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col> -->
      </a-form>
    </a-row>
  </a-modal>
</template>

<script>
export default {
  name: 'uploadTestReport',
  data() {
    return {
      visible: false,
      formData: {
        reportNo: undefined,
        partNo: undefined,
        partName: undefined
      },
      /**文件集合 */
      fileList: [],
      /**零部件编号字典 */
      partNoList: [],
      /**零部件名称字典 */
      partNameList: [],
      /**试验项目字典 */
      partProjectList: []
    }
  },
  methods: {
    /**
     * 文件上传
     * @param obj 回调对象
     */
    editUploadFile(obj) {
      let _that = this;
      _that.fileList = [];
      if (obj.file.size > 104857600) {
        _that.$message.warning(_that.$t('global-message.warn.uploaded-files-cannot-exceed'));
        return;
      }
      let fd = new FormData();
      fd.append('file', obj.file);
      fd.append('fileType', '17');
      _that.$api.uploadFile(fd).then((res) => {
        if (res.flag) {
          _that.$message.success(res.message, 1);
          _that.fileList.push({
            name: res.data.fileName,
            fileName: res.data.fileName,
            filePath: res.data.filePath,
            fileSize: res.data.fileSize,
            newFileName: res.data.newFileName,
            uid: obj.file.uid
          });
        } else {
          _that.$message.error(res.message, 1);
        }
      }).catch((error) => {
        _that.$message.error(error.response.data.message, 1);
      });
    },
    /**
     * 文件删除
     */
    editRemoveFile() {
      this.fileList = [];
    },
    // 零部件编号变换
    partNoChange(val) {
      this.formData.partName = [];
      val.forEach(element => {
        let data = this.partNameList.find(item => { return item.value === element });
        this.formData.partName.push(data.value);
      });
    },
    // 试验项目变换
    partProjectChange() {
      console.log("试验项目");
    },
		/**
		 * 非空校验 判断是否为空
		 * @returns {boolean}
		 */
		isNull() {
			let _that = this;
			let message = [];
			if (!_that.formData.reportNo) {
				message.push(_that.$t('global-btn.please-enter') + _that.$t('verity.dvp.material.list.number'));
			}
			if (!_that.formData.partNo) {
				message.push(_that.$t('global-btn.please-select') + _that.$t('verity.dvp.part.number'));
			}
			if (_that.fileList.length == 0) {
				message.push(_that.$t('global-message.warn..message.warn.select-file'));
			}
			if (message && message.length > 0) {
				for (let i = 0; i < message.length; i++) {
					_that.$notification.warning({message: message[i]});
				}
				return false;
			}
			return true;
		},
    /** 上传材料报告保存 */
    save() {
      let _that = this;
      // 非空校验
      if (!_that.isNull()) {
				return false;
			}
      // 试验报告文件
      let testReportFileModel = undefined;
      if (_that.fileList.length > 0 && _that.fileList[0].newFileName) {
        testReportFileModel = {
          newFileName: _that.fileList[0].newFileName,
          fileName: _that.fileList[0].fileName,
          filePath: _that.fileList[0].filePath,
          fileSize: _that.fileList[0].fileSize
        };
      }
      let data = {
        dvpId: _that.$store.getters.dvpId,
        attachmentType: 1, // 试验报告
        code: _that.formData.reportNo,
        partCodeList: _that.formData.partNo,
        dvpAddFileModel: testReportFileModel
      };
      _that.$api.dvpBatchUpload(data).then(res => {
        if (!res.flag) {
          _that.$notification.error({message: res.message});
        } else {
          _that.$notification.success({message: res.message});
        }
        _that.closeModal();
      });
    },
    /** 关闭弹窗 */
    closeModal() {
      this.formData = {
        reportNo: undefined,
        partNo: undefined,
        partName: undefined
      };
      this.fileList = [];
      this.visible = false;
      this.$emit("init");
    },
    /**
     * 获取零部件编号/名称字典
     */
    getPartCodeList() {
      let data = {
        dvpId: this.$store.getters.dvpId
      }
      this.$api.getPartList(data).then(res => {
        if (res.flag) {
          let allData = res.data.partList;
          if (allData.length > 0) {
            allData.forEach(element => {
              let code = {
                value: element.id,
                label: element.partCode
              };
              let name = {
                value: element.id,
                label: element.partName
              };
              this.partNoList.push(code);
              this.partNameList.push(name);
            });
          }
        } else {
          this.$notification.error({message: res.message});
        }
      });
    }
  },
  mounted() {
    this.getPartCodeList();
  }
}
</script>

<style>

</style>