<template>
</template>

<script>
import DeviceService from '@/domain/services/DeviceService.js'
import HelperUtil from '../../../utils/HelperUtil'
import SubnetService from '@/domain/services/SubnetService.js'

let curthis = null
export default {
  props: ['command'],

  data: function () {
    return {
      deviceList: this.command.target,
      subnetList: null,
      arr: null,
      defKey: ''
    }
  },

  created () {
    curthis = this
    this.getAllSubnet()
    this.open()
  },

  methods: {
    getAllSubnet: function () {
      let _this = this
      SubnetService.getAllSubnetCollection().then(result => {
        _this.subnetList = Object.values(result)
        _this.arr = _this.subnetList.find((item) => {
          return item.subnetName === '默认子网'
        })
        _this.defKey = _this.arr.subnetKey
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    },
    open () {
      this.InfoTip.conformTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.REMOVE_THE_DEVICE_FROM_SUBNET))
        .then(() => {
          this.$options.methods.removeDeviceFromSubnet()
        }).catch(() => {
          this.command.done()
        })
    },

    // 删除设备
    removeDeviceFromSubnet () {
      let devicesTemp = []
      curthis.deviceList.forEach(device => {
        let deviceTemp = {}
        deviceTemp.deviceKey = device.deviceKey
        deviceTemp.uuid = device.uuid
        deviceTemp.hostName = device.hostName
        deviceTemp.displayName = device.displayName
        deviceTemp.deviceTypeKey = device.deviceTypeKey
        deviceTemp.deviceModel = device.deviceModel
        deviceTemp.locationName = device.locationName
        deviceTemp.station = device.station
        deviceTemp.subnetKey = '0'
        deviceTemp.ipAddress = device.ipAddress
        deviceTemp.bussIpAddress = device.bussIpAddress
        deviceTemp.mac = device.mac
        deviceTemp.snmpVersion = device.snmpVersion
        deviceTemp.snmpPort = device.snmpPort
        deviceTemp.trapPort = device.trapPort
        deviceTemp.firmwareKey = device.firmwareKey
        deviceTemp.firmwareFileName = device.firmwareFileName
        deviceTemp.manageType = device.manageType
        deviceTemp.bussManageType = device.bussManageType
        deviceTemp.deviceStatus = device.deviceStatus
        deviceTemp.deviceVersion = device.deviceVersion
        deviceTemp.deviceDesc = device.deviceDesc
        deviceTemp.startTime = device.startTime
        deviceTemp.delFlag = device.delFlag
        deviceTemp.modifyTime = device.modifyTime
        deviceTemp.configFileKey = device.configFileKey
        deviceTemp.configFileName = device.configFileName
        deviceTemp.brand = device.brand
        deviceTemp.manufacturerLiaison = device.manufacturerLiaison
        deviceTemp.manufacturerPhone = device.manufacturerPhone
        deviceTemp.manager = device.manager
        deviceTemp.managerPhone = device.managerPhone
        devicesTemp.push(deviceTemp)
      })
      DeviceService.batchModifyDevice(devicesTemp).then(result => {
        curthis.InfoTip.successTip(curthis, HelperUtil.getStatusCodeObjectByCode(curthis.successCode.OTHERS_CODE))
        curthis.command.done()
      }).catch(err => {
        // 失败
        curthis.InfoTip.errorTip(curthis, err)
      })
    }
  }
}
</script>
