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

Page({

    /**
     * 页面的初始数据
     */
    data: {
        status:false,
        activityInfo:{},
        dataSource: {},
        list:[],
        userPage:{
            page:1,
            limit:10
        },
        total:0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let data = JSON.parse(decodeURIComponent(options.data))
        data.applyBeginTime = this.formatDate(data.applyBeginTime)
        data.applyEndTime = this.formatDate(data.applyEndTime)
        this.setData({
            activityInfo:data
        })
        let imgs=data.intro.match(/<img.*?src="(.*)".*?\/>/g);
        if(!imgs){
            this.setData({'dataSource.content': data.intro})
        } else {
            for(let img of imgs){
                let temp
                temp=img.replace(/(width=".*?"|height=".*?")/g,"");
                if(/style="(.*?)"/.test(temp)){
                    temp=temp.replace(RegExp.$1,RegExp.$1+"width:100% !important;height:auto;display:block")
                }else{
                    temp=temp.replace(/<img/,"<img style='width:100% !important;height:auto;display:block'")
                }
                data.intro=data.intro.replace(img,temp)
            }
            this.setData({'dataSource.content': data.intro})
        }
        this.getApplyStatus()
    },

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

    },

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

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

    },

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

    },

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

    },

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

    },
    getApplyStatus(){
        const openid = wx.getStorageSync('openid')
        const params = {
            openid,
            activityId: this.data.activityInfo.id,
        }
        api_getApply(params).then(res=>{
            if(res.code==0){
                let status = res.data?.status || 3
                this.setData({
                    status
                })
            }
        })
    },
    
    activitySubmit(){
        let tenantObj = wx.getStorageSync('tenant')
        let openid = wx.getStorageSync('openid')
        let data = {
            openid:openid,
            activityId: this.data.activityInfo.id,
            tenantId:tenantObj.tenantId,
            nickname:'',
            applyPhone:'',
            applyName:'',
            activityTitle:this.data.activityInfo.activityTitle
        }
        activitySubmit(data).then(res=>{
            console.log(res);
        }).catch(res=>{
            console.log(res);
        })
    },

    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}日`;
      },
      goApply(e){
        if(this.data.status === 1){
            wx.showToast({
              title: '您已参加该活动',
              icon:"none"
            })
            return
        }
        if(this.data.status === 3){
            let activityInfo = JSON.stringify(this.data.activityInfo)
            wx.navigateTo({
                url:"/package_first/pages/ActivityApply/index?activityInfo=" +  encodeURIComponent(activityInfo)
            })
        }
      },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        
    }
})