<template>
  <div id="main" class="h-all">
    <el-container class="h-all">
      <el-header height="auto">
      <div class="header-button-box">
        <!--<el-button size = 'mini' icon="el-icon-download" type="primary" @click="exportToExcelInit">导出</el-button>-->
        <el-button size = 'mini' icon="el-icon-document" type="primary" @click="save">{{$t('AlarmLevelManagement.save')}}</el-button>
      </div>
    </el-header>

      <!-- table数据表格 -->
      <el-main class="adapt-height-box">
        <el-table
          :empty-text="$t('common.noData')"
          ref="multipleTable"
          :data="alarmCfgList"
          tooltip-effect="dark"
          header-row-class-name="table-header"
          cell-class-name="table-cell"
          class="adapt-height"
          height="auto"
          border
          highlight-current-row
          stripe
          @selection-change="handleSelectionChange">
          <el-table-column
            type="index"
            width="50">
          </el-table-column>
<!--          <el-table-column-->
<!--            prop="alarmCode"-->
<!--            :label="$t('AlarmLevelManagement.alarmCode')"-->
<!--            align="center"-->
<!--            :show-overflow-tooltip="true">-->
<!--          </el-table-column>-->
          <el-table-column
            prop="alarmName"
            :label="$t('AlarmLevelManagement.cause')"
            align="center"
            :show-overflow-tooltip="true">
          </el-table-column>
<!--          <el-table-column-->
<!--            prop="alarmClass"-->
<!--            :label="$t('AlarmLevelManagement.alarmClass')"-->
<!--            align="center"-->
<!--            :formatter="formatAlarmClass"-->
<!--            :show-overflow-tooltip="true">-->
<!--          </el-table-column>-->
          <el-table-column
            prop="resourceType"
            :label="$t('AlarmLevelManagement.resourceType')"
            align="center"
            :formatter="formatResourceType"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="alarmLevel"
            :label="$t('AlarmLevelManagement.alarmLevel')"
            align="center"
            @formatter="alarmLevelTurnWord"
            :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-select
                size = 'mini'
                v-model="alarmCfgList[scope.$index].alarmLevel"
                :placeholder="$t('common.placeholder')"
                style="width: 180px"
                clearable="true"
              >
                <el-option
                  v-for="item in optionsAlarmLevel"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import ElRow from 'element-ui/packages/row/src/row'

import AlarmManagerService from '@/domain/services/AlarmManagerService.js'
import HelperUtil from '../../utils/HelperUtil'

