<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: users } = createNamespacedHelpers('users');
const { mapActions: exportuser } = createNamespacedHelpers('exportuser');
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: 'name', filter: 'input' },
      { label: '电话', prop: 'phone', filter: 'input' },
      { label: '用戶类型', prop: 'role', format: i => (i == '2' ? '个人用户' : i == '3' ? '企业用户' : i == '6' ? '专家' : '临时用户') },
      { label: '状态', prop: 'status', format: i => (i == '0' ? '待审核' : i == '1' ? '审核成功' : i == '2' ? '审核拒绝' : '待认证') },
    ],
    list: [],
    total: 0,
  }),
  created() {
    this.search();
  },
  methods: {
    ...users(['query', 'delete', 'update']),
    ...exportuser({ exportuserQuery: 'query' }),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const res = await this.query({ skip, limit, ...info });
      const resTwo = await this.exportuserQuery({ skip, limit, ...info });
      var newData = res.data.concat(resTwo.data);
      if (this.$checkRes(newData)) {
        var arr = newData.filter(item => item.pid == undefined && item.status != '3');
        this.$set(this, `list`, arr);
        this.$set(this, `total`, arr.length);
      }
    },
    toEdit({ data }) {
      this.$router.push({ path: './detail', query: { id: data.id, role: data.role } });
    },
    async toDelete({ data }) {
      const res = await this.delete(data.id);
      if (this.$checkRes(res, '删除成功', res.errmsg || '删除失败')) this.search();
    },
  },
  computed: {
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>
<style lang="less" scoped>
.add {
  height: 40px;
  line-height: 40px;
  padding: 0 15px;
}
</style>
