Page({
    data: {
        subscribeList: [
            { id: 1, name: '订单付款通知', status: 'unsubscribed' },
            { id: 2, name: '订单续费通知', status: 'unsubscribed' },
            { id: 3, name: '欠费通知', status: 'unsubscribed' },
            { id: 4, name: '新注册用户提醒', status: 'unsubscribed' },
            { id: 5, name: '工单状态通知', status: 'unsubscribed' },
            { id: 6, name: '工单完成通知', status: 'unsubscribed' },
            { id: 7, name: '工单派工通知', status: 'unsubscribed' },
            { id: 8, name: '设备告警通知', status: 'unsubscribed' },
            { id: 9, name: '订单重新指派提醒', status: 'unsubscribed' },
            { id: 10, name: '新订单提醒', status: 'unsubscribed' },
            { id: 11, name: '设备故障通知', status: 'unsubscribed' },
        ]
    },

    onLoad() {
        const tmplIds = {
            1: '5yPg-WOoP9-9ZU1fHjC4zg1KNaPWb76K87JzzKb58f0',
            2: '2BX7Zh5ccLzmHvvbCHuPWiaoZQyDCGfziCruxUct_EU',
            3: 'eEetEKoNpqXk_EY0xqMt22_Xd7NFEyiODY6j0t3_A24',
            4: 're34uubgvrwJLaiM3LhQEmvxrRxoNcJbo7b8gcbxx44',
            5: '_s7GcsGNqapbnlLAJ5lUFexCEAx-dl4RD-DwwL9QqC4',
            6: 'CWtF10H3syth9rdUaGr-4XojSa8TJMflb2z7zStw384',
            7: 'qLYGETdlX5pR8WvTs8v4g4zlBOhA04z46KS_Q_yesr4',
            8: 'T87KsBIrVrjgO4VETEOpIn4c4-bsxOTpzM6lR0ghduo',
            9: 'q3Aa07wgYPgB23mu4JOuSzgFoiYhouUcw-NhV5CNuSA',
            10: 'pUkdPwh7jWLjCWWv2zyz7I086xNO_GoaJQ6A-cYuGVg',
            11: 'VXRbeTG6gPPOrxTSwF_da8jzqH0UVHxQykYAH0XH51s'  // 促销活动通知模板ID
        };

        wx.getSetting({
            withSubscriptions: true,
            success: (res) => {
                if (res.subscriptionsSetting && res.subscriptionsSetting.mainSwitch) {
                    const itemSettings = res.subscriptionsSetting.itemSettings || {};
                    this.setData({
                        subscribeList: this.data.subscribeList.map(item => ({
                            ...item,
                            status: itemSettings[tmplIds[item.id]] === 'accept' ? 'subscribed' : 
                                   itemSettings[tmplIds[item.id]] === 'reject' ? 'rejected' : 'unsubscribed'
                        }))
                    });
                }
            }
        });
    },

    handleSubscribe(e) {
        const { id } = e.currentTarget.dataset;
        const item = this.data.subscribeList.find(item => item.id === id);
        
        if (item.status === 'subscribed') {
            return; // 已订阅的不做处理
        }

        // 如果是已拒绝状态，提示用户去设置开启权限
        if (item.status === 'rejected') {
            wx.showModal({
                title: '提示',
                content: '您之前拒绝了订阅，需要前往设置重新开启',
                confirmText: '去设置',
                success: (res) => {
                    if (res.confirm) {
                        wx.openSetting({
                            success: (res) => {
                                if (res.authSetting['scope.subscribeMessage']) {
                                    // 用户开启了权限，重新尝试订阅
                                    this.requestSubscribeMessage(id);
                                }
                            }
                        });
                    }
                }
            });
        } else {
            // 未订阅状态直接触发订阅
            this.requestSubscribeMessage(id);
        }
    },

    requestSubscribeMessage(id) {
        const tmplIds = {
            1: '5yPg-WOoP9-9ZU1fHjC4zg1KNaPWb76K87JzzKb58f0',
            2: '2BX7Zh5ccLzmHvvbCHuPWiaoZQyDCGfziCruxUct_EU',
            3: 'eEetEKoNpqXk_EY0xqMt22_Xd7NFEyiODY6j0t3_A24',
            4: 're34uubgvrwJLaiM3LhQEmvxrRxoNcJbo7b8gcbxx44',
            5: '_s7GcsGNqapbnlLAJ5lUFexCEAx-dl4RD-DwwL9QqC4',
            6: 'CWtF10H3syth9rdUaGr-4XojSa8TJMflb2z7zStw384',
            7: 'qLYGETdlX5pR8WvTs8v4g4zlBOhA04z46KS_Q_yesr4',
            8: 'T87KsBIrVrjgO4VETEOpIn4c4-bsxOTpzM6lR0ghduo',
            9: 'q3Aa07wgYPgB23mu4JOuSzgFoiYhouUcw-NhV5CNuSA',
            10: 'pUkdPwh7jWLjCWWv2zyz7I086xNO_GoaJQ6A-cYuGVg',
            11: 'VXRbeTG6gPPOrxTSwF_da8jzqH0UVHxQykYAH0XH51s'  // 促销活动通知模板ID
        };

        wx.requestSubscribeMessage({
            tmplIds: [tmplIds[id]],
            success: (res) => {
                if (res[tmplIds[id]] === 'accept') {
                    this.setData({
                        subscribeList: this.data.subscribeList.map(item => 
                            item.id === id ? {...item, status: 'subscribed'} : item
                        )
                    });
                    wx.showToast({ title: '订阅成功' });
                } else {
                    this.setData({
                        subscribeList: this.data.subscribeList.map(item => 
                            item.id === id ? {...item, status: 'rejected'} : item
                        )
                    });
                    wx.showToast({ title: '订阅被拒绝', icon: 'none' });
                }
            },
            fail: (err) => {
                wx.showToast({ title: '订阅失败', icon: 'none' });
            }
        });
    }
});