<template>
  <div id="main">
    <!-- 告警过滤条件创建对话框 -->
    <el-dialog v-dialogDrag style="font-size: 10px;" width='500px'
               :title="$t('AlarmFilerManagement.createAlarmFiler')"
               :visible.sync="dialog_alarmFilterCreate"
               @close="close">
      <el-main>
        <el-form :model="alarmFilter" inline="true">
          <el-form-item :label="'*' + $t('AlarmFilerManagement.alarmFilerName') + ':'"
                        :label-width="formLabelWidth" style="margin-bottom: -10px">
            <el-input size='mini'
                      :placeholder="$t('common.placeholder')"
                      v-model="alarmFilter.alarmFilterName"
                      v-on:blur="checkedFilterName"
                      clearable="true"
                      style="width: 180px">
            </el-input>
          </el-form-item>
          <el-form-item :label="$t('AlarmFilerManagement.deviceType')+':'"
                        :label-width="formLabelWidth" style="margin-bottom: -10px">
            <el-select size='mini'
                       style="width: 180px;"
                       multiple
                       collapse-tags
                       clearable="true"
                       @change="deviceTypeChange"
                       v-model="alarmFilter.deviceType" :placeholder="$t('common.placeholder')">
              <el-option v-for="item in deviceTypeList"
                         :key="item.deviceTypeKey"
                         :label="item.deviceTypeName"
                         :value="item.deviceTypeKey">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('AlarmFilerManagement.deviceModel')+':'"
                        :label-width="formLabelWidth" style="margin-bottom: -10px">
            <el-select size='mini'
                       style="width: 180px;"
                       multiple
                       collapse-tags
                       @change="deviceModelChange"
                       clearable="true"
                       v-model="alarmFilter.deviceModel" :placeholder="$t('common.placeholder')">
              <el-option
                v-for="item in deviceModelList1"
                :key="item.deviceModelKey"
                :label="item.deviceModel"
                :value="item.deviceModel">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('AlarmFilerManagement.deviceName')+':'"
                        :label-width="formLabelWidth" style="margin-bottom: -10px">
            <el-select size='mini'
                       style="width: 180px;"
                       multiple
                       collapse-tags
                       clearable="true"
                       v-model="alarmFilter.deviceKey" :placeholder="$t('common.placeholder')">
              <el-option
                v-for="item in optionsDeviceName"
                :key="item.deviceKey"
                :label="item.displayName"
                :value="item.deviceKey">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('AlarmFilerManagement.alarmCause')+':'"
                        :label-width="formLabelWidth" style="margin-bottom: -10px">
            <el-select size='mini'
                       v-model="alarmFilter.alarmCode"
                       clearable="true"
                       multiple
                       collapse-tags
                       :placeholder="$t('common.placeholder')"
                       style="width: 180px;">
              <el-option
                v-for="item in alarmCfgList"
                :key="item.alarmCode"
                :label="item.alarmName"
                :value="item.alarmCode"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('AlarmFilerManagement.alarmLevel')+':'"
                        :label-width="formLabelWidth" style="margin-bottom: -10px">
            <el-select size='mini'
                       style="width: 180px;"
                       multiple
                       collapse-tags
                       clearable="true"
                       v-model="alarmFilter.alarmLevel"
                       :placeholder="$t('common.placeholder')">
              <el-option
                v-for="item in optionsAlarmLevelList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('AlarmFilerManagement.startFilterTime')+':'"
                        :label-width="formLabelWidth" style="margin-bottom: -10px">
            <el-date-picker
              v-model="alarmFilter.startFilterTime"
              style="width: 180px"
              type="datetime"
              size="mini"
              format="yyyy-MM-dd HH:mm"
              clearable="false"
              align="center"
              @change="beginDateChange"
              :placeholder="$t('AlarmFilerManagement.chooseStartTime')">
            </el-date-picker>
          </el-form-item>
          <el-form-item :label="$t('AlarmFilerManagement.endFilterTime')+':'"
                        :label-width="formLabelWidth" style="margin-bottom: 0px">
            <el-date-picker
              v-model="alarmFilter.endFilterTime"
              style="width: 180px"
              type="datetime"
              size="mini"
              format="yyyy-MM-dd HH:mm"
              clearable="false"
              align="center"
              @change="endDateChange"
              :placeholder="$t('AlarmFilerManagement.chooseEndTime')">
            </el-date-picker>
          </el-form-item>
          <el-form-item :label="$t('DeviceConfigurationManagement.nodeDesc')+':'"
                        :label-width="formLabelWidth" style="margin-bottom: -10px">
            <el-input type="textarea" style="width: 180px" :placeholder="$t('common.placeholder')" v-model="alarmFilter.description">
            </el-input>
          </el-form-item>
        </el-form>
      </el-main>
      <div slot="footer" class="dialog-footer">
        <el-button size='mini' type="primary" icon='el-icon-circle-check' @click="createAlarmFilter">
          {{$t("common.ok")}}
        </el-button>
        <el-button size='mini' icon='el-icon-circle-close' @click="close">{{$t("common.cancel")}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import DeviceService from '../../../domain/services/DeviceService.js'
import AlarmFilterConfigService from '../../../domain/services/AlarmFilterConfigService.js'
import legitimacyCheck from '../../../utils/legitimacyCheck'
import HelperUtil from '../../../utils/HelperUtil'
import AlarmManagerService from "../../../domain/services/AlarmManagerService";

export default {
  name: 'CreateAlarmFilterDialog',

  components: {},

  props: ['command'],

  data: function () { // 存放数据
    return {
      // 界面显示
      dialog_alarmFilterCreate: false,
      formLabelWidth: '120px',
      formSelectionWidth: '180px',

      // 过滤条件
      deviceCollection: {},
      // 告警等级列表下拉菜单
      optionsAlarmLevelList: [
        {
          label: this.$t('AlarmFilerManagement.MinorAlarm'),
          value: '0'
        },
        {
          label: this.$t('AlarmFilerManagement.MajorAlarm'),
          value: '1'
        },
        {
          label: this.$t('AlarmFilerManagement.CriticalAlarm'),
          value: '2'
        }
      ],
      deviceTypeList: [],
      // 设备名称选择域
      optionsDeviceName: [],
      deviceList0: [],

      // ---------创建对话框下拉菜单----------------

      alarmCfgList: [], // 告警配置列表
      deviceModelList: [], // 设备型号列表
      deviceModelList1: [], // 设备型号列表

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

  // vue加载初始化函数，加载数据
  created: function () {
    this.getAllDevice()
    this.getAllDeviceType()
    this.getAllDeviceModel()
    this.getAllAlarmCfgList()
  },

  mounted () {
    this.dialog_alarmFilterCreate = true
  },
  methods: {
    /**
     * @Description  : deviceTypeChange
     * @author       : ls
     * @date         : 2021/3/1 14:25
     * @param        :
     * @return       :
     */
    deviceTypeChange: function () {
      var a = HelperUtil.deviceTypeChangeCURD(this.deviceModelList, this.alarmFilter.deviceType, this.alarmFilter.deviceModel, this.deviceList0)
      this.deviceModelList1 = a[0]
      this.alarmFilter.deviceModel = ''
      this.alarmFilter.deviceKey = ''
      this.optionsDeviceName = []
      this.optionsDeviceName = a[1]
    },
    /**
     * @Description  : deviceModelChange
     * @author       : ls
     * @date         : 2021/3/8 10:56
     * @param        :
     * @return       :
     */
    deviceModelChange: function () {
      this.alarmFilter.deviceKey = ''
      this.optionsDeviceName = []
      this.optionsDeviceName = HelperUtil.deviceModelChangeCURD(this.alarmFilter.deviceType, this.alarmFilter.deviceModel, this.deviceList0)
    },
    /**
     * @Description  : getAllDeviceModel
     * @author       : ls
     * @date         : 2020/11/16 14:59
     * @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)
      })
    },
    close: function () {
      this.command.done()
    },
    /**
     * @Description  : 开始时间
     * @author       : ls
     * @date         : 2020/7/23 17:20
     * @param        :
     * @return       :
     */
    beginDateChange: function () {
      const setTimes = new Date()
      if (this.alarmFilter.endFilterTime !== null && this.alarmFilter.endFilterTime !== '') {
        if (this.alarmFilter.startFilterTime >= this.alarmFilter.endFilterTime) {
          setTimes.setTime(this.alarmFilter.endFilterTime.getTime() - (60 * 60 * 1000))
          this.alarmFilter.startFilterTime = setTimes
          this.InfoTip.warningTip(this, {message: this.$t('AlarmFilerManagement.warnstartFilterTime')})
        }
      }
    },
    /**
     * @Description  : 结束时间
     * @author       : ls
     * @date         : 2020/7/23 17:21
     * @param        :
     * @return       :
     */
    endDateChange: function () {
      const setTimes = new Date()
      if (this.alarmFilter.endFilterTime <= this.alarmFilter.startFilterTime) {
        setTimes.setTime(this.alarmFilter.startFilterTime.getTime() + (60 * 60 * 1000))
        this.alarmFilter.endFilterTime = setTimes
        this.InfoTip.warningTip(this, {message: this.$t('AlarmFilerManagement.warnendFilterTime')})
      }
    },

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

    /**
     * @Description  : 获取网元类型
     * @author       : ls
     * @date         : 2020/7/23 11:15
     * @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 16:14
     * @param        :
     * @return       :
     */
    getAllAlarmCfgList: function () {
      let _this = this
      AlarmManagerService.getAllAlarmCfgList().then(result => {
        // 成功
        _this.alarmCfgList = Object.values(result)
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
     * @Description  : getALlDevice
     * @author       : ls
     * @date         : 2020/11/16 14:57
     * @param        :
     * @return       :
     */
    getAllDevice: function () {
      let _this = this
      DeviceService.getAllDeviceCollection().then(result => {
        // 成功
        console.log('1' + this.deviceModelList)
        _this.deviceCollection = result
        _this.optionsDeviceName = Object.values(result)
        _this.deviceList0 = Object.values(result)
        // _this.getAllDeviceModelList()
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /***
       * @Description  : 获取设备型号列表
       * @author       : lxy
       * @date         : 2018/12/24 11:29
       */
    getAllDeviceModelList: function () {
      console.log('1' + this.deviceModelList)
      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.deviceCollection[key].deviceModel !== '') {
          this.deviceModelList.push(this.deviceCollection[key].deviceModel)
        }
      }
    },
    /**
     * @Description  : 名称校验
     * @author       : ls
     * @date         : 2020/7/24 9:25
     * @param        :
     * @return       :
     */
    checkedFilterName: function () {
      let check = legitimacyCheck()
      if (check.stringNullCheck(this.alarmFilter.alarmFilterName)) {
        this.InfoTip.warningTip(this, {message: this.$t('AlarmFilerManagement.warnFilterName')})
        return false
      } else {
        if (this.textLengthCheck(this.alarmFilter.alarmFilterName)) {
          return true
        } else {
          return false
        }
      }
    },
    /**
     * @Description  : totalCheck
     * @author       : ls
     * @date         : 2021/3/1 16:49
     * @param        :
     * @return       :
     */
    totalCheck: function () {
      if (this.alarmFilter.deviceType.length === 0 && this.alarmFilter.deviceModel.length === 0 &&
        this.alarmFilter.deviceKey.length === 0 && this.alarmFilter.alarmCode.length === 0 &&
        this.alarmFilter.alarmLevel.length === 0 &&
        this.alarmFilter.endFilterTime === '' && this.alarmFilter.startFilterTime === '') {
        this.InfoTip.warningTip(this, {message: this.$t('AlarmFilerManagement.oneCondition')})
        return false
      } else {
        return true
      }
    },
    textLengthCheck: function (text) {
      let check = legitimacyCheck()
      if (!check.textLengthCheck(text)) {
        this.InfoTip.warningTip_alarm(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.LENGTH_CODE))
        return false
      } else {
        return true
      }
    },
    /**
     * @Description  : 创建
     * @author       : ls
     * @date         : 2020/7/23 14:07
     * @param        :
     * @return       :
     */
    createAlarmFilter: function () {
      if (!this.checkedFilterName()) {
        return
      }
      if (!this.totalCheck()) {
        return
      }
      var alarmFilter = {}
      alarmFilter.alarmFilterName = this.alarmFilter.alarmFilterName
      alarmFilter.deviceTypeKey = this.alarmFilter.deviceType === '' ? '' : this.alarmFilter.deviceType.join()
      alarmFilter.deviceModel = this.alarmFilter.deviceModel === '' ? '' : this.alarmFilter.deviceModel.join()
      alarmFilter.deviceKey = this.alarmFilter.deviceKey === '' ? '' : this.alarmFilter.deviceKey.join()
      alarmFilter.alarmCode = this.alarmFilter.alarmCode === '' ? '' : this.alarmFilter.alarmCode.join()
      // alarmFilter.deviceTypeKey = this.alarmFilter.deviceType.join()
      // alarmFilter.deviceModel = this.alarmFilter.deviceModel.join()
      // alarmFilter.deviceKey = this.alarmFilter.deviceKey.join()
      // alarmFilter.alarmCode = this.alarmFilter.alarmCode.join()
      if (this.alarmFilter.alarmLevel !== '' || this.alarmFilter.alarmLevel !== null) {
        alarmFilter.alarmLevel = this.alarmFilter.alarmLevel.join()
      } else {
        alarmFilter.alarmLevel = -999
      }
      alarmFilter.startFilterTime = HelperUtil.dateToString(this.alarmFilter.startFilterTime)
      alarmFilter.endFilterTime = HelperUtil.dateToString(this.alarmFilter.endFilterTime)
      alarmFilter.description = this.alarmFilter.description
      let _this = this
      let loadingInstance = _this.Loading.openLoading()
      AlarmFilterConfigService.addAlarmFilter(alarmFilter).then(result => {
        // 成功了！
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.ADD_CODE))
        _this.close()
      }).catch(err => {
        // 失败
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.errorTip(_this, err)
      })
    }
  }
}
</script>

<style scoped>
  /*!* 外层布局容器样式 *!*/
  .el-header {
    background-color: #f9fafc;
    color: #333;
    line-height: 40px;
  }
  /deep/ .el-input__inner{
    /*height: 28px !important;*/
  }
  /deep/ .el-dialog__body{
    padding-top: 0px !important;
  }
</style>
