<template>
  <div id="index">
    <el-row>
      <el-col :span="24">
        <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" :opera="opera" @edit="toEdit" :data="list" :total="total" @delete="toDelete" @query="search"></data-table>
        </el-col>
        <el-dialog :title="theme" width="60%" :visible.sync="dialog" @closed="handleClose" :destroy-on-close="true">
          <el-form ref="form" :model="form" label-width="100px">
            <el-form-item label="用户名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入用户名称"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号" :minlength="11" :maxlength="11"></el-input>
            </el-form-item>
            <el-form-item label="登录密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入登录密码" show-password></el-input>
            </el-form-item>
            <el-form-item label="身份证号" prop="cardnumber">
              <el-input v-model="form.cardnumber" placeholder="请输入身份证号" :minlength="18" :maxlength="18"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
            </el-form-item>
            <el-form-item label="地址" prop="addr">
              <el-input v-model="form.addr" placeholder="请输入地址"></el-input>
            </el-form-item>
            <el-form-item label="头像图片" prop="img_path">
              <upload :limit="1" :data="form.img_path" type="img_path" :url="'/files/imgpath/upload'" @upload="uploadSuccess"></upload>
            </el-form-item>
            <el-form-item label="简介" prop="resume">
              <el-input v-model="form.resume" placeholder="请输入备注"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="handleClose">返回</el-button>
              <el-button type="primary" @click="handleSave()">提交</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import upload from '@/components/uploadone.vue';
import dataTable from '@/components/data-table.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: users } = createNamespacedHelpers('users');
export default {
  name: 'index',
  props: {},
  components: {
    dataTable,
    upload,
  },
  data: function() {
    return {
      theme: 'vip用户',
      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' ? 'vip用户' : '无法识别') },
        { label: '状态', prop: 'status', format: i => (i == '1' ? '审核通过' : '') },
      ],
      list: [],
      total: 0,
      dialog: false,
      form: {},
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...users(['create', 'query', 'update', 'delete']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      if (this.user.code.length == 3) {
        const res = await this.query({ skip, limit, role: '2', ...info });
        if (this.$checkRes(res)) {
          this.$set(this, `list`, res.data);
          this.$set(this, `total`, res.total);
        }
      }
    },
    // 添加
    toAdd() {
      this.dialog = true;
    },
    // 提交
    async handleSave() {
      if (this.form.id) {
        let res = await this.update(this.form);
        this.$message({
          message: '修改信息成功',
          type: 'success',
        });
        this.handleClose();
      } else {
        let data = this.form;
        data.status = '1';
        data.code = this.user.code;
        data.role = '2';
        let res = await this.create(data);
        if (this.$checkRes(res)) {
          this.$message({
            message: '添加信息成功',
            type: 'success',
          });
          this.handleClose();
        }
      }
    },
    // 修改
    toEdit({ data }) {
      this.$set(this, 'form', data);
      this.dialog = true;
    },
    // 刪除
    async toDelete({ data }) {
      const res = await this.delete(data.id);
      if (this.$checkRes(res, '删除成功', res.errmsg || '删除失败')) this.search();
    },
    // 取消
    handleClose() {
      this.dialog = false;
      this.form = {};
    },
    uploadSuccess({ type, data }) {
      this.$set(this.form, `${type}`, data.uri);
    },
  },
  computed: {
    ...mapState(['user']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

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