<template>
  <wrapper
    fill
    margin
    padding
    background
  >
    <schema-table
      ref="table"
      :schema="schema"
    />
    <confirm-dialog
      ref="editDialog"
      :title="dialogTitle"
      @confirm="onSave"
    >
      <div class="c-grid-form u-align-self--center">
        <span class="c-grid-form__label required">名称：</span>
        <el-input
          v-model.trim="currObj.name"
          placeholder="请填写分组名称"
          maxlength="50"
        />
        <span class="c-grid-form__label">备注：</span>
        <el-input
          v-model="currObj.remark"
          type="textarea"
          maxlength="100"
          :rows="3"
          show-word-limit
        />
      </div>
    </confirm-dialog>
    <table-dialog
      ref="subDeviceDialog"
      title="设备"
      :schema="subDeviceSchema"
      size="large"
    />
    <table-dialog
      ref="deviceDialog"
      title="添加设备"
      :schema="deviceSchema"
      append-to-body
      @choosen="onChooseDevice"
    />
  </wrapper>
</template>

<script>
import {
  getDeviceGroups,
  addDeviceGroup,
  updateDeviceGroup,
  deleteDeviceGroup,
  getDevicesByGroup,
  getDevices,
  addDeviceToGroup,
  deleteDeviceFromGroup
} from '@/api/device'
import mixin from '../mixins'

export default {
  name: 'DeviceGroup',
  mixins: [mixin],
  data () {
    return {
      type: '分组',
      proxy: {
        add: addDeviceGroup,
        update: updateDeviceGroup,
        del: deleteDeviceGroup
      },
      schema: {
        condition: { name: '' },
        list: getDeviceGroups,
        buttons: [
          { type: 'add', on: this.onAdd }
        ],
        filters: [
          { key: 'name', type: 'search', placeholder: '分组名称' }
        ],
        cols: [
          { prop: 'name', label: '分组名称' },
          { prop: 'remark', label: '备注' },
          {
            type: 'invoke', render: [
              { label: '编辑', on: this.onEdit },
              { label: '设备', on: this.onViewDevices },
              { label: '删除', on: this.onDel }
            ]
          }
        ]
      },
      subDeviceSchema: {
        singlePage: true,
        list: this.getDevicesByGroup,
        buttons: [
          { type: 'add', label: '添加设备', on: this.onAddDevice }
        ],
        cols: [
          { prop: 'name', label: '设备名称' },
          { prop: 'productName', label: '产品' },
          { prop: 'serialNumber', label: '序列号' },
          { prop: 'mac', label: 'MAC' },
          { prop: 'resolutionRatio', label: '分辨率' },
          { prop: 'remark', label: '备注' },
          {
            type: 'invoke', render: [
              { label: '移除', on: this.onDelDevice }
            ]
          }
        ]
      },
      deviceSchema: {
        condition: { name: '' },
        filters: [
          { key: 'name', type: 'search', placeholder: '设备名称' }
        ],
        list: getDevices,
        cols: [
          { prop: 'name', label: '设备名称' },
          { prop: 'productName', label: '产品' },
          { prop: 'serialNumber', label: '序列号' },
          { prop: 'mac', label: 'MAC' },
          { prop: 'resolutionRatio', label: '分辨率' },
          { prop: 'remark', label: '备注' }
        ]
      }
    }
  },
  methods: {
    onSave (done) {
      if (!this.currObj.name) {
        this.$message({
          type: 'warning',
          message: '名称不能为空'
        })
        return false
      }
      this.save(done).then(() => {
        if (this.isAdd) {
          this.$refs.table.resetCondition({ name: this.currObj.name })
        } else {
          this.$refs.table.pageTo()
        }
      })
    },
    onViewDevices ({ id }) {
      this.$refs.subDeviceDialog.show({ id })
    },
    getDevicesByGroup ({ id }) {
      return getDevicesByGroup(id)
    },
    onDelDevice (device, index) {
      const table = this.$refs.subDeviceDialog.getTable()
      deleteDeviceFromGroup(table.getCondition().id, device).then(() => {
        table.getData().splice(index, 1)
      })
    },
    onAddDevice () {
      this.$refs.deviceDialog.show()
    },
    onChooseDevice ({ value: { id }, done }) {
      const table = this.$refs.subDeviceDialog.getTable()
      addDeviceToGroup(table.getCondition().id, id).then(() => {
        done()
        table.pageTo()
      })
    }
  }
}
</script>
