<template>
	<view class="videoDetail">
		<uni-menubar :title="videoDetail.name" />
			<tm-sheet >
				<video id="myVideo" :src="videoDetail.path" controls show-play-btn show-center-play-btn
				:poster-for-crawler="videoDetail.thumb"></video>
				<view class="mt-20">
					<tm-button theme="blue" block @click="downVideo">下载视频</tm-button>
				</view>
			</tm-sheet>
			
	</view>
</template>

<script>
	export default {
		data() {
			return {
				videoDetail:{},
				card_id:""
			}
		},
		onLoad(option) {
      if(option){
				this.card_id = option.card_id
				this.$tm.vx.commit('card/cardId', option.card_id)
				this.videoDetail = JSON.parse(option.detail)
				this.$point.point(11, this.videoDetail.id, this.card_id)
			}
		},
		onShareAppMessage(res) {
			return {
				title: this.videoDetail.name,
				path: '/pages/list/videoDetail?detail='+JSON.stringify(this.videoDetail)+'&card_id='+this.card_id,
				success: () => {
					this.$point.point(12, this.videoDetail.id, this.card_id)
				}
			}
		},
		methods:{
			downVideo() {
				let that = this
				const downloadTask = uni.downloadFile({
					url: that.videoDetail.path,
					success: (data) => {
						if (data.statusCode === 200) {
							wx.saveVideoToPhotosAlbum({
								filePath: data.tempFilePath,
								success: (res) => {
									uni.showToast({
										icon: 'none',
										mask: true,
										title: '视频已保存!',
										duration: 3000,
									});
									that.$point.point(13, that.videoDetail.id, that.card_id)
								}
							})
						}
					},
					fail: (err) => {
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '失败请重新下载',
						});
					},
				});
				downloadTask.onProgressUpdate((res) => {
					if (res.progress < 101) {
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '正在下载视频' + res.progress + '%',
						});
					} else {
						downloadTask.abort() // 取消下载任务
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.videoDetail {}
</style>
