<script setup>
	// 选择投流的收藏视频
	import {
		ref,
		reactive,
		getCurrentInstance
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	// 
	import api from '@/api/index.js'
	// 
	import util from '@/common/js/util.js'
	// 收藏列表
	import collectList from '@/components/index/collectList.vue'
	// 视频菜单
	import videoMenu from '@/components/index/videoMenu.vue';
	// 
	const {
		proxy
	} = getCurrentInstance()
	// 当前收藏夹对象
	const collects = ref({})
	// 选择的视频id
	const selectIds = reactive([])
	// 列表数据
	const list = reactive({
		data: [],
		pageSize: 10,
		pageNum: 1,
		total: 0,
	})

	onLoad((option) => {
		if (option.ids) selectIds.push(...option.ids.split(','))
	})

	/**
	 * 点击收藏列表
	 * @param {Object} item
	 */
	function handleCollects(item) {
		// console.log('点击收藏列表', item)
		collects.value = item
		refreshList()
	}

	// 刷新列表
	function refreshList() {
		list.pageNum = 1
		list.total = 0
		getList()
	}

	// 获取更多列表
	function getMoreList() {
		if (list.data.length >= list.total) return
		list.pageNum++
		getList()
	}

	// 获取收藏夹下的视频
	function getList() {
		// 
		api.video.getCollectVideoList({
			query: {
				collectId: collects.value.id,
			}
		}).then(rs => {
			if (rs.code == 200) {
				if (list.pageNum == 1) list.data.length = []
				list.data.push(...rs.rows.map(item => {
					item.format_videoUrl = util.format_url(item.videoUrl, 'video')
					item.format_imageUrl = util.format_url(item.imageUrl, 'img')
					return item
				}))
				list.total = rs.total

				proxy.$refs.select.open()
				return
			}
			util.alert({
				content: rs.msg,
				showCancel: false,
			})
		})
	}

	// 选择视频
	function handleVideo(item) {
		uni.$emit('selectPushCollectVideo', item)
	}
</script>

<template>
	<view class="appbw">
		<view>
			<!--  -->
			<collectList :pageSize="20" ref="collectListRef" @handleItem="handleCollects" />
		</view>

		<!-- 选择 -->
		<uni-popup ref="select" type="bottom">
			<view class="selectAlt popBot df fdc bfff">
				<view class="header rows ptb20 plr20">
					<view class="title plr30 c333 f34">
						<text>作品</text>
						<text class="ml10">{{list.total}}</text>
					</view>

					<view class="fmid c999 f28" @click="refreshList">
						<uni-icons type="refreshempty" color="" />
						<text>刷新</text>
					</view>
				</view>

				<!-- 视频菜单 -->
				<scroll-view scroll-y="true" class="scroll" @scrolltolower="getMoreList">
					<videoMenu :list="list.data" v-model:ids="selectIds" mode="checkbox" @item="handleVideo" />
				</scroll-view>
			</view>
		</uni-popup>
	</view>
</template>

<style lang="scss" scoped>
	// 
	.selectAlt {

		.scroll {
			height: 70vh;
		}
	}
</style>