<template>
  <container>
    <br />
    <a-card :bordered="false">
      <Auth auth="sysRole:page">
        <div class="yo-query-bar">
          <a-form-model :model="query" layout="inline">
            <a-form-model-item label="角色名称">
              <a-input placeholder="请输入角色名称" v-model="query.name" />
            </a-form-model-item>
            <a-form-model-item label="唯一编号">
              <a-input placeholder="请输入唯一编码" v-model="query.code" />
            </a-form-model-item>
            <a-form-model-item>
              <a-button-group>
                <a-button @click="onQuery" type="primary">查询</a-button>
                <a-button @click="() => {(query = {}), onQuery();}">重置</a-button>
              </a-button-group>
            </a-form-model-item>
          </a-form-model>
        </div>
      </Auth>

      <yo-table :columns="columns" :load-data="loadData" ref="table">
        <Auth auth="sysRole:add" slot="operator">
          <a-button @click="onOpen('add-form')" icon="plus">新建角色</a-button>
        </Auth>

        <span slot="action" slot-scope="text, record">
          <yo-table-actions>
            <Auth auth="sysRole:edit">
              <a @click="onOpen('edit-form', record)">编辑</a>
            </Auth>
            <Auth auth="sysRole:delete">
              <a-popconfirm @confirm="onDelete(record)" placement="topRight" title="是否确认删除">
                <a>删除</a>
              </a-popconfirm>
            </Auth>
            <Auth :auth="{ sysRole: [['grantMenu'], ['grantData']] }">
              <a-dropdown placement="bottomRight">
                <a class="ant-dropdown-link">
                  授权
                  <a-icon type="down" />
                </a>
                <a-menu slot="overlay">
                  <Auth auth="sysRole:grantMenu">
                    <a-menu-item>
                      <a @click="onOpen('menu-form', record)">授权菜单</a>
                    </a-menu-item>
                  </Auth>
                  <Auth auth="sysRole:grantData">
                    <a-menu-item>
                      <a @click="onOpen('org-form', record)">授权数据</a>
                    </a-menu-item>
                  </Auth>
                </a-menu>
              </a-dropdown>
            </Auth>
          </yo-table-actions>
        </span>
      </yo-table>
    </a-card>
    <br />
    <add-form @ok="onReloadData" ref="add-form" />
    <edit-form @ok="onReloadData" ref="edit-form" />
    <menu-form @ok="onReloadData" ref="menu-form" />
    <org-form @ok="onReloadData" ref="org-form" />
  </container>
</template>
<script>
import AddForm from './addForm';
import editForm from './editForm';
import menuForm from './menuForm';
import orgForm from './orgForm';
export default {
  components: {
    AddForm,
    editForm,
    menuForm,
    orgForm,
  },
  data() {
    return {
      query: {},
      columns: [
        {
          title: '角色名',
          dataIndex: 'name',
        },
        {
          title: '唯一编码',
          dataIndex: 'code',
        },
        {
          title: '排序',
          dataIndex: 'sort',
        },
      ],
    };
  },
  created() {
    const flag = this.$auth({
      sysRole: [['edit'], ['delete'], ['grantMenu'], ['grantData']],
    });

    if (flag) {
      this.columns.push({
        title: '操作',
        width: '200px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' },
      });
    }
  },
  methods: {
    /**
     * 必要的方法
     * 传给yo-table以示意数据接口及其参数和返回的数据结构
     */
    loadData(params) {
      return this.$api
        .getRolePage({
          ...params,
          ...this.query,
        })
        .then((res) => {
          return res.data;
        });
    },

    /**
     * 有查询功能时的必要方法
     * 加载数据时初始化分页信息
     */
    onQuery() {
      this.$refs.table.onReloadData(true);
    },

    /**
     * 必要方法
     * 重新列表数据
     */
    onReloadData() {
      this.$refs.table.onReloadData();
    },

    /**
     * 有编辑新增功能的必要方法
     * 从列表页调用窗口的打开方法
     */
    onOpen(formName, record) {
      this.$refs[formName].onOpen(record);
    },

    onResult(success, successMessage) {
      if (success) {
        this.$message.success(successMessage);
        this.onReloadData();
      }
      this.$refs.table.onLoaded();
    },

    onDelete(record) {
      this.$refs.table.onLoading();
      this.$api.sysRoleDel(record).then(({ success }) => {
        this.onResult(success, '删除成功');
      });
    },
  },
};
</script>