<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 :span="24" class="main">
          <data-table :fields="fields" @delete="toDelete" :data="list" :opera="opera" @edit="toEdit" :total="total" @query="search"></data-table>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import topInfo from '@/layout/public/top.vue';
import dataTable from '@/components/data-table.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: users } = createNamespacedHelpers('users');
export default {
  name: 'index',
  props: {},
  components: {
    topInfo,
    dataTable,
  },
  data: function() {
    return {
      opera: [
        {
          label: '修改',
          icon: 'el-icon-edit',
          method: 'edit',
        },
        {
          label: '删除',
          icon: 'el-icon-delete',
          method: 'delete',
          confirm: true,
        },
      ],
      fields: [
        { label: '邀请码', prop: 'code' },
        { label: '用户名', prop: 'name', filter: 'input' },
        { label: '机构名称', prop: 'deptname', filter: 'input' },
        { label: '电话', prop: 'phone', filter: 'input' },
      ],
      list: [],
      total: 0,
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...users(['query', 'fetch', 'create', 'update', 'delete']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const res = await this.query({ skip, limit, pid: this.user.uid, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    toEdit({ data }) {
      this.$router.push({ path: './detail', query: { id: data.id } });
    },
    async toDelete({ data }) {
      const res = await this.delete(data.id);
      if (this.$checkRes(res, '删除成功', res.errmsg || '删除失败')) this.search();
    },
  },
  computed: {
    ...mapState(['user']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

<style lang="less" scoped>
.add {
  padding: 0 20px;
}
.main {
  padding: 0 20px;
}
</style>
