<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-circle-plus-outline" type="primary" @click="createAlarmFilterInit" v-if="createAndCopyCommand.visible">
            {{$t("common.createBtn")}}
          </el-button>
          <el-button size='mini' icon="el-icon-delete" type="danger" @click="deleteAlarmFilterInit" v-if="deleteAlarmFilterCommand.visible">
            {{$t("common.deleteBtn")}}
          </el-button>
        </div>
        <div class="header-search">
          <div class="search-input-box">
            <label>{{ $t('AlarmFilerManagement.alarmFilerName')}}:</label>
            <el-input size='mini' :placeholder="$t('common.placeholder')" v-model="queryAlarmFilterName"
                      clearable>
            </el-input>
          </div>
          <div class="search-input-box">
            <label>{{ $t('AlarmFilerManagement.alarmCause')}}:</label>
            <el-select size='mini' v-model="alarmCause" :placeholder="$t('common.placeholder')" :clearable="true">
              <el-option
                v-for="item in optionsAlarmCause"
                :key="item.alarmCode"
                :label="item.alarmName"
                :value="item.alarmCode">
              </el-option>
            </el-select>
          </div>
          <div class="search-input-box">
            <label>{{ $t('AlarmFilerManagement.alarmLevel')}}:</label>
            <el-select size='mini' v-model="alarmLevel" :placeholder="$t('common.placeholder')" :clearable="true">
              <el-option
                v-for="item in alarmLevelList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </div>
          <div class="search-input-box">
            <label>{{ $t('AlarmFilerManagement.deviceType')}}:</label>
            <el-select size='mini' clearable
                       @change="deviceTypeChange"
                       v-model="deviceType">
              <el-option v-for="item in deviceTypeList"
                         :key="item.deviceTypeKey"
                         :label="item.deviceTypeName"
                         :value="item.deviceTypeKey">
              </el-option>
            </el-select>
          </div>
          <div class="search-input-box">
            <label>{{ $t('AlarmFilerManagement.deviceModel')}}:</label>
            <el-select size='mini' clearable
                       @change="deviceModelChange"
                       v-model="deviceModel">
              <el-option v-for="item in deviceModelList1"
                         :key="item.deviceModelKey"
                         :label="item.deviceModel"
                         :value="item.deviceModel">
              </el-option>
            </el-select>
          </div>
          <div class="search-input-box">
            <label>{{ $t("AlarmStatistics.DeviceName") }}</label>
            <el-select size='mini' v-model="deviceName"
                       clearable>
              <el-option
                v-for="item in optionsDeviceName"
                :key="item.deviceKey"
                :label="item.displayName"
                :value="item.deviceKey">
              </el-option>
            </el-select>
          </div>
          <div class="search-input-box">
            <el-button size='mini' icon="el-icon-search" type="primary" @click="queryAlarmFilterByCondition">
              {{ $t("common.queryBtn")}}
            </el-button>
          </div>
        </div>
      </el-header>
      <!-- 表格 -->
      <el-main class="adapt-height-box">
        <el-table
          :empty-text="$t('common.noData')"
          header-row-class-name="table-header"
          cell-class-name="table-cell"
          class="adapt-height"
          ref="singleTable"
          :data="alarmFilterList"
          highlight-current-row
          stripe
          tooltip-effect="dark"
          height="auto"
          border
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="28">
          </el-table-column>
          <el-table-column
            prop="alarmFilterName"
            :label="$t('AlarmFilerManagement.alarmFilerName')"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="alarmName"
            :label="$t('AlarmFilerManagement.alarmCause')"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="alarmLevel"
            :label="$t('AlarmFilerManagement.alarmLevel')"
            :formatter="formatAlarmLevels"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="deviceTypeName"
            :label="$t('AlarmFilerManagement.deviceType')"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="deviceModel"
            :label="$t('AlarmFilerManagement.deviceModel')"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="displayName"
            :label="$t('AlarmFilerManagement.deviceName')"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="startFilterTime"
            :label="$t('AlarmFilerManagement.startFilterTime')"
            :formatter="formatFirstDate"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="endFilterTime"
            :label="$t('AlarmFilerManagement.endFilterTime')"
            :formatter="formatClearDate"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="description"
            :label="$t('AlarmFilerManagement.description')"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            width="110px"
            v-if="updateAlarmFilterCommand.visible"
            :label="$t('common.operation')">
            <template slot-scope="scope">
              <el-button
                size="mini"
                style="background: #175BE3"
                :title="$t('common.copyBtn')"
                @click="copyAlarmFilterInit(scope.row)">
                <svg class="icon" aria-hidden="true">
                  <use xlink:href="#icon-fuzhi"></use>
                </svg>
              </el-button>
              <el-button
                size="mini"
                style="background: #008000"
                :title="$t('common.updateBtn')"
                @click="updateAlarmFilterInit(scope.row)">
                <svg class="icon" aria-hidden="true">
                  <use xlink:href="#icon-bianji"></use>
                </svg>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-container>

    <CommandExecuteContext v-bind:commands="commandContext"></CommandExecuteContext>
  </div>
