<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.applicant"></a-input>
                        </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>

                    <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>

                    <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="openAudit(1)" type="primary">初审</a-button>
                            <a-button @click="openAudit(2)" type="primary" style="left:10px">复审</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="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>

        <!--编辑----弹框-->
        <a-modal :title="titleName" :visible="auditVisible"  @ok="auditOk" @cancel="auditCancel" html-type="submit" width="440px">
            <a-form :form="Formtable">
                <a-form-item :labelCol="{span: 6}" :wrapperCol="{span: 8, offset: 1}" label="审核">
                    <a-select placeholder="请选择" v-model="state">
                        <a-select-option key="1">审核通过</a-select-option>
                        <a-select-option key="2" >审核不通过</a-select-option>
                    </a-select>
                </a-form-item>

            </a-form>
        </a-modal>

    </div>
</template>

<script>
    import STable from '@/components/table';
    import {auditOrderList,updateState} from "@/api/school-center/bonusAPI"
    import {cloneObject} from '@/utils/util';
    export default {
        name: "auditApply",
        components: {
            STable,
        },
        data: function () {
            return {
                emptyText: {emptyText: '暂无数据'},
                fileList: [
                ],
                // form
                Formtable: this.$form.createForm(this, {name: 'Formtable'}),
                auditVisible:false,
                applyNums:'',
                state: '',
                selectedState: [],
                selectedRowKeys: [],
                titleName: '',
                //输入框列表
                queryParam: {
                    applicant: '',
                    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: 'bonus', width: 100, title: '总佣金', align: "center"},
                    ]
                ,
                rowdata: parameter => {
                    let params = {
                        pageNo: parameter.pageNo,
                        pageSize: parameter.pageSize
                    }

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

                if(type == 1){
                    this.titleName = "初审";
                    for(let i = 0; i < this.selectedState.length; i++){
                        let theState = this.selectedState[i];
                        if(theState != 1 && theState != 2){
                            this.$message.info("初审只能审核未审核和初审不通过的申请单",2);
                            return false;
                        }
                    }
                }else{
                    this.titleName = "复审";
                    for(let i = 0; i < this.selectedState.length; i++){
                        let theState = this.selectedState[i];
                        if(theState != 3 && theState != 5){
                            this.$message.info("复审只能审核待复审和复审不通过的申请单",2);
                            return false;
                        }
                    }
                }

                this.auditVisible = true;
            },
            auditOk(){
                let params = {};
                params.applyNums = this.applyNums;
                if(this.titleName == "初审"){
                    params.state = this.state == 1 ? 3 : 2;
                }else{
                    params.state = this.state == 1 ? 4 : 5;
                }

                updateState(params).then(res => {
                    if(res.state == "success"){
                        this.$message.success("审核成功",2);
                        this.search();
                        this.auditVisible = false;
                        // 重置记录选中的状态
                        this.selectedState = [];
                        this.selectedState.push(params.state );
                        return null;
                    }

                    this.$message.error("审核失败",2);
                })
            },
            auditCancel(){
                this.auditVisible = false;
            },
            onSelectChange(selectedRowKeys,selectedRows) {
                this.selectedRowKeys = selectedRowKeys;
                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>
