<template>
  <div id="index">
    <el-row>
      <el-col
        :span="24"
        class="main animate__animated animate__backInRight"
        v-loading="loadings"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
      >
        <el-col :span="24" class="one"> <span>用户管理</span> </el-col>
        <el-col :span="24" class="two">
          <data-search :fields="searchFields" v-model="searchInfo" @query="search"> </data-search>
        </el-col>
        <el-col :span="24" class="four">
          <data-table :fields="fields" :opera="opera" @query="search" :data="list" :total="total" @del="toDel" @puton="toPuton"> </data-table>
        </el-col>
      </el-col>
    </el-row>
    <e-dialog :dialog="dialog" @toClose="toClose">
      <template v-slot:info>
        <data-form :span="24" :fields="fieldsForm" :rules="fieldRules" v-model="fieldform" labelWidth="150px" @save="onSubmit"> </data-form>
      </template>
    </e-dialog>
  </div>
</template>

<script>
const _ = require('lodash');
import { mapState, mapGetters, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('users');
const { mapActions: admin } = createNamespacedHelpers('admins');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
export default {
  name: 'index',
  props: {},
  components: {},
  data: function () {
    const that = this;
    return {
      loadings: true,
      // 列表
      opera: [
        { label: '设为团长', method: 'puton', display: (i) => i.is_leader != '0', type: 'success' },
        { label: '取消团长', method: 'puton', display: (i) => i.is_leader == '0', type: 'warning' },
        { label: '删除', method: 'del', confirm: true, type: 'danger' },
      ],
      fields: [
        { label: 'openid', model: 'openid', showTip: false },
        { label: '用户姓名', model: 'name', showTip: false },
        { label: '电话', model: 'phone' },
        { label: '邮箱', model: 'email' },
        { label: '出生年月', model: 'birth' },
        {
          label: '性别',
          model: 'gender',
          format: (i) => {
            let data = that.genderList.find((f) => f.value == i);
            if (data) return data.label;
            else return '';
          },
        },
        {
          label: '是否是团长',
          model: 'is_leader',
          format: (i) => {
            let data = that.is_useList.find((f) => f.value == i);
            if (data) return data.label;
            else return '';
          },
        },
        {
          label: '状态',
          model: 'status',
          format: (i) => {
            let data = that.useList.find((f) => f.value == i);
            if (data) return data.label;
            else return '';
          },
        },
      ],
      list: [],
      total: 0,
      // 查询
      searchInfo: {},
      searchFields: [{ label: '用户姓名', model: 'name' }],
      // 性别
      genderList: [],
      // 用户状态
      useList: [],
      // 是否
      is_useList: [],
      // 弹框
      dialog: { title: '信息管理', show: false, type: '1' },
      fieldform: {},
      fieldsForm: [{ label: '密码', model: 'password', type: 'password' }],
      fieldRules: { password: [{ required: true, message: '请输入密码', trigger: 'blur' }] },
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...dictData({ dictQuery: 'query' }),
    ...admin({ adminPass: 'pass' }),
    ...mapActions(['query', 'delete', 'fetch', 'update', 'create']),
    async search({ skip = 0, limit = this.$limit, ...info } = {}) {
      let condition = _.cloneDeep(this.searchInfo);
      let res = await this.query({ skip, limit, ...condition, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, 'list', res.data);
        this.$set(this, 'total', res.total);
      }
      this.loadings = false;
    },
    // 设置是否团长
    async toPuton({ data }) {
      this.$confirm('是否确认设置/取消该用户为团长?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        if (data.is_leader != '0') data.is_leader = '0';
        else data.is_leader = '1';
        let res;
        if (data._id) res = await this.update(data);
        if (this.$checkRes(res)) this.$message({ type: `success`, message: `修改成功` });
        this.search();
      });
    },
    // 删除用户
    async toDel({ data }) {
      this.$confirm('是否确认删除，需确认密码', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        this.$set(this.fieldform, 'target', data.id);
        this.dialog = { title: '信息管理', show: true, type: '1' };
      });
    },

    async onSubmit({ data }) {
      let res;
      res = await this.adminPass(data);
      if (this.$checkRes(res)) {
        if (res.errcode == 0) {
          this.$confirm('是否确认删除', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }).then(async () => {
            let info = { key: res.data, target: data.target };
            res = await this.delete(info);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `删除成功` });
              this.toClose();
            }
          });
        }
      }
    },
    // 重置
    toClose() {
      this.fieldform = {};
      this.dialog = { title: '信息管理', show: false, type: '1' };
      this.search();
    },
    async searchOther() {
      let res;
      // 性别
      res = await this.dictQuery({ code: 'gender' });
      if (this.$checkRes(res)) this.$set(this, `genderList`, res.data);
      // 用户状态
      res = await this.dictQuery({ code: 'user_status' });
      if (this.$checkRes(res)) this.$set(this, `useList`, res.data);
      // 是否使用
      res = await this.dictQuery({ code: 'use' });
      if (this.$checkRes(res)) this.$set(this, `is_useList`, res.data);
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    margin: 0 0 10px 0;

    span:nth-child(1) {
      font-size: 20px;
      font-weight: 700;
      margin-right: 10px;
    }
  }
  .two {
    margin: 0 0 10px 0;
  }
  .thr {
    margin: 0 0 10px 0;
  }
}
.el-col {
  margin: 10px 0;
}
</style>
