<template>
    <div class="main_box">
        <navbar title="潜客登记" special @handleClickLeft="handleGoBefore"></navbar>
        <div class="wrapper">
            <div class="wrapper_main">
                <van-tabs @change="tabChange()" v-model="activeTab" class="content" type="line" color="#4682ff"
                    title-active-color="#4682ff" line-width="80">
                    <!-- <van-tab title="企业" :disabled="isPrivate">
                        <div class="search-input">
                            <van-search type="text" :left-icon="null" v-model="searchValue" placeholder="输入企业名/证件号/手机号"
                                @clear="handleSearch()" @keyup.enter.native="handleSearch()" />
                            <div class="search_btn">
                                <van-icon name="search" @click="handleSearch"></van-icon>
                            </div>
                        </div>
                        <van-pull-refresh v-model="firmRefreshing" @refresh="onRefresh">
                            <van-list v-model="loading" :finished="finished" finished-text="没有更多了" :immediate-check="false"
                                @load="onLoad">
                                <div class="item" v-for="(item, index) in list" :key="index">
                                    <div class="item_top">
                                        <div class="item_top_left">
                                            <div class="icon_box">
                                                <img src="../../images/part_one/icon_company.png" alt="">
                                            </div>
                                            <div class="item_name">{{ item.custName }}</div>
                                        </div>
                                        <div class="special_row">
                                            <div class="item_viewDetailnew mgrt5" @click="handleViewFirmDetail(item)">查看</div>
                                            <div class="item_viewDetailnew" @click="handleAppointment(item)">预约</div>
                                        </div>
                                    </div>
                                    <div class="item_main">
                                        <div class="main_item"><span class="mian_title">法人代表：</span>{{ item.lpName }}</div>
                                        <div class="main_item"><span class="mian_title">证件号：</span>{{ item.custIdc
                                        }}</div>
                                        <div class="main_item"><span class="mian_title">联系方式：</span>{{ item.custPhone }}
                                        </div>
                                    </div>
                                </div>
                            </van-list>
                        </van-pull-refresh>

                    </van-tab> -->
                    <!-- <van-tab title="个人" :disabled="isPublic">
                        <div class="search-input">
                            <van-search type="text" :left-icon="null" v-model="searchValue" placeholder="输入个人名称/证件号/手机号"
                                @clear="handleSearch()" @keyup.enter.native="handleSearch()" />
                            <div class="search_btn">
                                <van-icon name="search" @click="handleSearch"></van-icon>
                            </div>
                        </div>
                        <van-pull-refresh v-model="firmRefreshing" @refresh="onRefresh">
                            <van-list v-model="loading" :finished="finished" finished-text="没有更多了" :immediate-check="false"
                                @load="onLoad">
                                <div class="item" v-for="(item, index) in list" :key="index">
                                    <div class="item_top">
                                        <div class="item_top_left">
                                            <div :class="item.custGender == '男' ? 'userMan_box' : 'userWoMan_box'">
                                                <van-icon name="manager item_user"></van-icon>
                                            </div>
                                            <div class="item_name">{{ item.custName }}</div>
                                        </div>
                                        <div class="special_row">
                                            <div class="item_viewDetailnew mgrt5" @click="handleViewPersonDetail(item)">查看</div>
                                            <div class="item_viewDetailnew" @click="handleAppointment(item)">预约</div>
                                        </div>
                                    </div>
                                    <div class="item_main">
                                        <div class="main_item"><span class="mian_title">身份证号：</span>{{ item.custIdc }}</div>
                                        <div class="main_item"><span class="mian_title">联系方式：</span>{{ item.custPhone }}
                                        </div>

                                    </div>
                                </div>
                            </van-list>
                        </van-pull-refresh>

                    </van-tab> -->
                    <van-tab title="商户" :disabled="isPublic">
                        <div class="search-input">
                            <van-search type="text" :left-icon="null" v-model="searchValue" placeholder="输入商户名"
                                @clear="handleSearch()" @keyup.enter.native="handleSearch()" />
                            <div class="search_btn">
                                <van-icon name="search" @click="handleSearch"></van-icon>
                            </div>
                        </div>
                        <van-pull-refresh v-model="firmRefreshing" @refresh="onRefresh">
                            <van-list v-model="loading" :finished="finished" finished-text="没有更多了" :immediate-check="false"
                                @load="onLoad">
                                <div class="item" v-for="(item, index) in list" :key="index">
                                    <div class="item_top">
                                        <div class="item_top_left">
                                            <div class="userMan_box">
                                                <img src="../../images/home/sh.png" alt=""
                                                    style="width: 25px;height: 25px;">
                                            </div>
                                            <div class="item_name">{{ item.custName }}</div>
                                        </div>
                                        <div class="special_row">
                                            <div class="item_viewDetailnew mgrt5" @click="handleViewCommercialDetail(item)">查看</div>
                                            <!-- <div class="item_viewDetailnew" @click="handleAppointment(item)">预约</div> -->
                                        </div>
                                    </div>
                                    <div class="item_main">
                                        <!-- <div class="main_item"><span class="mian_title">经营者：</span>{{ item.lpName }}
                                        </div>
                                        <div class="main_item"><span class="mian_title">证件号：</span>{{ item.custIdc }}</div>

                                        <div class="main_item"><span class="mian_title">联系方式：</span>{{ item.custPhone }}
                                        </div> -->
                                        <div class="main_item"><span class="mian_title">建档状态：</span>{{ item.regStatus?'是':'否' }}</div>
                                    </div>
                                </div>
                            </van-list>
                        </van-pull-refresh>


                    </van-tab>
                </van-tabs>

            </div>

        </div>
        <div class="footer">
            <div class="fotter_btn" @click="handleAdd">
                <van-icon name="plus"></van-icon>
            </div>
        </div>
    </div>
