// package_first/pages/volunteerActivity/index.js
import {api_westreamActivity,activitySubmit,api_getApply} from  '../../../api/volunteer';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activityId: null,
        list:[],
        nickname:'',
        activityTitle:'',
        applyName:'',
        applyPhone:'',
        userPage:{
            page:1,
            limit:10
        },
        total:0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const activityId = options.id
        this.setData({
            activityId
        })
        console.log(options);
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.westreamActivity()
    },

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

    },

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

    },

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

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if(this.data.userPage.page*this.data.userPage.limit<this.data.total){
            this.data.userPage.page++
            this.setData({
                userPage:this.data.userPage
            })
            this.westreamActivity()
        }
    },

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

    },

    formatDate(dateString) {
        const date = new Date(dateString);
        const year = date.getFullYear();
        const month = ("0" + (date.getMonth() + 1)).slice(-2);
        const day = ("0" + date.getDate()).slice(-2);
        return `${year}年${month}月${day}日`;
      },

    westreamActivity(){
        const tenantObj = wx.getStorageSync('tenant')
        let data = {
            tenantId:tenantObj.tenantId,
            ...this.data.userPage
        }
        api_westreamActivity(data).then(res=>{
            let list =[]
            res.data.records = res.data.records.reduce((pre,item)=>{
                return [...pre,{
                    ...item,
                    activityBeginTime:this.formatDate(item.activityBeginTime),
                    activityEndTime:this.formatDate(item.activityEndTime)
                }]
            },[])
            if(this.data.userPage.page==1){
                list =res.data.records
            }else{
                list = [...this.data.list,...res.data.records]
            }
            let total = res.data.total
            this.setData({
                total,
                list,
            }),
            console.log(res);
        })
    },

    showDetail(e) {
        let data = JSON.stringify(e.currentTarget.dataset.item)
        wx.navigateTo({
            url: '/package_first/pages/VolunteerDetail/index?data='+ encodeURIComponent(data),
        })
    },
})