<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">
        <h4>数据统计</h4>
        <el-divider></el-divider>
        <el-form label-width="80px" class="filter-form">
            <el-row>
                <el-col :span="4">
                    <el-form-item label="比赛名称" prop="match_id">
                        <el-select v-model="queryParams.match_id" placeholder="请选择比赛" size="mini" @change="getData()">
                            <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="3">
                    <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="3">
                    <el-form-item label="持股日期">
                        <el-date-picker
                        v-model="queryParams.stock_date"
                        type="date"
                        placeholder="选择日期"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        size="mini"
                        >
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="3">
                    <el-form-item label="比赛分组">
                        <el-select clearable @clear="getData()" v-model="queryParams.groupId" placeholder="请选择比赛分组" size="mini" @change="getData()">
                            <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="3">
                    <el-form-item label="请假次数">
                        <el-select clearable @clear="getData()" v-model="queryParams.auto_complete" placeholder="请选择" size="mini" @change="getData()">
                            <el-option v-for="item in missDayList" :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 style="margin-left:10px;" label-width="10">
                        <el-button type="primary" @click="getData" size="mini">筛选</el-button>
                        <el-button type="normal" @click="queryParams={},getData()" size="mini">重置</el-button>
                        <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        >上传数据</el-button>
                    </el-form-item>
                </el-col>
                <el-col :span="3">
                    <el-form-item style="margin-left:10px;" label-width="10">
                    <el-button icon="el-icon-download" type="primary" @click="downloadExcel" size="mini">导出Excel</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <el-table :data="list" height='55vh' style="width: 100%;margin-top:10px;" v-loading='loading'>
                <el-table-column prop="group_rank" label="排名"/>
                <el-table-column prop="username" label="选手"/>
                <el-table-column prop="stock_date" label="持股日期" width="150"/>
                <!-- <el-table-column prop="auto_complete" label="请假次数" width="100"/> -->
                <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="total_income" label="总盈亏" width="100"/>
                <!-- <el-table-column prop="date" label="是否开超市" width="150">
                    <template slot-scope="scope">
                        <span v-if="scope.row.is_markt">是</span>
                        <span v-else>否</span>
                    </template>
                </el-table-column> -->
                <el-table-column prop="yesterday_stock" label="昨日持股" width="150">
                    <template slot-scope="scope">
                        <div v-if="scope.row.auto_complete>0" style="color:red;">请假</div>
                        <div v-else>
                            <div v-if="scope.row.yesterday_is_markt==1" style="color:#409EFF;">开超市</div>
                            <div v-if="scope.row.yesterday_is_markt==0&&scope.row.yesterday_stock.length==0" style="color:#E6A23C;">空仓</div>
                            <div v-if="scope.row.yesterday_stock.length!=0">
                                <p v-for="(item,index) in scope.row.yesterday_stock" :key='index'>
                                    <span v-if="item.fund>0">{{item.name}}({{item.fund}}万元)</span>
                                    <span v-else>{{item.name}}</span>
                                </p>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="today_stock" label="今日持股" width="150">
                    <template slot-scope="scope">
                        <div v-if="scope.row.auto_complete>0" style="color:red;">请假</div>
                        <div v-else>
                            <div v-if="scope.row.is_markt==1" style="color:#409EFF;">开超市</div>
                            <div v-if="scope.row.is_markt==0&&scope.row.today_stock.length==0" style="color:#E6A23C;">空仓</div>
                            <div v-if="scope.row.today_stock.length!=0">
                                <p v-for="(item,index) in scope.row.today_stock" :key='index'>
                                    <span v-if="item.fund>0">{{item.name}}({{item.fund}}万元)</span>
                                    <span v-else>{{item.name}}</span>
                                </p>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="ctime" label="创建时间" width="160"/>
                <el-table-column prop="date" label="操作" fixed="right" width="200">
                    <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="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="player_id">
                    <el-select filterable v-model="form.player_id" placeholder="请选择用户代码">
                        <el-option v-for="item in playerList" :key='item.id' :label="item.label" :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="请输入今日资产" type="number">
                        <template slot="append">万元</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="开超市" prop="is_markt">
                    <el-switch
                        v-model="form.is_markt">
                    </el-switch>
                </el-form-item>
                <el-form-item label="股票名称" prop="today_stock">
                    <div v-for="(item,index) in form.today_stock" :key='index' style="margin-bottom:5px;">
                        <!-- <el-select  filterable v-model="item.name" placeholder="请选择股票" style="width:48%;display:inline-block">
                            <el-option v-for="item in stockList" :key='item.id' :label="item.label" :value="item.label"></el-option>
                        </el-select> -->
                        <el-input style="width:48%;display:inline-block" v-model="item.name" placeholder="请输入股票名称" :disabled="form.is_markt==true"></el-input>
                        &nbsp;<el-input style="width:39%" clearable v-model="item.fund" placeholder="资金" :disabled="form.is_markt==true">
                            <template slot="append">万元</template>
                        </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="primary" size="small" plain>新增股票</el-button>
                </el-form-item>
                <el-form-item label="股票截图" prop="today_stock_img">
                    <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="primary" 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:{
                match_id:0,
                page:1,
                stock_date:this.getNowDate()
            },
            form:{
                stock_date:this.getNowDate()
            },
            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' }
                ],
                // today_stock: [
                //     { required: true, message: '请添加股票', trigger: 'blur' }
                // ],
                // today_stock_img: [
                //     { required: true, message: '请输入股票截图', trigger: 'blur' }
                // ],
                is_markt: [
                    { required: false, message: '请选择', trigger: 'change' }
                ]
            },
            matchList:[],
            playerList:[],
            groupList:[],
            stockList:[],
            missDayList:[
                {id:0,name:"无"},
                {id:1,name:"1次"},
                {id:2,name:"2次"},
                {id:3,name:"3次"},
                {id:4,name:"4次"},
                {id:5,name:"5次"},
            ]
        }
    },
    methods:{
        getNowDate(){
            var y=new Date().getFullYear() 
            var m=Number(new Date().getMonth() + 1)
            var d = new Date().getDate()
            m=m>9?m:'0'+m;
            d=d>9?d:'0'+d
            return y+'-'+m+'-'+d;
        },
        downloadExcel(){
            this.$api.downloadRecords(this.queryParams).then((res)=>{
                var elink = document.createElement('a');
                let blob=new Blob([res.data], {type: 'application/vnd.ms-excel,charset=UTF-8'});
                let objUrl=URL.createObjectURL(blob);
                let file_name=decodeURIComponent(res.headers['content-disposition'].split('=')[1]);
                console.log(file_name)
                elink.download = "参赛数据";
                elink.style.display = 'none';                
                elink.href = objUrl;
                document.body.appendChild(elink);
                elink.click();
                document.body.removeChild(elink);      
                this.download_loading = false;
            })
        },
        del(id) {
        this.$confirm("确定删除吗?", "提示", {
            type: "warning",
        }).then(() => {
            this.$api.delRecord({ 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.getPlayerRecord(this.queryParams).then(res=>{
                this.list=res.data.data.list
                this.total = res.data.data.total;
                this.loading = false;
            })
        },
        edit(id){
            this.title = "编辑数据"
            this.$api.getRecordInfo({id:id}).then(res=>{
                this.form = res.data.data
                this.open = true
            })
        },
        handleAdd(){
            this.open=true;
            this.title='新增数据';
            this.form={
                match_id:this.matchList[0].id,
                today_stock:[{code: "", name:"",fund:''}],
                today_stock_img:[],
                stock_date:this.getNowDate()
            }
        },
        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() {
            console.log(this.form)
            this.$refs["form"].validate(valid => {
                if (valid) {
                // if(this.form.today_stock.length<=0){
                //     this.msgError('请添加股票');
                //     return
                // }else{
                //     if((!this.form.today_stock[0].name || !this.form.today_stock[0].fund) && !this.form.is_markt){
                //         this.msgError('请选择股票并输入资金额');
                //         return
                //     }
                // }
                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.$api.getMatchList().then(res=>{
           this.matchList=res.data.data
           this.queryParams.match_id = this.matchList[0].id
           this.getData()
       })
       this.$api.getPlayerList().then(res=>{
           this.playerList=res.data.data
       })
       this.$api.getGroupList().then(res=>{
           this.groupList=res.data.data
       })
       this.$api.getStockList().then(res=>{
           this.stockList=res.data.data
       })
    }
}
</script>