<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="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="操作">
        <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="用户代码" 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 {
      loading: false,
      queryParams: {
        page: 1,
      },
      form: {},
      form1: {},
      list: [{}, {}],
      total: 0,
      title: "新增用户",
      open: false,
      open1: false,
      matchList: [],
      groupList: [],
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
      },
      rules1: {
        fund: [{ required: true, message: "请输入比赛资金", trigger: "blur" }],
        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" },
        ],
      },
    };
  },
  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;
      });
    },
    /** 提交按钮 */
    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>