<template>
  <!-- 配置文件下载到设备对话框 -->
  <el-dialog
    v-dialogDrag
    @close="close"
    opened="open"
    style="font-size: 10px; " width='600px' :title="$t('FirmWareConfiguration.updateDialog')"
    v-if="updateFirmWareToDevice"
    :visible.sync="updateFirmWareToDevice">
    <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"
      @current-change="handleCurrentChange"
      @selection-change="handleSelectionChangeDownload">
      <el-table-column
        type="selection"
        :selectable='checkboxT'
        width="28">
      </el-table-column>
      <el-table-column
        prop="deviceName"
        :label="$t('ProfileManagement.deviceName')"
        :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column
        prop="ipAddress"
        :show-overflow-tooltip="true"
        :label="$t('DeviceConfigurationManagement.ipAddress')">
      </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="updateToDeviceFirmWarePrepare"
                 v-loading.fullscreen.lock="fullscreenLoading">{{$t("common.ok")}}
      </el-button>
      <el-button size='mini' @click="close">
        {{$t("common.cancel")}}
      </el-button>
    </el-row>
  </el-dialog>


</template>

<script>
  let curThis = '';

  import ConfigFileService from '@/domain/services/ConfigFileService.js'
  import DeviceService from '@/domain/services/DeviceService.js'
  import FirmWareService from '@/domain/services/FirmWareService.js'

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

  export default {


    props: ['command'],

    data() {
      return {
        deviceList: [],

        currentIndex: [],
        multipleSelectionDownload: [],

        currentDevice: this.command.target,
        currentDeviceKey: null,
        currentFirmWareKey: null,

        updateFirmWareToDevice: false,

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

      //更新配置文件：复选框
      checkboxT(row, rowIndex) {
//        console.log(this.currentIndex)
//        if( this.currentIndex===[]){
//          return true
//        }
        for (var i = 0; i < this.currentIndex.length; i++) {

          if (rowIndex === this.currentIndex[i]) {
            console.log(this.currentIndex[i])
            return false;
          }

        }
        return true
//        if(row.configFileKey===this.currentConfKey){
//          return false
//
//        }
//        else{
//          return false
//        }
      },
      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].firmwareKey == _this.currentFirmWareKey) {
              _this.currentIndex.push(i)
//              console.log(this.currentIndex[0])
            }
          }
        }).catch(err => {
          //失败
          _this.InfoTip.errorTip(_this, err);
        })
      },


      /**
       * @Description  :下载到设备表格多选框改变时，将所选行进行保存
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
      handleSelectionChangeDownload (val) {
        this.multipleSelectionDownload = val
//        alert(this.multipleSelectionDownload.length)
      },
      /**
       * @Description  :　第二步：　配置文件下载到设备的下载前准备
       * @author       ::
       * @date         :
       */
      updateToDeviceFirmWarePrepare: function () {
        if (this.multipleSelectionDownload.length == 0) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PLEASE_SELECT_AT_LEASE_ONE_DEVICE))
          return
        }
        else if (this.multipleSelectionDownload.length > 1) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PLEASE_SELECT_ONLY_ONE_DEVICE))
          return
        }
        else {
          // this.updateToDeviceFirmWare(this)
          this.updateFirmWareIntoDevice(this)

        }
      },

      /**
       * @Description  :下载到设备提示
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
      // updateToDeviceFirmWare: 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.updateFirmWareIntoDevice(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.updateFirmWareIntoDevice(this)
      //
      //     }).catch(() => {
      //
      //     });
      //   }
      // },

      /**
       * @Description  :固件下载到设备
       * @author       ::
       * @date         :
       */
      updateFirmWareIntoDevice: 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();
        FirmWareService.putUpdateFirmWareToDevice(this.currentFirmWareKey, 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);
//          curThis.command.done()
        })
      },
    },

    watch: {},
    created() {
      curThis = this

    },
    mounted () {
      this.updateFirmWareToDevice = true;
      this.currentDeviceKey = this.currentDevice.deviceKey
      this.currentFirmWareKey = this.currentDevice.firmwareKey
      this.getAllDevice()
    }
  }
  ;
</script>

<style scoped>


</style>
