// pages/home/xx/hy/index.js
const app = getApp();//新建页面时 默认引入
const req = app.xzjlReq();//初始化一个的request() 实例
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo:{},
        isPlay: true
    },
    go2Reback(){
        wx.redirectTo({
            url: '/pages/home/xx/lindex/lindex'
            })
    },
    go2Xz(){
        wx.redirectTo({
            url: '/pages/home/xx/hy/index'
            })
    },
    go2Yx(){
        wx.redirectTo({
            url: '/pages/home/xx/hy/yx/yx'
            })
    },
    go2Hx(){
        wx.redirectTo({
            url: '/pages/home/xx/hy/hx/hx'
            })
    },
    go2Zt(){
        wx.redirectTo({
            url: '/pages/home/xx/hy/zt/zt'
            })
    },
    delLetter(e){
        const id = e.target.dataset.item.id
        const isSelf = e.target.dataset.item.isSelf
        console.log(isSelf,'1111');
        if (isSelf) {
            req.deleteRequest(`/api/user/curt/message/${id}`,{}).then(res => {
                console.log(res);
                if(res.data.code === 200){
                    wx.showToast({
                      title: '删除成功',
                    })
                    this.getList()
                }
            })
        }else {
            console.log('只能删除自己消息');
            wx.showToast({
              title: '只能删除自己的回忆',
              icon: 'none'
            })
        }
    },
    getList(){
        req.getRequest('/api/user/curt/message/lover',{
                limit:-1,
                type: 'audio'
            }).then(res => {
            console.log(res);
            const list = res.data.data.records
            list.forEach(item => {
                // 将时间字符串转为Date对象
                const date = new Date(item.createdAt);

                // 获取年月日时分秒
                const year = date.getFullYear();
                const month = date.getMonth() + 1;
                const day = date.getDate();
                const hour = date.getHours();
                const minute = date.getMinutes();
                const second = date.getSeconds();
               // 补零
               const formattedHours = hour < 10 ? `0${hour}` : hour
               const formattedMinutes = minute < 10 ? `0${minute}` : minute

               // 格式化时间
               const formattedDate = `${year}年${month}月${day}日 ${formattedHours}:${formattedMinutes}`;
                item.createdAt = formattedDate
            })
            console.log(list);
            this.setData({list: list})
        })
    },
    getLoveInfo(){
        req.getRequest('/api/user/curt/lover',{}).then(res => {
            console.log(res);
            this.setData({
                avatar: res.data.data.avatar,
                nick: res.data.data.nick
            })
        })
    },
    // 播放音频
    playAudio(e){
        if (this.data.isPlay) {
            this.setData({isPlay: false})
            const resourceId = e.currentTarget.dataset.item.resourceId
            const id = this.data.userInfo.id
            const playSrc = `https://xzjl-api.windymuse.cn/api/resource/${resourceId}?k=${id}`
            const innerAudioContext = wx.createInnerAudioContext()
            innerAudioContext.src = playSrc
            innerAudioContext.play()
            console.log('22222');
            innerAudioContext.onEnded(() => {
                console.log('111');
                this.setData({isPlay: true})
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getList()
        this.getLoveInfo()
        this.setData({
            userInfo: wx.getStorageSync('xinyuan-userInfo')
        })
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})