const app = getApp()
const db = wx.cloud.database()

Page({
    data: {
        myRooms: [],
        joinedRooms: [],
        userOpenId: ''
    },

    onLoad: function () {
        // 先从本地加载
        const myRooms = wx.getStorageSync('myRooms') || []
        this.setData({ myRooms })

        // 获取用户openid后加载完整数据
        this.getUserOpenId()
    },

    onShow: function () {
        // 每次显示页面时重新加载聚会列表
        if (this.data.userOpenId) {
            this.loadRooms()
        } else {
            // 先显示本地数据，异步加载完整数据
            this.getUserOpenId()
        }
    },

    async getUserOpenId() {
        try {
            // 调用云函数获取 openid
            const res = await wx.cloud.callFunction({
                name: 'getOpenId'
            })
            if (res.result && res.result.openid) {
                this.setData({ userOpenId: res.result.openid })
                this.loadRooms()
            }
        } catch (err) {
            console.error('获取openid失败', err)
        }
    },

    async loadRooms() {
        try {
            const userOpenId = this.data.userOpenId
            if (!userOpenId) return

            const _ = db.command

            // 查询我创建的聚会
            const myRoomsRes = await db.collection('rooms')
                .where({
                    _openid: userOpenId
                })
                .orderBy('createdAt', 'desc')
                .limit(5)
                .get()

            // 查询我参与的聚会（members中有我，但不是我创建的）
            const joinedRoomsRes = await db.collection('rooms')
                .where({
                    _openid: _.neq(userOpenId),
                    'members.openid': userOpenId
                })
                .orderBy('createdAt', 'desc')
                .limit(5)
                .get()

            // 转换数据格式
            const myRooms = myRoomsRes.data.map(room => ({
                roomId: room._id,
                name: room.name,
                meetTime: room.meetTime
            }))

            const joinedRooms = joinedRoomsRes.data.map(room => ({
                roomId: room._id,
                name: room.name,
                meetTime: room.meetTime
            }))

            // 更新到本地存储
            const allRooms = [...myRooms, ...joinedRooms]
            const uniqueRooms = this.getUniqueRooms(allRooms)
            wx.setStorageSync('roomList', uniqueRooms)
            wx.setStorageSync('myRooms', myRooms.slice(0, 2))

            this.setData({
                myRooms: myRooms.slice(0, 2),
                joinedRooms: joinedRooms.slice(0, 2)
            })
        } catch (err) {
            console.error('加载聚会列表失败', err)
        }
    },

    getUniqueRooms(rooms) {
        const unique = {}
        rooms.forEach(room => {
            unique[room.roomId] = room
        })
        // 使用 Object.keys + map 代替 Object.values
        return Object.keys(unique).map(key => unique[key])
    },

    onJoinRoom(e) {
        const roomId = e.currentTarget.dataset.roomid
        wx.navigateTo({
            url: `/pages/room/room?roomId=${roomId}&isCreator=true`
        })
    },

    onDeleteRoom(e) {
        const roomId = e.currentTarget.dataset.roomid

        wx.showModal({
            title: '删除聚会',
            content: '确定要删除这个聚会吗？',
            confirmText: '删除',
            confirmColor: '#ff4d4f',
            success: (res) => {
                if (res.confirm) {
                    this.deleteRoom(roomId)
                }
            }
        })
    },

    async deleteRoom(roomId) {
        wx.showLoading({ title: '删除中...' })

        try {
            // 调用云函数删除聚会
            const res = await wx.cloud.callFunction({
                name: 'deleteRoom',
                data: { roomId }
            })

            wx.hideLoading()

            if (res.result && res.result.success) {
                // 从本地存储中移除
                let myRooms = wx.getStorageSync('myRooms') || []
                myRooms = myRooms.filter(item => item.roomId !== roomId)
                wx.setStorageSync('myRooms', myRooms)

                let roomList = wx.getStorageSync('roomList') || []
                roomList = roomList.filter(item => item.roomId !== roomId)
                wx.setStorageSync('roomList', roomList)

                // 更新页面数据
                this.setData({
                    myRooms
                })

                wx.showToast({ title: '删除成功', icon: 'success' })
            } else {
                wx.showToast({ title: res.result?.msg || '删除失败', icon: 'none' })
            }
        } catch (err) {
            wx.hideLoading()
            console.error('删除聚会失败', err)
            wx.showToast({ title: '网络错误', icon: 'none' })
        }
    },

    onCreateRoom() {
        wx.navigateTo({
            url: '/pages/create-room/create-room'
        })
    },

    createRoom(name, keywords, meetTime) {
        wx.showLoading({ title: '创建中...', mask: true })
        wx.cloud.callFunction({
            name: 'createRoom',
            data: {
                name,
                meetTime,
                keywords,
                requirements: ''
            },
            success: res => {
                wx.hideLoading()
                if (res.result.success) {
                    const roomId = res.result.roomId

                    // 保存到本地存储
                    let myRooms = wx.getStorageSync('myRooms') || []
                    myRooms = myRooms.filter(item => item.roomId !== roomId)
                    myRooms.unshift({
                        roomId,
                        name,
                        meetTime,
                        keywords
                    })
                    if (myRooms.length > 2) {
                        myRooms = myRooms.slice(0, 2)
                    }
                    wx.setStorageSync('myRooms', myRooms)

                    // 更新全局roomList
                    let roomList = wx.getStorageSync('roomList') || []
                    roomList = roomList.filter(item => item.roomId !== roomId)
                    roomList.unshift({
                        roomId,
                        name,
                        meetTime,
                        keywords
                    })
                    if (roomList.length > 5) {
                        roomList = roomList.slice(0, 5)
                    }
                    wx.setStorageSync('roomList', roomList)

                    wx.navigateTo({
                        url: `/pages/room/room?roomId=${roomId}&isCreator=true`
                    })
                } else {
                    wx.showToast({ title: res.result.msg || '创建失败', icon: 'none' })
                }
            },
            fail: err => {
                wx.hideLoading()
                console.error('云函数调用失败', err)
                wx.showToast({ title: '网络错误', icon: 'none' })
            }
        })
    }
})


