<template>
  <!--本地上传配置文件对话框-->
  <el-dialog v-dialogDrag style="font-size: 10px;"
             width='450px'
             :title="$t('ProfileManagement.oneKeyUpload')"
             :visible.sync="uploadFirmwareVisible"
             @close="close">
    <div class="search-input-box">
      <label>{{ $t("AlarmQuery.DeviceType") }}</label>
      <el-select size='mini'
                 v-model="deviceType"
                 :placeholder="$t('common.placeholder')"
                 :clearable="true"
                 @change="deviceTypeChange">
        <el-option
          v-for="item in deviceTypeList"
          :key="item.deviceTypeKey"
          :label="item.deviceTypeName"
          :value="item.deviceTypeKey">
        </el-option>
      </el-select>
    </div>
    <div class="search-input-box">
      <label>{{ $t("AlarmQuery.DeviceType1") }}</label>
      <el-select size='mini'
                 v-model="deviceModel"
                 :placeholder="$t('common.placeholder')"
                 @change="deviceModelChange"
                 :clearable="true">
        <el-option v-for="item in deviceModelList"
                   :key="item.deviceModelKey"
                   :label="item.deviceModel"
                   :value="item.deviceModel">
        </el-option>
      </el-select>
    </div>
    <div class="search-input-box">
      <label>{{ $t("AlarmQuery.DeviceName") }}</label>
      <el-select size='mini' v-model="deviceName" :placeholder="$t('common.placeholder')" :clearable="true">
        <el-option
          v-for="item in deviceList"
          :key="item.deviceKey"
          :label="item.displayName"
          :value="item.deviceKey">
        </el-option>
      </el-select>
    </div>
    <div class="search-input-box">
      <label>{{ $t("ProfileManagement.effectiveImmediately1") }}</label>
      <el-switch
        style="width: 178px"
        v-model="flag">
      </el-switch>
    </div>
    <el-upload
      action="doUpload"
      :limit="1"
      :before-upload="beforeUpload">
      <div>
        <el-button slot="trigger" size="small" type="primary">{{ $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'
import DeviceService from '@/domain/services/DeviceService.js'

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

  data: function () {
    return {
      deviceName: '',
      deviceType: '',
      deviceModel: '',
      deviceTypeList: [],
      deviceModelList: [],
      deviceModelList1: [],
      deviceList: [],
      deviceList0: [],
      uploadFirmwareVisible: true,
      flag: false,
      showFlag: 1,
      fileName: '',
      files: '',

      fullscreenLoading: false,
      tableHeight: window.innerHeight * 0.7,
      formLabelWidth: '120px'
    }
  },
  methods: {
    /**
     * @Description  : deviceTypeChange
     * @author       : ls
     * @date         : 2021/3/25 11:05
     * @param        :
     * @return       :
     */
    deviceTypeChange: function () {
      var a = HelperUtil.deviceTypeChangeDevice(this.deviceModelList1, this.deviceType, this.deviceModel, this.deviceList0)
      this.deviceModelList = a[0]
      this.deviceModel = a[1]
      this.queryDeviceName = ''
      this.deviceList = []
      this.deviceList = a[2]
    },
    /**
     * @Description  : deviceModelChange
     * @author       : ls
     * @date         : 2021/3/25 11:06
     * @param        :
     * @return       :
     */
    deviceModelChange: function () {
      this.queryDeviceName = ''
      this.deviceList = []
      this.deviceList = HelperUtil.deviceModelChange(this.deviceType, this.deviceModel, this.deviceList0)
    },
    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_alarm(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UPLOAD_FILES_CAN_ONLY_BE_IN_CONF_FORMAT))
        return
      }
      if (!isLt2M) {
        this.InfoTip.warningTip_alarm(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UPLOADFILE_SIZE_CODE))
        return
      }
      this.fileName = file.name
      return false // 返回false不会自动上传
    },
    /**
     * @Description  : deviceNameCheck
     * @author       : ls
     * @date         : 2021/3/25 11:15
     * @param        :
     * @return       :
     */
    deviceNameCheck: function () {
      if (this.deviceName === '' || this.deviceName === null) {
        // this.InfoTip.warningTip_alarm(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PLEASE_SELECT_ONLY_ONE_DEVICE), this.$t('AlarmQuery.DeviceName'))
        this.InfoTip.warningTip_alarm(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PLEASE_SELECT_ONLY_ONE_DEVICE))
        return false
      }
      return true
    },
    /**
     * @Description  : configFileCheck
     * @author       : ls
     * @date         : 2021/3/25 11:17
     * @param        :
     * @return       :
     */
    configFileCheck: function () {
      if (this.files === '') {
        this.InfoTip.warningTip_alarm(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.CHOOSE_UPLOADFILE_CODE))
        return false
      }
      return true
    },
    /**
     * @Description  : totalCheck
     * @author       : ls
     * @date         : 2021/3/25 11:15
     * @param        :
     * @return       :
     */
    totalCheck: function () {
      if (!this.deviceNameCheck()) {
        return false
      }
      if (!this.configFileCheck()) {
        return false
      }
      return true
    },
    /**
     * @Description  : submitUpload
     * @author       : ls
     * @date         : 2021/3/25 11:10
     * @param        :
     * @return       :
     */
    submitUpload: function () {
      if (!this.totalCheck()) {
        return
      }
      if (this.flag) {
        this.$confirm(this.$t('ProfileManagement.EffectiveImmediatelyRebootNow'),
          this.$t('ProfileManagement.Attention'), {
            confirmButtonText: this.$t('common.ok'),
            cancelButtonText: this.$t('common.cancel'),
            type: 'warning',
            center: true
          }).then(() => {
          this.showFlag = 1
          this.oneKeyUploadToDevice()
        })
      } else {
        this.$confirm(this.$t('ProfileManagement.NotEffectiveImmediatelyNotRebootNow'),
          this.$t('ProfileManagement.Attention'), {
            confirmButtonText: this.$t('common.ok'),
            cancelButtonText: this.$t('common.cancel'),
            type: 'warning',
            center: true
          }).then(() => {
          this.showFlag = 0
          this.oneKeyUploadToDevice(this)
        })
      }
    },
    /**
     * @Description  : oneKeyUploadToDevice
     * @author       : ls
     * @date         : 2021/3/25 14:05
     * @param        :
     * @return       :
     */
    oneKeyUploadToDevice: function () {
      let _this = this
      let fileFormData = new FormData()
      console.log(this.files)
      fileFormData.append('file', this.files, this.files.name)
      let loadingInstance = _this.Loading.openLoading()
      ConfigFileService.oneKeyUploadConfig(this.fileName, this.showFlag, this.deviceName, 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)
      })
    },
    /**
     * @Description  : getAllDevice
     * @author       : ls
     * @date         : 2021/3/25 10:59
     * @param        :
     * @return       :
     */
    getAllDevice: function () {
      let _this = this
      DeviceService.getAllDeviceCollection().then(result => {
        _this.deviceList = Object.values(result)
        _this.deviceList0 = Object.values(result)
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
     * @Description  : getAllDeviceType
     * @author       : ls
     * @date         : 2021/3/25 10:56
     * @param        :
     * @return       :
     */
    getAllDeviceType: function () {
      let _this = this
      DeviceService.getAllDeviceTypeCollection().then(result => {
        _this.deviceTypeList = Object.values(result)
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
     * @Description  : getAllDeviceModel
     * @author       : ls
     * @date         : 2021/3/25 10:56
     * @param        :
     * @return       :
     */
    getAllDeviceModel: function () {
      let _this = this
      DeviceService.getAllDeviceModelCollection().then(result => {
        _this.deviceModelList = Object.values(result)
        _this.deviceModelList1 = Object.values(result)
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    },
  },
  created () {
    this.getAllDevice()
    this.getAllDeviceModel()
    this.getAllDeviceType()
    console.log('导入配置文件 初始化加载数据')
  }

}
</script>
