<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="add">
          <el-button type="primary" size="mini" @click="add">添加</el-button>
        </el-col>
        <el-col :span="24" class="list">
          <data-table :fields="fields" :opera="opera" :data="list" :total="total" @query="search" @edit="toEdit" @delete="toDelete"></data-table>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: adminLogin } = createNamespacedHelpers('adminLogin');
export default {
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  name: 'index',
  props: {},
  components: {},
  data: function () {
    return {
      opera: [
        {
          label: '编辑',
          method: 'edit',
        },
        {
          label: '删除',
          method: 'delete',
        },
      ],
      fields: [
        { label: '机构代码或邀请码', prop: 'code', model: 'code' },
        { label: '姓名', prop: 'name', model: 'name' },
        { label: '手机号', prop: 'phone', model: 'phone', options: { maxLength: 11, minLength: 11, type: 'number' } },
        { label: '机构名称', prop: 'deptname', model: 'deptname' },
      ],
      list: [],
      total: 0,
      // 分配权限
      dialog: false,
      formfields: [
        { label: '用户名', model: 'name' },
        { label: '机构名称', model: 'deptname' },
        { label: '权限', model: 'menus', custom: true },
      ],
      form: {
        menus: [],
      },
      rules: {},
      // 菜单列表
      menuList: [],
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...adminLogin(['query', 'fetch', 'update', 'delete']),
    // 查询列表
    async search({ skip = 0, limit = 10, ...info } = {}) {
      if (this.user.role == '1' || this.user.role == '2') info.pid = this.user.id;
      let res = await this.query({ skip, limit, role: '2', ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 修改
    toEdit({ data }) {
      this.$router.push({ path: '/adminCenter/mechanism/detail', query: { id: data.id } });
    },
    // 删除
    async toDelete({ data }) {
      let res = await this.delete(data.id);
      if (this.$checkRes(res)) {
        this.$message({
          message: '信息修改成功',
          type: 'success',
        });
        this.search();
      }
    },
    // 添加数据
    add() {
      this.$router.push({ path: '/adminCenter/mechanism/detail' });
    },
  },
  computed: {
    ...mapState(['user']),
  },
  watch: {},
};
</script>

<style lang="less" scoped>
.main {
  .add {
    text-align: right;
    margin: 0 0 10px 0;
  }
}
</style>
