<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="角色名称">
                <a-input v-model="queryParam.name" allow-clear placeholder="请输入角色名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button @click="() => {queryParam = {}, this.loadData()}">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="$refs.roleForm.add()">新增角色</a-button>
      </div>
      
      <s-table
        ref="table"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="$refs.roleForm.edit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="$refs.roleMenuForm.roleMenu(record)">授权菜单</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => handleDel(record)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </s-table>
      <role-form ref="roleForm" @ok="handleOk"/>
      <role-menu-form ref="roleMenuForm" @ok="handleOk"/>
    </a-card>
  </div>
</template>

<script>
import { STable } from '@/components'
import { rolePage, roleDel } from '@/api/security/role'
import RoleForm from './RoleForm'
import RoleMenuForm from './RoleMenuForm'

export default {
  components: {
    STable,
    RoleForm,
    RoleMenuForm
  },
  data () {
    return {
      data: [],
      queryParam: {},
      loading: true,
      columns: [
        {
          title: '角色名称',
          dataIndex: 'name',
          width: '20%'
        },
        {
          title: '唯一编码',
          dataIndex: 'code'
        },
        {
          title: '排序',
          dataIndex: 'sort'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      loadData: parameter => {
        return rolePage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res
        })
      }
    }
  },
  created () {
    // 根据权限加载，待处理
  },
  methods: {
    handleDel (record) {
      roleDel({id: record.id}).then((res) => {
        if (res.code == 200) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败：' + res.msg)
        }
      })
    },
    handleOk () {
      this.$refs.table.refresh()
    }
  }
}

</script>
<style scoped>
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
