// pages/moments/list/momentslist.js
const app = getApp()
var innerAudioContext = wx.createInnerAudioContext()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        columnId: '',
        type: '',
        speedStep: 5, //快进快退秒数
        waitFlag: false,
        configMode: '2', //1单条数据  2多条数据
        dataList: [],
        trendsList: [{
            auto: false,
            seeMore: false,
            text: '小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟',
        }, {
            auto: false,
            seeMore: false,
            text: '小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟小老弟',
        }],
        photoList: [{
                path: 'https://t7.baidu.com/it/u=2604797219,1573897854&fm=193&f=GIF'
            },
            {
                path: 'https://t7.baidu.com/it/u=2942499027,2479446682&fm=193&f=GIF'
            },
            {
                path: 'https://t7.baidu.com/it/u=3165657288,4248157545&fm=193&f=GIF'
            },
            {
                path: 'https://t7.baidu.com/it/u=3240224891,3518615655&fm=193&f=GIF'
            },
            {
                path: 'https://t7.baidu.com/it/u=3240224891,3518615655&fm=193&f=GIF'
            }
        ],
        videoList: [{
            imgPath: '../../../images/ic_temp.png',
            path: 'https://media.w3.org/2010/05/sintel/trailer.mp4'
        }],
        locationList: [{
            name: '满都海公园',
            address: '乌兰察布西街南',
            latitude: 40.816084,
            longitude: 111.688774
        }],
        audioList: [{
            id: '', //id
            path: 'http://em.sycdn.kuwo.cn/0e15005756dcda91a39bec26dd824614/60d99781/resource/n3/30/66/2205407643.mp3', //地址
            isPlay: false, //是否播放中
            duration: 100, //时长
            curDuation: 0,
            curDurationStr: '00:00',
            totalDurationStr: '00:56'
        }, {
            id: '', //id
            path: 'http://em.sycdn.kuwo.cn/0e15005756dcda91a39bec26dd824614/60d99781/resource/n3/30/66/2205407643.mp3', //地址
            isPlay: false, //是否播放中
            duration: 100, //时长
            curDuation: 0,
            curDurationStr: '00:00',
            totalDurationStr: '00:56'
        }],
        linkList: [{
            url: 'https://www.baidu.com'
        }, {
            url: 'https://www.baidu.com'
        }, {
            url: 'https://www.baidu.com'
        }],
        count: 2,
        isLoadMore: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const query = wx.createSelectorQuery()
        let _self = this
        console.log(options.type)
        _self.setData({
            columnId: options.id,
            configMode: options.mode,
            type: options.type
        })
        query.selectAll('.textFour_box').boundingClientRect(res => {
            var lineHeight = 26
            for (var i = 0; i < res.length; i++) {
                if (res[i].height / lineHeight > 3) {
                    _self.data.trendsList[i].auto = true
                    _self.data.trendsList[i].seeMore = true
                }
            }
            _self.setData({
                trendsList: _self.data.trendsList
            })
        }).exec()
        query.select('#it').boundingClientRect(res => {
            if (res.height > app.globalData.windowHeight) {

            }
        }).exec()
        _self.getList()
    },
    //获取栏目数据
    getList() {
        var _self = this
        wx.showLoading({})
        app.http.get(app.urls.getMomentsList.format({
                configColumnId: _self.data.columnId
            }), {
                header: {
                    token: app.globalData.token
                }
            })
            .then(res => {
                wx.hideLoading({})
                _self.setData({
                    dataList: res.data
                })
            })
            .catch(err => {
                console.log(err)
            })
    },
    //展开更多
    toggleHandler: function (e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        for (var i = 0; i < that.data.trendsList.length; i++) {
            if (index == i) {
                that.data.trendsList[index].auto = true;
                that.data.trendsList[index].seeMore = false;
            }
        }
        that.setData({
            trendsList: that.data.trendsList
        })
    },
    //收起更多
    toggleContent: function (e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        for (var i = 0; i < that.data.trendsList.length; i++) {
            if (index == i) {
                that.data.trendsList[index].auto = true;
                that.data.trendsList[index].seeMore = true;
            }
        }
        that.setData({
            trendsList: that.data.trendsList
        })
    },
    //预览图片
    viewImg(e) {
        wx.previewImage({
            urls: [e.currentTarget.dataset.url],
        })
    },
    viewVideo(e) {
        var url = e.currentTarget.dataset.url
        wx.previewMedia({
            sources: [{
                url: url,
                type: 'video'
            }],
        })
    },
    onPullDownRefresh() {
        wx.stopPullDownRefresh({})
    },
    onReachBottom() {
        console.log('触底了')
    },
    play(e) {

        //进行播放
        var index = e.currentTarget.dataset.index
        var item = e.currentTarget.dataset.item
        var _self = this
        _self.data.audioList.forEach(it => {
            if (it.isPlay) {
                innerAudioContext.stop()
            }
            it.isPlay = false
            it.curDuation = 0
            it.curDuationStr = '00:00'
        })
        if (!item.isPlay) {
            // innerAudioContext.stop()
            //找到该文件
            innerAudioContext = wx.createInnerAudioContext()
            // 播放音频文件
            innerAudioContext.src = _self.data.audioList[index].path
            innerAudioContext.autoplay = true
        }
        //先需要停止其他录音文件的播放

        innerAudioContext.onPlay(() => {
            _self.data.audioList[index].isPlay = true
            _self.setData({
                audioList: _self.data.audioList
            })
        });

        innerAudioContext.onCanplay((res) => {
            if (_self.data.waitFlag) {
                innerAudioContext.play()
                _self.setData({
                    waitFlag: false
                })
            }
        })
        innerAudioContext.onStop((res) => {
            _self.data.audioList[index].isPlay = false
            _self.data.audioList[index].curDuration = 0
            _self.data.audioList[index].curDurationStr = '00:00'
            _self.setData({
                audioList: _self.data.audioList,
            })
        })
        innerAudioContext.onEnded((res) => {
            _self.data.audioList[index].isPlay = false
            _self.data.audioList[index].curDuration = 0
            _self.data.audioList[index].curDurationStr = '00:00'
            _self.setData({
                audioList: _self.data.audioList,
            })
        })
        innerAudioContext.onError((res) => {
            _self.data.audioList[index].isPlay = false
            _self.data.audioList[index].curDuration = 0
            _self.data.audioList[index].curDurationStr = '00:00'
            _self.setData({
                audioList: _self.data.audioList,
            })
        })
        innerAudioContext.onSeeking(() => {
            console.log(innerAudioContext.currentTime)
        })
        innerAudioContext.onWaiting(() => {
            innerAudioContext.pause()
            _self.setData({
                waitFlag: true
            })
        })
        innerAudioContext.onTimeUpdate(() => {
            if (innerAudioContext.duration != Infinity) {
                _self.data.audioList[index].curDuration = parseInt(innerAudioContext.currentTime)
                _self.data.audioList[index].duration = parseInt(innerAudioContext.duration)
                console.log(innerAudioContext.duration)
                var curM = parseInt(innerAudioContext.currentTime / 60) //分钟
                var curS = parseInt(innerAudioContext.currentTime % 60) //秒
                var tM = parseInt(innerAudioContext.duration / 60) //分钟
                var tS = parseInt(innerAudioContext.duration % 60) //秒
                var curMStr = curM > 9 ? curM : '0' + curM
                var curSStr = curS > 9 ? curS : '0' + curS
                var totalMStr = tM > 9 ? tM : '0' + tM
                var totalSStr = +tS > 9 ? tS : '0' + tS
                _self.data.audioList[index].curDurationStr = curMStr + ':' + curSStr
                _self.data.audioList[index].totalDurationStr = totalMStr + ':' + totalSStr
                _self.setData({
                    audioList: _self.data.audioList
                })
            }
        })
    },
    //倒退5秒
    rewind(e) {
        var _self = this
        var index = e.currentTarget.dataset.index
        var item = e.currentTarget.dataset.item
        if (item.isPlay) {
            _self.data.audioList[index].curDuration = _self.data.audioList[index].curDuration - _self.data.speedStep
            innerAudioContext.seek(_self.data.audioList[index].curDuration)
            _self.setData({
                audioList: _self.data.audioList
            })
        }

    },
    //快进5秒
    speed(e) {
        var _self = this
        var index = e.currentTarget.dataset.index
        var item = e.currentTarget.dataset.item
        if (item.isPlay) {
            _self.data.audioList[index].curDuration = _self.data.audioList[index].curDuration + _self.data.speedStep
            innerAudioContext.seek(_self.data.audioList[index].curDuration)
            _self.setData({
                audioList: _self.data.audioList
            })
        }
    },
    slider4change(e) {
        var index = e.currentTarget.dataset.index
        var item = e.currentTarget.dataset.item
        if (item.isPlay) {
            innerAudioContext.seek(e.detail.value)
        }
    },
    //删除
    delItem(e) {

    },
    doPublish() {
        wx.navigateTo({
            url: '../publish/momentpublishline?id=' + this.data.columnId,
        })
    }
})