<template>
  <!-- 配置文件下载到设备对话框 -->
  <el-dialog
    v-dialogDrag
    @close="close"
    style="font-size: 10px; " width='600px' :title="$t('ProfileManagement.downloadToDevice')"
    :visible.sync="configurationDownloadToDevice">
    <el-table
      @close="close"
      fit="true"
      height="350"
      header-row-class-name="table-header"
      cell-class-name="table-cell"
      ref="singleTable"
      :data="deviceList"
      highlight-current-row
      stripe
      tooltip-effect="dark"
      @selection-change="handleSelectionChangeDownload">
      <el-table-column
        type="selection"
        :selectable='checkboxT'
        width="28">
      </el-table-column>
      <el-table-column
        prop="displayName"
        :label="$t('ProfileManagement.deviceName')"
        :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column
        prop="configFileName"
        :label="$t('ProfileManagement.currentProfile')"
        :show-overflow-tooltip="true">

      </el-table-column>

    </el-table>

    <el-row style="line-height: 30px;" slot="footer" class="dialog-footer">
      <el-checkbox style="margin:0px;position: absolute;left:14px;" v-model="flag">
        {{$t("ProfileManagement.effectiveImmediately")}}
      </el-checkbox>

      <el-button size='mini' type="primary" @click="downloadToDevicePrepare"
                 v-loading.fullscreen.lock="fullscreenLoading">{{$t("ProfileManagement.download")}}
      </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 DeviceService from '@/domain/services/DeviceService.js'

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

let curThis = ''

export default {

  props: ['command'],

  data () {
    return {
      deviceList: [],

      // 是否立即生效提示
      downloadToDeviceFlag: -1,
      flag: false,
      currentIndex: [],
      multipleSelectionDownload: [],

      currentDevice: this.command.target,
      currentConfKey: null,

      configurationDownloadToDevice: true,

      // 数据加载过程中,覆盖界面
      fullscreenLoading: false
    }
  },
  methods: {

    // 更新配置文件:复选框
    checkboxT (row, rowIndex) {
      for (var i = 0; i < this.currentIndex.length; i++) {
        if (rowIndex === this.currentIndex[i]) {
          console.log(this.currentIndex[i])
          return false
        }
      }
      return true
    },
    close () {
      this.command.done()
    },
    getAllDevice: function () {
      console.log('获取全部网元信息:')
      let _this = this
      DeviceService.getAllDeviceCollection().then(result => {
        // 成功
        _this.deviceList = Object.values(result)
        for (var i = 0; i < _this.deviceList.length; i++) {
          if (_this.deviceList[i].configFileKey === _this.currentConfKey) {
            _this.currentIndex.push(i)
          }
        }
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
       * @Description  :下载到设备表格多选框改变时,将所选行进行保存
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
    handleSelectionChangeDownload (val) {
      this.multipleSelectionDownload = val
      //        alert(this.multipleSelectionDownload.length)
    },
    /**
     * @Description  : 文件下载到设备初始化
     * @author       : ls
     * @date         : 2020/7/17 15:10
     * @param        :
     * @return       :
     */
    downloadToDevicePrepare: function () {
      if (this.multipleSelectionDownload.length == 0) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PLEASE_SELECT_AT_LEASE_ONE_DEVICE))
      } else if (this.multipleSelectionDownload.length > 1) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PLEASE_SELECT_ONLY_ONE_DEVICE))
      } else {
        this.downloadPrompt(this)
      }
    },

    /**
       * @Description  :下载到设备提示
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
    downloadPrompt: function () {
      if (this.flag === 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.downloadToDevice(this)
        }).catch(() => {

        })
      } else if (this.flag === 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.downloadToDevice(this)
        }).catch(() => {

        })
      }
    },
    /**
     * @Description  : 文件下载到设备
     * @author       : ls
     * @date         : 2020/7/17 15:08
     * @param        :
     * @return       :
     */
    downloadToDevice: function (vue) {
      var selectDeviceList = new Array()
      for (var i = 0; i < vue.multipleSelectionDownload.length; i++) {
        selectDeviceList[i] = vue.multipleSelectionDownload[i]['deviceKey']
      }
      let _this = vue
      let loadingInstance = _this.Loading.openLoading()
      ConfigFileService.downloadConfigToDevice(curThis.currentConfKey, curThis.downloadToDeviceFlag, selectDeviceList).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)
      })
    }
  },

  watch: {},
  created () {
    curThis = this
  },
  mounted () {
    this.configurationUpdate = true
    this.currentConfKey = this.currentDevice
    this.getAllDevice()
  }
}

</script>

<style scoped>
</style>