// nova-werun/pages/home/medal/index.js
const Parse = getApp().Parse;
const company = getApp().globalData.company;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //屏幕高度
        statusBarHeight: 0, // 状态栏高度
        screenHeight: 0, // 屏幕高度
        customHeight: 0, // 自定义导航栏高度（如小程序右上角胶囊按钮）
        bottomNavHeight: 0, // 底部导航栏高度
        contentHeight: 0, // 可用内容高度
        contentHeight2: 0,
        contentpadding: 0, //顶部padding高度

        userList: [],
        alreadyList: [],
        MedalList: [],
        show: false,
        title:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 计算
        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 contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
        const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
        this.setData({
            statusBarHeight,
            screenHeight,
            customHeight,
            bottomNavHeight,
            contentpadding,
            contentHeight
        });
        this.getuser()
        this.alreadyMedal()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    async getuser() {
        const currentUser = Parse.User.current();
        let userquery = new Parse.Query('_User');
        userquery.equalTo('company', company);
        userquery.equalTo('objectId', currentUser.id);
        userquery.notEqualTo('isDeleted', true)
        let user = await userquery.find();
        let userList = user.map(item => item.toJSON());
        this.setData({
            userList
        })
        console.log(this.data.userList);
    },
    // 已获得勋章
    async alreadyMedal() {
        const currentUser = Parse.User.current();
        let MedalLogquery = new Parse.Query('MedalLog');
        MedalLogquery.equalTo('company', company);
        MedalLogquery.equalTo('user', currentUser.id);
        MedalLogquery.notEqualTo('isDeleted', true);
        MedalLogquery.include('medal');

        let MedalLog = await MedalLogquery.find();
        let alreadyList = MedalLog.map(item => {
            let itemData = item.toJSON();
            // 格式化 createdAt 为 YYYY-M-D
            const createdAt = new Date(itemData.createdAt);
            const formattedDate = `${createdAt.getFullYear()}-${createdAt.getMonth() + 1}-${createdAt.getDate()}`;
            itemData.createdAt = formattedDate; // 将格式化后的日期赋值回 createdAt
            return itemData;
        });

        this.setData({
            alreadyList
        });
        console.log('alreadyList', this.data.alreadyList);

        // 获取 MedalList，并剔除 alreadyList 中的勋章
        await this.lossMedal(alreadyList);
    },

    // 为获得勋章
    async lossMedal(alreadyList) {
        let Medalquery = new Parse.Query('Medal');
        Medalquery.equalTo('company', company);
        Medalquery.notEqualTo('isDeleted', true);
        Medalquery.ascending('order');

        let r = await Medalquery.find();
        let MedalList = r.map(item => item.toJSON());

        // 创建一个集合存储 alreadyList 中的 medal.objectId
        const alreadyMedalIds = new Set(alreadyList.map(item => item.medal.objectId));

        // 从 MedalList 中剔除已获得的勋章
        MedalList = MedalList.filter(item => !alreadyMedalIds.has(item.objectId));

        this.setData({
            MedalList
        });
        console.log('MedalList', this.data.MedalList);
    },
    showPopup(e) {
        const objectId = e.currentTarget.dataset.id
        const type = e.currentTarget.dataset.type
        if(type=='alreadyList'){
            this.data.alreadyList.forEach((item)=>{
                if(item.objectId==objectId){
                    this.setData({
                        title:item.medal.title
                    })
                }
            })
            console.log(this.data.title);
        }else{
            this.data.MedalList.forEach((item)=>{
                if(item.objectId==objectId){
                    this.setData({
                        title:item.title
                    })
                }
            })
            console.log(this.data.title);
        }
        
        this.setData({ show: true });
      },
    
      onClose() {
        this.setData({ show: false });
      },
})