<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>
                <video
                    autoplay
                    object-fit="contain"
                    show-mute-btn
                    show-background-playback-button
                    :poster="item.cover_img_url"
                    :src="item.video_url"
                    enable-play-gesture
                    :id="item.community_video_id"
                    @ended="handleVideoEnd"
                    @pause="handleVideoPause"
                    @timeupdate="handleTimeUpdate"
                    v-if="item.community_video_id==curVideoId"
                ></video>
                <image @click="handelClickPlay(item)" v-else class="poster" :src="item.cover_img_url" mode="aspectFill" lazy-load/>
                <comment :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'
export default {
    components: {
        searchBox,
        comment,
        collectBox
    },
    data() {
        return {
            searchVal:'',
            focus:true,
            list:[],
            finished:false,
            page:1,
            pageSize:10,

            curVideoId:0,
            curVideoIns:null,
            curVideoTime:0,
            videoRecordId:0,
        }
    },
    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: {
        // 处理评论模块需要的数据
        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
            setTimeout(() => {
                this.curVideoIns=uni.createVideoContext(this.curVideoId.toString())
            }, 300);
            // 记录播放
            apiVideoPlayLog({video_id:Number(item.community_video_id)}).then(res=>{
                if(res.code===200){
                    console.log('视频埋点成功');
                    this.videoRecordId=res.data
                }
            })
        },

        //视频播放结束
        handleVideoEnd(){
            // 此处因为如果不调用退出全屏方法 安卓和ios页面均会表现异常，安卓横屏不恢复竖屏，ios底部tabbar渲染异常
            this.curVideoIns.exitFullScreen()
            setTimeout(() => {
                this.curVideoId=0
                this.curVideoIns=null
            }, 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{
                width: 100%;
                height: 400rpx;
                margin: 30rpx 0 20rpx 0;
                border-radius: 20rpx;
            }
            .poster{
                width: 100%;
                height: 400rpx;
                margin: 30rpx 0 20rpx 0;
                border-radius: 20rpx;
                position: relative;
                &::after{
                    content:'';
                    display: block;
                    position: absolute;
                    width: 120rpx;
                    height: 120rpx;
                    top: 50%;
                    left: 50%;
                    transform: translate(-50%,-50%);
                    background-image: url('@/static/video-play-btn.png');
                    background-size: cover;
                }
            }
            .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>