<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
               :disabled="method==='view'"
               label-width="120px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="评价年份" prop="yearOfEvaluation"
                          :rules="[
                  {required: true, message:'评价年份不能为空', trigger:'blur'}
                 ]">
              <el-date-picker
                :disabled="method === 'view' || method === 'edit'"
                style="width: 100%;"
                v-model="inputForm.yearOfEvaluation"
                type="year"
                value-format="yyyy"
                placeholder="选择年份">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上传文件" prop="fileList"
                          :rules="[
                  {required: true, message:'上传文件不能为空', trigger:'blur'}
                 ]">
              <el-upload
                class="upload-demo"
                ref="upload"
                v-model="inputForm.fileList"
                :http-request="httpRequest"
                :before-upload="beforeUpload"
                :on-exceed="handleExceed"
                action
                :limit="1"
                :show-file-list="true">
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary"  @click="submitImportForm()" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          yearOfEvaluation: '',
          unitName: '',
          fileList: []
        }
      }
    },
    components: {},
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建施工单位`
        } else if (method === 'edit') {
          this.title = '修改施工单位'
        } else if (method === 'view') {
          this.title = '查看施工单位'
        }
        this.visible = true
        this.loading = false
      },
      httpRequest (option) {
        this.inputForm.fileList.push(option)
      },
      // 上传前处理
      beforeUpload (file) {
        let fileSize = file.size
        const FIVE_M = 5 * 1024 * 1024
        // 大于5M，不允许上传
        if (fileSize > FIVE_M) {
          this.$message.error('最大上传5M')
          return false
        }
        // 判断文件类型，必须是xlsx格式
        let fileName = file.name
        let reg = /^.+(\.xlsx)$/
        if (!reg.test(fileName)) {
          this.$message.error('只能上传xlsx!')
          return false
        }
        return true
      },
      // 文件数量过多时提醒
      handleExceed () {
        this.$message({type: 'error', message: '最多支持1个附件上传'})
      },
      submitImportForm () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            const params = new FormData()
            this.inputForm.fileList.forEach((x) => {
              params.append('file', x.file)
            })
            // 将输入表单数据添加到params表单中
            params.append('yearOfEvaluation', this.inputForm.yearOfEvaluation)
            this.$http({
              url: `/constructionUnit/constructionUnit/import`,
              method: 'post',
              data: params,
              headers: {
                'Content-Type': 'multipart/form-data'
              }
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              } else {
                this.visible = false
              }
              this.loading = false
            })
          }
        })
        this.$refs.inputForm.resetFields()
        this.$refs.upload.clearFiles()
        this.inputForm.fileList = []
      }
    }
  }
</script>
