<template>
  <!-- 配置文件更新到设备对话框 -->
  <el-dialog
    @close="close"
    opened="open"
    v-dialogDrag
    style="font-size: 10px; " width='600px'
    :title="$t('DeviceConfigurationManagement.profileUpdateTitle')"
    v-if="configurationUpdate"
    :visible.sync="configurationUpdate">

    <el-table
      fit="true"
      height="350"
      ref="singleTable"
      :data="configList"
      header-row-class-name="table-header"
      cell-class-name="table-cell"
      highlight-current-row
      stripe
      tooltip-effect="dark"
      @selection-change="handleSelectionChangeUpdate">
      <el-table-column
        type="selection"
        :selectable='checkboxT'
        width="28">
      </el-table-column>
      <el-table-column
        prop="configFileName"
        :label="$t('ProfileManagement.configFileName')"
        :show-overflow-tooltip="true">

      </el-table-column>
      <el-table-column
        prop="timestamp"
        :formatter="formatTime"
        :label="$t('ProfileManagement.timestamp')"
        :show-overflow-tooltip="true">
      </el-table-column>

      <el-table-column
        prop="description"
        :label="$t('ProfileManagement.description')"
        :show-overflow-tooltip="true">
      </el-table-column>

    </el-table>

    <el-row slot="footer" class="dialog-footer" style="line-height: 30px;">

      <el-checkbox style="margin:0px;position: absolute;left:14px;" v-model="flagUpdate">
        {{$t("ProfileManagement.effectiveImmediately")}}
      </el-checkbox>

      <el-button size='mini' type="primary" @click="updateProfile"
                 v-loading.fullscreen.lock="fullscreenLoading">{{$t("DeviceConfigurationManagement.updateProfile")}}
      </el-button>
      <el-button size='mini' @click="close">
        {{$t("common.cancel")}}
      </el-button>
    </el-row>
  </el-dialog>

</template>

<script>
import ConfigFileService from '@/domain/services/ConfigFileService.js'

import HelperUtil from '../../../utils/HelperUtil'

let curThis = ''

export default {

  props: ['command'],

  data () {
    return {
      configList: [],

      // 为了禁用当前行
      currentIndex: -1,

      // 是否立即生效提示
      downloadToDeviceFlag: -1,
      flagUpdate: false,

      multipleSelectionUpdate: [],

      configurationUpdate: false,
      currentDevice: this.command.target,
      currentDeviceKey: null,
      currentConfKey: null,
      // 数据加载过程中，覆盖界面
      fullscreenLoading: false
    }
  },
  methods: {
    /**
     * @Description  : 格式化时间
     * @author       : ls
     * @date         : 2020/6/11 16:44
     * @param        :
     * @return       :
     */
    formatTime (val) {
      return HelperUtil.timeTran(val.timestamp)
    },
    // 更新配置文件：复选框
    checkboxT (row, rowIndex) {
      if (rowIndex === this.currentIndex) {
        return false
      } else {
        return true
      }
    },
    open () {
      console.log('获取全部配置信息：')
      this.getAllConfigFile()
    },

    close () {
      this.command.done()
    },
    getAllConfigFile () {
      console.log('获取全部配置信息：')
      let _this = this
      // 为了测试其他先注释了
      ConfigFileService.getAllConfigFileCollection().then(result => {
        // 成功
        _this.configList = Object.values(result)
        for (var i = 0; i < _this.configList.length; i++) {
          if (_this.configList[i].configFileKey == _this.currentConfKey) {
            _this.currentIndex = i
          }
        }
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },

    /**
       * @Description  :设备配置更新表格多选框改变时，将所选行进行保存
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
    handleSelectionChangeUpdate (val) {
      this.multipleSelectionUpdate = val
    },

    /**
       * @Description  :第二步：更新配置信息选中数目验证
       * @author       :
       * @param        :
       * @date         :
       */
    updateProfile: function () {
      if (this.multipleSelectionUpdate.length == 0) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UPDATE_SELECT_CODE))
      } else if (this.multipleSelectionUpdate.length > 1) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UPDATE_ONLY_ONE_CODE))
      } else {
        // 第三步生效提示
        this.updateEffectiveTip()
      }
    },

    /**
       * @Description  :第三步：更新配置信息生效提示
       * @author       :
       * @param        :
       * @date         :
       */
    updateEffectiveTip: function () {
      if (this.flagUpdate == true) {
        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.downloadToDeviceFlag = 1
          this.$options.methods.updateToDevice()
        }).catch(() => {
        })
      } else if (this.flagUpdate == false) {
        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.downloadToDeviceFlag = 0
          this.$options.methods.updateToDevice()
        }).catch(() => {
        })
      }
    },

    /**
       * @Description  :第四步：更新配置信息
       * @author       :
       * @param        :
       * @date         :
       */
    updateToDevice: function () {
      let _this = curThis
      var deviceKeyList = []
      deviceKeyList.push(_this.currentDeviceKey)
      let loadingInstance = _this.Loading.openLoading()
      ConfigFileService.downloadConfigToDevice(_this.multipleSelectionUpdate[0].configFileKey, _this.downloadToDeviceFlag, deviceKeyList).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 () {
    curThis = this
  },
  mounted () {
    this.configurationUpdate = true
    this.getAllConfigFile()
    this.currentDeviceKey = this.currentDevice.deviceKey
    this.currentConfKey = this.currentDevice.configFileKey
  }
}

</script>

<style scoped>

</style>
