<template>
  <el-dialog @close="close"
             @opened="dialogInit"
             style="font-size: 10px;" width='450px'
             v-dialogDrag
             v-if="systemImportDialogVisibel"
             :title="$t('SystemConfig.systemConfigDataImport')"
             :visible.sync="systemImportDialogVisibel">
    <el-upload
      action="doUpload"
      :limit="1"
      :before-upload="beforeUpload"
      style="margin-bottom: 10px;margin-left: 10px">
      <el-button slot="trigger" size="small" type="primary">{{ $t('SystemConfig.SelectType')}}</el-button>
      <div slot="tip" class="el-upload__tip">{{$t('SystemConfig.importConfigFile')}}</div>
      <div slot="tip" class="el-upload-list__item-name">{{fileName}}</div>
    </el-upload>
      <el-form :model="form" inline="true">
        <el-form-item :label="$t('SystemConfig.configType')+':'" :label-width="formLabelWidth"
                      style="margin-bottom: -10px;">
          <el-select v-model="selectConfig" size="mini" :placeholder="$t('SystemConfig.SelectType')">
            <el-option
              v-for="item in selectConfigList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item style="margin-bottom: -10px;" :label="$t('SystemConfig.operation')+':'" :label-width="formLabelWidth">-->
          <!--<template>-->
            <!--<el-radio-group v-model="chooseOperaton">-->
              <!--<el-radio :label=0 border size="mini" style="width: 67px;">{{$t("SystemConfig.overwrite")}}</el-radio>-->
              <!--<el-radio :label=1 border size="mini" style="width: 67px;">{{$t("SystemConfig.update")}}</el-radio>-->
            <!--</el-radio-group>-->
          <!--</template>-->
        <!--</el-form-item>-->
      </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size='mini' type="primary" @click="confirmUploadConfig">
        {{$t("SystemConfig.confirm")}}
      </el-button>
      <el-button size='mini' @click="close">{{$t("common.cancel")}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  OID_CFG_UPLOAD,
  DEVICE_TYPE_CFG_UPLOAD,
  DEVICE_TYPE_UPLOAD,
  RESOURCE_TYPE_CFG_UPLOAD,
  RESOURCE_TYPE_UPLOAD,
  PERFORM_TYPE_CFG_UPLOAD,
  PERFORM_TYPE_UPLOAD,
  ALARM_TYPE_CFG_UPLOAD,
  ALARM_TYPE_UPLOAD,
  LANGUAGE_UPLOAD
} from '../../utils/RequestUrls'
import ElButton from "../../../node_modules/element-ui/packages/button/src/button.vue";
import SystemConfigService from '@/domain/services/SystemConfigService.js'
import HelperUtil from '@/utils/HelperUtil'

export default {
  name: 'ImportSystem',
  components: {ElButton},
  props: {
    systemImportDialogVisibel: null,//控制弹框的显示
  },
  data () {
    return {
      selectConfig: '0',
      fileName: '',
      files: '',
      chooseOperaton: 1,
      selectConfigList: [
        {
          label: this.$t('SystemConfig.oidCfg'),
          value: '0'
        },
        {
          label: this.$t('SystemConfig.deviceTypeCfg'),
          value: '1'
        },
        {
          label: this.$t('SystemConfig.deviceType'),
          value: '2'
        },
        {
          label: this.$t('SystemConfig.resourceTypeCfg'),
          value: '3'
        },
        {
          label: this.$t('SystemConfig.resourceType'),
          value: '4'
        },
        {
          label: this.$t('SystemConfig.performTypeCfg'),
          value: '5'
        },
        {
          label: this.$t('SystemConfig.performType'),
          value: '6'
        },
        {
          label: this.$t('SystemConfig.alarmTypeCfg'),
          value: '7'
        },
        {
          label: this.$t('SystemConfig.alarmType'),
          value: '8'
        },
        {
          label: this.$t('SystemConfig.languageSheet'),
          value: '9'
        }
      ],
      formLabelWidth: '120px',
    }
  },
  methods: {
    /**
    * @Author      : lS
    * @Date        : 11:42 2019/11/18
    * @Description : 上传处理
    **/
    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) {
      //   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 = this.files.name;
      return false
    },
    /**
    * @Author      : lS
    * @Date        : 11:44 2019/11/18
    * @Description : 上传
    **/
    confirmUploadConfig: function () {
      let _this = this
      if (this.fileName === '') {
        _this.InfoTip.warningTip(_this, HelperUtil.getCheckStatusCodeObjectByCode(_this.successCode.CHOOSE_UPLOADFILE_CODE))
        return false
      }
      let fileFormData = new FormData();
      fileFormData.append('file', this.files, this.files.name)
      var url = ''
      switch (this.selectConfig) {
        case '0':
          url = OID_CFG_UPLOAD
          this.CfgUpload(url, this.chooseOperaton, fileFormData)
          break
        case '1':
          url = DEVICE_TYPE_CFG_UPLOAD
          this.CfgUpload(url, this.chooseOperaton, fileFormData)
          break
        case '2':
          url = DEVICE_TYPE_UPLOAD
          this.CfgUpload(url, this.chooseOperaton, fileFormData)
          break
        case '3':
          url = RESOURCE_TYPE_CFG_UPLOAD
          this.CfgUpload(url, this.chooseOperaton, fileFormData)
          break
        case '4':
          url = RESOURCE_TYPE_UPLOAD
          this.CfgUpload(url, this.chooseOperaton, fileFormData)
          break
        case '5':
          url = PERFORM_TYPE_CFG_UPLOAD
          this.CfgUpload(url, this.chooseOperaton, fileFormData)
          break
        case '6':
          url = PERFORM_TYPE_UPLOAD
          this.CfgUpload(url, this.chooseOperaton, fileFormData)
          break
        case '7':
          url = ALARM_TYPE_CFG_UPLOAD
          this.CfgUpload(url, this.chooseOperaton, fileFormData)
          break
        case '8':
          url = ALARM_TYPE_UPLOAD
          this.CfgUpload(url, this.chooseOperaton, fileFormData)
          break
        case '9':
          url = LANGUAGE_UPLOAD
          this.CfgUpload(url, this.chooseOperaton, fileFormData)
          break
      }
    },
    CfgUpload: function (url, ope, fileData) {
      let _this = this
      let loadingInstance = _this.Loading.openLoading();
      SystemConfigService.uploadSystemConfig(url, ope, fileData).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)
      })
    },
    /**
     * @Author      : lS
     * @Date        : 11:42 2019/11/18
     * @Description : 关闭
     **/
    close () {
      this.$emit('update:systemImportDialogVisibel', false)
      this.files = ''
      this.fileName = ''
      this.chooseOperaton = 1
      this.selectConfig = '0'
    }
},
  computed: {},
  watch: {

  },
  //无法获取 data中的数据、methods中的方法
  beforeCreate() {},
  //常用的生命周期，可以调用methods中的方法、改变data中的数据
  created() {
  },
  //在挂载开始之前被调用
  beforeMount() {},
  //此时，vue实例已经挂载到页面中，可以获取到el中的DOM元素，进行DOM操作
  mounted() {},
  //运行期间
  beforeUpdate() {},
  updated() {},
  //销毁期间
  beforeDestroy() {},
  destroyed() {},
}
</script>

<style scoped>
</style>
