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

    /**
     * 页面的初始数据
     */
    data: {
        // 页面Loading
        pageLoading: true,
        customerId: null,
        // 路由接受的参数
        currentData: {},
        // 感兴趣的需求----
        // item行加载
        itemLoading: false,
        // 数据是否加载完成
        isFinished: false,
        listData: [],
        page: 1,
        size: 10,
        total: 0,
        // 是否加载数据，true加载，false不加载
        onRefresh: true,
        currentIndex: 0,
        // 当前登录器用户登录ID
        userCustomerId: null,
        businessCommunicationDemandId: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(option) {
        // console.log(option,`option触发--11`)
        this.setData({
            businessCommunicationDemandId: option.businessCommunicationDemandId
        })
    },
    onShow() {
        this.setData({
            pageLoading: true,
        })
        const businessCommunicationCustomer = wx.getStorageSync('businessCommunicationCustomer') || null;
        if (businessCommunicationCustomer) {
            this.setData({
                userCustomerId: businessCommunicationCustomer.customerId
            })
        }
        // 获取当前数据
        this.getCurrentData();
        if (app.globalData.customerId) {
            this.setData({
                customerId: app.globalData.customerId
            })
            // this.getIsCollect();
        } else {
            this.getListData();
        }
        // 设置当前访问量
        this.setDemandCollect();  
    },
    setDemandCollect(){
        let businessCommunicationDemandId = this.data.businessCommunicationDemandId;
        $request.post('/statisticsBusinessCommunicationDemand/saveDemandCollect.action',
            { businessCommunicationDemandId }
        ).then(res => { }).catch(err => { console.log(err) })
        $request.post('/statisticsBusinessCommunicationDemand/demandPageView.action',
            { businessCommunicationDemandId,customerId: app.globalData.customerId }
        ).then(res => { }).catch(err => { console.log(err) })
    },
    // 热门详情点击
    goDetails(e) {
        this.setData({
            currentData: null,
            type: [],
            pageLoading: true,
            listData: [],
            page: 1,
            onRefresh: true,
            isCollect: false,
            businessCommunicationDemandId: e.detail
        })
        this.getCurrentData();
        wx.pageScrollTo({
            scrollTop: 0,
        });
    },
    getCurrentData() {
        let { businessCommunicationDemandId } = this.data;
        $request.get('/businessCommunicationDemand/getDemandByDemandId.action',
            { demandId: businessCommunicationDemandId }
        ).then(res => {
            if (res.status == 0) {
                let listDetail = res.data;
                listDetail.createdOn = $util.formatTime(new Date(listDetail.createdOn), true);
                listDetail.createdOn = listDetail.createdOn.replaceAll('/', '-');
                if (typeof listDetail.type == 'string') {
                    listDetail.type = listDetail.type.split(',');
                    // console.log(listDetail.type)
                }
                this.setData({
                    currentData: listDetail,
                    type: listDetail.type,
                })
                this.getIsCollect();
            }
        }).catch(err => {
            console.log(err, 'error appletLogin')
        })
    },
    // 判断是否收藏
    getIsCollect() {
        let { currentData, customerId } = this.data;
        $request.get('/businessCommunicationCollect/booleanIsCollect.action',
            { customerId, businessCommunicationDemandId: currentData.businessCommunicationDemandId }
        ).then(res => {
            if (res.status == 0) {
                this.setData({
                    isCollect: res.data == 0 ? false : true,
                    pageLoading: true,
                })
            }
            if(this.data.currentData.customerId == this.data.userCustomerId){
                this.setData({
                    pageLoading: false,
                })
            }else{
                this.getListData();
            }
        }).catch(err => {
            console.log(err, 'error appletLogin')
        })
    },
    // 切换收藏
    changeCollect() {
        let { currentData, customerId, isCollect } = this.data;
        if (!customerId) {
            app.goLogin();
            return
        }
        let url = "";
        let msg = "";
        // 如果已收藏就取消收藏 else 反之
        if (isCollect) {
            url = "/businessCommunicationCollect/deleteCollect.action",
                msg = "取消收藏成功"
        } else {
            url = "/businessCommunicationCollect/saveCollect.action",
                msg = "收藏成功"
        }
        wx.showLoading({
            title: '操作中...',
            mask: true
        })
        $request.post(url,
            { customerId, businessCommunicationDemandId: currentData.businessCommunicationDemandId }
        ).then(res => {
            wx.hideLoading()
            if (res.status == 0) {
                this.setData({
                    isCollect: !isCollect
                })
                wx.showToast({
                    title: msg,
                    icon: 'success',
                    duration: 2000
                })
            }

        }).catch(err => {
            console.log(error, 'error appletLogin')
        })
    },
    // 获取list数据
    getListData() {
        let { page, size, type, currentData } = this.data;
        if(typeof type == 'object'){
            type = type.join(',');
        }
        $request.get('/businessCommunicationDemand/getDemandByKeywordOrType.action',
            { page, size, type, isInterest: 1, demandId: currentData.businessCommunicationDemandId }
        ).then(res => {
            this.setData({
                pageLoading: false,
            })
            let tempListData = this.data.listData;
            if (res.status == 0) {
                let datas = res.data;
                // 先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(',');
                    }
                })
                // 设置总数
                this.setData({
                    listData: tempListData,
                    total: res.data.total,
                })
                // 如果数据大于了返回的总数
                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
                    })
                }
            }
        }).catch(error => {
            console.log(error, 'error appletLogin')
        })
    },
    /*
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.data.onRefresh) {
            this.setData({
                itemLoading: true
            })
            this.getListData();
        }
    },

    /**
   * 用户点击右上角分享
   */
    onShareAppMessage() {
        return {
            title: "发现了一个适合你的需求！推荐你一起来关注~",
            imageUrl: '/images/home/ShareApp-details.png',
            path: 'pages/index/components/listDetails/Details'
        }
    },
})