import httpRequestApi from '../../../utils/APIClient';
import {
    formatDate
} from '../../../utils/util';
Page({
    data: {
        fullScreenBtn: false,
        playBtn: false,
        gesture: true,
        author: '',
        videoSrc: '',
        total: '',
        authorAvatar: '',
        user: [],
        inputValue: 'smdx',
        replyList: [],
        howMuch: '2000',
        moneySelect: 'moneySelect',
        moneyNormal: 'moneyNormal',
        ifReward: false,
        id: '',
        // shareFlag: false
    },
    onLoad: function (option) {
        console.log(option)

        wx.setNavigationBarTitle({
            title: option.title //页面标题为路由参数
        })

        this.setData({
            title: option.title,
            id: option.id
        })
        this.uid = wx.getStorageSync('uid');
        this.getWorks(this.uid, option.id);
    },
    // onReady: function () {

    // },
    getWorks: function (uid, id) {
        httpRequestApi.getWorksDetail(uid, id).success((res) => {
            console.log(res);
            const others = res.data.data.otherRead;
            const author = res.data.data.user;
            const works = res.data.data.userRead;
            const othersTemp = [];
            others.forEach((item) => {
                const temp = {};
                temp.image = item.user.avatar;
                temp.nickName = item.user.wechatName;
                othersTemp.push(temp);
            });
            this.setData({
                user: othersTemp,
                author: author.wechatName,
                authorAvatar: author.avatar,
                authorUid: author.uid,
                videoSrc: works.originVideo,
                audioSrc: works.audioPath,
                iconImg: works.iconImg
            })
            // 设置音频路径
            this.innerAudioContext = wx.createInnerAudioContext();
            this.innerAudioContext.onError((res) => {
                // 播放音频失败的回调
            })
            this.innerAudioContext.src = this.data.audioSrc; // 这里可以是录音的临时路径
            this.getReply();
        });
    },
    likeWorks: function (e) {
        httpRequestApi.likeWorks(this.uid, this.data.id).success(res => {
            console.log(this.uid)
        });
    },
    // 弹出分享框
    openShare: function (e) {
        // this.setData({
        //     shareFlag: !this.data.shareFlag
        // })
        this.shareDialog = this.selectComponent("#share-dialog");
        const data = {
            avatar: this.data.authorAvatar,
            author: this.data.author,
            iconImg: this.data.iconImg,
            title: this.data.title,
            // tip: this.data.tip,
        }
        this.shareDialog.share(data);
    },
    videoPlay: function () {
        this.innerAudioContext.play();
        httpRequestApi.playWorks(this.uid, this.data.id).success(res => {

        })
    },
    videoEnd: function () {
        this.innerAudioContext.stop();
    },
    videoPause: function () {
        this.innerAudioContext.pause();
    },
    goToReading: function () {
        let id = this.data.id;
        let title = this.data.title;
        wx.navigateTo({
            url: `../../main/reading/reading?id=${id}&title=${title}`
        })
    },
    onShareAppMessage: function (res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
            console.log(res.target)
        }
        return {
            title: '测试',
            path: '/pages/social/works/works'
        }
    },
    follow: function () {
        // let uid = wx.getStorageSync('uid');
        let followUid = 2;
        httpRequestApi.followUser(this.uid, followUid).success((res) => {
            console.log(res)
        });
    },
    // 点赞评论
    likeCommend: function (e) {
        console.log(e);
        // let uid = wx.getStorageSync('uid');
        let followUid = e.currentTarget.dataset.id;
        let index = e.currentTarget.dataset.index;
        httpRequestApi.likeCommend(this.uid, followUid).success(res => {
            console.log(res);
            const str = `replyList[${index}].likes`;
            this.setData({
                [str]: res.data.data.favors
            })
        });
    },
    // 去其他用户的作品页
    goToOthers: function (e) {
        wx.navigateTo({
            url: `../../main/reading/reading?id=${id}&title=${title}`
        })
    },
    // 查询回复
    getReply: function () {
        // let uid = wx.getStorageSync('uid');
        let columnId = this.data.id;
        let pageNo = 1;
        let pageSize = 10;
        httpRequestApi.getReply(this.uid, columnId, pageNo, pageSize).success((res) => {
            console.log(res.data.data.list);
            const replyList = res.data.data.list;
            const replyTemp = [];
            replyList.forEach((item) => {
                const temp = {};
                temp.nickName = item.user.wechatName;
                temp.avatar = item.user.avatar;
                temp.text = item.detailDesc;
                temp.id = item.id;
                temp.replyCount = item.replyCount;
                temp.time = formatDate(item.gmtCreated, 3);
                temp.likes = item.postsAttributeInfo.favors || 0;
                console.log(temp.time)
                replyTemp.push(temp);
            });
            this.setData({
                replyList: replyTemp,
                total: res.data.data.totalSize
            })
        });
    },
    // 打开回复详情页
    goToDetail: function (e) {
        let id = e.currentTarget.dataset.id;
        let count = e.currentTarget.dataset.count;
        console.log(e);
        wx.navigateTo({
            url: `../../social/replyDetail/replyDetail?id=${id}&count=${count}`
        })
    },
    //  绑定输入框内容
    inputValue: function (e) {
        this.setData({
            inputValue: e.detail.value
        });
    },
    // 发布回复
    sendHandler: function () {
        console.log(this.data.inputValue);
        if (this.data.inputValue !== '') {
            // let uid = wx.getStorageSync('uid');
            let data = {
                "columnId": this.data.id,
                colunmNames: 'what',
                "detailDesc": this.data.inputValue
            }
            httpRequestApi.postReply(this.uid, data).success(res => {
                console.log(res);
            });
        }
    },
    // 选择金额
    setMoney: function (e) {
        this.setData({
            howMuch: e.currentTarget.dataset.money
        })
    },
    reward: function () {
        this.setData({
            ifReward: true
        })
    },
    // 奖励
    rewardMoney: function () {
        console.log(this.data.authorUid);
        const data = {
            targetUid: this.data.authorUid,
            amount: this.data.howMuch
        }
        // let uid = wx.getStorageSync('uid');
        httpRequestApi.rewardMoney(this.uid, data).success(res => {
            console.log(res);
            this.payMoneyt(res.data.data);
        })

    },
    //支付
    payMoneyt: function (orderInfo) {
        wx.requestPayment({
            'appId': orderInfo.appId,
            'timeStamp': orderInfo.timeStamp,
            'nonceStr': orderInfo.nonceStr,
            'package': orderInfo.package,
            'signType': orderInfo.signType,
            'paySign': orderInfo.sign,
            'success': function (res) {
                console.log(res)
                wx.showModal({
                    title: '提示',
                    content: '支付成功',
                    success(res) {
                        if (res.confirm) {
                            this.setData({
                                ifReward: false
                            })
                        } else if (res.cancel) {
                            this.setData({
                                ifReward: false
                            })
                        }
                    }
                })

            },
            'fail': function (res) {
                this.setData({
                    ifReward: false
                })
                console.log('支付失败', res)
            }
        })
    },

})