<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">
        <template v-if="isAdd">
          <span class="c-grid-form__label required">标识：</span>
          <div
            class="c-grid-form__info"
            data-info="仅可包含数字、字母和-_符号"
          >
            <el-input
              v-model.trim="group.name"
              placeholder="最多50个字符"
              maxlength="50"
              clearable
            />
          </div>
        </template>
        <span class="c-grid-form__label required">名称：</span>
        <el-input
          v-model.trim="group.remark"
          placeholder="最多50个字符"
          maxlength="50"
          clearable
        />
      </div>
    </confirm-dialog>
  </wrapper>
</template>

<script>
import { mapGetters } from 'vuex'
import {
  getTopGroups,
  addSubGroup,
  updateGroup,
  deleteGroup
} from '@/api/user'
import { unbindDevices } from '@/api/device'

export default {
  name: 'Tenant',
  data () {
    return {
      isAdd: false,
      dialogTitle: '',
      schema: {
        singlePage: true,
        list: getTopGroups,
        buttons: [
          { type: 'add', on: this.onAdd }
        ],
        cols: [
          { prop: 'name', label: '部门标识' },
          { prop: 'remark', label: '部门名称' },
          { type: 'invoke', render: [
            { label: '编辑', on: this.onEdit },
            { label: '删除', on: this.onDel }
          ] }
        ]
      },
      group: {}
    }
  },
  computed: {
    ...mapGetters(['tenantId'])
  },
  methods: {
    onAdd () {
      this.isAdd = true
      this.dialogTitle = '新增部门'
      this.group = { name: '', remark: '' }
      this.$refs.editDialog.show()
    },
    onEdit (group) {
      this.isAdd = false
      this.dialogTitle = '编辑部门'
      const { id, name, remark } = group
      this.$group = group
      this.group = { id, name, remark }
      this.$refs.editDialog.show()
    },
    onSave (done) {
      if (this.isAdd) {
        const { name, remark } = this.group
        if (!name) {
          this.$message({
            type: 'warning',
            message: '请填写部门标识'
          })
          return
        }
        if (!/^[\da-zA-Z\-_]+$/.test(name)) {
          this.$message({
            type: 'warning',
            message: '部门标识格式错误'
          })
          return
        }
        if (!remark) {
          this.$message({
            type: 'warning',
            message: '请填写部门名称'
          })
          return
        }
        addSubGroup({ id: this.tenantId }, this.group).then(() => {
          done()
          this.$refs.table.pageTo()
        })
        return
      }
      if (this.$group.remark !== this.group.remark) {
        updateGroup(this.group).then(() => {
          done()
          this.$group.remark = this.group.remark
        })
      }
    },
    onDel (group) {
      this.$confirm(
        `删除部门 ${group.remark} ？`,
        { type: 'warning' }
      ).then(() => {
        const loading = this.$showLoading()
        unbindDevices({ org: group.path })
          .then(() => deleteGroup(group))
          .then(() => {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.$refs.table.decrease(1)
          })
          .finally(() => {
            this.$closeLoading(loading)
          })
      })
    }
  }
}
</script>
