// nova-tourism/components/collect/index.js
let Parse = getApp().Parse;
const company = getApp().globalData.company
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        statusBarHeight: 0,
        screenHeight: 0,
        customHeight: 0,
        bottomNavHeight: 0,
        contentHeight: 0,



        taps: [{
                url: 'https://file-cloud.fmode.cn/EbxZUK5lBI/20241230/r1kr6b044821198.png',
                tex: '望仙礼遇',
            },
            {
                url: 'https://file-cloud.fmode.cn/EbxZUK5lBI/20241230/r1on6t044840728.png',
                tex: '妆造旅拍',
            },
            {
                url: 'https://file-cloud.fmode.cn/EbxZUK5lBI/20241230/v16vb1044858389.png',
                tex: '望仙果蔬',
            },
            {
                url: 'https://file-cloud.fmode.cn/EbxZUK5lBI/20241230/dm2ol9044926231.png',
                tex: '房务服务',
            },
        ],
        imageUrls: ['https://img95.699pic.com/photo/60072/5330.jpg_wh860.jpg',
            'https://c-ssl.dtstatic.com/uploads/blog/201409/27/20140927230814_4QfVC.thumb.1000_0.jpeg',
            'https://ts1.cn.mm.bing.net/th/id/R-C.37f4077f8eaca918401b84b4ee23af7c?rik=7HYWC88a9q4sZw&riu=http%3a%2f%2fpic.616pic.com%2fphotoone%2f00%2f06%2f03%2f618e27ad7ce8e1124.jpg&ehk=jkc6iZWAg%2bAEjAk6%2br0VAAy1lfffqslT6n%2fMWl293Yc%3d&risl=&pid=ImgRaw&r=0'
        ],

        date_start: '',
        date_end: '',
        start: '',
        end: '',
        istoday: true,
        daysBetween: 1,
        currentTab: 0
    },
    lifetimes: {
        detached: function () {},
        attached: async function () {
            const systemInfo = wx.getSystemInfoSync();
            const statusBarHeight = systemInfo.statusBarHeight || 0;
            const screenHeight = systemInfo.screenHeight || 0;
            const custom = wx.getMenuButtonBoundingClientRect();
            const customHeight = custom.height + 10 + 2 || 0;
            const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;

            const contentHeight = (screenHeight - bottomNavHeight - 50 - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
            this.setData({
                statusBarHeight,
                screenHeight,
                customHeight,
                bottomNavHeight,
                contentHeight
            });
            this.gethomestar()
            this.getcurrentdate()
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        switchTab (e) {
            const index = e.currentTarget.dataset.index;
            this.setData({
              currentTab: index
            });
            console.log(this.data.currentTab);
          },
        //获取收藏店铺消息
        async gethomestar() {
            const currentUser = Parse.User.current();
            console.log(currentUser);
            let ShopStore = new Parse.Query('DramaShopCollect');
            ShopStore.equalTo('company', company);
            ShopStore.equalTo('user', currentUser.id);
            ShopStore.equalTo('isCollect', 'true');
            ShopStore.notEqualTo('isDeleted', "true");
            ShopStore.include('homestayStore');
            let store = await ShopStore.find();
            let storeList = store.map(item => item.toJSON());
            this.setData({
                storeList
            });

            console.log('123', this.data.storeList);
        },
        //点击取消
        async cancle(e) {
            const object = e.currentTarget.dataset.id
            const currentUser = Parse.User.current();
            let Collect = new Parse.Query('DramaShopCollect');
            Collect.equalTo('company', company);
            Collect.equalTo('user', currentUser.id);
            Collect.equalTo('isCollect', true);
            Collect.equalTo('homestayStore', object);
            Collect.notEqualTo('isDeleted', "true");
            let collect = await Collect.first();

            await this.changeiscollect(object)
            if (collect) {
                collect.set('isCollect', false)
                try {
                    let saveDate = await collect.save();
                    console.log(saveDate);
                    console.log("取消成功");
                } catch (error) {
                    console.error("保存数据时出现错误:", error);
                }
            }
        },
        // 点击收藏后把storeList中对应的isCollect变成true
        changeiscollect(objectId) {
            // 创建一个新的 storeList 数组，以确保视图更新
            const updatedStoreList = this.data.storeList.map(item => {
                if (item.homestayStore.objectId === objectId) {
                    return {
                        ...item,
                        isCollect: !item.isCollect // 切换 iscollect 的值
                    };
                }
                return item; // 返回未修改的项
            });

            // 更新 storeList
            this.setData({
                storeList: updatedStoreList
            });

            console.log('修改成功', objectId);
        },
        //转换日期
        formatDate(date) {
            date = new Date(date);
            return `${date.getMonth() + 1}月${date.getDate()}日`;
        },
        //获取今日明日日期
        getcurrentdate() {
            const today = new Date();
            const tomorrow = new Date();
            tomorrow.setDate(today.getDate() + 1);

            this.setData({
                date_start: this.formatDate(today),
                date_end: this.formatDate(tomorrow),
                start: today,
                end: tomorrow
            });
            console.log(this.data.start, this.data.end);
        },
        //去收藏商铺
        gourl(e) {
            const url = e.currentTarget.dataset.url;
            const id = e.currentTarget.dataset.id;

            // 构造要传递的信息
            const info = {
                objectId: id,
                date_start: this.data.date_start,
                date_end: this.data.date_end,
                daysBetween: this.data.daysBetween,
                istoday: this.data.istoday,
                start: this.data.start,
                end: this.data.end
            };

            // 将信息转为查询字符串
            const queryString = Object.keys(info)
                .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(info[key])}`)
                .join('&');

            // 使用查询字符串跳转
            wx.navigateTo({
                url: `${url}?${queryString}`,
            });
        },

        //去商品详细
        gourl2(e){
            wx.navigateTo({
                url: `../../pages/collect/collect-detail/index`,
            });
        }


    }
})