<template>
  <!--本地上传配置文件对话框-->
  <el-dialog v-dialogDrag style="font-size: 10px;"
             width='450px'
             v-if="uploadFirmwareVisible"
             :title="$t('ProfileManagement.localUpload')"
             :visible.sync="uploadFirmwareVisible"
             @close="close">
    <el-upload
      action="doUpload"
      :limit="1"
      :before-upload="beforeUpload">
      <div id="home" class="demo-input-suffix">
        <el-button slot="trigger" size="small" type="success">{{ $t('common.selectFileBtn')}}</el-button>
      </div>
      <div slot="tip" class="el-upload__tip">{{$t('common.importConfFile')}}</div>
      <div slot="tip" class="el-upload__tip">{{fileName}}</div>
    </el-upload>

    <el-row style="margin-bottom: 0px;" slot="footer" class="dialog-footer">
      <el-button style="margin-bottom: -10px;" size='mini' icon='el-icon-circle-check' type="primary" @click="submitUpload"
                 v-loading.fullscreen.lock="fullscreenLoading">{{$t("ProfileManagement.upload")}}
      </el-button>
      <el-button style="margin-bottom: -10px;" size='mini' icon='el-icon-circle-close' @click="close">
        {{$t("common.cancel")}}
      </el-button>
    </el-row>

  </el-dialog>
</template>

<script>
import ElButton from '../../../../node_modules/element-ui/packages/button/src/button.vue'
import ConfigFileService from '@/domain/services/ConfigFileService.js'
import HelperUtil from '../../../utils/HelperUtil'

export default {
  components: {ElButton},
  props: ['command'],

  data: function () {
    return {
      uploadFirmwareVisible: true,

      fileName: '',
      files: '',

      fullscreenLoading: false,
      tableHeight: window.innerHeight * 0.7,
      formLabelWidth: '120px'
    }
  },
  methods: {
    close: function () {
      console.log('关闭')
      this.command.done()
    },

    /**
       * @Description  ：导出文件之前执行
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
    beforeUpload (file) {
      console.log(file, '文件')
      this.files = file
      const extension = file.name.substring(file.name.toString().length - 5, file.name.toString().length) == '.conf'
      const isLt2M = file.size / 1024 / 1024 < 5
      if (!extension) { // && !extension2) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UPLOAD_FILES_CAN_ONLY_BE_IN_CONF_FORMAT))
        return
      }
      if (!isLt2M) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UPLOADFILE_SIZE_CODE))
        return
      }
      this.fileName = file.name
      return false // 返回false不会自动上传
    },

    // 从本地上传：提交上传的文件
    submitUpload: function () {
      let _this = this
      if (this.fileName == '') {
        _this.InfoTip.warningTip(_this, HelperUtil.getCheckStatusCodeObjectByCode(_this.successCode.CHOOSE_UPLOADFILE_CODE))
        return false
      }
      let fileFormData = new FormData()
      console.log(this.files)
      fileFormData.append('file', this.files, this.files.name)
      let loadingInstance = _this.Loading.openLoading()
      ConfigFileService.uploadLocalConfigFile(this.files.name, fileFormData).then(result => {
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.OTHERS_CODE))
        _this.close()
      }).catch(err => {
        // 失败
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.errorTip(_this, err)
      })
    }
  },
  created () {
    console.log('导入配置文件 初始化加载数据')
  }

}
</script>
