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

Page({

    /**
     * 页面的初始数据
     */
    data: {
        to:'',
        from:'',
        content:'',
        img:'https://wk921-1316969570.cos.ap-nanjing.myqcloud.com/xzimg.png',
        messageList:[],
        sound:'',
        soundTime:0,
        showBtn:true,
        showImg:true,
        showLtqp:true,
        showImgBig:false, //图片太大弹层
        showSoundMore:false, //语音太多提示
        showSend:false, //寄出提示
        Preview:false, //预览
        showSound:false,
        showAlready:false,
        btn1:true,
        btn2:false,
        btn3:false
    },
    writeLetters(){
        console.log('写信');
        this.setData({
            showImg:false,
            showLtqp:false,
        })
    },
    already(){
        console.log('确定寄出');
        this.setData({showAlready:true})
        const list = this.data.messageList
        list.push({
            type: 'text',
            content: this.data.content
        })
        const formData = {
            from: this.data.from,
            to: this.data.to,
            messageList: list
        }
        console.log(formData);
        req.putRequest('/api/user/curt/letter',{formData}).then(res => {
            console.log(res);
        })
    },
    closeShowImgBig(){
        this.setData({showImgBig:false})
    },
    closeShowSoundMore(){
        this.setData({showSoundMore:false})
    },
    openShowSend(){
        this.setData({showSend:true})
    },
    closeShowSend(){
        this.setData({showSend:false})
    },
    openPreview(){
        console.log('预览');
        this.setData({Preview:true})
    },
    ClearYl(){
        console.log('清除语音');
        // this.setData({showLtqp:false})
    },
    goBack(){
        console.log('返回');
        wx.navigateBack()
    },
    ClearImg(){
        console.log('清除图片');
        const list = this.data.messageList.filter(item => {
            return item.type !== "graphic"
        })
        this.setData({
            img:'https://wk921-1316969570.cos.ap-nanjing.myqcloud.com/xzimg.png',
            messageList: list
        })
        console.log(this.data.messageList);
    },
    // 上传图片
    addImg(){
        console.log('添加图片');
        this.setData({
            showImg:true
        })
        wx.chooseMedia({
            mediaType: 'image',
            success: (res) => {
                const files = res.tempFiles[0]
                if(files.size > 5 * 1024 * 1024 ){
                    this.setData({showImgBig:true})
                }
                this.uploadFile(res.tempFiles[0].tempFilePath,'poster')
            }
        })
    },
    uploadFile(file,type) {
        wx.uploadFile({
            url: 'https://xzjl-api.windymuse.cn/api/resource',
            filePath: file,
            name: 'file',
            header: {
                'Authorization': wx.getStorageSync('token'),        
                'Content-Type': 'multipart/form-data'
            },
            formData: {
            type: type
            },
            success: (async(res) => {
                const data = JSON.parse(res.data);
                console.log(data);
                if (data.data.type === 'audio') {
                    console.log('添加音频');
                    const list = this.data.messageList
                    const audio = this.data.messageList.filter(item => {
                        return item.type === 'audio'
                    })
                    // 判断语音条数大于3条
                    if(audio.length >= 3){
                        this.setData({showSoundMore:true})
                    }else {
                        list.push({
                            type: 'audio',
                            resourceId: data.data.id,
                        })
                        this.setData({
                            messageList: list
                        })
                    }
                    console.log(this.data.messageList);

                } else if (data.data.type === 'poster') {
                    console.log('添加图片');
                    const list = this.data.messageList
                    list.push({
                        type: 'graphic',
                        resourceId: data.data.id
                    })
                    this.setData({
                        img:data.data.abUrl,
                        messageList:list
                    })
                    console.log(this.data.messageList);
                }
            }),
            // success: async res => {
            //   let data = JSON.parse(res.data);
            //   console.log(data, '111111');
            //   if (data.code == 200) {
            //       this.setData({imgUrl:data.data.abUrl})
            //       console.log('打印1111')
            //       console.log(this.data.imgUrl)
                // res.data = JSON.parse(res.data);
                // console.log(res.data);
                // if (res.data.code == 1) {
                //   console.log(res.data.data.url);
                //   this.img = this.baseUrl + res.data.data.url;
                // 图片路径
                //   this.userinfo.avatar = res.data.data.url;
                // let n = await this.$api.upload({ file: res.data.fullurl });
                // console.log(n, '上传后');
                // }
            //   }
            // },
            fail(res) {
            console.log('上传失败');
            }
        });
},
    openSound(){
        console.log('添加声音');
        this.setData({showSound:true,showLtqp:true})
    },
    closeSound(){
        this.setData({showSound:false})
    },
    startSound(){
        console.log('开始录音');
        const time = setInterval(()=>{
            this.setData({soundTime:this.data.soundTime + 1})
        },1000)
        this.setData({ timeId: time })
        wx.startRecord({
            success: (res) => {
                console.log(res.tempFilePath);
                this.setData({sound:res.tempFilePath,btn1:false,btn2:true})
            }
          })
    },
    endSound(){
        console.log('结束录音');
        wx.stopRecord()
        clearInterval(this.data.timeId)
    },
    playVoice(){
        console.log('播放录音');
        this.setData({btn2:false,btn3:true})
        wx.playVoice({
            filePath: this.data.sound,
            complete: () => {
              console.log('录音播放结束');
                this.setData({btn2:true,btn3:false})
            }
        });
    },
    addSound(){
        console.log('添加录音');
        this.uploadFile(this.data.sound,'audio')
        this.setData({
            btn1:true,
            btn2:false,
            btn3:false,
            soundTime:0
        })
        // console.log(this.data.messageList);
    },
    delSound(){
        console.log('不添加该条语音');
        this.setData({
            sound:'',
            btn1:true,
            btn2:false,
            btn3:false,
            soundTime:0
        })
    },
    logout(){
        req.deleteRequest('/api/user/logout',{}).then((res)=>{
            if(res.data.code===200){
                wx.navigateTo({
                    url: '/pages/index/index'
                  })
                  wx.showToast({
                    title:'寄出成功'
                  })
            }
           })
    },
    getLetter(){
        console.log('获取当前用户信札内容');
        req.getRequest('/api/user/curt/letter',{}).then(res => {
            console.log(res);
            this.setData({
                to: res.data.data.to,
                from: res.data.data.from,
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getLetter()
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})