<template>
	<view class="container">
		<image class="topbg" :src="info.thumbnail" mode="aspectFill"></image>
		<view class="container-body">
			<view class="coursebox">
				<view class="coursebox-info">
					<view class="coursebox-name">
						<text class="more-t">{{info.title}}-{{info.courseName}}</text>
						<view class="btn_icon" @click="copyId">ID
							<image src="https://fbylive.obs.cn-southwest-2.myhuaweicloud.com:443/app/images/copy_icon.png" mode="aspectFill"></image>
						</view>
					</view>
					<view style="margin-top: 5px;">{{info.courseName}}</view>
					<view class="coursebox-time x-start">
						<!-- <view class="coursebox-time-item" style="margin-right: 14px;">
							<view>直播时间</view>
							<view class="coursebox-time-t">{{info.createTime}}</view>
						</view> -->
						<view class="coursebox-time-item duration">
							<view class="fs24">课程时长</view>
							<view class="coursebox-time-t">
							{{formatSeconds(info.duration || 0,1)}}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="statistics">
				<u-tabs :list="list1" :current='current' :scrollable="false" :lineWidth="40" lineColor="#1677ff"
					:activeStyle="activeStyle" :inactiveStyle="inactiveStyle" @click="clickTab"></u-tabs>
				<u-collapse :border='false' :value='collapseValue' @change="changeCollapse" v-if="current == 0">
					<!-- #ifdef MP-WEIXIN -->
					<u-collapse-item name="live" title="课程数据">
					<!-- #endif -->
					<!-- #ifndef MP-WEIXIN -->
					<u-collapse-item name="live">
						<text slot="title" class="bold fs32">课程数据</text>
					<!-- #endif -->
						<view slot="value"
							class="statistics-slot-title">{{collapseValue.includes('live')?'收回':'展开'}}</view>
						<view slot="right-icon">
							<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
						</view>
						<view class="collapse-content x-ac">
							<view class="collapse-content-item">
								<view class="collapse-content-title">观看人数</view>
								<view class="collapse-content-num"><text>{{courseCount.courseWatchNum || 0}}</text>人</view>
							</view>
							<view class="collapse-content-item">
								<view class="collapse-content-title">完播人数</view>
								<view class="collapse-content-num"><text>{{courseCount.courseCompleteNum || 0}}</text>人
								</view>
							</view>
							<view class="collapse-content-item">
								<view class="collapse-content-title">完播率</view>
								<view class="collapse-content-num"><text>{{courseCount.courseCompleteRate || 0}}</text>%</view>
							</view>
						</view>
					</u-collapse-item>
					<!-- #ifdef MP-WEIXIN -->
					<u-collapse-item name="questions" title="答题数据">
					<!-- #endif -->
					<!-- #ifndef MP-WEIXIN -->
					<u-collapse-item name="questions">
						<text slot="title" class="bold fs32">答题数据</text>
					<!-- #endif -->
						<view slot="value"
							class="statistics-slot-title">{{collapseValue.includes('questions')?'收回':'展开'}}</view>
						<view slot="right-icon">
							<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
						</view>
						<view class="collapse-content">
							<view class="x-ac">
								<view class="collapse-content-item">
									<view class="collapse-content-title">答题人数</view>
									<view class="collapse-content-num"><text>{{courseCount.answerNum || 0}}</text>人</view>
								</view>
								<view class="collapse-content-item">
									<view class="collapse-content-title">正确人数</view>
									<view class="collapse-content-num"><text>{{courseCount.answerRightNum || 0}}</text>人
									</view>
								</view>
								<view class="collapse-content-item">
									<view class="collapse-content-title">答题红包数</view>
									<view class="collapse-content-num"><text>{{courseCount.redPacketNum || 0}}</text>个
									</view>
								</view>
							</view>
							<view class="redenvelope x-bc">
								<view class="x-f">
									<image src="https://fbylive.obs.cn-southwest-2.myhuaweicloud.com:443/app/images/redenvelope.png" mode="aspectFill"></image>
									<text>答题红包金额</text>
								</view>
								<view class="collapse-content-num"><text>{{courseCount.redPacketAmount || '0.00'}}</text>元
								</view>
							</view>
						</view>
					</u-collapse-item>
					<!-- #ifdef MP-WEIXIN -->
					<u-collapse-item name="funnelplot" title="转化漏斗图">
					<!-- #endif -->
					<!-- #ifndef MP-WEIXIN -->
					<u-collapse-item name="funnelplot">
						<text slot="title" class="bold fs32">转化漏斗图</text>
					<!-- #endif -->
						<view slot="value"
							class="statistics-slot-title">{{collapseValue.includes('funnelplot')?'收回':'展开'}}</view>
						<view slot="right-icon">
							<u-icon name="arrow-right" color="#1677ff" size="12"></u-icon>
						</view>
						<view style="height: 600rpx;" v-if="!ratelistState">
							<funnelChart :getratelist="getrateimg"/>
						</view>
						<view v-show="ratelistState" class="center">暂无数据</view>
					</u-collapse-item>
				</u-collapse>
			</view>
			<!-- 参与记录 -->
			<view v-if="current == 1">
				<view class="participate-search">
					<view class="x-bc">
						<view class="flex-1">
							<u-search placeholder="会员ID、昵称、姓名、手机" v-model="keyword" :showAction="false"
								height="30px" @search="getkeyword"></u-search>
						</view>
						<view>
							<u-button type="primary" class="refresh" color='#1677ff' size="small" :disabled="disabled"
								text="刷新" @click="refresh"></u-button>
						</view>
						
					</view>
					<view class="x-bc">
						<u-tabs :list="list2" :current='currentType' :lineWidth="40" lineColor="#1677ff"
							:activeStyle="activeStyle" :inactiveStyle="inactiveStyle" @click="clickTypeTab"></u-tabs>
						<!-- <view class="participate-order x-f">
							<image src="https://fbylive.obs.cn-southwest-2.myhuaweicloud.com:443/app/images/order_icon2.png" mode="aspectFill" v-if="searchTypeIndex == 3">
							</image>
							<image src="https://fbylive.obs.cn-southwest-2.myhuaweicloud.com:443/app/images/order_icon.png" mode="aspectFill" v-else></image>
							<picker @change="bindPickerChange" :value="searchTypeIndex" :range="typeArray">
								{{typeoption[searchTypeIndex]}}
							</picker>
						</view> -->
					</view>
				</view>
				<mescroll-body top="0" bottom="0" ref="mescrollRef" @init="mescrollInit" :down="downOption"
					:up="upOption" @down="downCallback" @up="upCallback">
					<view class="list">
						<view class="list-item" v-for="(item,index) in dataList" :key="index">
							<view class="list-item-head x-bc">
								<view class="x-f" style="flex: 1;overflow: hidden;">
									<u-avatar :src='item.avatar'></u-avatar>
									<view class="list-item-head-l">
										<view style="flex: 1;overflow: hidden;display: flex;">
											<text class="list-item-name one-t">{{item.nickName}}</text>
											<image class="list-item-copy" src="https://fbylive.obs.cn-southwest-2.myhuaweicloud.com:443/app/images/copy_icon.png"
												mode="aspectFill"></image>
										</view>
										<view class="list-item-re">
											注册时间：{{item.createTime?item.createTime.substring(0,10):'--'}}</view>
									</view>
								</view>
								<image class="phone" src="https://fbylive.obs.cn-southwest-2.myhuaweicloud.com:443/app/images/phone.png" mode="aspectFill"></image>
							</view>
							<view class="list-item-desc">
								<view class="taglist">
									<view v-for="(e,i) in item.tags">
										<u-tag :text="e.tag" size="mini" color="#999" bgColor="#f5f5f5"
											borderColor="#f5f5f5"></u-tag>
									</view>
								</view>
								<view style="margin-top: 5px;">
									<text class="label">观看次数</text><text
										class="value-num">{{item.watchCount || 0}}</text>
									<text class="label">完播次数</text><text
										class="value-num">{{item.finishCount || 0}}</text>
									<text class="label">累计时长</text><text
										class="value-num"> {{$formatSeconds(item.watchTime,1)}}</text>
										
								</view>
								<view style="margin-top: 5px;">
									<text class="label">红包领取状态</text>
									<!-- 发送中  1  已发送 -->
									<text
										class="value-num">{{item.redStatus == 1 ? '已发送': item.redStatus == 0 ? '发送中': item.redStatus}}</text>
								</view>
								<view style="margin-top: 5px;"><text class="label">观看时间</text><text
										class="value-num">{{item.watchDate}}</text></view>
							</view>
							<!-- <view class="list-item-footer x-f">
								<view class="list-item-footer-btn x-f" @click="openModel('name',item)">改姓名</view>
								<view class="list-item-footer-btn x-f" @click="openModel('remark',item)">改备注</view>
								<view class="list-item-footer-btn footer-tagbtn x-f" @click="openModel('tag',item)">改标签
								</view>
							</view> -->
						</view>
					</view>
				</mescroll-body>
			</view>
			<!-- 修改名字/备注-->
			<u-modal :show="modelShow" :title="modelTitle" content='content' class="model" :showCancelButton='true'
				@cancel="closeModel" @confirm="confirmModel">
				<view class="setTimebox">
					<u-input fontSize="14px" :placeholder="modelTitle == '修改姓名' ? '请输入姓名':''" v-model="inputVal"
						maxlength="5" style="width: 520rpx;"></u-input>
				</view>
			</u-modal>
			<!-- 修改标签 -->
			<u-popup :show="showTagSelect"  @close='closetagselect' :closeOnClickOverlay='true' mode="bottom">
				<view class=" w100 bgf">
					<view class="plr28 ptb16 justify-between" style="flex-direction: row-reverse">
						<u-icon class=""  name="close-circle" color="#ccc" size="24" @click="closetagselect"></u-icon>
					</view>
					<view class="justify-start p32">
						<view class="mlr10 " v-for="(item,index) in changetag" :key='item.tagId'>
							<u-tag :text="item.tag" :plain="!item.checked" :name="index"
								@click="choosechangeTag"></u-tag>
						</view>
					</view>
					<view class="justify-between p32">
						<view class="changetagbtn base-bg-f8" @click="showTagSelect=!showTagSelect">取消</view>
						<view class="changetagbtn colorf base-bg-blue" @click="suretagchangeAll">确定</view>
					</view>
				</view>
			</u-popup>
		</view>
	</view>
