<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"
                   @change="deviceTypeChange"
                   size="mini" :placeholder="$t('common.selectOption')">
          <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('AlarmMangExp.deviceModel') + ':'"
                    :label-width="formLabelWidth"
                    style="margin-bottom: -4px">
        <el-select v-model="form.deviceModel" size="mini" :placeholder="$t('common.selectOption')">
          <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('AlarmMangExp.alarmCause') + ':'"
                    :label-width="formLabelWidth"
                    style="margin-bottom: -4px">
        <el-select v-model="form.alarmCause" 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">
        <el-input type="textarea" :rows="4" :placeholder="$t('common.placeholder')" v-model="form.mangExp"
                  autocomplete="off" style="width: 178px; 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: [],
      deviceModelList1: [],
      alarmCauseList: [],
      formLabelWidth: '150px',
      check: null
    }
  },
  methods: {
    /**
     * @Description  : deviceTypeChange
     * @author       : ls
     * @date         : 2020/11/11 14:24
     * @param        :
     * @return       :
     */
    deviceTypeChange: function () {

      var a = []
      if (this.form.deviceTypeKey !== '') {
        a = HelperUtil.deviceTypeChange(this.deviceModelList, this.form.deviceTypeKey, this.form.deviceModel)
        this.form.deviceModel = a[1]
      } else {
        a[0] = this.deviceModelList
      }
      this.deviceModelList1 = a[0]
    },
    close () {
      this.command.done()
    },
    createAlarmMangExpInit () {
      this.form.deviceTypeKey = this.initList.deviceTypeKey
      this.form.deviceModel = this.initList.deviceModel
      this.form.alarmCause = this.initList.alarmCode
      this.form.mangExp = this.initList.mangExp
    },
    /**
     * @Description  : 创建
     * @author       : ls
     * @date         : 2020/7/7 16:51
     * @param        :
     * @return       :
     */
    createAlarmMangExp () {
      if (!this.totalCheck()) {
        return
      }
      let alarmMangExp = {}
      alarmMangExp.deviceTypeKey = this.form.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)
        _this.deviceTypeChange()
        _this.form.deviceModel = _this.initList.deviceModel
      }).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>