const Apps = getApp();
const api = require('../../config/api.js');
const util = require('../../config/util.js');
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        videurl: "https://xyg.jsjbkc.com/pics/xyg1.mp4?v" + Date.now(),
        imagecount: 4,
        courseid: 1,
        itemdata: [],
        baseurl: Apps.globalData.baseimgurl,
        showview: false,
        title: '',
        content: '',
        images: '',
        upload_picture_list: [],
        imagesListp: [],
        imagestr: [],
        imageslists: [], //图片预览
        ismdata: 1, //是否材料型数据跳转
        typeid: 1,
        isdisplay: 1,
        ispic: 0,
        accesstoken: '',
        xid: 0,
        onefeilei: 0, //课程一级分类
        viptype_id: 1, //VIP类型
        videoContext: {},  // 用于绑定视频标签
        showvide: false,
        videtimes: 5,
        videtimes2: 10,
        cousreshow: false, //检查用户是对课程有权限，对VIP用户onWaiting 
        headpicvalue: 0,//课程积分
        sliderValue: 0, //控制进度条slider的值，
        updateState: false, //防止视频播放过程中导致的拖拽失效
        playStates: false, //控制播放 & 暂停按钮的显示
        fullScreen: false,//是否是全屏
        recommendListArr: [],
        chongzhivalue: '怼币购买',
        poptitle: '怼币购买后可学习14天',
        isfinall: 0, //检测用户的课程是否完成
        video_data: {}, //要播放的视频对象
        show: false,
        isvip:false, //是否VIP,
        showdiage: false,
        pathid:"",
        actions: [
            {
                name: '1.0倍速',
                value: 1
            },
            {
                name: '1.25倍速',
                value: 1.25
            },
            {
                name: '1.5倍速',
                value: 1.5
            },
            {
                name: '2.0倍速',
                value: 2
            },
        ],
    },
    showdiagePopup(e) {
        console.log(e);
        let pathid=e.currentTarget.id;

        let files = pathid; // knowfiles.currentTarget.id;
        if(files.length==0){

            wx.showToast({
                title: '暂时还没有讲义,不可下载',//提示文字
                duration: 2000,//显示时长
                mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                icon: 'none', //图标，支持"success"、"loading"  
                success: function () {
                    return;
                },//接口调用成功
                fail: function () { },  //接口调用失败的回调函数  
                complete: function () { } //接口调用结束的回调函数  
            })
        }else{
            this.setData({ showdiage: true,pathid:pathid });
        }
       
      },
    
      onshowdiageClose() {
        this.setData({ showdiage: false });
      },
    vippopmsg: function () {
        //vip课程弹出消息框 
        //判断VIP课程还有多少天
        this.vipkechengday(this.data.onefeilei, wx.getStorageSync('wxuserid'));
    },
    vipkechengday: function (onefeilei, wxuserid) {
        //判断VIP课程还有多少天
        const that = this;
        let url = Apps.globalData.baseUrl + '/user/vipscourseuserday';
        var param = {
            user_id: wxuserid,
            sourceid: onefeilei
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {
            console.log(res);
            //   您的VIP会员剩余374天
            Toast('您的VIP会员剩余' + res + '天');
        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    popmsg: function () {
        //怼币课程弹出消息框 
        //判断怼币课程还有多少天
        console.log(this.data.courseid);
        console.log(wx.getStorageSync('wxuserid'));
        this.duibikechengday(this.data.courseid, wx.getStorageSync('wxuserid'));
        // Toast('您的课程还有8天到期，到期后需重新购买');
    },
    popclick: function () {
        if (this.data.isdisplay == 1 && this.data.typeid != 1) {
            //弹出购买信息
            this.setData({
                showvide: true
            });
            this.videoContext.pause();

        }

    },
    duibikechengday: function (sourceid, wxuserid) {
        //判断怼币课程还有多少天
        //检查用户是否可以听课
        const that = this;
        let url = Apps.globalData.baseUrl + '/user/setdecuserscoreday';
        var param = {
            user_id: wxuserid,
            sourceid: sourceid
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {
            console.log(res);
            Toast('您的课程还有' + res + '天到期，到期后需重新购买');
        }).catch((errMsg) => {
            console.log(errMsg);
        });

    },
    bsbtnclick: function () {
        let bsdata = !this.data.show;
        console.log(bsdata);
        this.setData({
            show: bsdata
        })
    },
    onClose: function () {
        this.setData({ show: false });
    },
    onSelect: function (event) {
        console.log(event.detail);
        console.log(event.detail.value);
        let rate = event.detail.value;
        this.videoContext.playbackRate(Number(rate));
        this.onClose();
    },
    bindButtonRate: function (e) {
        let rate = e.detail;
        console.log(rate);
        this.videoContext.playbackRate(Number(rate));
    },
    fullvideo: function (params) {
        console.log(params);
        // 全屏
        this.videoContext = wx.createVideoContext('myVideo');
        this.videoContext.requestFullScreen({
            direction: 1
        });
    },
    bindfullscreenchange: function (e) {
        // let fullScreen = e.detail.fullScreen //值true为进入全屏，false为退出全屏
        // let videoContext = wx.createVideoContext('myVideo')
        if (!e.detail.fullScreen) {
            // 退出全屏
            this.videoContext.exitFullScreen()
        }
        else {
            // 全屏
            this.videoContext = wx.createVideoContext('myVideo');
            this.videoContext.requestFullScreen({
                direction: -90
            });
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        Apps.islogin();
        console.log(options);
        // if (options.typeid == 3) {
        //     this.setData({
        //         chongzhivalue: '充值VIP',
        //         poptitle: 'VIP期限内可无限畅学'
        //     })
        // }
        this.setData({
            courseid: options.id,
            typeid: options.typeid,
            updateState: true
        });
       
    },
    buyclick(event) {
        console.log(event.detail);
        console.log("===========")
        //购买是跳转，还是在这里需要判断购买
        console.log(this.data.viptype_id);
        if (this.data.viptype_id == 2) {
            //实现怼币购买
            this.kqcheckdescusersorce(wx.getStorageSync('wxuserid'), this.data.cousrceid, this.data.headpicvalue);
        } else if (this.data.viptype_id == 3) {
            //跳转购买课程页面
            wx.navigateTo({
                url: '/pages/viprecharge/viprecharge',
            })
        }
    },
    cancleclick() {
        this.setData({ showvide: false });
    },
    videoUpdate(e) {
        if (this.data.updateState) {
            //判断拖拽完成后才触发更新，避免拖拽失效
            const times = e.detail.currentTime.toFixed(0);
            let sliderValue = e.detail.currentTime / e.detail.duration * 100;
            // console.log(sliderValue);
            this.setData({
                sliderValue: sliderValue,
                duration: e.detail.duration
            })
            // console.log('sliderValue=>' + times);
            // console.log('11试看=>' + times);
          //  this.publicdata(times);

        }
    },
    publicdata: function (times) {

        //公共数据

        //对2，3类型课程需要监控，一判断是否已用怼币购买过，二是判断是否为VIP用户
        if (this.data.viptype_id != 1) {

            //购买用户，怼币购买用户
            //1免费，2怼币，3VIP,4优惠值
            //先检查是否有怼币
            //增加一个两个小时的时长
            // console.log('先检查是否有怼币');
            // console.log(that.data.isdisplay);
            if (this.data.isdisplay == 1) {

                if (this.data.cousreshow == false) {

                    if (times >= this.data.videtimes) {
                        this.setData({
                            showvide: true,
                            playStates: false,
                            sliderValue: times
                        });
                        this.videoContext.pause();
                    }
                }
            }

        }
    },
    sliderChanging(e) {

        const times = e.detail.value.toFixed(0);
        this.setData({
            updateState: false //拖拽过程中，不允许更新进度条        
        })
        if (times >= this.data.videtimes) {
            this.videoContext.pause();
            this.publicdata(times);
        } else {
            this.videoContext.play();
        }
    },
    sliderChange(e) {

        const times = e.detail.value.toFixed(0);
        this.setData({
            sliderValue: e.detail.value,
            updateState: false //完成拖动后允许更新滚动条
        })

        if (this.data.duration) {
            this.videoContext.seek(e.detail.value / 100 * this.data.duration); //完成拖动后，计算对应时间并跳转到指定位置            
            this.setData({
                sliderValue: e.detail.value.toFixed(0),
                updateState: true //完成拖动后允许更新滚动条            
            })
        }
        if (times >= this.data.videtimes) {
            this.videoContext.pause();
            this.setData({
                showvide: true,
                duration: e.detail.duration,
                playStates: false,
                sliderValue: times
            });
            this.publicdata(times);

        } else {
            this.videoContext.play();
        }


    },
    videoOpreation() {

        const playstats = !this.data.playStates;
        this.setData({
            playStates: playstats,
        });
        this.data.playStates ? this.videoContext.pause() : this.videoContext.play();
        const times = this.data.sliderValue;
        console.log(times);
        if (times >= this.data.videtimes) {
            this.videoContext.pause();
            this.publicdata(times);
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.videoContext = wx.createVideoContext('myVideo');
        this.setData({
            updateState: true
        })
    },

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

        this.isdisplay();
        this.getcoursedata(this.data.courseid);
       // this.wrodtypelist(this.data.courseid);
      //  this.isjumpurl(this.data.typeid, 1);
       // this.isfinallfun(this.data.courseid, wx.getStorageSync('wxuserid'), 2); //2不删除

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

    },

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

    },

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

    },

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

    },

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

    },
    messageclick: function (e) {

        wx.reLaunch({
            url: '/pages/communityinfo/communityinfo?id=' + e.currentTarget.id,
        })
    },
    deletewordreply: function (par) {

        //删除留言
        const that = this;
        let url = Apps.globalData.baseUrl + '/wordslist/deletewordreply';
        var param = {
            wordid: par
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {
            console.log(res);
            this.wrodtypelist(that.data.courseid);

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    getcoursedata: function (params) {
        const that = this;
        let url = Apps.globalData.baseUrl + '/classcourse/getcousrefourlist';
        var param = {
            id: params,
            user_id: wx.getStorageSync('wxuserid') == '' ? 0 : wx.getStorageSync('wxuserid')
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log('res====================');
            console.log(res);
            let cousrceid = res.data.id;
            let headpicvalue = res.data.headpicvalue;
            console.log(res.data.onefeilei);
            that.setData({
                itemdata: res.data, //课程数据
                onefeilei: res.data.onefeilei, //课程一级分类
                viptype_id: res.data.viptype_id,//课程类型
                headpicvalue: headpicvalue, //课程积分
                cousrceid: cousrceid, //课程ID
                videtimes: res.data.bz, //视听时长
                videurl:res.data.vodurl
            });

            if (that.data.isdisplay == 1) {


                // if (res.data.viptype_id == 3 || res.data.viptype_id == 2) {
                    //检查用户是否有课程权限
                    // that.checkusercousre(res.data.onefeilei);
                // }
                console.log(res.data.viptype_id);
                if(res.data.viptype_id == 1){
                    // console.log(res.data.viptype_i)
                    // that.setData({
                    //     cousreshow:true,
                    //     isvip:true
                    // })
                }else{

                    // that.checkusercousre(res.data.onefeilei);
                }
                that.checkusercousre(res.data.onefeilei);
                // if (that.data.cousreshow == false) {
                //     //如果怼币课程看否，购买
                //     that.checkuserscource(wx.getStorageSync('wxuserid'), cousrceid, headpicvalue);
                // }

                //  if (res.data.viptype_id == 2) {
                //     //如果怼币课程看否，购买
                //     that.checkuserscource(wx.getStorageSync('wxuserid'), cousrceid, headpicvalue);
                // }
                // if (res.data.viptype_id == 3 || res.data.viptype_id == 2) {
                //     //检查用户是否有课程权限
                //     that.checkusercousre(res.data.onefeilei, cousrceid);
                // }
            }
        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    kqcheckdescusersorce: function (user_id, sourceid, headpicvalue) {
        //扣除用户怼币
        const that = this;
        let url = Apps.globalData.baseUrl + '/user/checkdescuserscore';
        var param = {
            user_id: user_id,
            sourceid: sourceid,
            headpicvalue: headpicvalue
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {

            console.log(res);

            that.setData({
                cousreshow: res.data
            })

            if (res.data == false) {


                wx.showModal({
                    title: '提示',
                    content: '您的怼币不够,需要' + headpicvalue + '怼币,才能安心学习该课程',
                    success (res) {
                      if (res.confirm) {
                        console.log('确定');
                        wx.navigateTo({
                            url: '/pages/index/index',
                        })
                      } else if (res.cancel) {
                        console.log('取消');
                        wx.navigateTo({
                            url: '/pages/vipquanyi/vipquanyi',
                        })
                      }
                    }
                  })

                // wx.showModal({
                //     title: '提示',
                //     content: '您的怼币不够,需要' + headpicvalue + '怼币,才能安心学习该课程',
                //     cancelText: '退出',
                //     confirmText: '去充怼币',
                //     success: function (res) {
                //         if (res.cancel) {
                //             //这个跳转是左边按钮的跳转链接
                //             //这里取消操作
                //             console.log('确认扣除怼币' + headpicvalue);
                //             wx.navigateTo({
                //                 url: '/pages/index/index',
                //             })

                //         } else {
                //             //这里是右边按钮的跳转链接
                //             wx.navigateTo({
                //                 url: '/pages/vipquanyi/vipquanyi',
                //             })
                //         }
                //     }
                // })
            }else{

                Toast('课程购买成功,您花费了' + headpicvalue + '怼币,请安心学习该课程');

                // wx.showModal({
                //     title: '怼币购买成功',
                //     content: '课程购买成功,您花费了' + headpicvalue + '怼币,请安心学习该课程',
                //     success (res) {
                //       if (res.confirm) {
                //         console.log('确定');
                //         wx.navigateTo({
                //             url: '/pages/courselearning/courselearning?id=' + that.data.courseid + "&typeid=" + that.data.typeid,
                //         })
                //       } else if (res.cancel) {
                //         console.log('取消')
                //       }
                //     }
                //   })
                  
                // wx.showModal({
                //     title: '怼币购买成功',
                //     content: '课程购买成功,您花费了' + headpicvalue + '怼币,请安心学习该课程',
                //     cancelText: '好的，去学习',
                //     confirmText: '去充怼币',
                //     success: function (res) {
                        
                //         // wx.navigateTo({
                //         //     url: '/pages/courselearning/courselearning?id=' + that.data.courseid + "&typeid=" + that.data.typeid,
                //         // })
                //     }
                // }).catch((errMsg) => {
                //     console.log(errMsg);
                // });

            }
        })
    },
    checkuserscource: function (user_id, sourceid, headpicvalue) {
        //检查用户是否可以听课
        const that = this;
        let url = Apps.globalData.baseUrl + '/user/checkuserscource';
        var param = {
            user_id: user_id,
            sourceid: sourceid,
            headpicvalue: headpicvalue
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {
            that.setData({
                cousreshow: res.data
            })

        }).catch((errMsg) => {
            console.log(errMsg);
        });

    },
    finallclick: function (params) {

        if (this.data.cousreshow == false) {
            wx.showToast({
                title: '试听课程,不可操作,请先购买课程',//提示文字
                duration: 2000,//显示时长
                mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                icon: 'none', //图标，支持"success"、"loading"  
                success: function () {
                    return;
                },//接口调用成功
                fail: function () { },  //接口调用失败的回调函数  
                complete: function () { } //接口调用结束的回调函数  
            })
            return;
        }
        this.isfinallfun(this.data.courseid, wx.getStorageSync('wxuserid'), 1);
    },
    isfinallfun: function (id, userid, typeid) {
        //是否完成函数
        const that = this;
        let url = Apps.globalData.baseUrl + '/classcourse/finallclick';
        var param = {
            id: id,
            user_id: userid,
            typeid: typeid
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {

            console.log(res);
            that.setData({
                isfinall: res
            })

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    downfile: function (knowfiles) {

        if (this.data.cousreshow == false && this.data.typeid != 1) {
            wx.showToast({
                title: '试听课程,不可操作,请先购买课程',//提示文字
                duration: 2000,//显示时长
                mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                icon: 'none', //图标，支持"success"、"loading"  
                success: function () {
                    return;
                },//接口调用成功
                fail: function () { },  //接口调用失败的回调函数  
                complete: function () { } //接口调用结束的回调函数  
            })
            return;
        }
        
        // let files = knowfiles.currentTarget.id;
        let files = this.data.pathid;// knowfiles.currentTarget.id;
       

        wx.downloadFile({
            url: this.data.baseurl + files,
            success(res) {

                wx.shareFileMessage({
                    filePath: res.tempFilePath,
                    success() {},
                    fail: console.error,
                  })
                
                // wx.openDocument({
                //     filePath: res.tempFilePath,
                //     showMenu:true, //关键点
                //     success() {
                //         // wx.showToast({
                //         //     title: '下载成功',//提示文字
                //         //     duration: 2000,//显示时长
                //         //     mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                //         //     icon: 'success', //图标，支持"success"、"loading"  
                //         //     success: function () { },//接口调用成功
                //         //     fail: function () { },  //接口调用失败的回调函数  
                //         //     complete: function () { } //接口调用结束的回调函数  
                //         // })
                //     }
                // })
            }
        })
    },
    downfilepoto: function (knowfiles) {

        if (this.data.cousreshow == false && this.data.typeid != 1) {
            wx.showToast({
                title: '试听课程,不可操作,请先购买课程',//提示文字
                duration: 2000,//显示时长
                mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                icon: 'none', //图标，支持"success"、"loading"  
                success: function () {
                    return;
                },//接口调用成功
                fail: function () { },  //接口调用失败的回调函数  
                complete: function () { } //接口调用结束的回调函数  
            })
            return;
        }

        // let files = knowfiles.currentTarget.id;
        let files = this.data.pathid
       
        wx.downloadFile({
            url: this.data.baseurl + files,
            success(res) {
                wx.openDocument({
                    filePath: res.tempFilePath,
                    showMenu:true, //关键点
                    success() {                      
                    }
                })
            }
        })
    },
    collectionclick: function (params) {

        let id = params.currentTarget.id;
        console.log(id);
        this.collectionclickdata(id, wx.getStorageSync('wxuserid'));

    },
    collectionclickdata: function (collection_id, user_id) {
        const that = this;
        let url = Apps.globalData.baseUrl + '/course/coursecollection';
        var param = {
            id: collection_id,
            user_id: user_id
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log(res);
            this.getcoursedata(that.data.courseid);

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    displayview: function () {
        console.log(this.data.showview);
        this.setData({
            showview: (!this.data.showview)
        })
        this.getacctoken();
        this.pageScrollToBottom();
    },
    wrodtypelist: function (params) {

        //获取课程留言
        const that = this;
        let url = Apps.globalData.baseUrl + '/wordslist/wordscourselist';
        var param = {
            id: params,
            user_id: wx.getStorageSync('wxuserid')
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log(res);
            that.setData({
                replylist: res.data
            });

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    delesourceclick: function (par) {
        const that = this;
        wx.showModal({
            title: '删除提示',
            content: '您将删除该留言信息',
            cancelText: '退出',
            confirmText: '确认',
            success: function (res) {
                if (res.cancel) {
                    //这个跳转是左边按钮的跳转链接
                    //这里取消操作
                    console.log(res);

                } else {
                    //这里是右边按钮的跳转链接                   
                    let id = par.currentTarget.id;
                    //删除课程留言数据                  
                    let url = Apps.globalData.baseUrl + '/wordslist/wordscoursedelete';
                    var param = {
                        id: id
                    };
                    //此处为使用封装的post请求
                    util.post(url, param).then((res) => {
                        console.log(res);
                        that.wrodtypelist(that.data.courseid);

                    }).catch((errMsg) => {
                        console.log(errMsg);
                    });
                }
            }
        })

    },
    wordsclick: function (ee) {

        let e1 = ee.currentTarget.id;
        // let e2 = Apps.globalData.currentuserinfo.id;
        let e2 =wx.getStorageSync('wxuserid');
        const that = this;
        let url = Apps.globalData.baseUrl + '/wordslist/wordscourseclickcount';
        var param = {
            words_id: e1,
            user_id: e2,
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log(res);

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    uploadpic: function () {
        var that = this;
        let imagesList = [];
        let maxSize = 1024 * 1024;
        let maxLength = 6;
        let flag = true;
        wx.chooseImage({
            count: 9, //最多可以选择的图片总数
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                wx.showToast({
                    title: '正在上传...',
                    icon: 'loading',
                    mask: true,
                    duration: 500
                })
                for (let i = 0; i < res.tempFiles.length; i++) {
                    if (res.tempFiles[i].size > maxSize) {
                        flag = false;
                        console.log(111)
                        wx.showModal({
                            content: '图片太大，不允许上传',
                            showCancel: false,
                            success: function (res) {
                                if (res.confirm) {
                                    console.log('用户点击确定')
                                }
                            }
                        });
                    }
                }
                if (res.tempFiles.length > maxLength) {
                    console.log('222');
                    wx.showModal({
                        content: '最多能上传' + maxLength + '张图片',
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                                console.log('确定');
                            }
                        }
                    })
                }
                if (flag == true && res.tempFiles.length <= maxLength) {
                    that.setData({
                        imagesListp: res.tempFilePaths
                    })
                    that.data.imagestr = [];
                    var arr = '';
                    for (let i = 0; i < that.data.imagesListp.length; i++) {

                        // that.getpicchekc(that.data.imagesListp[i]);

                        // if (that.data.ispic == 1) {

                        wx.uploadFile({
                            url: Apps.globalData.baseUrl + '/wordslist/uploadpics',
                            filePath: that.data.imagesListp[i],
                            name: 'file',
                            success: function (res) {
                                //打印
                                console.log("------------响应数据：");
                                console.log(res.data);

                                var message = JSON.parse(res.data);
                                that.data.imagestr.push(message);
                                that.setData({
                                    imagestr: that.data.imagestr
                                });
                            },
                        })
                    }

                    // }


                }
                console.log(res);
            },
            fail: function (res) {
                console.log(res);
            }
        })

    },
    // 删除图片
    removeChooseImage(e) {
        let imgs = this.data.imagestr;
        let { index } = e.currentTarget.dataset;
        imgs.splice(index, 1);
        this.setData({
            imagestr: imgs
        })
    },
    // 预览图片
    previewBigImage(e) {
        let that = this;
        let imgs = that.data.imagestr;
        let index = e.currentTarget.dataset.index;
        let arr = [];
        imgs.forEach(function (item, index) {
            arr[index] = that.data.baseurl + item
        });
        wx.previewImage({
            //当前显示图片
            current: arr[index],
            //所有图片
            urls: arr
        })
    },
    isjumpblack:function(){
        console.log(wx.getStorageSync('userisblack'));
        if(wx.getStorageSync('userisblack')>=2){

            wx.showToast({
                title: "您已被禁言,请联系管理员",//提示文字
                duration: 5000,//显示时长
                mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                icon: 'none', //图标，支持"success"、"loading"  
                success: function () {
                   
                 },//接口调用成功
                fail: function () { },  //接口调用失败的回调函数  
                complete: function () { } //接口调用结束的回调函数  
            })
        }        
    },
    formSubmit: function (e) {

        Apps.islogin();

        if(wx.getStorageSync('userisblack')>=2){
            this.isjumpblack();
            return;
        }

        let content = this.data.wordstextname;
        if (content == '') {
            wx.showToast({
                title: '留言内容为空,请填写内容',
                icon: 'none',
                duration: 1500
            })
            return false;
        }
        this.getmsgcheck(content);

    },
    bindTextAreaBlur: function (e) {
        this.setData({
            wordstextname: e.detail.value
        })
    },
    uploadpicsimage: function (e) {

        const that = this;
        let url = Apps.globalData.baseUrl + '/wordslist/uploadpics';
        var param = {
            url: e
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {
            console.log(res);

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    wordsadd: function (content, imagestr) {

        const that = this;
        let url = Apps.globalData.baseUrl + '/wordslist/wordscouresadd';
        var param = {
            content: content,
            imagestr: imagestr,
            userid: wx.getStorageSync('wxuserid') == '' ? 3 : wx.getStorageSync('wxuserid'),
            wordsid: that.data.courseid,
            xid: that.data.xid
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {
            console.log(res.data);
            if (res.data > 0) {
                wx.showToast({
                    title: '留言内容成功',
                    icon: 'none',
                    duration: 1500
                })

                that.wrodtypelist(that.data.courseid);

                that.setData({
                    wordstextname: '',
                    imagestr: ''
                })

            }

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    contentclick: function (params) {

        console.log(params);
        let id = params.currentTarget.id;
        let xid = params.currentTarget.dataset.xid;
        console.log(id);
        this.setData({
            wordstextname: '回复@' + id,
            showview: true,
            xid: xid
        });

        //  this.addinccomment(xid);

        this.getacctoken();

        wx.showToast({
            title: '@成功，留言请移底部留言',
            icon: 'none',
            duration: 1500
        })
        this.pageScrollToBottom();

    }, rdclick: function (params) {

        this.pageScrollToBottom();
    }, addinccomment: function (params) {
        //增加评论数
        console.log(params);
        let userid = wx.getStorageSync('wxuserid');
        const that = this;
        let url = Apps.globalData.baseUrl + '/wordslist/addcommentcount';
        var param = {
            id: params,
            userid: userid
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log(res);
            // this.wrodtypelist(this.data.courseid)
            this.wrodtypelist(that.data.courseid);
        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    clickbtn: function (params) {
        console.log(params);
        let userid = wx.getStorageSync('wxuserid');
        const that = this;
        let url = Apps.globalData.baseUrl + '/wordslist/wordscourseclickcount';
        var param = {
            words_id: params.currentTarget.id,
            user_id: userid
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log(res);
            // this.wrodtypelist(this.data.courseid)
            this.wrodtypelist(this.data.courseid);
        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    knowledgeclick: function (params) {
        //知识要点
        if (this.data.cousreshow == false && this.data.typeid != 1) {
            wx.showToast({
                title: '试听课程,不可操作,请先购买课程',//提示文字
                duration: 2000,//显示时长
                mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                icon: 'none', //图标，支持"success"、"loading"  
                success: function () {
                    return;
                },//接口调用成功
                fail: function () { },  //接口调用失败的回调函数  
                complete: function () { } //接口调用结束的回调函数  
            })
            return;
        }

        let examid = params.currentTarget.id;
        if(examid>0){
            wx.navigateTo({
                url: '/pages/knowledge/knowledge?examid=' + examid,
            })
        }else{
            Toast('此课程暂无知识要点');
        }
      

    },
    examingclick: function (params) {

        if (this.data.cousreshow == false && this.data.typeid != 1) {
            wx.showToast({
                title: '试听课程,不可操作,请先购买课程',//提示文字
                duration: 2000,//显示时长
                mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                icon: 'none', //图标，支持"success"、"loading"  
                success: function () {
                    return;
                },//接口调用成功
                fail: function () { },  //接口调用失败的回调函数  
                complete: function () { } //接口调用结束的回调函数  
            })
            return;
        }

        let examid = params.currentTarget.id;
        let ismdata = this.data.ismdata;
        let grouppaper_id = this.data.itemdata.grouppaper_id;
        if(grouppaper_id>0){

       
                if (ismdata == 2) {
                    wx.navigateTo({
                        url: '/pages/exammakings/exammakings?examid=' + examid + '&grouppaper_id=' + grouppaper_id,
                    })
                } else {
                    wx.navigateTo({
                        url: '/pages/examing/examing?examid=' + examid + '&grouppaper_id=' + grouppaper_id,
                    })
                }
        }else{

            Toast('该课程无练习试题');
        }
    },
    preview: function (params) {
        let that = this;
        console.log(params)
        let currentUrl = params.currentTarget.dataset.src;
        let arrindex = params.currentTarget.dataset.id;

        let rearr = that.data.replylist[arrindex].pics;
        console.log(rearr);
        let arr = [];
        rearr.forEach(function (item, index) {
            arr[index] = that.data.baseurl + item
        });
        console.log(arr);
        wx.previewImage({
            current: currentUrl, // 当前显示图片的http链接
            urls: arr // 需要预览的图片http链接列表
        })
    },
    isjumpurl: function (id, groupid) {

        const that = this;
        let url = Apps.globalData.baseUrl + '/exam/isarrdata';
        var param = {
            id: id,
            groupid: groupid
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {
            console.log(res);
            that.setData({
                ismdata: res.data
            });
        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    isdisplay: function (params) {
        //是否显示视频
        const that = this;
        let url = Apps.globalData.baseUrl + '/index/isdisplay';
        var param = {
            pareid: params
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log(res);
            that.setData({
                isdisplay: res.data
            });

        }).catch((errMsg) => {
            console.log(errMsg);
        });

    },
    getacctoken: function () {
        const that = this;
        let url = Apps.globalData.baseUrl + '/user/acctoken';
        var param = {
            pareid: '1213'
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {

            console.log('-=-=-=-=-=-=-=-=');
            console.log(res);
            wx.setStorageSync('accesstoen', res.access_token)
            that.setData({
                accesstoken: res.access_token
            });


        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    getmsgcheck: function (params) {
        // $url ='https://api.weixin.qq.com/wxa/msg_sec_check?access_token=' .$accessToken;
        const that = this;
        let url = Apps.globalData.baseUrl + '/user/checkmsg'
        var param = {
            "openid": wx.getStorageSync('wxopenid'),
            "scene": 2,
            "version": 2,
            "content": params,
            "access_token": that.data.accesstoken
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {

            that.wordsadd(params, that.data.imagestr);
            // if (res.errcode == "0") {
            //     that.wordsadd(params, that.data.imagestr)
            // } else {

            //     wx.showToast({
            //         title: '留言内容有敏感词请重新输入',//提示文字
            //         duration: 2000,//显示时长
            //         mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            //         icon: 'none', //图标，支持"success"、"loading"  
            //         success: function () { },//接口调用成功
            //         fail: function () { },  //接口调用失败的回调函数  
            //         complete: function () { } //接口调用结束的回调函数  
            //     })
            // }

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    checkusercousre: function (para) {
        //vip课程
        //检测用户是否这个课程的权限
        Apps.islogin();
        const that = this;
        let url = Apps.globalData.baseUrl + '/user/checkusercousre'
        var param = {
            user_id: wx.getStorageSync('wxuserid'),
            courseid: para,
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {

            console.log('// 检查一下用户是否有课程权限 cousreshow=true');
            console.log(res);
            console.log(res.data);
            // 检查一下用户是否有课程权限 cousreshow=true
            that.setData({
                cousreshow: res.data.iscousreshow,
                isvip:res.data.isvip
            });

            if(res.data.isvip==false && that.data.viptype_id==2){

                that.checkuserscource(wx.getStorageSync('wxuserid'), that.data.cousrceid, that.data.headpicvalue);
            }

        }).catch((errMsg) => {
            console.log(errMsg);
        });

    },
    getpicchekc: function (params) {
        // let url="https://api.weixin.qq.com/wxa/media_check_async?access_token="+wx.getStorageSync('accesstoen');
        let url = Apps.globalData.baseUrl + '/user/checkpic'
        var param = {
            "openid": wx.getStorageSync('wxopenid'),
            "scene": 2,
            "version": 2,
            "media_url": params,
            "media_type": 2,
            "version": 2,
            "access_token": wx.getStorageSync('accesstoen')
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {

            console.log("res===========");
            console.log(res);

            this.setData({
                ispic: 1
            })


        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    pageScrollToBottom() {
        wx.createSelectorQuery().select('#time-mo').boundingClientRect(function (rect) {

            rect.id      // 节点的ID
            rect.dataset // 节点的dataset
            rect.left    // 节点的左边界坐标
            rect.right   // 节点的右边界坐标
            rect.top     // 节点的上边界坐标
            rect.bottom  // 节点的下边界坐标
            rect.width   // 节点的宽度
            rect.height
            console.log(rect.bottom * 20)
            // 使页面滚动到底部
            wx.pageScrollTo({
                scrollTop: rect.bottom * 20
            })
        }).exec()
    },

})