// pages/index/components/homepage/homepage.js
const app = getApp()
const $request = require('../../../../utils/request.js');
const $util = require('../../../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageLoading: false,
        customerId: null,
        // 我发布的需求----
        // item行加载
        itemLoading: false,
        // 数据是否加载完成
        isFinished: false,
        listData: [],
        page: 1,
        size: 10,
        total: 0,
        // 是否加载数据，true加载，false不加载
        onRefresh: true,
        // 顶部数据
        customer: {},
        // 被关注
        beCollectDemand: 0,
        // 关注
        collectDemand: 0,
        // 发布需求
        postDemand: 0,
        // 被浏览量
        totalPageView: 0,
        // 是他人主业还是个人主业
        isOther: false,
        // 登录用户ID
        loginCustomerId:"",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const businessCommunicationCustomer = wx.getStorageSync('businessCommunicationCustomer') || null;
        if (businessCommunicationCustomer) {
            this.setData({
                loginCustomerId: businessCommunicationCustomer.customerId
            })
        }
        const eventChannel = this.getOpenerEventChannel()
        // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
        eventChannel.on('customerid', data => {
            console.log(data, 'customerid')
            this.setData({
                customerId: data.customerid,
                pageLoading: true,
                isOther: data.customerid != app.globalData.customerId
            })
            // 记录浏览量
            if (this.data.isOther) {
                this.saveHomeCollect();
            }
            this.getListData();
        })
    },
    // 进入详情
    goDetails(e) {
        wx.navigateTo({
            url: '/pages/index/components/listDetails/Details?businessCommunicationDemandId=' + e.detail,
        })
    },
    saveHomeCollect() {
        $request.post('/statisticsBusinessCommunicationDemand/saveHomeCollect.action',
            { customerId: this.data.customerId }
        ).then(res => { }).catch(err => { console.log(err) })
    },
    // 底部按钮点击
    footBtnClick() {
        console.log(`点击了底部按钮`)
        console.log(this.data.isOther)
        // 前往聊一聊
        if (this.data.isOther) {
            console.log(this.data.customer)
            let customer = this.data.customer;
            wx.navigateTo({
                url: "/pages/msgModule/wechat2/wechat2",
                success: function (res) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('customerid', { customerid: customer.customerId, chatHeads: customer.chatHeads })
                }
            })
        }
        // 前往发布需求
        else {
            wx.navigateTo({
                url: '/pages/releaseModule/index',
            })
        }
    },
    getListData() {
        let { page, size, customerId } = this.data;
        $request.get('/businessCommunicationDemand/getDemandHomePage.action',
            { page, size, customerId }
        ).then(res => {
            this.setData({
                pageLoading: false,
            })
            console.log(res)
            let tempListData = this.data.listData;
            if (res.status == 0) {
                let datas = res.data;
                console.log(datas, 'datas')
                // 先push数据
                tempListData.push(...datas.demandList);
                tempListData.forEach(el => {
                    el.createdOn = $util.formatTime(new Date(el.createdOn), true);
                    el.createdOn = el.createdOn.replaceAll('/', '-');
                    if (typeof el.type == 'string') {
                        el.type = el.type.split(',');
                    }
                })
                // datas.businessCommunicationCustomer.serviceProject = "公司注册，注销变更，银行开户，代理记账，园区合作，招募合作伙伴"
                // 设置总数
                this.setData({
                    listData: tempListData,
                    total: datas.total,
                    customer: datas.businessCommunicationCustomer,
                    beCollectDemand: datas.beCollectDemand,
                    collectDemand: datas.collectDemand,
                    postDemand: datas.postDemand,
                    totalPageView: datas.totalPageView,
                })
                // 如果数据大于了返回的总数
                if (tempListData.length >= this.data.total) {
                    // 停止累加数据
                    this.setData({
                        onRefresh: false,
                        itemLoading: false,
                        isFinished: true,
                    })
                } else {
                    this.setData({
                        onRefresh: true,
                        itemLoading: false,
                        isFinished: false,
                        page: page + 1
                    })
                }
                console.log('-------------个人主页 beg-------------------')
                console.log(this.data.listData);
                console.log('-------------个人主页 end-------------------')
            }
        }).catch(error => {
            console.log(error, 'error appletLogin')
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.onRefresh) {
            this.setData({
                itemLoading: true
            })
            this.getListData();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        if (this.data.isOther) {
            return {
                title: "发现了一个适合你的客户需求！推荐你一起来关注~",
                imageUrl: '/images/home/ShareApp-homePage2.png',
                path: 'pages/index/components/homepage/homepage'
            }
        } else {
            return {
                title: "分享给你我的需求信息！推荐你一起来关注~",
                imageUrl: '/images/home/ShareApp-homePage1.png',
                path: 'pages/index/components/homepage/homepage'
            }
        }
    }
})