<template>
    <section class="content">
        <h1>用户管理</h1>
        <el-divider></el-divider>
        <el-form label-width="40px" class="filter-form">
            <el-row>
                <el-col :span="9">
                    <el-form-item label="用户">
                        <el-input clearable v-model="queryParams.username" placeholder="请输入用户名/代码"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="4">
                    <el-form-item>
                        <el-button type="danger" @click="getData">筛选</el-button>
                    </el-form-item>
                </el-col>
            </el-row>   
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                >新增</el-button>
            </el-col>
        </el-row>
        <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="title='编辑用户',open=true,form=scope.row" size="mini" type="primary">编辑</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>
    </section>
</template>
<script>
import Page from "../../components/Page";
export default {
    components: {
        Page,
    },
    data(){
        return{
            loading:false,
            queryParams:{
                page:1
            },
            form:{},
            list:[{},{}],
            total:0,
            title:'新增用户',
            open:false,
            rules:{
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                usercode: [
                    { required: true, message: '请输入用户代码', trigger: 'blur' }
                ],
            }
        }
    },
    methods:{
        gopage(size) {
            if (size) {
                this.queryParams.page_size = size;
            }
            this.queryParams.page = this.$refs.pageButton.page;
            this.getData();
        },
        getData(){
            this.loading = true;
            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={}
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                if (this.form.id != null) {
                    this.$api.editUser(this.form).then(response => {
                    this.msgSuccess("修改成功");
                    this.open = false;
                    this.getData();
                    });
                } else {
                    this.$api.addUser(this.form).then(response => {
                    this.msgSuccess("新增成功");
                    this.open = false;
                    this.getData();
                    });
                }
        }
      });
    },
    },
    created(){
       this.getData() 
    }
}
</script>