</template>

<script>
	import {
		getCompanyUserAndUserCount,
		getshopCoursenum,
		getUserLogListByCourseId,
		updateFsUser,
		getcompanyTaglist,
		changeLable,
		changeUserName,
		getcourseRates
	} from "@/api/courseManage.js"
	import funnelChart from '@/components/chart.vue'
	export default {
		components: {
			funnelChart,
		},
		data() {
			return {
				modelShow: false,
				showTagSelect:false,
				changetag:'',
				modelTitle: '',
				inputVal: '',
				info: {},
				list1: [{
					name: '课程数据',
				}, {
					name: '参与记录',
				}],
				current: 0,
				list2: [{
					name: '答题正确',
					label: '答题正确',
					value: 0
				}, {
					name: '仅播完',
					label: '仅播完',
					value: 2
				}, {
					name: '未完播',
					label: '未完播',
					value: 1
				}, {
					name: '未播放',
					label: '未播放',
					value: 3
				}],
				currentType: 0,
				activeStyle: {
					color: '#1677ff',
					fontSize: '14px',
					fontWeight: 'bold'
				},
				inactiveStyle: {
					fontSize: '14px'
				},
				collapseValue: ['live', 'questions', 'funnelplot'],
				keyword: '',
				disabled: false,
				searchTypeIndex: 0,
				typeArray: ['按看课时间从晚到早', '按完播时间从晚到早', '按注册时间从晚到早', '按会员姓名0-9-A-Z'],
				typeoption: ['看课时间', '完播时间', '注册时间', '会员姓名'],
				typeoptionOrder: ['desc', 'watchDesc', 'registerDesc', 'nameDesc'],
				mescroll: null,
				downOption: {
					auto: false //不要自动加载
				},
				upOption: {
					onScroll: false,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					textNoMore: "已经到底了",
					empty: {
						icon: 'https://fbylive.obs.cn-southwest-2.myhuaweicloud.com:443/app/images/empty.png',
						tip: '暂无数据'
					}
				},
				dataList: [],
				user: {},
				todayday: uni.$u.timeFormat(new Date(), 'yyyy-mm-dd'),
				courseCount: {},
				courseId: '',
				chooseUserId: '',
				companytag:[],
				selectidtag:[],
				getrateimg: {},
				ratelistState:false,
				videoId:''
			}
		},
		onLoad(option) {
			this.info = option.info ? JSON.parse(option.info) : {},
			this.videoId=this.info.videoId
			console.log(this.info)
			uni.setNavigationBarTitle({
				title: '数据统计'
			});
			this.user = uni.getStorageSync("companyUserInfo") ? JSON.parse(uni.getStorageSync("companyUserInfo")) : {}
			this.courseId = this.info.courseId || ''
			this.getrateList()
			if (this.courseId)
				this.getCount()
		},
		methods: {
			getkeyword(value){
				this.keyword=value
				this.upCallback()
			},
			//转化时间格式
			 formatSeconds(seconds) {
			  const hours = Math.floor(seconds / 3600);
			  const minutes = Math.floor((seconds % 3600) / 60);
			  const remainingSeconds = seconds % 60;
			  
			  // 补零函数：确保两位数显示
			  const pad = (num) => num.toString().padStart(2, '0');
			  
			  return `${pad(hours)}:${pad(minutes)}:${pad(remainingSeconds)}`;
			},
			getrateList(){
				//获取漏斗图
				const params={
					videoId:this.info.courseId,
				}
				getcourseRates(params).then(res=>{
					if(res.code==200){
						this.ratelistState = res.data.every(item => item.value === 0);
						this.getrateimg=res
						console.log(res)
					}else{
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			suretagchangeAll(){
				this.selectidtag = this.changetag.filter(item => item.checked).map(v=>v.tagId).join(',')
				this.changeLabelmore()
			},
			changeLabelmore() {
				//改标签选择按钮
				const selid=[]
				selid[0]=this.chooseUserId
				const params = {
					companyId: this.user.companyId,
					companyUserId: this.user.userId,
					creatTime: '',
					createBy: '',
					createTime: '',
					id: '',
					params: {},
					remark: '',
					searchValue: '',
					updateBy: '',
					tagIds:this.selectidtag,
					updateTime: '',
					userId: selid
				}
				changeLable(params).then(res => {
					if (res.code == 200) {
						const timer = setTimeout(function() {
							uni.showToast({
								icon: 'none',
								title: '标签更改成功'
							})
						}, 500);
						this.showTagSelect = !this.showTagSelect
						this.mescroll.resetUpScroll()
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			closetagselect() {
				this.showTagSelect = !this.showTagSelect
			},
			choosechangeTag(i) {
				this.changetag[i].checked = !this.changetag[i].checked
			},
			getcompanyTag() {
				getcompanyTaglist().then(res => {
					this.showCompanytag = res.data
					this.companytag = res.data.map(item => {
						return {
							...item,
							checked: false,
						}
					})
					this.changetag=this.companytag
				})
			},
			copyId() {
				uni.setClipboardData({
					data: this.info.courseName,
					success: () => {
						uni.showToast({
							title: '复制课程标题成功',
							icon: 'none'
						})
					}
				});
			},
			clickTab(e) {
				this.current = e.index
			},
			clickTypeTab(e) {
				this.currentType = e.index
				this.mescroll.resetUpScroll()
			},
			bindPickerChange(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.searchTypeIndex = e.detail.value
			},
			changeCollapse(e) {
				this.collapseValue = e.filter(item => item.status == 'open').map(it => it.name)
			},
			refresh() {
				this.mescroll.resetUpScroll()
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			upCallback(page) {
				//联网加载数据
				var that = this;
				var data = {
					videoId: this.videoId,
					type: this.list2[this.currentType].value, // 0:答题正确 1:未完播 2:已完播 3:未播放
					pageNum: page.num,
					pageSize: page.size,
					keyword:this.keyword
				};
				uni.showLoading({
					title: "加载中..."
				})
				// this.list2[this.currentType].name = this.list2[this.currentType].label
				getUserLogListByCourseId(data).then(res => {
					uni.hideLoading()
					if (res.code == 200) {
						//设置列表数据
						if (page.num == 1) {
							that.dataList = res.data.list;

						} else {
							that.dataList = that.dataList.concat(res.data.list);

						}
						// this.list2[this.currentType].name = this.list2[this.currentType].label + (res.data.total)
						that.mescroll.endBySize(res.data.list.length, res.data.total);

					} else {
						uni.showToast({
							icon: 'none',
							title: "请求失败",
						});
						that.dataList = null;
						that.mescroll.endErr();
					}
				});
			},
			getCount() {
				const param = {
					videoId: this.courseId,
					// endTime: this.todayday + ' 23:59:59',
					// startTime: this.todayday + ' 00:00:00',
				}
				this.getCourseCount(param)
			},
			getCourseCount(param) {
				getshopCoursenum(param).then(res => {
					if (res.code == 200) {
						this.courseCount = res.data
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg,
						});
					}
				})
			},
			openModel(type, item) {
				this.chooseUserId = item.userId
				if (type == 'name') {
					this.modelTitle = '修改姓名'
					this.inputVal = item.nickName || ''
					this.modelShow = true
				} else if (type == 'remark') {
					this.modelTitle = '修改备注'
					this.inputVal = item.remark || ''
					this.modelShow = true
				}else{
					this.getcompanyTag()
					this.showTagSelect=!this.showTagSelect
				}
			},
			closeModel() {
				this.modelShow = false
				this.inputVal = ''
			},
			confirmModel() {
				this.modelShow = false
				if (this.modelTitle == '修改姓名') {
					const param = {
						userId: this.chooseUserId,
						nickName: this.inputVal,
					}
					this.updateFsUser(param)
					// this.updataName()
					
				} else if (this.modelTitle == '修改备注') {
					const param = {
						userId: this.chooseUserId,
						remark: this.inputVal,
					}
					this.updateFsUser(param)
					// this.updataName()
				}
			},
			updateFsUser(param) {
				console.log(param)
				updateFsUser(param).then(res => {
					if (res.code == 200) {
						uni.showToast({
							icon: 'none',
							title: '标签更改成功'
						})
						this.redPacketCount = res.data
						this.mescroll.resetUpScroll()
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg,
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.checked-bg {
		border: 1px solid #1677ff !important;
		color: #1677ff !important;
		background-color: #e7f1fe !important;
	}
	.changetagbtn{
		width: 45%;
		height: 80rpx;
		border-radius: 50rpx;
		text-align: center;
		line-height: 80rpx;
	}
	.container {
		position: relative;
		z-index: 2;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 14px;
		color: #222;

		.topbg {
			position: absolute;
			z-index: -1;
			top: 0;
			left: 0;
			width: 100%;
			height: 280px;
		}

		&-body {}

		.coursebox {
			padding: 120px 50rpx 20px 50rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 14px;
			color: #888;

			.status {
				position: absolute;
				top: 0;
				right: 0;
				z-index: 2;
				height: 30px;
				padding: 0 8px;
				box-sizing: border-box;
				line-height: 30px;
				border-radius: 0 12px 0 12px;
				text-align: center;
				color: #fff;
				background-color: #08ce36;
			}

			&-info {
				padding: 12px;
				background-color: rgba(255, 255, 255, 0.8);
				border-radius: 12px;
				overflow: hidden;
				position: relative;
			}

			&-name {
				color: #222;
				font-weight: bold;
				font-size: 16px;
				padding-right: 50px;

				.more-t {
					flex: 1;
					font-size: 14px;
					color: #222;
					display: inline;
				}
			}

			&-time-item {
				flex: 1;
				margin-top: 5px;
			}

			&-time-t {
				color: #222;
				font-weight: bold;
				margin-top: 5px;
				font-size: 12px;
			}

			.duration {
				position: relative;
				// padding-left: 14px;

				&::after {
					content: "";
					height: 30px;
					width: 1px;
					background-color: #f5f5f5;
					position: absolute;
					left: 0;
					top: 5px;
				}
			}
		}
	}

	.statistics {
		background-color: #fff;

		&-title {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 16px;
			color: #222222;
		}

		&-title-tip {
			font-weight: 400;
			font-size: 11px;
			color: #999;
			padding-left: 10px;
		}

		&-slot-title {
			font-size: 12px;
			color: #1677ff;
		}

		.collapse-content {
			margin: 0 -8px -8rpx 0;

			&-item {
				flex: 1;
				padding: 12px;
				box-sizing: border-box;
				border-radius: 10px;
				background: #f5f5f5;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 10px;
				color: #222222;
				margin: 0 8px 8rpx 0;
			}

			&-title {
				font-size: 14px;
				margin-bottom: 10px;
			}

			&-num {
				color: #1677ff;
				font-size: 10px;

				text {
					font-family: DIN, DIN;
					font-weight: bold;
					font-size: 25px;
				}
			}
		}

		.redenvelope {
			background-color: #f5f5f5;
			margin-right: 8px;
			margin-top: 4px;
			padding: 15px;
			box-sizing: border-box;
			border-radius: 10px;
			color: #222222;

			image {
				width: 50px;
				height: 50px;
				margin-right: 10px;
			}
		}
	}

	.participate-search {
		background-color: #fff;
		padding: 10px 15px;

		.refresh {
			height: 26px;
			min-width: 40px;
			width: 40px;
			padding: 0;
			display: inline-flex;
			margin-left: 15px;
			border-radius: 5px;
			overflow: hidden;
		}
	}

	.participate-order {
		font-size: 12px;

		image {
			width: 15px;
			height: 15px;
		}
	}

	.btn_icon {
		font-weight: 400;
		font-size: 14px;
		color: #1677ff;
		display: inline-flex;
		align-items: center;
		margin-left: 5px;

		image {
			width: 20px;
			height: 20px;
		}
	}

	.list {
		padding: 20px 10px;
	}

	.list-item {
		padding: 10px;
		margin-bottom: 10px;
		background-color: #fff;
		border-radius: 12px;

		&-head {
			.phone {
				flex-shrink: 0;
				width: 30px;
				height: 30px;
			}

			&-l {
				flex: 1;
				overflow: hidden;
				margin-left: 10px;
				margin-right: 10px;
			}
		}

		&-copy {
			width: 20px;
			height: 20px;
		}

		&-re {
			font-size: 10px;
			margin-top: 5px;
		}

		&-desc {
			padding: 5px;
			color: #999;
			font-size: 12px;
		}

		.label {
			margin-right: 4px;
		}

		.value-num {
			margin-right: 18px;
			color: #222;
			font-weight: bold;
		}

		.taglist {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			color: #555;
			padding-top: 5px;

			view {
				margin: 0 5px 5px 0;
			}
		}

		&-footer {
			justify-content: flex-end;
			padding: 6px 0;

			.footer-tagbtn {
				color: #1677ff;
				background-color: #e7f2fe;
				border: 1px solid #c9e1fb;
			}

			&-btn {
				height: 26px;
				padding: 0 10px;
				margin-left: 10px;
				background-color: #f5f5f5;
				border-radius: 25px;
				font-weight: 400;
				font-size: 10px;
				line-height: 26px;
				border: 1px solid #f5f5f5;
				box-sizing: border-box;
			}
		}
	}
</style>