<template>
    <view class="video-search-page">
        <van-sticky style="background: #fff">
            <view style="padding:30rpx 34rpx">
            <searchBox 
                :focus="focus" 
                placeholder="关键字搜索" 
                @change="onChange"
                @search="onSearch"
            ></searchBox>
            </view>
        </van-sticky>
        <view class="empty-box" v-if="list.length==0&&finished">
            <image
                :src="globalImgUrls.activityNoAuth"
                mode="widthFix"
            />
            <view>暂无数据，试试别的搜索词吧~</view>
        </view>
        <view class="list-wrap">
            <view class="item" v-for="item in list" :key="item.community_video_id">
                <view class="title-box">
                    <text class="tag">{{item.chart_permission_name}}</text>
                    <text class="title">{{item.title}}</text>
                </view>
                <collectBox
                    :type="2"
                    :primaryId="item.community_video_id"
                    :collectId="item.collection_id"
                    class="share-btn collect-btn"
                />
                <button 
                    class="share-btn" 
                    open-type="share" 
                    :data-item="item">
                    <image class="share-img" src="@/static/share-icon.png" mode="aspectFill"/>
                </button>
                <view class="time">发布时间：{{item.publish_time}}</view>
                <view class="video-content">
                    <videoBox 
                        :videoInfo="getDanmuData(item)"
                        :curVideoId="curVideoId"
                        :showDanmu="showDanmu"
                        @videoPlay="handelClickPlay"
                        @ended="handleVideoEnd"
                        @pause="handleVideoPause"
                        @timeupdate="handleTimeUpdate"
                    />
                </view>
                <comment :showDanmu="showDanmu" :videoInfo="getCommentData(item)"></comment>
            </view>
        </view>
    </view>
</template>

<script>
import searchBox from '@/components/searchBox/searchBox.vue'
import {apiVideoList,apiVideoPlayLog} from '@/api/video'
import {apiViewLogUpdate} from '@/api/common'
import comment from '@/components/videoComment/comment.vue'
import collectBox from '@/components/collectBox/collectBox.vue'
import videoBox from '@/components/videoBox/videoBox.vue'
export default {
    components: {
        searchBox,
        comment,
        collectBox,
        videoBox
    },
    data() {
        return {
            searchVal:'',
            focus:true,
            list:[],
            finished:false,
            page:1,
            pageSize:10,

            curVideoId:0,
            curVideoTime:0,
            videoRecordId:0,

            showDanmu:true,//控制整个模块是否显示弹幕功能（没用了本来后台有个开关功能的 后来不要了 但是还是留着吧）
        }
    },
    onReachBottom() {
        if(this.finished) return
        this.page++
        this.getList()
    },
    onShareAppMessage({from,target}) {
        console.log(from,target);
        let path='/pages/video/videoList?videoId=0'
        let title='FICC视频社区'
        let imageUrl=''
        if(from=='button'){
            title=target.dataset.item.title
            path=`/pages/video/videoList?videoId=${target.dataset.item.community_video_id}`
            imageUrl=target.dataset.item.cover_img_url
        }
        return {
            title:title,
            path:path,
            imageUrl:imageUrl
        }
    },
    methods: {
         // 处理弹幕模块需要的数据
        getDanmuData(item){
            return {
                source:1,
                id:item.community_video_id,
                ...item
            }
        },
        // 处理评论模块需要的数据
        getCommentData(item){
            return {
                source:2,
                id:item.community_video_id,
                ...item
            }
        },
        onChange(e){
            this.searchVal=e
        },

        async onSearch(){
            this.finished=false
            this.page=1
            this.list=[]
            if(!this.searchVal){
                this.finished=true
                return
            }
            this.getList()
        },

        async getList(){
            this.curVideoId=0
            const res=await apiVideoList({
                page_index:Number(this.page),
                page_size:Number(this.pageSize),
                keywords:this.searchVal
            })
            if(res.code===200){
                let arr=res.data||[]
                this.list=[...this.list,...arr]
                if(arr.length===0){
                    this.finished=true
                }
            }
        },

        handelClickPlay(item){
            this.curVideoId=item.community_video_id
            // 记录播放
            apiVideoPlayLog({video_id:Number(item.community_video_id)}).then(res=>{
                if(res.code===200){
                    console.log('视频埋点成功');
                    this.videoRecordId=res.data
                }
            })
        },

        //视频播放结束
        handleVideoEnd(){
            setTimeout(() => {
                this.curVideoId=0
            }, 200);
        },

        //时长变化
        handleTimeUpdate(e){
            // console.log(this.curVideoId,e.detail.currentTime);
            this.curVideoTime=e.detail.currentTime
        },

        handleVideoPause(){
            // console.log(`视频 pause---${this.videoRecordId}----${this.curVideoTime}`);
            this.handleUpdateVideoPlayTime()
        },

        // 更新播放时长
        handleUpdateVideoPlayTime(){
            if(!this.videoRecordId) return
            apiViewLogUpdate({
                id:this.videoRecordId,
                stop_seconds:parseInt(this.curVideoTime),
                source:3
            }).then(res=>{
                console.log('更新播放时长成功');
            })
        }
    },
}
</script>

<style>
.van-sticky-wrap--fixed{
  background-color: #fff;
}
page{
    padding-bottom: 0;
}
</style>

<style lang="scss" scoped>
.video-search-page{
    .search-wrap {
        background-color: #fff;
        padding: 30rpx 34rpx;
        align-items: center;
        .menu-icon{
            width: 52rpx;
            height: 40rpx;
            display: block;
            flex-shrink: 0;
            margin-left: 30rpx;
        }
    } 
    
    .list-wrap{
        .item{
            padding: 30rpx 34rpx;
            position: relative;
            .title-box{
                margin-right: 110rpx;
            }
            .share-btn{
                position: absolute;
                top: 34rpx;
                right: 34rpx;
                background-color: transparent;
                width: 36rpx;
                height: 36rpx;
                line-height: 1;
                padding: 0;
                &::after{
                    border: none;
                }
            }
            .share-img{
                width: 32rpx;
                height: 32rpx;
            }
            .collect-btn{
                right: 96rpx;
            }
            .tag{
                color: #E4B478;
                background-color: #333;
                padding: 5rpx 20rpx;
                border-radius: 20rpx;
                font-size: 24rpx;
                margin-right: 26rpx;
            }
            .title{
                font-size: 32rpx;
                color: #000;
            }
            .video-content{
                width: 100%;
                height: 400rpx;
                margin: 30rpx 0 20rpx 0;
            }
            .time{
                font-size: 28rpx;
                color: #999;
                margin-top: 20rpx;
            }
        }
    }

    .empty-box{
        text-align: center;
        font-size: 32rpx;
        color: #999;
        padding-top: 150rpx;
        image{
            width: 80vw;
            margin-bottom: 57rpx;
        }
    }
}
</style>