<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="info">
        <el-col :span="24" class="top">
          <topInfo :topTitle="pageTitle"></topInfo>
          <el-col :span="24" class="topAdd">
            <el-button type="primary" size="mini" @click="toAdd()"><i class="el-icon-plus"></i></el-button>
          </el-col>
        </el-col>
        <el-col :span="24" class="main">
          <data-table :fields="fields" @delete="toDelete" :data="list" :opera="opera" @edit="toEdit" :usePage="false"></data-table>
        </el-col>
      </el-col>
    </el-row>
    <el-drawer title="子管理员" :visible.sync="drawer" direction="rtl" @closed="handleClose" :destroy-on-close="true">
      <data-form :fields="formFields" :data="form" :rules="{}" @save="drawerSave" :isNew="drawerIsNew"> </data-form>
    </el-drawer>
  </div>
</template>

<script>
import dataTable from '@/components/data-table.vue';
import topInfo from '@/layout/public/top.vue';
import dataForm from '@/components/form.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: authUser } = createNamespacedHelpers('authUser');
export default {
  name: 'index',
  props: {},
  components: {
    topInfo, //头部标题
    dataTable,
    dataForm,
  },
  data: () => {
    return {
      drawer: false,
      form: {},
      drawerIsNew: true,
      opera: [
        {
          label: '编辑',
          icon: 'el-icon-edit',
          method: 'edit',
        },
        {
          label: '删除',
          icon: 'el-icon-delete',
          method: 'delete',
          confirm: true,
        },
      ],
      fields: [
        { label: '用户名', prop: 'name', model: 'name' },
        { label: '手机号', prop: 'phone', model: 'phone' },
        { label: '备注', prop: 'remark', model: 'phone' },
      ],
      formFields: [
        { label: '用户名', model: 'name' },
        { label: '手机号', model: 'phone' },
        { label: '密码', model: 'passwd', type: 'password' },
        { label: '备注', model: 'remark' },
      ],
      list: [],
      total: 0,
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...authUser(['query', 'update', 'create']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const res = await this.query({ skip, limit, ...info, role: '4' });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    toAdd() {
      this.drawer = true;
    },
    async drawerSave({ data, isNew }) {
      let res;
      let msg;
      if (isNew) {
        data.role = '4';
        res = await this.create(data);
        msg = '创建成功';
      } else {
        res = await this.update(data);
        msg = '修改成功';
      }
      if (this.$checkRes(res, msg, res.errmsg)) {
        this.handleClose();
        this.search();
      }
    },
    toEdit({ data }) {
      this.$set(this, 'form', data);
      this.drawer = true;
      this.drawerIsNew = false;
    },
    async toDelete({ data }) {
      const res = await this.delete(data.id);
      if (this.$checkRes(res, '删除成功', res.errmsg || '删除失败')) this.search();
    },
    handleClose() {
      this.drawer = false;
      this.form = {};
      this.drawerIsNew = true;
    },
  },
  computed: {
    ...mapState(['user']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

<style lang="less" scoped>
.main {
  padding: 20px;
  margin: 10px 20px;
  border: 1px solid #ccc;
  width: 96%;
}
.column {
  width: 258px;
  min-height: 500px;
  border: 1px solid #ccc;
  margin: 0 20px 0 0;
}
.top .topAdd {
  padding: 0 10px 0 0;
  text-align: right;
}
.message {
  border: 1px solid #ccc;
  min-height: 500px;
  // min-width: 715px;
}
</style>
