<style lang="scss">
    .del{
            display: inline-block;
            vertical-align: top;
            font-size: 28px;
            font-weight: 500;
            margin-left: 10px;
            color: #000;
            cursor: pointer;
    }
</style>
<template>
    <section class="content data">
        <h1>数据统计</h1>
        <el-divider></el-divider>
        <el-form label-width="80px" class="filter-form">
            <el-row>
                <el-col :span="9">
                    <el-form-item label="选手名字">
                        <el-input v-model="queryParams.username" placeholder="请输入选手名字/代码"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="9">
                    <el-form-item label="比赛日期">
                        <el-date-picker
                        v-model="queryParams.date"
                        type="date"
                        placeholder="选择日期"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="9">
                    <el-form-item label="比赛名称" prop="match_id">
                        <el-select v-model="queryParams.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-col>
                <el-col :span="9">
                    <el-form-item label="比赛分组">
                        <el-select v-model="queryParams.groupId" 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-col>
                <el-col :span="4">
                    <el-form-item>
                        <el-button type="primary" @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 :data="list" height='39vh' style="width: 100%;margin-top:10px;">
                <el-table-column prop="rank" label="排名"/>
                <el-table-column prop="username" label="选手"/>
                <el-table-column prop="init_fund" label="初始资产"/>
                <el-table-column prop="yesterday_fund" label="昨日资产"/>
                <el-table-column prop="today_fund" label="今日资产"/>
                <el-table-column prop="today_income" label="今日盈亏"/>
                <el-table-column prop="date" label="总盈亏"/>
                <el-table-column prop="date" label="金日持股" width="150">
                    <template slot-scope="scope">
                        <p v-for="(item,index) in scope.row.today_stock" :key='index'>
                            {{item.name}}({{item.code}})
                        </p>
                    </template>
                </el-table-column>
                <el-table-column prop="date" label="昨日持股" width="150">
                    <template slot-scope="scope">
                        <p v-for="(item,index) in scope.row.yesterday_stock" :key='index'>
                            {{item.name}}({{item.code}})
                        </p>
                    </template>
                </el-table-column>
                <el-table-column 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="match_id">
                        <el-select v-model="form.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="stock_date">
                        <el-date-picker
                        v-model="form.stock_date"
                        type="date" style="width:100%"
                        placeholder="选择日期"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="今日资产" prop="today_fund">
                    <el-input clearable v-model="form.today_fund" placeholder="请输入代码"></el-input>
                </el-form-item>
                <el-form-item label="用户代码" prop="player_id">
                    <el-select v-model="form.player_id" placeholder="请选择用户代码">
                            <el-option v-for="item in matchList" :key='item.id' :label="item.usercode" :value="item.id"></el-option>
                    </el-select>
                    <!-- <el-input clearable v-model="form.player_id" placeholder="请输入代码"></el-input> -->
                </el-form-item>
                <el-form-item label="股票名称">
                    <div v-for="(item,index) in form.today_stock" :key='index' style="margin-bottom:5px;">
                        <el-input style="width:28%" clearable v-model="item.name" placeholder="名称"></el-input>
                        &nbsp;<el-input style="width:28%" clearable v-model="item.code" placeholder="代码"></el-input>
                        &nbsp;<el-input style="width:29%" clearable v-model="item.fund" placeholder="资金"></el-input>
                        <i @click="form.today_stock.splice(index,1)" style="margin-top:5px;" class="el-icon-circle-close del"></i>
                    </div>
                    <el-button @click="addCode" type="danger" size="small" plain>新增代码</el-button>
                </el-form-item>
                <el-form-item label="股票名称">
                    <div class="imgs" v-for="(item,index) in form.today_stock_img" :key='index'>
                        <img  width="150" :src="item" alt="">
                        <i @click="form.today_stock_img.splice(index,1)" class="el-icon-circle-close del"></i>
                    </div>
                    <el-upload
                        class="upload-demo"
                        :show-file-list	='false'
                        :on-success="handleSuccess"
                        action="/api/admin/uploadfile">
                        <el-button size="small" type="danger" plain>上传图片</el-button>
                    </el-upload>
                </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:{
                match_id: [
                    { required: true, message: '请选择比赛', trigger: 'change' }
                ],
                player_id: [
                    { required: true, message: '请输入用户代码', trigger: 'blur' }
                ],
                stock_date: [
                    { required: true, message: '请选择持仓日期', trigger: 'blur' }
                ],
                today_fund: [
                    { required: true, message: '请输入今日资产', trigger: 'blur' }
                ],
            },
            matchList:[],
            playerList:[],
            groupList:[]
        }
    },
    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.getPlayerRecord(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={
                today_stock:[{code: "", name:"",fund:''}],
                today_stock_img:[]
            }
        },
        addCode(){
             this.form.today_stock.push({code: "", name:"",fund:''})
        },
        handleSuccess(res, file) {
            console.log(res)
            this.form.today_stock_img.push(res.data.url);
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                if (this.form.id != null) {
                    this.$api.editRecord(this.form).then(response => {
                        if(response.data.code != 0){
                            this.msgError(response.data.message);
                            return
                        }
                        this.msgSuccess("修改成功");
                        this.open = false;
                        this.getData();
                    });
                } else {
                    this.$api.addRecord(this.form).then(response => {
                        if(response.data.code != 0){
                            this.msgError(response.data.message);
                            return
                        }
                        this.msgSuccess("新增成功");
                        this.open = false;
                        this.getData();
                    });
                }
        }
      });
    },
    },
    created(){
       this.getData() 
       this.$api.getMatchList().then(res=>{
           this.matchList=res.data.data
       })
       this.$api.getPlayerList().then(res=>{
           this.playerList=res.data.data
       })
       this.$api.getGroupList().then(res=>{
           this.groupList=res.data.data
       })
       
       
    }
}
</script>