// components/listItem/listItem.js
const app = getApp();
const $request = require('../../utils/request.js');
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        // 数据
        item: {
            type: Object,
        },
        // index => 首页 detatil => 详情  homepage => 个人主页
        pageStatus: {
            type: String,
            value: 'index',
        },
        // 是否收藏
        isCollect: {
            type: Boolean,
            value: false,
        },
        // 是否显示收藏
        isShowCollect: {
            type: Boolean,
            value: false,
        },
        // 是否其他查看主页---用于是否显示聊一聊
        isOther: {
            type: Boolean,
            value: true,
        },
        // 类名
        listClass: {
            type: String,
            value: "",
        },
        customerId: {
            type: Number,
            value: null,
        },
    },
    /**
     * 组件的初始数据
     */
    data: {
    },

    /**
     * 组件的方法列表
     */
    methods: {
        getCustomerId() {
            return 123
        },
        // 进入详情
        goDetails(e) {
            if (this.data.listClass == 'disabled-model-view') {
                return
            }
            let value = e.currentTarget.dataset.value;
            this.triggerEvent('getItem', value);
        },
        // 进入个人主页
        goHomePage(e) {
            let customerid = e.currentTarget.dataset.customerid;
            wx.navigateTo({
                url: '/pages/index/components/homepage/homepage',
                success: function (res) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('customerid', { customerid })
                }
            })
        },
        // 收藏
        productCollect(e) {
            this.triggerEvent('changeCollect');
        },
        // 聊一聊
        goToChat(e) {
            if (!app.globalData.customerId) {
                app.goLogin();
                return
            }
            console.log(`点击了聊一聊`)
            let item = e.currentTarget.dataset.item;
            let businessCommunicationDemandId = item.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) })
            wx.navigateTo({
                url: "/pages/msgModule/wechat2/wechat2",
                success: function (res) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('customerid', {
                        customerid: item.customerId,
                        businessCommunicationDemandId,
                        chatHeads: item.chatHeads,
                        info: item.info
                    })
                }
            })
        },
    }
})
