<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="40%"
    @close="close"
  >
    <div class="store-employee-rights-group-container">
      <el-table v-loading="listLoading" border :data="list" height="500">
        <el-table-column align="center" :label="__('操作')" width="120">
          <template #default="{ row }">
            <el-button type="text" @click="handleEdit(row)">
              {{ __('编辑') }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="__('权限组名称')"
          prop="rights_group_name"
          show-overflow-tooltip
        />
      </el-table>
      <el-pagination
        background
        :current-page="queryForm.pageNum"
        :layout="layout"
        :page-size="queryForm.pageSize"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
      <edit ref="edit" @fetch-data="fetchData" />
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList } from '@/api/chain/rights/group'
  import Edit from './ChainUserRightsGroupEdit'

  export default {
    name: 'JurisdictionTable',
    components: { Edit },
    data() {
      return {
        list: [],
        listLoading: true,
        dialogFormVisible: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        title: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      handleEdit(row) {
        if (row.rights_group_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      close() {
        this.dialogFormVisible = false
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      showTable() {
        this.dialogFormVisible = true
      },

      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.total
        this.listLoading = false
      },
    },
  }
</script>
