<template>
  <div id="role">
    <el-row>
      <el-col :span="24" class="add" style="text-align:right;padding: 10px 20px;">
        <el-button size="mini" type="primary" @click="toAdd" icon="el-icon-plus">添加{{ theme }}</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-dialog :title="theme" width="60%" :visible.sync="dialog" @closed="handleClose" :destroy-on-close="true">
        <data-form :data="form" :fields="formFields" @save="toSave" :isNew="dialogIsNew"></data-form>
      </el-dialog>
    </el-row>
  </div>
</template>

<script>
import dataForm from '@/components/form.vue';
import dataTable from '@/components/data-table.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('authUser');
export default {
  name: 'role',
  props: {},
  components: { dataTable, dataForm },
  data: function() {
    return {
      theme: '管理员',
      dialog: false,
      form: {},
      dialogIsNew: true,
      opera: [
        {
          label: '编辑',
          icon: 'el-icon-edit',
          method: 'edit',
        },
        {
          label: '删除',
          icon: 'el-icon-delete',
          method: 'delete',
          confirm: true,
        },
      ],
      fields: [
        { label: '机构代码或邀请码', prop: 'code', model: 'code' },
        { label: '姓名', prop: 'name', model: 'name' },
        { label: '机构名称', prop: 'deptname', model: 'deptname' },
        { label: '手机号', prop: 'phone', model: 'phone', options: { maxLength: 11, minLength: 11, type: 'number' } },
      ],
      formFields: [
        { label: '机构代码或邀请码', prop: 'code', model: 'code' },
        { label: '姓名', prop: 'name', model: 'name' },
        { label: '机构名称', prop: 'deptname', model: 'deptname' },
        { label: '手机号', prop: 'phone', model: 'phone', options: { maxLength: 11, minLength: 11, type: 'number' } },
        { label: '密码', prop: 'passwd', model: 'passwd', type: 'password' },
      ],
      list: [],
      total: 0,
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...mapActions(['query', 'create', 'update', 'delete']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const res = await this.query({ skip, limit, ...info, pid: this.user.uid, role: '0' });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data.reverse());
        this.$set(this, `total`, res.total);
      }
    },
    // 添加
    toAdd() {
      this.dialog = true;
    },
    toEdit({ data }) {
      this.$set(this, 'form', data);
      this.dialog = true;
      this.dialogIsNew = false;
    },
    async toSave({ isNew, data }) {
      let res;
      let msg;
      if (isNew) {
        data.pid = this.user.uid;
        data.role = '0';
        res = await this.create(data);
        msg = '创建成功';
      } else {
        res = await this.update(data);
        msg = '修改成功';
      }
      if (this.$checkRes(res, msg, res.errmsg)) {
        this.handleClose();
        this.search();
      }
    },
    async toDelete({ data }) {
      const res = await this.delete(data.id);
      if (this.$checkRes(res, '删除成功', res.errmsg || '删除失败')) this.search();
    },
    handleClose() {
      this.dialog = false;
      this.form = {};
      this.dialogIsNew = true;
    },
  },
  computed: {
    ...mapState(['user']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

<style lang="less" scoped></style>
