<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="4" :sm="12">
                        <a-form-item :labelCol="{span: 6}"  :wrapperCol="{span: 8, offset: 1}"  label="县分">
                            <a-select placeholder="请选择" @change="changeSubst" v-model="queryParam.substName" >
                                <a-select-option key="">请选择</a-select-option>
                                <a-select-option v-for="data in substList" :value="data">
                                    {{ data}}
                                </a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>

                    <a-col :md="6" :sm="12">
                        <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}"  label="学校名称">
                            <a-select showSearch placeholder="请选择"  v-model="queryParam.schoolName" >
                                <a-select-option key="">请选择</a-select-option>
                                <a-select-option v-for="data in schoolList" :key="data.school_name" :value="data.school_name">
                                </a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>

                    <a-col :md="4" :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="1">未审核</a-select-option>
                                <a-select-option key="2">初审不通过</a-select-option>
                                <a-select-option key="3">待复审</a-select-option>
                                <a-select-option key="4">复审通过</a-select-option>
                                <a-select-option key="5">复审不通过</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>

                    <a-col :md="6" :sm="12">
                        <a-form-item  label="申请单号：">
                            <a-input v-model="queryParam.applyNum"></a-input>
                        </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="applyAgain()" 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="{ onChange: onSelectChange }">
            <!--拦截器-->
            <template slot="createTime" slot-scope="text">
                {{ text | dayjs}}
            </template>
            <template slot="orderState" slot-scope="text">
                <span v-if="text == '1'">未审核</span>
                <span v-if="text == '2'">初审不通过</span>
                <span v-if="text == '3'">待复审</span>
                <span v-if="text == '4'">复审通过</span>
                <span v-if="text == '5'">复审不通过</span>
            </template>
        </s-table>

    </div>
</template>

<script>
    import STable from '@/components/table';
    import {applyOrderList,updateState,getSubstList,getSchoolList} from "@/api/school-center/bonusAPI"
    import {cloneObject} from '@/utils/util';
    export default {
        name: "applyOrder",
        components: {
            STable,
        },
        data: function () {
            return {
                emptyText: {emptyText: '暂无数据'},
                fileList: [
                ],
                // form
                Formtable: this.$form.createForm(this, {name: 'Formtable'}),
                substList: [],
                schoolList:[],
                chosePages:[],
                selectedState: [],
                applyNums:'',
                products:[],
                //输入框列表
                queryParam: {
                    userName: '',
                    account: '',
                    substName: '',
                    schoolName: '',
                    applyNum: '',
                    state: '',
                },
                columns: [
                    {dataIndex: 'applyNum', width: 200, title: '申请单号', align: "center"},
                    {dataIndex: 'state', width: 100, title: '状态', align: "center",scopedSlots:{customRender: 'orderState'}},
                    {dataIndex: 'createDate', width: 200, title: '创建时间', align: "center",scopedSlots: {customRender: 'createTime'}},
                    {dataIndex: 'applicant', width: 100, title: '申请人', align: "center"},
                    {dataIndex: 'substName', width: 100, title: '放号人县分', align: "center"},
                    {dataIndex: 'schoolName', width: 200, title: '放号人学校', align: "center"},
                    {dataIndex: 'userName', width: 100, title: '放号人', align: "center"},
                    {dataIndex: 'bonus', width: 100, title: '佣金', align: "center"},
                    ]
                ,
                rowdata: parameter => {
                    let params = {
                        pageNo: parameter.pageNo,
                        pageSize: parameter.pageSize
                    }

                    if(this.substList == '') {
                        let param = {};
                        getSubstList(Object.assign(param, obj)).then(res => {
                            if (res.state == 'success') {
                                res.data.forEach((key) => {
                                    this.substList.push(key.sub_name)
                                })
                            }
                        })
                    }

                    //表示
                    let obj = cloneObject(this.queryParam)
                    return applyOrderList(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})
            },
            applyAgain(){
                if(this.selectedRowKeys == ''){
                    this.$message.info("请勾选数据！",2);
                    return null;
                }

                for(let i = 0; i < this.selectedState.length; i++){
                    let theState = this.selectedState[i];
                    if(theState != 2 && theState != 5){
                        this.$message.info("再次申请只能选择初审不通过和复审不通过的申请单",2);
                        return false;
                    }
                }

                let params = {};

                let applyNumArray = this.applyNums.split(",");
                if(applyNumArray.length > 1){
                    let theApplyNum = applyNumArray[0];
                    this.applyNums = theApplyNum;

                    applyNumArray.forEach(data => {
                        if(theApplyNum != data){
                            this.applyNums += "," + theApplyNum;
                            theApplyNum = data;
                        }
                    })
                }

                params.applyNums = this.applyNums;
                params.state = "1";

                updateState(params).then(res => {
                    if(res.state == "success"){
                        this.$message.success("提交成功",2);
                        this.search();
                        return null;
                    }

                    this.$message.error("提交失败",2);
                })
            },
            changeSubst() {
                let param = {};
                param.substName = this.queryParam.substName;
                this.schoolList = [];
                this.queryParam.substSchool = "请选择";

                getSchoolList(param).then(res => {
                    if(res.state == "success"){
                        res.data.forEach(key => {
                            this.schoolList.push(key);
                        })
                    }
                })
            },
            onSelectChange(selectedRowKeys,selectedRows) {
                this.applyNums = '';
                this.selectedState = [];

                selectedRows.forEach(data => {
                    this.applyNums += data.applyNum +",";
                    this.selectedState.push(data.state);
                })

                this.applyNums = this.applyNums.substring(0,this.applyNums.length-1);
            },
        }
    }
</script>

<style scoped>

</style>