export default {
  components: {ElRow},
  name: 'AlarmLevelManagement',
  data: function () {
    return {
      /** ------------------下拉菜单内容-------------------------------------- */
      // 告警等级
      optionsAlarmLevel: [
        {
          label: this.$t('AlarmLevelManagement.MinorAlarm'),
          value: 0
        },
        {
          label: this.$t('AlarmLevelManagement.MajorAlarm'),
          value: 1
        },
        {
          label: this.$t('AlarmLevelManagement.CriticalAlarm'),
          value: 2
        }
      ],
      // 告警原因
      optionsAlarmCause: [],
      // 告警码
      optionsAlarmCode: [],
      /** -----------------下拉菜单选中------------------------------------------- */
      selectAlarmLevel: '', // 选择的告警等级
      selectAlarmCode: '', // 选择的告警码
      selectCause: '', // 选择的告警原因

      alarmLevel: '', // 记录

      // 告警列表
      alarmCode: '', // 告警码
      cause: '', // 告警原因
      alarmCount: '', // 频次
      alarmStatus: '', // 状态：0告警；1已确认；2已清除；3已恢复
      firstTime: '', // 首次发生时间
      curTime: '', // 最近发生时间
      recoverTime: '', // 恢复时间
      clearTime: '', // 清除时间
      confirmTime: '', // 确认时间
      confirmUser: '', // 确认用户
      note: '', // 其他信息

      // 双向绑定展示数据=
      alarmList: [], // 报警列表
      alarmCfgList: [], // 报警配置列表
      alarmCfgList_backup: [], // 报警配置列表备份，用来判定是否发生了数据的更改
      alarmCfgListUpdate: [], // 报警配置列表上传内容
      alarmCfgListUpdateNum: 0, // 报警配置列表上传列表长度
      // 表格 checkbox 选择数据集合
      multipleSelection: []
    }
  },
  // 初始化函数，加载数据
  created: function () {
    // this.getAllAlarmInit()
    this.getAlarmCfgList()
    this.getAlarmCfgList1()
  },
  methods: {
    /***
       * @Description  : 获取告警列表
       * @author       : lxy
       * @date         : 2018/12/20 20:17
       */
    getAlarmTypeList: function () {
      // console.log('获取告警列表')
      let _this = this
      AlarmManagerService.getAlarmTypeList().then(result => {
        // 成功
        _this.optionsAlarmCause = Object.values(result)
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
       * @Description  ：获取表格的选择项
       * @author       ::lxy
       * @date         :2018/12/20
       */
    handleSelectionChange (val) {
      this.multipleSelection = val
    },

    /**
       * @Description  ：获取全部告警类型配置信息（备份）
       * @author       ::lxy
       * @date         :2018/12/20
       */
    getAlarmCfgList1: function () {
      let _this = this
      AlarmManagerService.getAllAlarmCfgList().then(result => {
        // 成功
        _this.alarmCfgList_backup = Object.values(result)
        _this.alarmCfgList_backup = _this.alarmCfgList_backup.filter(item => {
          return item.alarmClass === 0
        })
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
       * @Description  ：获取全部告警类型配置信息
       * @author       ::lxy
       * @date         :2018/12/20
       */
    getAlarmCfgList: function () {
      let _this = this
      AlarmManagerService.getAllAlarmCfgList().then(result => {
        // 成功
        _this.alarmCfgList = Object.values(result)
        _this.alarmCfgList = _this.alarmCfgList.filter(item => {
          return item.alarmClass === 0
        })
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },

    /**
       * 保存修改内容
       *
       */
    save: function () {
      this.alarmCfgListUpdate = []
      this.alarmCfgListUpdateNum = 0
      // 判断是否产生了告警等级修改,当发生改变时将改变的数据上传
      for (let i = 0; i < this.alarmCfgList.length; i++) {
        // 判断如果不相等
        if (this.alarmCfgList[i].alarmLevel !== this.alarmCfgList_backup[i].alarmLevel) {
          this.alarmCfgListUpdate.push(this.alarmCfgList[i])
          this.alarmCfgListUpdateNum++
        }
      }
      if (this.alarmCfgListUpdateNum !== 0) {
        this.updateAlarmConfig()
      } else {
        // 未修改任何内容！"
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UNUPDATE_CODE))
      }
    },

    /**
       * 上传修改值
       */
    updateAlarmConfig: function () {
      let _this = this
      let loadingInstance = _this.Loading.openLoading()
      AlarmManagerService.updateAlarmConfig(this.alarmCfgListUpdate).then(result => {
        // 成功
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.MODIFY_CODE))
        _this.getAlarmCfgList1()
      }).catch(err => {
        // 失败
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.errorTip(_this, err)
      })
    },

    /**
       * 判断是否产生了告警等级修改
       */
    handleAlarmLevelChange: function () {
      for (let i = 0; i < this.alarmCfgList.length; i++) {
        if (this.alarmCfgList[i] === '') {

        }
      }
    },

    /**
       * 告警等级转换为文字，0：警告，1：严重，2：紧急
       */
    alarmLevelTurnWord: function (row, column) {
      if (row.alarmLevel === 0) {
        return '一般告警'
      } else if (row.alarmLevel === 1) {
        return '严重告警'
      } else if (row.alarmLevel === 2) {
        return '紧急告警'
      }
    },
    /**
       * 设置一般告警
       */
    setCommonAlarm: function () {
      console.log('设置一般告警')
      if (this.multipleSelection.length === 0) {
        // 请至少选择一条告警信息进行确认
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.CONFIRM_ONLY_ONE_CODE))
        return
      }
      for (var i = 0; i < this.multipleSelection.length; i++) {

      }
    },

    formatResourceType: function (row, colum) {
      let result = row.resourceType
      if (row.resourceType === 0) {
        result = this.$t('AlarmLevelManagement.system')
      }
      if (row.resourceType === 1) {
        result = this.$t('AlarmLevelManagement.port')
      }
      return result
    },

    formatAlarmClass: function (row, colum) {
      let result = row.alarmClass
      if (row.alarmClass === 0) {
        result = this.$t('AlarmLevelManagement.alarm')
      }
      if (row.alarmClass === 1) {
        result = this.$t('AlarmLevelManagement.perform')
      }
      return result
    },

    /**
       * @Description  ：导出文件
       * @author       : bjh
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2018/12/19 18:39
       */
    exportToExcelInit: function () {
      let selectList1 = ''
      let _this = this
      let loadingInstance = _this.Loading.openLoading()
      AlarmManagerService.downloadAllAlarmCfgList(selectList1).then(result => {
        // 成功
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.OTHERS_CODE))
      }).catch(err => {
        // 失败
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.errorTip(_this, err)
      })
    }

  },
  computed: {
    queryNodeTypeChange () {
      this.optionsAlarmLevel[0].label = this.$t('AlarmLevelManagement.MinorAlarm')
      this.optionsAlarmLevel[1].label = this.$t('AlarmLevelManagement.MajorAlarm')
      this.optionsAlarmLevel[2].label = this.$t('AlarmLevelManagement.CriticalAlarm')
    }
  },
  watch: {
    '$i18n.locale' () {
      this.queryNodeTypeChange()
    }
  }
}
</script>

<style>
</style>
