<template>

  <!--导入文件对话框-->
  <el-dialog
    @close="close"
    v-dialogDrag style="font-size: 10px;" width='450px'
    v-if="importDeviceVisible"
    :title="$t('LinkConfigurationManagement.importLinkData')"
    :visible.sync="importDeviceVisible">
    <el-upload
      action="doUpload"
      :limit="1"
      :before-upload="beforeUpload">
      <el-button slot="trigger" size="small" type="primary">{{$t('common.selectFileBtn')}}</el-button>
      <div slot="tip" class="el-upload__tip">{{$t('common.importMessage')}}</div>
      <div slot="tip" class="el-upload-list__item-name">{{fileName}}</div>
    </el-upload>
    <span slot="footer" class="dialog-footer">
        <el-button size='mini' type="primary" @click="submitUpload()">{{$t('common.ok')}}</el-button>
       <el-button size='mini'   @click="close">{{$t('common.cancel')}}</el-button>
      </span>
  </el-dialog>

</template>

<script>
import ElButton from '../../../../node_modules/element-ui/packages/button/src/button.vue'
import HelperUtil from '../../../utils/HelperUtil'
import LinkService from "../../../domain/services/LinkService";
export default {
  components: {ElButton},
  props: ['command'],

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

      fileName: '',
      files: ''

    }
  },
  methods: {
    close () {
      console.log('关闭')
      this.command.done()
    },

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

    /**
       * @Description  ：提交导出
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
    submitUpload: function () {
      console.log(this.files)
      console.log('上传' + this.files.name)
      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 _this = this
      let loadingInstance = _this.Loading.openLoading()
      LinkService.uploadLinkFile(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>
