<template>
  <!-- 设备加入子网规划对话框 -->
  <el-dialog
    @close="close"
    v-dialogDrag
    v-if="DeviceAddtoSubnetVisible"
    :title="$t('DeviceAddToSubnetDialog.deviceAddSubnet')"
    style="font-size: 10px;" width='450px'
    :visible.sync="DeviceAddtoSubnetVisible">

    <div id="main">
    <el-form inline="true">
      <el-main>
        <el-table
          tooltip-effect="dark"
          highlight-current-row
          ref="singleTable"
          :data="subnetList"
          border="true"
          stripe
          height="400px"
          header-row-class-name="table-header"
          cell-class-name="table-cell"
          @current-change="handleCurrentChange">
          <el-table-column
            prop="subnetKey"
            v-if="false">
          </el-table-column>
          <el-table-column
            prop="subnetName"
            :label="$t('DeviceAddToSubnetDialog.subnetName')"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="domain"
            :label="$t('DeviceAddToSubnetDialog.managementArea')"
            :show-overflow-tooltip="true">
          </el-table-column>
        </el-table>
      </el-main>
    </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size='mini' type="primary" @click="deviceAddToSubnetInit" v-loading.fullscreen.lock="fullscreenLoading">
        {{$t("common.ok")}}
      </el-button>
      <el-button size='mini' @click="close">{{$t("common.cancel")}}</el-button>
    </div>
  </el-dialog>
</template>

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

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

  data: function () {
    return {
      DeviceAddtoSubnetVisible: true,
      subnetList: null,
      deviceList: this.command.target,
      selectSubnet: null,
      tableHeight: 200
    }
  },

  created () {
    curThis = this
    this.getAllSubnet()
  },

  methods: {
    close () {
      this.command.done()
    },

    /**
       * @Description  ：获取单选行子网
       * @author       :
       * @date         : 2019/05/08 11:17
       */
    handleCurrentChange (val) {
      this.selectSubnet = val
    },

    /**
       * @Description  ：设备加入子网规划初始化
       * @author       :
       * @date         : 2019/05/08 11:17
       */
    deviceAddToSubnetInit () {
      if (this.selectSubnet == null) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.SELECT_ONE_SUBNET))
        return
      }
      this.InfoTip.conformTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.ADD_THE_DEVICE_TO_SUBNET))
        .then(() => {
          // 执行加入子网规划操作
          this.$options.methods.deviceAddToSubnet()
        }).catch((err) => {
          this.InfoTip.errorTip(this, err)
        })
    },

    /**
       * @Description  ：设备加入子网规划
       * @author       :
       * @date         : 2019/05/08 11:17
       */
    deviceAddToSubnet: function () {
      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 = curThis.selectSubnet.subnetKey
        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.close()
      }).catch(err => {
        curThis.InfoTip.errorTip(curThis, err)
      })
    },

    /**
       * @Description  ：获取所有子网
       * @author       :
       * @date         : 2019/05/08 11:17
       */
    getAllSubnet: function () {
      let _this = this
      SubnetService.getAllSubnetCollection().then(result => {
        // 成功
        _this.subnetList = Object.values(result)
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    }
  }
}
</script>