</template>

<script>
import CommandExecuteContext from '../../commands/CommandExecuteContext'
import AlarmCommands from '../../commands/AlarmCommands'
import AlarmManagerService from '../../domain/services/AlarmManagerService.js'
import AlarmFilterConfigService from '../../domain/services/AlarmFilterConfigService.js'
import DeviceService from '../../domain/services/DeviceService.js'
import HelperUtil from '../../utils/HelperUtil'

export default {
  name: 'AlarmFilerManagement',
  components: {
    CommandExecuteContext
  },
  data: function () { // 存放 数据
    return {
      // 命令
      commandContext: {},

      // 表格数据
      alarmFilterList: [],

      // 过滤条件
      deviceCollection: {},
      deviceList0: [],
      // 设备名称选择域
      optionsDeviceName: [],
      optionsAlarmCause: [],
      deviceModelList: [],
      deviceModelList1: [],
      deviceTypeList: [],
      queryAlarmFilterName: '',
      alarmCause: '',
      alarmLevel: '',
      deviceType: '',
      deviceModel: '',
      deviceName: '',

      // 告警等级列表下拉菜单
      alarmLevelList: [
        {
          label: this.$t('AlarmFilerManagement.MinorAlarm'),
          value: '0'
        },
        {
          label: this.$t('AlarmFilerManagement.MajorAlarm'),
          value: '1'
        },
        {
          label: this.$t('AlarmFilerManagement.CriticalAlarm'),
          value: '2'
        }
      ],

      // 告警过滤创建、修改使用
      updateAlarmFilter: {},
      alarmFilter: {
        alarmCode: '',
        alarmFilterKey: '',
        alarmFilterName: '',
        alarmLevel: '',
        delFlag: 0,
        deviceKey: '',
        deviceModel: '',
        endFilterTime: '',
        modifyTime: '',
        deviceType: '',
        startFilterTime: ''
      },

      // 告警过滤删除使用
      deleteList: [],
      selectList: [],

      // 过滤
      alarmCfgList: [], // 告警配置列表

      multipleSelection: [],
      beginItem: 1,
      endItem: 100,
      total: 100,
      currentPage: 1,
      pageSize: 100
    }
  },

  created: function () {
    this.getAllDevice()
    this.getAlarmTypeList()
    this.getAllDeviceType()
    this.getAllDeviceModel()
    this.getAlarmFilterList()
  },
  computed: {
    createAndCopyCommand () {
      return AlarmCommands.createAlarmFilterCommand(this.alarmFilter, this.commandContext)
    },
    updateAlarmFilterCommand () {
      return AlarmCommands.updateAlarmFilterCommand(this.updateAlarmFilter, this.commandContext)
    },
    copyAlarmFilterCommand () {
      return AlarmCommands.copyAlarmFilterCommand(this.alarmFilter, this.commandContext)
    },
    deleteAlarmFilterCommand () {
      return AlarmCommands.deleteAlarmFilterCommand(this.deleteList, this.commandContext)
    }
  },

  methods: {
    /**
     * @Description  : deviceTypeChange
     * @author       : ls
     * @date         : 2020/11/12 9:47
     * @param        :
     * @return       :
     */
    deviceTypeChange: function () {
      var a = HelperUtil.deviceTypeChangeDevice(this.deviceModelList, this.deviceType, this.deviceModel, this.deviceList0)
      this.deviceModelList1 = a[0]
      this.deviceModel = a[1]
      this.deviceName = ''
      this.optionsDeviceName = []
      this.optionsDeviceName = a[2]
    },
    /**
     * @Description  : deviceModelChange
     * @author       : ls
     * @date         : 2020/11/11 12:06
     * @param        :
     * @return       :
     */
    deviceModelChange: function () {
      this.deviceName = ''
      this.optionsDeviceName = []
      this.optionsDeviceName = HelperUtil.deviceModelChange(this.deviceType, this.deviceModel, this.deviceList0)
    },
    /**
       * @Description  :表格多选框改变时,将所选行进行保存
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    /**
     * @Description  : 格式化告警等级
     * @author       : ls
     * @date         : 2020/7/23 17:22
     * @param        :
     * @return       :
     */
    formatAlarmLevels: function (row, colum) {
      if (row.alarmLevel !== '' || row.alarmLevel !== null || row.alarmLevel === '-999') {
        var level = row.alarmLevel.split(',')
        let ret = ''
        for (let i = 0; i < level.length; i++) {
          switch (level[i]) {
            case '0':
              ret = ret + this.$t('AlarmFilerManagement.MinorAlarm') + ','
              break
            case '1':
              ret = ret + this.$t('AlarmFilerManagement.MajorAlarm') + ','
              break
            case '2':
              ret = ret + this.$t('AlarmFilerManagement.CriticalAlarm') + ','
              break
          }
        }
        return ret.substring(0, ret.length - 1)
      } else {
        return ''
      }
    },

    /***
       * @Description  : 设备名称过滤器
       * @author       : zf
       * @date         : 2018/12/24 16:07
       */
    formatDeviceKeys: function (row, column) {
      for (var j = 0; j < this.optionsDeviceName.length; j++) {
        if (row.deviceKey == this.optionsDeviceName[j]['deviceKey']) {
          return this.optionsDeviceName[j]['displayName']
        }
      }
    },

    /***
       * @Description  : 首次发生日期过滤
       * @author       : zf
       * @date         : 2019/1/2 19:40
       */
    formatFirstDate: function (row) {
      if (row.startFilterTime === '' || row.startFilterTime === null) {
        return ''
      } else {
        return HelperUtil.timeTran(row.startFilterTime)
      }
    },

    /***
       * @Description  : 最近发生日期过滤
       * @author       : zf
       * @date         : 2019/1/2 19:40
       */
    formatClearDate: function (row) {
      if (row.endFilterTime === '' || row.endFilterTime === null) {
        return ''
      } else {
        return HelperUtil.timeTran(row.endFilterTime)
      }
    },

    // ---------------------初始化页面----------------------------------------

    /***
       * @Description  : 获取所有告警过滤信息
       * @author       : zf
       * @date         : 2019/05/22 10:12
       */
    getAllAlarmFilterList: function () {
      let _this = this
      AlarmFilterConfigService.getAllAlarmFilterList().then(result => {
        // 成功
        _this.alarmFilterList = Object.values(result)
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
     * @Description  : 条件查询
     * @author       : ls
     * @date         : 2020/7/23 9:45
     * @param        :
     * @return       :
     */
    queryAlarmFilterByCondition: function () {
      this.beginItem = 1
      this.setSelectList()
      this.queryAlarmFilterTotalNumber()
    },
    queryAlarmFilterTotalNumber: function () {
      let _this = this
      let loadingInstance = _this.Loading.openLoading()
      AlarmFilterConfigService.queryAlarmFilterTotalNumber(_this.selectList).then(result => {
        _this.Loading.closeLoading(loadingInstance)
        _this.total = result
        if (_this.total === 0 || _this.total < 0) {
          _this.alarmFilterList = []
          _this.InfoTip.warningTip_alarm(_this, HelperUtil.getCheckStatusCodeObjectByCode(_this.successCode.QUERY_NULL_CODE))
        } else {
          if (_this.total < 100 && _this.total > 0) {
            _this.endItem = _this.total
          } else {
            _this.endItem = 100
          }
          _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.QUERY_CODE))
          _this.alarmFilterByConditionCurrentPage()
        }
      }).catch(err => {
        _this.endItem = 100
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
     * @Description  : 分页变化
     * @author       : ls
     * @date         : 2020/7/23 10:45
     * @param        :
     * @return       :
     */
    handleCurrentChange (val) {
      this.currentPage = val
      this.beginItem = ((val - 1) * 100) + 1
      this.endItem = this.beginItem + 99
      if (this.endItem > this.total) {
        this.endItem = this.total
      }
      this.alarmFilterByConditionCurrentPage()
    },
    /**
     * @Description  : 获取总数
     * @author       : ls
     * @date         : 2020/7/23 9:38
     * @param        :
     * @return       :
     */
    getAlarmFilterList: function () {
      this.beginItem = 1
      this.setSelectList()
      let _this = this
      let loadingInstance = _this.Loading.openLoading()
      AlarmFilterConfigService.queryAlarmFilterTotalNumber(this.selectList).then(result => {
        _this.Loading.closeLoading(loadingInstance)
        _this.total = result
        if (_this.total === 0 || _this.total < 0) {
          _this.alarmFilterList = []
        } else {
          if (_this.total < 100 && _this.total > 0) {
            _this.endItem = _this.total
          } else {
            _this.endItem = 100
          }
          _this.alarmFilterByConditionCurrentPage()
        }
      }).catch(err => {
        _this.endItem = 100
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
     * @Description  : 分页查询
     * @author       : ls
     * @date         : 2020/7/23 10:04
     * @param        :
     * @return       :
     */
    alarmFilterByConditionCurrentPage: function () {
      let _this = this
      let loadingInstance = _this.Loading.openLoading()
      AlarmFilterConfigService.queryAlarmFilterDataSplitNumber(this.beginItem, this.endItem, this.selectList).then(result => {
        // 成功
        _this.alarmFilterList = Object.values(result)
        _this.Loading.closeLoading(loadingInstance)
      }).catch(err => {
        // 失败
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.errorTip(_this, err)
      })
    },

    /***
       * @Description  : 获取所有设备列表
       * @author       : lxy
       * @date         : 2018/12/24 11:29
       */
    getAllDevice: function () {
      let _this = this
      DeviceService.getAllDeviceCollection().then(result => {
        // 成功
        _this.deviceCollection = result
        _this.optionsDeviceName = Object.values(result)
        _this.deviceList0 = Object.values(result)
        // _this.getAllDeviceModelList()
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
     * @Description  : 获取告警原因
     * @author       : ls
     * @date         : 2020/7/23 9:11
     * @param        :
     * @return       :
     */
    getAlarmTypeList: function () {
      let _this = this
      AlarmManagerService.getAlarmTypeList().then(result => {
        _this.optionsAlarmCause = Object.values(result)
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
     * @Description  : 获取网元类型
     * @author       : ls
     * @date         : 2020/7/23 9:21
     * @param        :
     * @return       :
     */
    getAllDeviceType: function () {
      let _this = this
      DeviceService.getAllDeviceTypeCollection().then(result => {
        _this.deviceTypeList = Object.values(result)
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
     * @Description  : 获取网元型号
     * @author       : ls
     * @date         : 2020/7/23 9:23
     * @param        :
     * @return       :
     */
    getAllDeviceModel: function () {
      let _this = this
      DeviceService.getAllDeviceModelCollection().then(result => {
        _this.deviceModelList = Object.values(result)
        _this.deviceModelList1 = Object.values(result)
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
     * @Description  : 设置条件
     * @author       : ls
     * @date         : 2020/7/23 10:28
     * @param        :
     * @return       :
     */
    setSelectList: function () {
      this.selectList = []
      if (this.queryAlarmFilterName !== '' && this.queryAlarmFilterName !== null) {
        this.selectList.push({
          quaryAttribute: 'alarmFilterName',
          compareSymbol: 'like',
          compareValue: this.queryAlarmFilterName
        })
      }
      if (this.deviceName !== '' && this.deviceName !== null) {
        this.selectList.push({
          quaryAttribute: 'deviceKey',
          compareSymbol: 'like',
          compareValue: this.deviceName
        })
      }
      if (this.deviceType !== null && this.deviceType !== '') {
        this.selectList.push({
          quaryAttribute: 'deviceTypeKey',
          compareSymbol: '=',
          compareValue: this.deviceType
        })
      }
      if (this.deviceModel !== null && this.deviceModel !== '') {
        this.selectList.push({
          quaryAttribute: 'deviceModel',
          compareSymbol: '=',
          compareValue: this.deviceModel
        })
      }
      if (this.alarmCause !== '' && this.alarmCause !== null) {
        this.selectList.push({
          quaryAttribute: 'alarmCode',
          compareSymbol: '=',
          compareValue: this.alarmCause
        })
      }
      if (this.alarmLevel !== '' && this.alarmLevel !== null) {
        this.selectList.push({
          quaryAttribute: 'alarmLevel',
          compareSymbol: '=',
          compareValue: this.alarmLevel
        })
      }
    },
    /***
       * @Description  : 获取设备型号列表
       * @author       : lxy
       * @date         : 2018/12/24 11:29
       */
    getAllDeviceModelList: function () {
      for (let key in this.deviceCollection) {
        // 标记该设备型号是否已经存在在列表当中
        let containIs = false
        // 判断设备型号是否已经存在在设备型号列表当中
        for (let j = 0; j < this.deviceModelList.length; j++) {
          if (this.deviceCollection[key].deviceModel === this.deviceModelList[j]) {
            containIs = true
            break
          }
        }
        // 如果没有存在,存入设备型号列表当中
        if (containIs === false) {
          this.deviceModelList.push(this.deviceCollection[key].deviceModel)
        }
      }
    },
    /**
     * @Description  : 创建
     * @author       : ls
     * @date         : 2020/7/24 14:56
     * @param        :
     * @return       :
     */
    createAlarmFilterInit: function () {
      this.createAndCopyCommand.execute()
    },

    /**
     * @Description  : 修改
     * @author       : ls
     * @date         : 2020/7/23 10:48
     * @param        :
     * @return       :
     */
    updateAlarmFilterInit: function (val) {
      this.updateAlarmFilter = val
      this.updateAlarmFilterCommand.execute()
    },
    /**
     * @Description  : 复制
     * @author       : ls
     * @date         : 2020/7/23 10:48
     * @param        :
     * @return       :
     */
    copyAlarmFilterInit: function (val) {
      this.alarmFilter = val
      this.copyAlarmFilterCommand.execute()
    },
    /**
     * @Description  : 删除
     * @author       : ls
     * @date         : 2020/7/24 8:59
     * @param        :
     * @return       :
     */
    deleteAlarmFilterInit: function () {
      if (this.multipleSelection.length === 0) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.DELETE_SELECT_CODE))
      } else {
        this.deleteList = []
        for (let i = 0; i < this.multipleSelection.length; i++) {
          this.deleteList.push(this.multipleSelection[i]['alarmFilterKey'])
        }
        this.deleteAlarmFilterCommand.execute()
      }
    }
  },
  watch: {
    commandContext (newVal, oldVal) {
      if (JSON.stringify(newVal) === '{}') {
        this.getAlarmFilterList()
      }
      deep: true
    }
  }
}
</script>

<style lang="scss" scoped>
</style>