<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <breadcrumb :breadcrumbTitle="this.$route.meta.title"></breadcrumb>
        <el-col :span="24" class="container info">
          <el-col :span="24" class="top">
            <el-button type="primary" size="mini" @click="dialog = true">添加</el-button>
          </el-col>
          <el-col :span="24" class="list">
            <data-table :fields="fields" :opera="opera" :data="list" :total="total" @edit="toEdit" @delete="toDelete" @query="search"></data-table>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog :visible.sync="dialog" title="增加业务管理员" @close="toClose" :destroy-on-close="true" width="50%">
      <data-form :data="form" :fields="formFields" :rules="rules" @save="turnSave"> </data-form>
    </el-dialog>
  </div>
</template>

<script>
import breadcrumb from '@c/common/breadcrumb.vue';
import dataTable from '@/components/frame/filter-page-table.vue';
import dataForm from '@/components/frame/form.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: marketuser } = createNamespacedHelpers('marketuser');
const { mapActions: authUser } = createNamespacedHelpers('authUser');
export default {
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  name: 'index',
  props: {},
  components: {
    breadcrumb,
    dataTable,
    dataForm,
  },
  data: function() {
    return {
      opera: [
        {
          label: '修改',
          icon: 'el-icon-edit',
          method: 'edit',
        },
        {
          label: '删除',
          icon: 'el-icon-delete',
          method: 'delete',
        },
      ],
      fields: [
        { label: '机构代码或邀请码', prop: 'code' },
        { label: '用户名', prop: 'name' },
        { label: '机构名称', prop: 'deptname' },
        { label: '电话', prop: 'phone' },
      ],
      list: [],
      total: 0,
      // 增加
      dialog: false,
      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: 'password', model: 'password', type: 'password' },
      ],
      form: {},
      rules: {
        code: [{ required: true, message: '请输入推荐码' }],
      },
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...marketuser(['query', 'fetch', 'create', 'update', 'delete']),
    ...authUser({ authUserDelete: 'delete', getBusinessUser: 'getBusinessUser' }),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      let query = { skip, limit, ...info };
      this.user.role == '0' && !_.get(this.user, 'pid') ? '' : (query.pid = this.user.uid);
      const res = await this.getBusinessUser(query);
      if (this.$checkRes(res)) {
        if (this.user.role == '0') {
          let newarr = res.data.filter(i => i.role == '1');
          this.$set(this, `list`, newarr);
          this.$set(this, `total`, newarr.length);
        } else {
          this.$set(this, `list`, res.data);
          this.$set(this, `total`, res.total);
        }
      }
    },
    // 修改
    toEdit({ data }) {
      this.$set(this, 'form', data);
      this.dialog = true;
    },
    // 删除
    async toDelete({ data }) {
      const res = await this.delete(data.id);
      const arr = await this.authUserDelete(data.id);
      if (this.$checkRes(arr, '删除成功', res.errmsg || '删除失败')) this.search();
    },
    // 增加菜单
    // 保存
    async turnSave({ data }) {
      let res;
      let msg;
      if (data.id) {
        res = await this.update(data);
        msg = '修改成功';
      } else {
        data.pid = this.user.uid;
        data.status = '1';
        data.role = '1';
        res = await this.create(data);
        msg = '创建成功';
      }
      if (this.$checkRes(res, msg, res.errmsg)) {
        this.toClose();
        this.search();
      }
    },
    // 取消增加
    toClose() {
      this.form = {};
      this.dialog = false;
    },
  },
  computed: {
    ...mapState(['user']),
  },
};
</script>

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