<template>
  <!-- 查看配置历史对话框 -->
  <el-dialog v-dialogDrag
             @close="close"
             style="font-size: 10px; " width='600px'
             :title="$t('DeviceConfigurationManagement.configurationHistory')"
             v-if="ConfigFileHistory"
             :visible.sync="ConfigFileHistory">

    <el-table
      :row-class-name="tableRowClassName"
      header-row-class-name="table-header"
      cell-class-name="table-cell"
      height="350"
      ref="singleTable"
      :data="historyConfList"
      stripe
      tooltip-effect="dark"
      @current-change="handleCurrentChange"
      @selection-change="handleSelectionChangeRestory">
      <el-table-column
        type="selection"
        :selectable='checkboxTHistory'
        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="source"
        :label="$t('ProfileManagement.source')"
        :show-overflow-tooltip="true"
        :formatter="formatHistorySource">
      </el-table-column>

      <el-table-column
        prop="modifyTime"
        :label="$t('DeviceConfigurationManagement.modifyTime')"
        :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="flagCopy">
        {{$t("ProfileManagement.effectiveImmediately")}}
      </el-checkbox>
      <el-button size='mini' type="primary" @click="restoreProfile"
                 v-loading.fullscreen.lock="fullscreenLoading">{{$t("DeviceConfigurationManagement.restore")}}
      </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 HelperUtil from '../../../utils/HelperUtil'
  export default {


    props: ['command'],

    data() {
      return {
        historyConfList: [],

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

        //是否立即生效提示
        restoryToDeviceFlag: -1,
        flagCopy: false,

        multipleSelectionHistory: [],

        ConfigFileHistory: true,
        currentDeviceKey: this.command.target,
        // 数据加载过程中，覆盖界面
        fullscreenLoading: false,
      };
    },
    methods: {
      //历史配置文件：复选框
      checkboxTHistory(row, rowIndex) {
        if (rowIndex == 0) {
          return false;
        } else {
          return true;
        }
      },

      //标红历史配置文件第一行
      tableRowClassName({row, rowIndex}) {
        if (rowIndex === 0) {
          return 'warning-row';
        }
        return '';
      },

      close() {
        this.command.done()
      },


      /**
       * @Description  :过滤历史配置页面
       */
      formatHistorySource: function (row, column) {

        if (row.source == 0) {
          return '上传'
        }
        else if (row.source == 1) {
          return '下载'
        }

      },

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


      /**
       * @Description  :获取全部配历史配置信息
       * @author       ::
       * @date         :
       */
      getAllHistory(vue) {

        var selectList = new Array()
        selectList.push({
          quaryAttribute: 'deviceKey',
          compareSymbol: '=',
          compareValue: this.currentDeviceKey,
        })
        console.log(selectList)
//        const loading = vue.$loading({
//          lock: true,
//          text: 'Loading',
//          spinner: 'el-icon-loading',
//          background: 'rgba(0, 0, 0, 0.7)',
//          target: document.querySelector('.el-row')
//        });
        let _this = this;
        DeviceService.getDeviceHistoryConfig(selectList).then(result => {
          //成功
          vue.historyConfList = Object.values(result)

//          loading.close();
        }).catch(err => {
          //失败
          _this.InfoTip.errorTip(_this,err);
//          loading.close();
        })

      },

      /**
       * @Description  :第二步：恢复配置信息选中条数验证
       * @author       :
       * @param        :
       * @date         :
       */
      restoreProfile: function () {
        if (this.multipleSelectionHistory.length == 0) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UPDATE_SELECT_CODE))
          return
        }
        else if (this.multipleSelectionHistory.length > 1) {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UPDATE_ONLY_ONE_CODE))
          return
        }
        else {
          //生效提示
          this.restoryEffectiveTip()


        }
      },

      /**
       * @Description  :第三步：恢复配置信息生效提示
       * @author       :
       * @param        :
       * @date         :
       */
      restoryEffectiveTip: function () {
        if (this.flagCopy == 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.restoryToDeviceFlag = 1
            this.$options.methods.restoryToDevice(this)

          }).catch(() => {
//            this.$message({
//              type: 'info',
//              message: '已取消'
//            });
          });
        }
        else if (this.flagCopy == 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.restoryToDeviceFlag = 0
            this.$options.methods.restoryToDevice(this)

          }).catch(() => {
//            this.$message({
//              type: 'info',
//              message: '已取消'
//            });
          });
        }
      },

      /**
       * @Description  :第四步：恢复配置信息
       * @author       :
       * @param        :
       * @date         :
       */
      restoryToDevice: function (vue) {
        let _this = vue;
//        //传参
        var selectDeviceList = new Array()
        selectDeviceList[0] = _this.currentDeviceKey

        let loadingInstance = _this.Loading.openLoading();
        ConfigFileService.downloadConfigToDevice(
          _this.multipleSelectionHistory[0].configFileKey,
          _this.restoryToDeviceFlag, selectDeviceList).then(result => {
          //成功
          _this.InfoTip.successTip(_this,HelperUtil.getStatusCodeObjectByCode(_this.successCode.OTHERS_CODE));
          _this.Loading.closeLoading(loadingInstance);
          _this.close()
        }).catch(err => {
          _this.InfoTip.errorTip(_this,err);
          //失败
          _this.Loading.closeLoading(loadingInstance);
//          _this.close()
        })
      },

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

    },

  };
</script>

<style scoped>


</style>
