<template>
	<div class="mt-2 el-card p-14">
		<div class="flex justify-between">
			<Title left-line :title="'版本用户来源'">
				<template #default>
					<el-popover class="box-item" placement="right" trigger="hover" width="300">
						<template #reference>
							<el-icon class="ml-1" style="color: #a4b8cf"><QuestionFilled /></el-icon>
						</template>
						<template #default>
							<div class="ant-popover-inner-content">
								<div class="um-page-tips-content" style="line-height: 24px">
									<p><span class="highlight">新增用户：</span><span>第一次启动应用的用户（以设备为判断标准）</span></p>
									<p>
										<span class="highlight">活跃用户：</span><span>启动过应用的用户（去重），启动过一次的用户即视为活跃用户，包括新用户与老用户</span>
									</p>
									<p>
										<span class="highlight">启动次数：</span
										><span
											>打开应用视为启动。完全退出或后台运行超过30s后再次进入应用，视为一次新启动。开发过程中可以通过setSessionContinueMills来自定义两次启动的间隔，默认30s</span
										>
									</p>
									<p><span class="highlight">升级用户：</span><span>从其他版本升级到该版本的用户（以设备为判断标准）</span></p>
									<p>
										<span
											>如果当日用户先启动老版本然后升级到新版本，分版本查看数据时，此用户在新老版本都会被算为活跃用户（按总体查看数据时不受影响）</span
										>
									</p>
								</div>
							</div>
						</template>
					</el-popover>
				</template>
			</Title>
		</div>
		<div class="flex items-center justify-between mt-3">
			<el-select v-model="channelDistribution" style="width: 180px" placeholder="新增用户渠道分布">
				<el-option label="新增用户渠道分布" value="distribution" />
			</el-select>
			<div class="ml-4">
				<el-radio-group v-model="timeRange">
					<el-radio-button label="yesterday">昨天</el-radio-button>
					<el-radio-button label="7days">过去7天</el-radio-button>
					<el-radio-button label="30days">过去30天</el-radio-button>
				</el-radio-group>
			</div>
		</div>

		<!-- 水平柱状图 -->
		<div class="mt-4">
			<div class="relative">
				<div ref="barChartRef" style="width: 100%; height: 200px"></div>
			</div>
		</div>

		<!-- 明细表格 -->
		<div class="mt-4">
			<div class="flex items-center justify-between mb-2">
				<div class="text-base font-medium cursor-pointer select-none ml-3 items-center flex text-[#167AF0]" @click="showDetail = !showDetail">
					{{ showDetail ? '收起明细数据' : '展开明细数据' }} <el-icon class="ml-2"><ArrowDown v-if="showDetail" /> <ArrowUp v-else /> </el-icon>
				</div>

				<div>
					<el-button>导出</el-button>
				</div>
			</div>
			<el-table v-if="showDetail" :data="pagedSourceRows" border>
				<el-table-column prop="channel" label="渠道" min-width="140" />
				<el-table-column prop="newUsers" label="新增用户" min-width="140" />
				<el-table-column prop="upgradeRatio" label="升级用户比例" min-width="140" />
			</el-table>
			<div v-if="showDetail" class="flex justify-end mt-3">
				<el-pagination
					v-model:current-page="sourcePage"
					v-model:page-size="sourcePageSize"
					background
					layout="total, prev, pager, next, sizes"
					:total="sourceRows.length"
					:page-sizes="[5, 10, 20]"
				/>
			</div>
		</div>
	</div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch, computed, defineAsyncComponent } from 'vue';
import * as echarts from 'echarts';
import { useI18n } from 'vue-i18n';
import { QuestionFilled } from '@element-plus/icons-vue';

const { t } = useI18n();

const props = defineProps<{
	selectedVersion?: string;
}>();

const channelDistribution = ref('distribution');
const timeRange = ref('yesterday');
const barChartRef = ref<HTMLDivElement | null>(null);
let barChartInstance: echarts.ECharts | null = null;

const sourceRows = ref<Array<{ channel: string; newUsers: number; upgradeRatio: string }>>([
	{ channel: '渠道A', newUsers: 100, upgradeRatio: '10%' },
	{ channel: '渠道B', newUsers: 80, upgradeRatio: '15%' },
	{ channel: '渠道C', newUsers: 70, upgradeRatio: '20%' },
	{ channel: '渠道D', newUsers: 60, upgradeRatio: '25%' },
	{ channel: '渠道E', newUsers: 50, upgradeRatio: '30%' },
	{ channel: '渠道F', newUsers: 40, upgradeRatio: '35%' },
	{ channel: '渠道G', newUsers: 30, upgradeRatio: '40%' },
	{ channel: '渠道H', newUsers: 20, upgradeRatio: '45%' },
	{ channel: '渠道I', newUsers: 10, upgradeRatio: '50%' },
	{ channel: '渠道J', newUsers: 5, upgradeRatio: '55%' },
]);

function initBarChart(): void {
	if (!barChartRef.value) return;
	if (barChartInstance) barChartInstance.dispose();
	barChartInstance = echarts.init(barChartRef.value);
	const option: echarts.EChartsOption = {
		tooltip: { trigger: 'axis' },
		grid: { left: 40, right: 20, top: 20, bottom: 30 },
		xAxis: {
			type: 'value',
			axisLine: { show: false },
			splitLine: { lineStyle: { color: '#f3f4f6' } },
			axisLabel: { color: '#6b7280' },
		},
		yAxis: {
			type: 'category',
			data: sourceRows.value.map((d) => d.channel),
			axisLine: { lineStyle: { color: '#e5e7eb' } },
			axisLabel: { color: '#6b7280' },
			axisTick: { alignWithLabel: true },
		},
		series: [
			{
				name: '新增用户',
				type: 'bar',
				barWidth: '60%',
				itemStyle: { color: '#409EFF' },
				data: sourceRows.value.map((d) => d.newUsers),
			},
		],
	};
	barChartInstance.setOption(option);
}

onMounted(() => {
	initBarChart();
});

watch(timeRange, () => {
	initBarChart();
});

watch(
	() => props.selectedVersion,
	() => {
		initBarChart();
	}
);

const sourcePage = ref(1);
const sourcePageSize = ref(5);
const pagedSourceRows = computed(() => {
	const startIndex = (sourcePage.value - 1) * sourcePageSize.value;

	return sourceRows.value.slice(startIndex, startIndex + sourcePageSize.value);
});

const Title = defineAsyncComponent(() => import('/@/components/Title/index.vue'));
const showDetail = ref(true);
</script>

<style lang="scss" scoped>
.highlight {
	color: #2196f3;
}
</style>


