<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="30%"
    @close="close"
  >
    <el-form ref="form" label-width="80px">
      <el-checkbox-group v-model="rights_group_id">
        <el-form-item
          v-for="rightsGroup in rightsGroupList"
          :key="rightsGroup.rights_group_id"
        >
          <el-checkbox border :label="rightsGroup.rights_group_id">
            {{ rightsGroup.rights_group_name }}
          </el-checkbox>
        </el-form-item>
      </el-checkbox-group>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList } from '@/api/store/employee/rights/group'
  import { doEdit } from '@/api/store/employee'

  export default {
    name: 'StoreEmployeeRightsGroupTable',
    data() {
      return {
        rightsGroupList: [],
        rights_group_id: [],
        form: {},
        rules: {},
        title: this.__('修改权限组'),
        dialogFormVisible: false,
      }
    },
    created() {
      this.getRightsGroupList()
    },
    methods: {
      __,
      showEdit(row) {
        this.form = Object.assign({}, row)
        this.rights_group_id = row.rights_group_id
        this.dialogFormVisible = true
      },
      close() {
        this.form = {}
        this.rights_group_id = []
        this.dialogFormVisible = false
      },
      async save() {
        if (this.form.employee_id) {
          let param = {
            employee_id: this.form.employee_id,
            rights_group_id: this.rights_group_id.toString(),
          }
          const { msg, status } = await doEdit(param)
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          this.$emit('fetch-data')
          this.close()
        }
      },
      async getRightsGroupList() {
        const { data } = await getList({ pageNum: 1, pageSize: 50 })
        if (data && data.items) {
          this.rightsGroupList = data.items
        }
      },
    },
  }
</script>
