<style lang="scss">
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<template>
  <section class="content">
    <h4>用户管理</h4>
    <el-divider></el-divider>
    <el-form label-width="40px" class="filter-form">
      <el-row>
        <el-col :span="4">
          <el-form-item label="用户">
            <el-input
              clearable
              @clear="getData()"
              v-model="queryParams.username"
              placeholder="请输入用户名/代码"
              size="mini"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label-width="10" style="margin-left: 10px">
            <el-button type="primary" @click="getData" size="mini"
              >筛选</el-button
            >
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              >新增用户</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleFastAdd"
              >新增选手</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%; margin-top: 10px"
      height="50vh"
    >
      <el-table-column align="center" prop="usercode" label="登录代码" />
      <el-table-column align="center" prop="username" label="用户名" />
      <el-table-column align="center" prop="nickname" label="微信昵称" />
      <el-table-column label="头像">
        <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.avatar"
              :preview-src-list="[scope.row.avatar]"
              ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        prop="prop"
        label="选手类型" >
        <template slot-scope="scope">
          <span v-if="scope.row.player_type==0">游客</span>
          <span v-if="scope.row.player_type==1">普通选手</span>
          <span v-if="scope.row.player_type==2">种子选手</span></span>
          <span v-if="scope.row.player_type==3">开户选手</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="phone" label="手机号" />
      <el-table-column align="center" prop="zq" label="周期" />
      <el-table-column align="center" prop="cw" label="仓位" />
      <el-table-column align="center" prop="df" label="打法" />
      <!-- <el-table-column align="center" prop="pz" label="品种" /> -->
      <el-table-column align="center"  prop="badge" label="徽章"/>
      <el-table-column align="center" prop="date" label="绑定">
        <template slot-scope="scope">
          <span v-if="scope.row.is_bind">已绑定</span>
          <span v-else>未绑定</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center"  prop="date" label="资产"/> -->
      <el-table-column align="center" prop="date" label="操作" width="150">
        <template slot-scope="scope">
          <el-button @click="edit(scope.row.id)" size="mini" type="warning"
            >编辑</el-button
          >
          <el-button @click="del(scope.row.id)" size="mini" type="danger"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Page
      ref="pageButton"
      :current="form.page"
      :page_size="form.page_size"
      :total="total"
      @pageChange="gopage"
    />
    <!-- 新增用户 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input
            clearable
            v-model="form.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户代码" prop="usercode">
          <el-input
            clearable
            v-model="form.usercode"
            placeholder="请输入代码"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户类型" prop="player_type">
          <el-select v-model="form.player_type" placeholder="">
            <el-option
              v-for="item in playerType"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-upload
            class="avatar-uploader"
            action="/api/admin/uploadfile"
            :data="{watermark:0}"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.avatar" :src="form.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <span style="color:red;">尺寸要求100*100</span>
        </el-form-item>
        <el-form-item label="手机号" prop="phone" v-if="form.player_type==2||form.player_type==3">
          <el-input
            clearable
            v-model="form.phome"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="徽章">
          <el-select v-model="form.badge" placeholder="请选择徽章">
            <el-option v-for="(item,index) in badgeList" :key="index" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="周期">
          <el-select v-model="form.zq" placeholder="请选择周期">
            <el-option v-for="(item,index) in zqList" :key="index" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓位">
          <el-select v-model="form.cw" placeholder="请选择仓位">
            <el-option v-for="(item,index) in cwList" :key="index" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="打法">
          <el-select v-model="form.df" placeholder="请选择打法">
            <el-option v-for="(item,index) in dfList" :key="index" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户代码" v-if="form.id">
          <el-select v-model="form.is_bind" placeholder="请选择比赛分组">
            <el-option label="已绑定" :value="1"></el-option>
            <el-option label="未绑定" :value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 新增选手 -->
    <el-dialog
      :title="title"
      :visible.sync="open1"
      width="500px"
      append-to-body
    >
      <el-form ref="form1" :model="form1" :rules="rules1" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input
            clearable
            v-model="form1.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户代码" prop="usercode">
          <el-input
            clearable
            v-model="form1.usercode"
            placeholder="请输入用户代码"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="初始资金" prop="fund">
          <el-input clearable v-model="form1.fund" placeholder="请输入初始资金">
            <template slot="append">万元</template>
          </el-input>
        </el-form-item> -->
        <el-form-item label="比赛名称" prop="match_id">
          <el-select v-model="form1.match_id" placeholder="请选择比赛">
            <el-option
              v-for="item in matchList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="比赛分组" prop="match_group">
          <el-select v-model="form1.match_group" placeholder="请选择比赛分组">
            <el-option
              v-for="item in groupList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参数状态" prop="match_status">
          <el-select v-model="form1.match_status" placeholder="请选择参数状态">
            <el-option key="1" label="参赛中" :value="1"></el-option>
            <el-option key="0" label="暂停" :value="0"></el-option>
            <el-option key="-1" label="已退赛" :value="-1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="fastSavePlayer">确 定</el-button>
        <el-button @click="open1 = false">取 消</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import Page from "../../components/Page";