</template>
<script>
import navbar from '../../components/navbar/navbar.vue';
import { custLeadList } from "@/service/api/filing";
import {
    Tab,
    Tabs,
    Icon,
    Button,
    Search,
    List,
    PullRefresh,
    Toast
} from 'vant';
export default {
    data() {
        return {
            activeTab: 0,
            searchValue: '',
            list: [

            ],
            personList: [

            ],
            commerList: [

            ],
            pageNum: 1,
            pageSize: 5,
            custPattern: '2',
            firmRefreshing: false,
            loading: false,
            finished: false,
            currentPage: 1,
            isHeadAdmin: false,
            isPublic: false,
            isPrivate: false,
            isHeadOps: false,
            isBranchAdmin: false,
            isOutletAdmin: false,
            isCommonManager: false,
        }
    },
    components: {
        navbar: navbar,
        VanTab: Tab,
        VanTabs: Tabs,
        VanIcon: Icon,
        VanButton: Button,
        VanSearch: Search,
        VanList: List,
        VanPullRefresh: PullRefresh,
        VanToast: Toast,
    },
    created() {
        let userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
        if (userInfo.roles.includes('headAdmin')) {
            this.isHeadAdmin = true
        } else if (userInfo.roles.includes('headPublic')) {
            this.isPublic = true
        } else if (userInfo.roles.includes('headPrivate')) {
            this.isPrivate = true
            this.custPattern = '0'
        } else if (userInfo.roles.includes('headOps')) {
            this.isHeadOps = true
        } else if (userInfo.roles.includes('branchAdmin')) {
            this.isBranchAdmin = true
        } else if (userInfo.roles.includes('outletAdmin')) {
            this.isOutletAdmin = true
        } else if (userInfo.roles.includes('commonManager')) {
            this.isCommonManager = true
        }
        // let activeTab = sessionStorage.getItem('activeTab')
        // if (activeTab) {
        //     this.activeTab = Number(activeTab);
        // }
        // if (activeTab == 0) {
        //     this.custPattern = '2'
        // } else if (activeTab == 1) {
        //     this.custPattern = '0'
        // } else if (activeTab == 2) {
        //     this.custPattern = '1'
        // }
        this.custPattern = '1'
        this.initList();
    },
    methods: {
        async initList() {

            if (this.currentPage === 1) {
                this.list = [];
            }
            const params = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                custType: this.custPattern,
                keyword: this.searchValue
            };
            const res = await custLeadList(params)
            console.log(res, 'ressss')
            
            if (res.code == 200) {
                sessionStorage.removeItem('activeTab')
                if (this.firmRefreshing) {
                    this.firmRefreshing = false;
                }
                this.loading = false;
                if (res.rows) {
                    
                    this.list = params.pageNum == "1" ? res.rows : [...this.list, ...res.rows];
                    
                    if (this.list.length == res.total) {
                        this.finished = true;
                    }
                    this.currentPage++;
                } else {
                    this.list = []
                    this.finished = true;
                }
            }
        },
        tabChange() {
            
            if (this.activeTab == 0) {
                this.custPattern = '2'
            } else if (this.activeTab == 1) {
                this.custPattern = '0'
            } else if (this.activeTab == 2) {
                this.custPattern = '1'
            }
            this.searchValue = ''
            this.currentPage = 1;
            this.pageNum = 1;
            this.finished=false
            this.initList();

        },
        //下拉刷新
        onRefresh() {
            
            this.list = [];
            this.finished = false; //解除上拉加载之后数据不能再次上拉
            this.currentPage = 1;
            this.pageNum = this.currentPage;
            this.initList();
        },
        //上拉加载
        onLoad() {
            this.pageNum = this.currentPage + "";
            this.initList();
        },
        handleSearch() {
            this.initList();
        },
        handleViewFirmDetail(item) {
            sessionStorage.setItem('custId', item.id);
            sessionStorage.setItem("activeTab", this.activeTab);
            this.$router.push('/filingDetail');
        },
        handleViewPersonDetail(item) {
            sessionStorage.setItem('custId', item.id);
            sessionStorage.setItem("activeTab", this.activeTab);
            this.$router.push('/filingDetail');
        },
        handleViewCommercialDetail(item) {
            sessionStorage.setItem('custId', item.id);
            // sessionStorage.setItem("activeTab", this.activeTab);
            this.$router.push('/filingqkDetail');
        },
        async handleAppointment(item){
            let custIdc="";
            if(item.custType == '0'){
                custIdc=item.custIdc;
            }else {
                custIdc=item.socialCreditCode
            }
            let params={
                custIdc:custIdc,
                custName:item.custName,
                custType:item.custType,
            }
            const res = await appointmentCust(params)
            if (res.code == 200) {
                Toast.success('预约成功');
            }
        },
        handleAdd() {
            this.$router.push('/addFilingqk');
        },
        handleGoBefore(){
            this.$router.push("/dashboard/home");
        }
    }
}
</script>
<style lang="less" scoped>
.wrapper {
    padding-top: 46px;
    min-height: 100vh;
    font-size: 16px;
    padding-bottom: 60px;
}

