<template>
    <div class="expenseMobile-all">
        <el-dialog top="20vh" custom-class="selfDialog" width="95%" title="详情" :visible.sync="expenseMobileVisible">
            <div class="dialog-title">详情</div>
            <div class="dialog-info">
                <div class="dialog-info-ms">费用描述：{{dailydata.instructions}}</div>
                <div class="dialog-info-zj">费用总计：{{townum(dailydata.sumPrice)}}</div>
                <div class="dialog-info-sh">
                    <div>财务审核：{{dailydata.fAuditDesc}}</div>
                    <div>总经理审核：{{dailydata.mAuditDesc}}</div>
                    <div>是否付款：{{ dailydata.isPay==1?'已付款':'未付款' }}</div>
                </div>
            </div>
            <div class="dialog-title">具体费用</div>
            <div class="dialog-list">
                <div v-for="(items,index) in dailydata.feeContents" :key="index" class="dialog-list-li">
                    <div>费用项目:{{items.priceName}}</div>
                    <div class="info-sl">
                        <div>数量:{{items.quantity}}</div>
                        <div>单价(CNY):{{townum(items.price)}}</div>
                        <div>小计(CNY):{{townum(items.itemTotal)}}</div>
                    </div>
                    <div>备注:{{items.remark}}</div>
                </div>
            </div>
        </el-dialog>
        <div v-if="platejudgment==1">
            <div class="expenseMobile-top">
                <div class="expenseMobile-title">{{this.portType!=2?'日常付款申请':''}}</div>
                <div class="expenseMobile-search">
                    <el-autocomplete style="flex:19;" v-model="input"
                        :fetch-suggestions="querySearchAsyncs" :trigger-on-focus="false" placeholder="请输入或选择"
                        @select="handleSelects">
                        <template slot-scope="{ item }">
                            <div style="width: 300px">{{ item.instructions }}</div>
                        </template>
                    </el-autocomplete>
                    <el-button style="flex:1;" @click.stop="searchbtn" type="primary" icon="el-icon-search"></el-button>
                    <!-- <el-select popper-class="my-select" style="width:100%" v-model="input" clearable filterable remote :remote-method="DailyFeeKeywordSearch" :loading="selectLoadingrf" @change="searchbtn" placeholder="请选择">
                        <el-option
                        v-for="item in inputArrrf"
                        :key="item.id"
                        :label="item.instructions"
                        :value="item.instructions">
                        </el-option>
                    </el-select> -->
                    <!-- <el-button @click="searchbtn" slot="append" icon="el-icon-search"></el-button> -->
                    <!-- <el-input clearable placeholder="请输入费用描述" v-model="input" class="input-with-select">
                        <el-button @click="searchbtn" slot="append" icon="el-icon-search"></el-button>
                    </el-input> -->
                </div>
            </div>
            <div id="scroll-div" ref="element" class="daily-box">
                <div class="daily-card" v-for="(item,index) in tableData" :key="index">
                    <div class="card-description">费用描述：{{item.instructions}}</div>
                    <div class="card-date">申请时间：{{item.createTime}}</div>
                    <div class="card-expense">
                        <span>费用总计：{{townum(item.sumPrice)}}</span>
                        <span>申请人：{{item.createUser}}</span>
                    </div>
                    <div class="card-btn">
                        <el-button @click="infobtn(item)" size="mini">详情</el-button>
                        <el-button size="mini" @click="PostDelDailyPaymentAudit(item.id,1)"
                            type="primary">通过</el-button>
                        <el-button size="mini" @click="PostDelDailyPaymentAudit(item.id,2)" type="danger">拒绝</el-button>
                    </div>
                </div>
            </div>
            <div id="tshezi" style="padding-bottom: 65px;">
                <div v-if="!zwgd" class="expenseMobile-zwgd">暂无更多</div>
                <div v-if="zwgd" class="expenseMobile-zwgd">加载中！</div>
            </div>
        </div>
        <div v-else>
            <div class="expenseMobile-top">
                <div class="expenseMobile-title">{{this.portType!=2?'费用审核':''}}</div>
                <div class="expenseMobile-search">
                    <el-autocomplete style="flex:19;" v-model="value" :fetch-suggestions="querySearchAsync"
                        :trigger-on-focus="false" placeholder="请输入或选择团组" @select="handleSelect">
                        <template slot-scope="{ item }">
                            <div>{{ item.teamName }}</div>
                        </template>
                    </el-autocomplete>
                    <el-button type="primary" style="flex:1;" @click.stop="searchexamine"
                        icon="el-icon-search"></el-button>
                    <!-- <el-select popper-class="my-select" style="width:100%" v-model="value" clearable filterable remote
                        :remote-method="GroupItemKeywordSearch" :loading="selectLoading" @change="searchexamine"
                        placeholder="请选择">
                        <el-option v-for="item in inputArr" :key="item.id" :label="item.teamName"
                            :value="item.teamName">
                        </el-option>
                    </el-select> -->
                    <!-- <el-input clearable placeholder="请输入团组名称" v-model="value" class="input-with-select">
                        <el-button @click="searchexamine" slot="append" icon="el-icon-search"></el-button>
                    </el-input> -->
                </div>
            </div>
            <div id="examine-div" class="examine-box">
                <div v-for="(info,index) in groupData" :key="index" class="examine-card">
                    <div class="examine-card-name">团组名称：{{info.teamName}}</div>
                    <div class="examine-card-date">出访时间：{{cltade(info.visitDate)}}</div>
                    <div class="examine-card-client">客户名称：{{info.clientName}}</div>
                    <div class="examine-card-operator">
                        <div>天数/人数：{{info.visitDays}}/{{info.visitPNumber}}</div>
                        <div>接团操作人：{{info.jietuanOperator}}</div>
                    </div>
                    <div class="examine-card-btn">
                        <el-button @click="CheckCost(info)" size="mini" type="primary">查看费用</el-button>
                    </div>
                </div>
            </div>
            <div id="tshezi" style="padding-bottom: 65px;">
                <div v-if="!tzzwgd" class="expenseMobile-zwgd">暂无更多</div>
                <div v-if="tzzwgd" class="expenseMobile-zwgd">加载中！</div>
            </div>
        </div>
        <div class="expenseMobile-tabtn">
            <div class="tabtn-box">
                <div @click="rcbtn">
                    <div v-if="platejudgment==1"><i style="font-size:22px;" class="el-icon-s-goods"></i></div>
                    <div v-else><i style="font-size:20px;" class="el-icon-goods"></i></div>
                    <div style="font-size:14px;">日常</div>
                </div>
                <div @click="tzbtn">
                    <div v-if="platejudgment==2"><i style="font-size:22px;" class="el-icon-s-goods"></i></div>
                    <div v-else><i style="font-size:20px;" class="el-icon-goods"></i></div>
                    <div style="font-size:14px;">团组</div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    data () {
        return {
            uid:'',
            portType:0,
            tableData:[],
            input:'',
            expenseMobileVisible:false,
            dailydata:{},
            pageIndex:1,
            pageIndexExamine:1,
            loadflagExamine:false,
            loadflag:false,
            zwgd:true,
            tzzwgd:true,
            platejudgment:2,
            value:'',
            inputArr: [],
            selectLoading: false,
            inputArrrf: [],
            selectLoadingrf: false,
            groupData:[],
        }
    },
    methods:{
        // 异步搜索方法
        querySearchAsync(queryString, cb) {
            if (queryString === '' || queryString.trim() === '') {
                cb(this.inputArr);
                return;
            }

            this.selectLoading = true;
            var url = "/api/Groups/GroupItemKeywordSearch?keyword=" + queryString;
            var that = this;

            this.$axios({
                method: 'get',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
            }).then(function (res) {
                if (res.data.code == 200) {
                    const results = res.data.data || [];
                    // 如果没有搜索结果，添加一个自定义选项
                    if (results.length === 0 && queryString.trim() !== '') {
                        results.push({
                            id: 'custom',
                            teamName: queryString,
                            clientUnit: '自定义输入',
                            isCustom: true
                        });
                    }
                    cb(results);
                } else {
                    cb([]);
                }
            }).finally(() => {
                that.selectLoading = false;
            });
        },

        // 处理选择
        handleSelect(item) {
            if (item.isCustom) {
                // 如果是自定义输入，直接使用输入的值
                this.value = item.teamName;
                this.searchexamine();
            } else {
                this.value = item.teamName;
                this.searchexamine();
            }
        },

        //异步搜索方法
        querySearchAsyncs(queryString, cb) {
            if (queryString === '' || queryString.trim() === '') {
                cb(this.inputArr);
                return;
            }
            this.selectLoading = true;
            var url = "/api/System/DailyFeeKeywordSearch?keyword=" + queryString+"&auditType=1";
            var that = this;

            this.$axios({
                method: 'get',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
            }).then(function (res) {
                if (res.data.code == 200) {
                    const results = res.data.data || [];
                    // 如果没有搜索结果，添加一个自定义选项
                    if (results.length === 0 && queryString.trim() !== '') {
                        results.push({
                            id: 'custom',
                            teamName: queryString,
                            clientUnit: '自定义输入',
                            isCustom: true
                        });
                    }
                    console.log(results);
                    
                    cb(results);
                } else {
                    cb([]);
                }
            }).finally(() => {
                that.selectLoading = false;
            });
        },

        // 处理选择
        handleSelects(item) {
            if (item.isCustom) {
                // 如果是自定义输入，直接使用输入的值
                this.input = item.instructions;
                this.searchbtn();
            } else {
                this.input = item.instructions;
                this.searchbtn();
            }
        },



        //搜索提示
        DailyFeeKeywordSearch(query) {
            if (query !== '') {
                this.selectLoadingrf=true;
                var url = "/api/System/DailyFeeKeywordSearch?keyword=" + query+"&auditType=1"
                var that = this
                this.$axios({
                    method: 'get',
                    url: url,
                    headers: {
                        Authorization: 'Bearer ' + this.token
                    },
                }).then(function (res) {
                    if (res.data.code == 200) {
                        that.inputArrrf = res.data.data;
                    }else{
                        that.inputArrrf=[];
                    }
                }).finally(()=>{
                    that.selectLoadingrf=false;
                })
            }
        },
        //搜索提示
        GroupItemKeywordSearch(query) {
            if (query !== '') {
                this.selectLoading=true;
                var url = "/api/Groups/GroupItemKeywordSearch?keyword=" + query
                var that = this
                this.$axios({
                    method: 'get',
                    url: url,
                    headers: {
                        Authorization: 'Bearer ' + this.token
                    },
                }).then(function (res) {
                    if (res.data.code == 200) {
                        that.inputArr = res.data.data;
                    }else{
                        that.inputArr=[];
                    }
                }).finally(()=>{
                    that.selectLoading=false;
                })
            }
        },
        //保留两位小数
        townum(val){
            val=Number(val);
            return val.toFixed(2);
        },
        //保留两位小数
        cltade(val){
            return val.split(" ")[0];
        },
        //翻页
        PageTurning(){
            this.pageIndex++;
            this.postPageDailyPaymentList();
        },
        //翻页Examine
        PageTurningExamine(){
            this.pageIndexExamine++;
            this.PostExpenseAuditGroupPageItems();
        },
        //获取列表
        postPageDailyPaymentList() {
            var url = "/api/Financial/PostPageSearchDailyPaymentList"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 1,
                    pageIndex: that.pageIndex,
                    pageSize: 10,
                    companyId: -1,
                    financialAuditStatus: -1,//财务审核
                    managerAuditStatus: that.uid==21?0:-1,  //总经理审核
                    feeTypeId: -1,           //费用类型
                    feeSubTypeId: -1,       //费用子类型
                    feeDesc: that.input,    //描述
                    createUserId: -1, //申请人
                    isPaySign:-1 //是否支付
                }
            }).then(function (res) {
                if (res.data.code == 200) {
                    var magarr=res.data.data.dataList;
                    if(magarr.length<10){
                        that.zwgd=false;
                    }
                    for(let j=0;j<magarr.length;j++){
                        that.tableData.push(magarr[j])
                    }
                    that.loadflag=true;
                    // that.tableData = res.data.data.dataList;
                }
            })
        },
        //审核
        PostDelDailyPaymentAudit(val1,val2){
            if (this.uid==undefined||this.uid!=21||this.uid==22) {
                return  this.$message.error("无审核权限");
            }
            var url = "/api/Financial/PostDelDailyPaymentAudit"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + that.token
                },
                data: {
                    id: val1,
                    auditType:that.uid==21?2:1,
                    auditStatus:val2
                }
            }).then(function (res) {
                if (res.data.code == 200) {
                    that.$message({
                        message: res.data.msg,
                        type: 'success',
                        offset:50
                    });
                    that.zwgd=true;
                    that.tableData=[];
                    that.pageIndex=1;
                    that.postPageDailyPaymentList()
                }else{
                    that.$message.error(res.data.msg);
                }
            })
        },
        //详情按钮
        infobtn(val){
            this.expenseMobileVisible=true;
            this.dailydata={};
            this.getPostSearchDailyPaymentInfo(val.id);
        },
        //获取详情
        getPostSearchDailyPaymentInfo(val){
            var url = "/api/Financial/PostSearchDailyPaymentInfo"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + that.token
                },
                data: {
                    PortType: 1,
                    id:val
                }
            }).then(function (res) {
                if (res.data.code == 200) {
                    that.dailydata=res.data.data;
                }
            })
        },
        //搜索
        searchbtn(){
            this.zwgd=true;
            this.pageIndex=1;
            this.tableData=[];
            this.postPageDailyPaymentList();
        },
        //搜索examine
        searchexamine(){
            this.tzzwgd=true;
            this.pageIndexExamine=1;
            this.groupData=[];
            this.PostExpenseAuditGroupPageItems();
            localStorage.setItem("Examinevalue",this.value );
        },
        scrollBottomExamine(e) {
            let scrollTop=document.documentElement.scrollTop || document.body.scrollTop;
            let clientHeight=document.documentElement.clientHeight || document.body.clientHeight;
            let scrollHeight=document.documentElement.scrollHeight || document.body.scrollHeight;
            if (Math.abs(scrollHeight-clientHeight-scrollTop) < 1&&this.loadflagExamine&&this.tzzwgd) {
                this.PageTurningExamine()
            }
        },
        scrollBottom(e) {
            let scrollTop=document.documentElement.scrollTop || document.body.scrollTop;
            let clientHeight=document.documentElement.clientHeight || document.body.clientHeight;
            let scrollHeight=document.documentElement.scrollHeight || document.body.scrollHeight;
            if(Math.abs(scrollHeight-clientHeight-scrollTop) < 1&&this.loadflag&&this.zwgd){
                this.PageTurning()
            }
            // if (Math.floor(scrollTop + clientHeight) == scrollHeight&&this.loadflag) {
            //     this.PageTurning()
            // }
        },
        //日常切换
        rcbtn(){
            this.loadflag=false;
            this.platejudgment=1;
            this.zwgd=true;
            this.tzzwgd=true;
            this.pageIndex=1;
            this.tableData=[];
            window.addEventListener('scroll', this.scrollBottom, true);
            window.removeEventListener('scroll', this.scrollBottomExamine, true);
            this.postPageDailyPaymentList();
        },
        //tuanzhuqiehuan
        tzbtn(){
            this.loadflagExamine=false;
            this.platejudgment=2;
            this.pageIndexExamine=1;
            this.groupData=[];
            window.addEventListener('scroll', this.scrollBottomExamine, true);
            window.removeEventListener('scroll', this.scrollBottom, true);
            this.PostExpenseAuditGroupPageItems();
        },
        //费用审核列表
        PostExpenseAuditGroupPageItems(){
            var url = "/api/Groups/PostExpenseAuditGroupPageItems"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 1,
                    pageId:47,
                    userId:21,
                    pageIndex: that.pageIndexExamine,
                    pageSize: 10,
                    isSure: -1,
                    searchCriteria: that.value,
                }
            }).then(function (res) {
                if (res.data.code == 200) {
                    var groupDatas=res.data.data.data;
                    if(groupDatas.length<10){
                        that.tzzwgd=false;
                    }
                    for(let j=0;j<groupDatas.length;j++){
                        that.groupData.push(groupDatas[j])
                    }
                    that.loadflagExamine=true;
                }
            })
        },
        //查看费用
        CheckCost(row){
            this.$router.push({path: '/expenseMobileinfo?id=' + row.id +'&uid='+this.uid});
        }
    },
    watch:{
   
    },
    mounted(){
        this.uid = this.$route.query.uid;
        this.portType = this.$route.query.portType?this.$route.query.portType:0;
        // if(localStorage.getItem('Examinevalue')!=""){
        //     this.value=localStorage.getItem('Examinevalue');
        // }
        // console.log(localStorage.getItem('Examinevalue'));
        // this.token=JSON.parse(localStorage.getItem('Examinevalue'));
        if(this.platejudgment==1){
            window.addEventListener('scroll', this.scrollBottom, true)
            window.removeEventListener('scroll', this.scrollBottomExamine, true)
            this.postPageDailyPaymentList();
        }else{
            window.addEventListener('scroll', this.scrollBottomExamine, true)
            window.removeEventListener('scroll', this.scrollBottom, true)
            this.PostExpenseAuditGroupPageItems();
        }
    },
    destroyed(){
        window.removeEventListener('scroll', this.scrollBottomExamine, true);
        window.removeEventListener('scroll', this.scrollBottom, true);
    }
}
</script>
<style>
*{
    margin: 0;
    padding: 0;
}EntryDetails
.expenseMobile-all{
    background-color: #fff;
    color: #555;
}
.expenseMobile-all .el-dialog__header{
    display: none;
}
.expenseMobile-all .el-dialog__body{
    padding: 10px;
}
.daily-box{
    padding: 8px;
    padding-top: 93px;
}
.examine-box{
    padding: 8px;
    padding-top: 93px;
}
.card-description{
    font-weight: 600;
}
.daily-card{
    padding: 8px;
    box-shadow: 0 0 10px #0005;
    border-radius:5px ;
    font-size: 14px;
    margin-bottom: 10px;
}
.examine-card{
    padding: 8px;
    box-shadow: 0 0 10px #0005;
    border-radius:5px ;
    font-size: 14px;
    margin-bottom: 10px;
}
.card-date{
    margin: 5px 0;
}
.card-expense{
    display: flex;
    justify-content: space-between;
}
.card-btn{
    margin-top: 10px;
    text-align: right;
}

