<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="index">
        <el-col :span="24" class="top">
          <topInfo :topTitle="pageTitle"></topInfo>
        </el-col>
        <el-col :span="24" class="add" style="text-align:right">
          <el-button size="mini" type="primary" @click="$router.push({ path: './detail' })" icon="el-icon-plus">添加菜单</el-button>
        </el-col>
      </el-col>
      <el-col :span="24" class="info">
        <data-table :fields="fields" @delete="toDelete" :data="list" :opera="opera" @edit="toEdit" :total="total" @query="search"></data-table>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import topInfo from '@/layout/public/top.vue';
import dataTable from '@/components/data-table.vue';
import { mapActions, mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: role } = createNamespacedHelpers('role');
export default {
  name: 'index',
  props: {},
  components: {
    topInfo,
    dataTable,
  },
  data: () => ({
    opera: [
      {
        label: '编辑',
        icon: 'el-icon-edit',
        method: 'edit',
      },
      {
        label: '删除',
        icon: 'el-icon-delete',
        method: 'delete',
        confirm: true,
      },
    ],
    fields: [
      { label: '菜单名称', prop: 'role_name' },
      // { label: '权限code', prop: 'code' },
      { label: 'url', prop: 'url' },
    ],
    list: [],
    total: 0,
  }),
  created() {
    this.search();
  },
  methods: {
    ...role(['query', 'delete', 'update', 'fetch']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      let res = await this.query({ skip, limit, ...info });
      this.$set(this, `list`, res.data);
      this.$set(this, `total`, res.total);
    },
    toEdit({ data }) {
      this.$router.push({ path: '/role/detail', query: { id: data.id } });
    },
    async toDelete({ data }) {
      const res = await this.delete(data.id);
      this.$checkRes(res, '删除成功', '删除失败');
      this.search();
    },
  },
  computed: {
    ...mapState(['user']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>
<style lang="less" scoped>
.add {
  height: 40px;
  line-height: 35px;
  padding: 0 15px;
}
</style>