.search-input {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 15px;
}

.search_btn {
    background-color: rgb(24, 144, 255);
    border-radius: 4px;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 40px;
    height: 40px;
    color: #fff;
    font-size: 20px;
}

/deep/ .van-search {
    width: 100%;
    padding: 5px 8px;
}

.tot_box {
    padding: 10px 15px 5px 15px;

}

.red_num {
    color: red;
    display: inline-block;
    padding: 0 10px;
}

.item {
    margin: 10px 0;
    background-color: #fff;
    padding: 15px 20px;
}

.item_top {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.item_top_left {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    width: calc(100% - 100px)
}

.item_name {
    font-size: 18px;
    font-weight: 600;
    margin-left: 10px;
    display: flex;
    flex-wrap: wrap;
    word-break: break-all;
}

.item_viewDetail {
    background-color: rgba(233, 241, 253);
    color: rgb(35, 114, 235);
    padding: 10px 15px;
    border-radius: 5px;
    width: 30%;
}
.special_row{
    display: flex;
    flex-direction: row;
    align-items: center;
}
.item_viewDetailnew {
    background-color: rgba(233, 241, 253);
    color: rgb(35, 114, 235);
    padding: 5px 5px;
    border-radius: 5px;
    width: 44px;
}
.mgrt5{
    margin-right: 5px;
}

.icon_box {
    background-color: rgb(35, 114, 235);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 5px 5px;
}

.item_main {
    margin-top: 10px;
}

.main_item {
    font-size: 16px;
    line-height: 25px;
    word-break: break-all;
}

.mian_title {
    color: rgb(140, 140, 140);
}

.item_user {
    font-size: 25px;
    color: #fff;
}

.userMan_box {
    background-color: rgb(35, 114, 235);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 5px 5px;
}

.userWoMan_box {
    background-color: rgb(252, 123, 80);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 5px 5px;
}

.main_box {
    position: relative;
}

.footer {
    position: fixed;
    top: 600px;
    left: 80%;

}

.fotter_btn {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background-color: rgb(70, 130, 255);
    font-size: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
}
</style>