.expenseMobile-top{
    position: fixed;
    top: 0;
    background-color: #fff;
    width: 100%;
    text-align: center;
    border-bottom: 1px solid #ede8e8;
}
.expenseMobile-title{
    margin: 10px;
    margin-bottom:0px;
    font-size: 17px;
    font-weight:600 ;
}
.expenseMobile-search{
    padding: 8px;
    display: flex;
}
.expenseMobile-tabtn{
    position: fixed;
    bottom: 0;
    background-color: #fff;
    width: 100%;
    padding: 8px;
    text-align: center;
    border-top: 1px solid #ede8e8;
}
.tabtn-box{
    display: flex;
    justify-content: space-around;
}
.tabtn-box div{
    width: 15%;
}
.tabtn-box div div{
    width: 100%;
}
.expenseMobile-all .dialog-title{
    font-weight: 600;
    margin-bottom: 10px;
}
.expenseMobile-all .dialog-info{
    margin-bottom: 10px;
}
.dialog-info-zj{
    margin: 5px 0;
}
.dialog-info-sh{
    display: flex;
    flex-wrap: wrap;
    justify-content:space-between;
}
.dialog-info-sh div{
    width: 50%;
    margin-bottom: 5px;
}
.info-sl{
    margin: 5px 0;
    display: flex;
    justify-content: space-between;
    flex-wrap:wrap ;
}
.dialog-list-li{
    padding: 5px 0;
    border-top: 1px dotted #afadad;
}
.dialog-list-li:last-child{
    border-bottom: 1px dotted #afadad;
}
.expenseMobile-all .dialog-list{
    max-height: 240px;
    overflow: auto;
}
.expenseMobile-zwgd{
    font-size: 14px;
    font-weight: 600;
    text-align: center;
}
.examine-card-operator{
    display: flex;
    justify-content: space-between;
}
.examine-card-btn{
    text-align: right;
}
.examine-card-date{
    margin: 5px 0;
}
.examine-card-operator{
    margin: 5px 0;
}
.examine-card-name{
    font-weight: 600;
}
.my-select .el-select-dropdown__item {
    width: 350px;
    display: inline-block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.my-select .el-select-dropdown__list {
    width: 350px;
}
.expenseMobile-search .el-input-group__append{
    padding: 0px 20px
}
</style>