<template>
  <CommandExecuteContext v-bind:commands="commandContext"></CommandExecuteContext>
</template>

<script>
import UserCommands from '../../../commands/UserCommands'
import CommandExecuteContext from '../../../commands/CommandExecuteContext'
import DeviceService from '@/domain/services/DeviceService.js'
import LinkService from '@/domain/services/LinkService'
import PortService from '@/domain/services/PortListService.js'
import HelperUtil from '../../../utils/HelperUtil'

export default {
  props: ['command'],

  components: {
    CommandExecuteContext
  },

  data: function () {
    return {
      deleteList: this.command.target,
      // 级联删除链路
      linkList: [],
      // 级联删除接入业务portList
      portList: [],
      deviceList: [],

      // 命令
      commandContext: {},

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

    open () {
      let _this = this
      _this.InfoTip.conformTip(_this, HelperUtil.getCheckStatusCodeObjectByCode(_this.successCode.DELETE_CONFIRM)
      ).then(() => {
        this.deleteDevicePro()
      }).catch(() => {
        this.command.done()
      })
    },
    // 删除设备
    deleteDevice () {
      let _this = this
      let loadingInstance = _this.Loading.openLoading()
      _this.getAllLinkInit(() => {
        _this.getAllPort(() => {
          _this.getAllDevice(() => {
            DeviceService.deleteDeviceAndLinks(_this.deleteList, _this.deviceList, _this.linkList, _this.portList).then(result => {
              _this.Loading.closeLoading(loadingInstance)
              _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.DELETE_CODE))
              _this.close()
            }).catch(err => {
              //   console.log("删除失败");
              _this.Loading.closeLoading(loadingInstance)
              _this.InfoTip.errorTip(_this, err)
              _this.close()
            })
          })
        })
      })
    },

    // 删除设备    需求修改： 删除设备时，后台进行链路与接入业务的级联删除操作
    deleteDevicePro () {
      let _this = this
      let loadingInstance = _this.Loading.openLoading()
      DeviceService.batchDeleteDevice(_this.deleteList).then(result => {
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.DELETE_CODE))
        _this.close()
      }).catch(err => {
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.errorTip(_this, err)
        _this.close()
      })
    },
    /**
       * @Description  ：获取全部port信息
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2018/12/7 14:06
       */
    getAllPort: function (mutator = () => {}) {
      let _this = this
      PortService.getAllPortList().then(result => {
        _this.portList = Object.values(result)
        mutator()
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    },
    /**
       * @Description  ：获取全部device信息
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2018/12/7 14:06
       */
    getAllDevice: function (mutator = () => {}) {
      let _this = this
      DeviceService.getAllDeviceCollection().then(result => {
        _this.deviceList = result
        mutator()
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    },
    // 删除port
    deletePort (delLinkList) {
      let _this = this
      PortService.batchDeletePortList(JSON.stringify(delLinkList)).then(result => {
        // 成功
        _this.command.done()
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
        _this.command.done()
      })
    },
    /**
       * @Description  ：获取全部link信息
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2018/12/7 14:06
       */
    getAllLinkInit: function (mutation = () => {}) {
      let _this = this
      // 为了测试其他先注释了
      LinkService.getAllLinkCollection().then(result => {
        // 成功
        _this.linkList = Object.values(result)
        mutation()
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    },
    // 删除link
    deleteLink (delLinkList) {
      let _this = this
      LinkService.batchLinkDelete(JSON.stringify(delLinkList)).then(result => {
        // 成功
        _this.command.done()
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
        // 失败
        _this.command.done()
      })
    }
  },

  watch: {
    commandContext (newVal, oldVal) {
      let _this = this
      if (_this.confirmFlag.confirm) {
        this.open()
        this.confirmFlag.confirm = false
      } else {
        if (JSON.stringify(newVal) === '{}') {
          _this.close()
        }
      }
      deep: true
    }
  },

  computed: {
    confirmUserCommand () {
      this.confirmFlag.confirm = false
      return UserCommands.confirmUserCommand(this.commandContext, this.confirmFlag)
    }
  },
  created () {
    this.confirmUserCommand.execute()
    this.getAllPort()
  }
}
</script>
