<template>
  <el-dialog v-loading="loading" v-if="visible" :visible.sync="visible" :title="title" width="600" @close="goBack">
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-form-item label="行业类型" prop="INDUSTRY_TYPE_ARR">
        <cascader-dict
          v-model = "form.INDUSTRY_TYPE_ARR"
          :form.sync="form"
          :dict-id = "dictId"
          :disabled="importType !== 'import'"
          @cascaderNames="cascaderNames"
        />
      </el-form-item>
      <el-form-item label="标准名称" prop="CHECK_STANDARD_NAME">
        <el-input
          id="CHECK_STANDARD_NAME"
          ref="CHECK_STANDARD_NAME"
          v-model="form.CHECK_STANDARD_NAME"
          :disabled="importType !== 'import'"
          maxlength="255"
          placeholder="这里输入标准名称..."
          title="标准名称"/>
      </el-form-item>
      <el-form-item label="导入标准Excel:" prop="excelFileList">
        <el-upload
          ref="fileRef"
          :on-change="uploadChange"
          :before-remove="uploadBeforeRemove"
          :on-remove="uploadRemoved"
          :auto-upload="false"
          :file-list="form.excelFileList"
          :limit="1"
          class="avatar-uploader"
          action="#"
          accept=".xls">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传xls文件</div>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="downloadTemplate">下载模板</el-button>
      <el-button @click="goBack">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>

import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import { upload } from '@/utils/upload'
import uploadFile from '../../../../components/UploadFile/index.vue'
import CascaderDict from '../../../../components/CascaderDict'

export default {
  components: { Pagination, uploadFile, CascaderDict },
  directives: { waves },
  data() {
    return {
      config: config,
      visible: false,
      importType: '',
      title: '',
      dictId: 'f2598ba72e864eadabf0ca4b664d26b9', // 行业类型
      form: {
        INDUSTRY_TYPE_ARR: [],
        INDUSTRY_TYPE: '',
        INDUSTRY_TYPE_TREE: '',
        INDUSTRY_TYPE_NAMES: '',
        CHECK_STANDARD_NAME: '',
        excelFileList: []
      },
      rules: {
        INDUSTRY_TYPE_ARR: [{ required: true, message: '行业类型不能为空', trigger: 'blur' }],
        CHECK_STANDARD_NAME: [{ required: true, message: '标准名称不能为空', trigger: 'blur' }],
        excelFileList: [{ required: true, message: '导入文件不能为空', trigger: 'blur' }]
      },
      loading: false
    }
  },
  methods: {
    init(e) {
      this.visible = true
      this.importType = e.importType
      switch (e.importType) {
        case 'reimport':
          this.title = '重新导入'
          break
        case 'appendImport':
          this.title = '追加导入'
          break
        default:
          this.title = '导入标准表'
          break
      }
      if (e.importType !== 'import') {
        this.form = e.form
        this.$set(this.form, 'INDUSTRY_TYPE_ARR', this.form.INDUSTRY_TYPE_TREE ? this.form.INDUSTRY_TYPE_TREE.split(',') : [])
        this.$set(this.form, 'excelFileList', [])
      }
    },
    goBack() {
      this.resetForm()
      this.visible = false
    },
    cascaderNames(e) {
      this.form.INDUSTRY_TYPE_NAMES = e
    },
    /** 上传组件 Begin */
    uploadChange(file, fileList) {
      const types = ['application/vnd.ms-excel']
      const isImage = types.includes(file.raw.type)
      if (!isImage) {
        this.$message.error('上传文件只能是 XLS 格式!')
        fileList.pop()
      } else {
        this.form.excelFileList = fileList
      }
    },
    uploadBeforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    uploadRemoved(file, fileList) {
      this.form.excelFileList = fileList
    },
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '上传中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          this.form.INDUSTRY_TYPE = this.form.INDUSTRY_TYPE_ARR[this.form.INDUSTRY_TYPE_ARR.length - 1]
          this.form.INDUSTRY_TYPE_TREE = this.form.INDUSTRY_TYPE_ARR.join(',')
          const formData = new FormData()
          Object.keys(this.form).map(key => {
            formData.append(key, this.form[key])
          })
          formData.append('importType', this.importType)
          formData.delete('INDUSTRY_TYPE_ARR')
          formData.delete('excelFileList')
          for (let i = 0; i < this.form.excelFileList.length; i++) {
            if (this.form.excelFileList[i].raw) {
              formData.append('FILE', this.form.excelFileList[i].raw)
            }
          }
          upload(
            '/hiddenDangerCheckStandardCommon/importExcel',
            formData
          ).then((data) => {
            loading.close()
            if (data.resultStr) {
              this.$message({
                dangerouslyUseHTMLString: true,
                message: data.resultStr,
                type: data.resultType,
                showClose: true,
                duration: 5 * 1000
              })
            }
            this.resetForm()
            this.visible = false
            this.$emit('logical-end', { result: 'OK' })
          }).catch((e) => {
            loading.close()
            console.log(e)
          })
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.$refs.fileRef.clearFiles()
      this.form = {
        INDUSTRY_TYPE_ARR: [],
        INDUSTRY_TYPE: '',
        INDUSTRY_TYPE_TREE: '',
        INDUSTRY_TYPE_NAMES: '',
        CHECK_STANDARD_NAME: '',
        excelFileList: []
      }
    },
    downloadTemplate() {
      window.open(config.httpurl + 'hiddenDangerCheckStandardCommon/excelModel')
    }
  }
}

</script>
