<template>
  <el-dialog
    @close="close"
    @open="createAlarmMangExpInit"
    v-dialogDrag
    :title=title
    width='450px'
    :visible.sync="createAndCopySubnetVisible">
    <el-form :model="form" inline="true">
      <el-form-item :label="$t('AlarmMangExp.deviceType') + ':'"
                    :label-width="formLabelWidth"
                    style="margin-bottom: -4px">
        <el-select v-model="form.deviceTypeKey" disabled size="mini" :placeholder="$t('common.selectOption')">
          <el-option
            v-for="item in deviceTypeList"
            :key="item.deviceTypeKey"
            :label="item.deviceTypeKey"
            :value="item.deviceTypeKey">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('AlarmMangExp.deviceModel') + ':'"
                    :label-width="formLabelWidth"
                    style="margin-bottom: -4px">
        <el-select v-model="form.deviceModel" disabled size="mini" :placeholder="$t('common.selectOption')">
          <el-option v-for="item in deviceModelList"
                     :key="item.deviceModelKey"
                     :label="item.deviceModel"
                     :value="item.deviceModel">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('AlarmMangExp.alarmCause') + ':'"
                    :label-width="formLabelWidth"
                    style="margin-bottom: -4px">
        <el-select v-model="form.alarmCause" disabled size="mini" :placeholder="$t('common.selectOption')">
          <el-option
            v-for="item in alarmCauseList"
            :key="item.alarmCode"
            :label="item.alarmName"
            :value="item.alarmCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('AlarmMangExp.mangExp')+':'" :label-width="formLabelWidth"
                    style="margin-bottom: -10px; margin-left: 0px;">
        <el-input type="textarea" :rows="4" :placeholder="$t('common.placeholder')" v-model="form.mangExp"
                  autocomplete="off" style="width: 172px; margin-top: 10px;"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size='mini' icon="el-icon-circle-check" type="primary" @click="createAlarmMangExp">
        {{$t("common.ok")}}
      </el-button>
      <el-button size='mini' icon="el-icon-circle-close" @click="close">{{$t("common.cancel")}}</el-button>
    </div>
  </el-dialog>
</template>

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

export default {
  props: ['command'],
  data: function () {
    return {
      title: this.command.title,
      initList: this.command.target,
      createAndCopySubnetVisible: false,
      form: {
        deviceTypeKey: '',
        deviceModel: '',
        alarmCause: '',
        mangExp: ''
      },
      deviceTypeList: [],
      deviceModelList: [],
      alarmCauseList: [],
      formLabelWidth: '100px',
      check: null
    }
  },
  methods: {
    close () {
      this.command.done()
    },
    createAlarmMangExpInit () {
      this.form.deviceTypeKey = this.initList.deviceTypeName
      this.form.deviceModel = this.initList.deviceModel
      this.form.alarmCause = this.initList.alarmCode
      this.form.mangExp = ''
    },
    /**
     * @Description  : 创建
     * @author       : ls
     * @date         : 2020/7/7 16:51
     * @param        :
     * @return       :
     */
    createAlarmMangExp () {
      if (!this.totalCheck()) {
        return
      }
      let alarmMangExp = {}
      alarmMangExp.deviceTypeKey = this.initList.deviceTypeKey
      alarmMangExp.deviceModel = this.form.deviceModel
      alarmMangExp.alarmCode = this.form.alarmCause
      alarmMangExp.mangExp = this.form.mangExp
      let _this = this
      let loadingInstance = _this.Loading.openLoading()
      AlarmMangExpService.addAlarmMangExp(alarmMangExp).then(result => {
        // 成功
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.ADD_CODE))
        _this.command.done()
      }).catch(err => {
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
     * @Description  : 类型校验
     * @author       : ls
     * @date         : 2020/7/7 16:54
     * @param        :
     * @return       :
     */
    deviceTypeKeyCheck: function () {
      if (!this.check.textNullCheck(this.form.deviceTypeKey)) {
        this.InfoTip.warningTip_alarm(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('AlarmMangExp.deviceType'))
        return false
      }
      return true
    },
    /**
     * @Description  : 网元型号
     * @author       : ls
     * @date         : 2020/7/7 16:56
     * @param        :
     * @return       :
     */
    deviceModelCheck: function () {
      if (!this.check.textNullCheck(this.form.deviceModel)) {
        this.InfoTip.warningTip_alarm(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('AlarmMangExp.deviceModel'))
        return false
      }
      return true
    },
    /**
     * @Description  : 告警原因
     * @author       : ls
     * @date         : 2020/7/7 16:56
     * @param        :
     * @return       :
     */
    alarmCodeCheck: function () {
      if (!this.check.textNullCheck(this.form.alarmCause)) {
        this.InfoTip.warningTip_alarm(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('AlarmMangExp.alarmCause'))
        return false
      }
      return true
    },
    /**
     * @Description  : 措施校验
     * @author       : ls
     * @date         : 2020/7/7 16:57
     * @param        :
     * @return       :
     */
    mangExpCheck: function () {
      if (!this.check.textNullCheck(this.form.mangExp)) {
        this.InfoTip.warningTip_alarm(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('AlarmMangExp.mangExp'))
        return false
      }
      return true
    },
    /**
     * @Description  : 校验
     * @author       : ls
     * @date         : 2020/7/7 16:52
     * @param        :
     * @return       :
     */
    totalCheck: function () {
      if (!this.deviceTypeKeyCheck(this.form.deviceTypeKey)) {
        return false
      }
      if (!this.deviceModelCheck(this.form.deviceModel)) {
        return false
      }
      if (!this.alarmCodeCheck(this.form.alarmCause)) {
        return false
      }
      if (!this.mangExpCheck(this.form.mangExp)) {
        return false
      }
      return true
    },
    /**
     * @description   : 获取网元型号
     * @author        : ls
     * @date          : 2020/7/4 14:47
     * @param         :
     * @return        :
     **/
    getAllDeviceModel: function () {
      let _this = this
      DeviceService.getAllDeviceModelCollection().then(result => {
        _this.deviceModelList = Object.values(result)
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
     * @description   : 获取网元类型
     * @author        : ls
     * @date          : 2020/7/4 14:48
     * @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/4 15:02
     * @param         :
     * @return        :
     **/
    getAlarmTypeList: function () {
      let _this = this
      AlarmManagerService.getAlarmTypeList().then(result => {
        _this.alarmCauseList = Object.values(result)
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    },
  },

  created () {
    this.check = legitimacyCheck()
    this.getAllDeviceModel()
    this.getAllDeviceType()
    this.getAlarmTypeList()
  },

  mounted () {
    this.createAndCopySubnetVisible = true
  }
}
</script>