export default {
  components: {
    Page,
  },
  data() {
    return {
      badgeList:[
        "选手",
        "冠军",
        "第一",
        "榜眼",
        "探花郎",
        "腰斩VIP"
      ],
      zqList:[
        "短线",
        "中线",
        "长线",
        "中短线"
      ],
      cwList:[
        "轻仓",
        "分仓",
        "重仓",
        "全仓"
      ],
      dfList:[
        "低吸",
        "半路",
        "首板",
        "接力",
        "撬板",
        "核按钮"
      ],
      loading: false,
      queryParams: {
        page: 1,
      },
      form: {},
      form1: {},
      list: [{}, {}],
      total: 0,
      title: "新增用户",
      open: false,
      open1: false,
      matchList: [],
      groupList: [],
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        player_type: [
          { required: true, message: "请选择用户类型", trigger: "change" },
        ],
        phone: [
          { required: true, message: "请输入用户手机号", trigger: "blur" },
        ],
        avatar: [
          { required: true, message: "请输入用户手机号", trigger: "change" },
        ],
      },
      rules1: {
        match_id: [
          { required: true, message: "请选择比赛", trigger: "change" },
        ],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        match_group: [
          { required: true, message: "请选择分组", trigger: "change" },
        ],
        match_status: [
          { required: true, message: "请选择参数状态", trigger: "change" },
        ],
      },
      playerType:[
          {label:"游客",value:0},
          {label:"普通选手",value:1},
          {label:"种子选手",value:2},
          {label:"开户选手",value:3}
      ]
    };
  },
  methods: {
    del(id) {
      this.$confirm("删除用户会删除相关数据,确定删除吗?", "提示", {
        type: "warning",
      }).then(() => {
        this.$api.delUser({ id: id }).then((res) => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getData();
        });
      });
    },
    gopage(size) {
      if (size) {
        this.queryParams.page_size = size;
      }
      this.queryParams.page = this.$refs.pageButton.page;
      this.getData();
    },
    getData() {
      this.loading = true;
      this.$api.getMatchList().then((res) => {
        this.matchList = res.data.data;
      });
      this.$api.getGroupList().then((res) => {
        this.groupList = res.data.data;
      });
      this.$api.getUserList(this.queryParams).then((res) => {
        this.list = res.data.data.list;
        this.total = res.data.data.total;
        this.loading = false;
      });
    },
    handleAdd() {
      this.open = true;
      this.title = "新增用户";
      this.form = {};
    },
    handleFastAdd() {
      this.open1 = true;
      this.title = "新增选手";
      this.form1 = {
        match_status: 1,
        match_id: this.matchList[0].id,
      };
    },
    edit(id) {
      this.title = "编辑用户";
      this.$api.getUserInfo({ id: id }).then((res) => {
        this.form = res.data.data;
        this.open = true;
      });
    },
    handleAvatarSuccess(res, file) {
      this.form.avatar = res.data.url;
    },
    beforeAvatarUpload(file) {
      return;
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.$api.editUser(this.form).then((res) => {
              if (res.data.code == 0) {
                this.msgSuccess("成功!");
                this.open = false;
                this.getData();
              } else {
                this.msgError(res.data.message);
              }
            });
          } else {
            this.$api.addUser(this.form).then((res) => {
              if (res.data.code == 0) {
                this.msgSuccess("成功!");
                this.open = false;
                this.getData();
              } else {
                this.msgError(res.data.message);
              }
            });
          }
        }
      });
    },
    // 创建选手同时创建用户
    fastSavePlayer() {
      this.$refs["form1"].validate((valid) => {
        if (valid) {
          if (this.form1.id != null) {
            this.$api.fastSavePlayer(this.form1).then((res) => {
              if (res.data.code == 0) {
                this.msgSuccess("成功!");
                this.open = false;
                this.getData();
              } else {
                this.msgError(res.data.message);
              }
            });
          } else {
            this.$api.fastSavePlayer(this.form1).then((res) => {
              if (res.data.code == 0) {
                this.msgSuccess("成功!");
                this.open1 = false;
                this.getData();
              } else {
                this.msgError(res.data.message);
              }
            });
          }
        }
      });
    },
  },
  created() {
    this.getData();
  },
};
</script>