<template>
    <div class="flex-container">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <!-- 搜索区域 -->
            <a-form layout="inline">
                <a-row :gutter="24">
                    <a-col :md="4" :sm="12">
                        <a-form-item  label="放号人：">
                            <a-input v-model="queryParam.userName"></a-input>
                        </a-form-item>
                    </a-col>

                    <a-col :md="5" :sm="12">
                        <a-form-item  label="放号人账号：">
                            <a-input v-model="queryParam.account"></a-input>
                        </a-form-item>
                    </a-col>

                    <a-col :md="5" :sm="12">
                        <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}" label="佣金状态">
                            <a-select showSearch v-model="queryParam.state" >
                                <a-select-option key="">请选择</a-select-option>
                                <a-select-option key="4">未结算</a-select-option>
                                <a-select-option key="6">结算成功</a-select-option>
                                <a-select-option key="7">结算失败</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>

                    <span class="table-page-search-submitButtons" style="float: left; overflow: hidden;">
                        <a-col :md="6" :sm="24">
                            <a-button @click="search()" type="primary">查询</a-button>
                             <a-divider type="vertical"/>
                            <a-button @click="sendBonus()" type="primary">结算</a-button>
                            <a-button @click="exportList()" style="left: 10px;" type="primary">导出</a-button>
                        </a-col>
                    </span>
                </a-row>
            </a-form>
           </div>

        <!--列表-->
        <s-table  :locale="emptyText" :columns="columns" :data="rowdata"  bordered ref="table" size="small" class="flex-filling"
                  :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }">
                <template slot="orderState" slot-scope="text">
                    <span v-if="text == '4'">待结算</span>
                    <span v-if="text == '6'">结算成功</span>
                    <span v-if="text == '7'">结算失败</span>

                </template>
<!--            <template slot="operation" slot-scope="text,record">-->
<!--                <a  @click="modifyfunction(record)">编辑</a>-->
<!--                <a-divider type="vertical"/>-->
<!--                    <a-popconfirm title="是否要删除此行？" @confirm="deleteActivity(record.id)">-->
<!--                        <a>删除</a>-->
<!--                    </a-popconfirm>-->
<!--                <a-divider type="vertical"/>-->
<!--            </template>-->
        </s-table>

    </div>
</template>

<script>
    import STable from '@/components/table';
    import {bonusList,sendBonus,exportSettlementList,exportResult,getSubstList,getSchoolList} from "@/api/school-center/bonusAPI"
    import {cloneObject,exportFile} from '@/utils/util';
    export default {
        name: "settlement",
        components: {
            STable,
        },
        data: function () {
            return {
                emptyText: {emptyText: '暂无数据'},
                fileList: [
                ],
                //输入框列表
                queryParam: {
                    userName: '',
                    account: '',
                    state: '',
                },
                bonusNums: [],
                selectedRowKeys: [],
                columns: [
                    {dataIndex: 'account', width: 100, title: '账号', align: "center"},
                    {dataIndex: 'userName', width: 100, title: '姓名', align: "center"},
                    {dataIndex: 'substName', width: 100, title: '县分', align: "center"},
                    {dataIndex: 'schoolName', width: 200, title: '学校', align: "center"},
                    {dataIndex: 'bonus', width: 100, title: '金额', align: "center"},
                    {dataIndex: 'state', width: 100, title: '佣金状态', align: "center", scopedSlots: {customRender: 'orderState'}},
                    {dataIndex: 'isBind', width: 100, title: '微信绑定', align: "center"},
                    // {
                    //     dataIndex: 'operation',
                    //     width: 200,
                    //     title: '操作',
                    //     align: "center",
                    //     scopedSlots: {customRender: 'operation'},
                    //     fixed: "right"
                    // }
                    //
                    ]
                ,
                rowdata: parameter => {
                    let params = {
                        pageNo: parameter.pageNo,
                        pageSize: parameter.pageSize
                    }

                    //表示
                    let obj = cloneObject(this.queryParam)
                    return bonusList(Object.assign(params, obj)).then(res => {
                        let data = {}
                        if (res.state !== 'success') {
                            this.emptyText.emptyText = '查询失败！'
                            this.$message.error("查询失败！", 5);
                            data = {
                                data: [],
                                pageSize: parameter.pageSize,
                                pageNo: 1,
                                totalCount: 0
                            }
                        } else {
                            if (res.data.records.length==0){
                                return data
                            }
                            data = {
                                data: res.data.records,
                                pageSize: parameter.pageSize,
                                pageNo: parameter.pageNo,
                                totalCount: res.data.total
                            }
                        }
                        return data
                    })
                }
            }
        },
        methods:{
            search() {
                this.$refs.table.refresh({search: true})
            },
            sendBonus(){
                if(this.selectedRowKeys == ''){
                    this.$message.info("请勾选数据！",2);
                    return null;
                }

                let params = {};
                params.bonusNums = this.bonusNums;

                sendBonus(params).then(res => {
                    if(res.state == "success"){
                        this.$message.success(res.data,2);
                        if(res.data.indexOf("结算失败") > -1){
                            exportFile(exportResult(this.queryParam),'结算结果清单.xlsx');
                        }

                        this.search();
                        return null;
                    }

                    this.$message.error("结算失败",2);
                })
            },
            onSelectChange(selectedRowKeys,selectedRows) {
                this.selectedRowKeys = selectedRowKeys;
                this.bonusNums = '';
                selectedRows.forEach(data => {
                    this.bonusNums += data.bonusNum +",";
                })

                this.bonusNums = this.bonusNums.substring(0,this.bonusNums.length-1);
            },
            exportList(){
                exportFile(exportSettlementList(this.queryParam),'账单清单.xlsx');
            }
        }
    }
</script>

<style scoped>

</style>
