<template>
	<view class="invaterecords_container">
		<!-- 提示信息 -->
		<view class="info_note">
			<text class="note_text">*邀请记录只展示邀请成功的用户</text>
		</view>
		
		<!-- 邀请记录列表 -->
		<view class="records_list">
			<view class="record_item" v-for="(record, index) in records" :key="index">
				<view class="record_left">
					<text class="phone_number">{{ maskPhone(record.inviteePhone) }}</text>
					<text class="invite_time">{{ formatTimestamp(record.createTime) }}</text>
				</view>
				<view class="record_right">
					<text class="status_text">邀请成功</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useIntegralStore } from '@/stores/integral';
import { showLoading, hideLoading } from '@/utils';

const integralStore = useIntegralStore();
const records = ref([]);

// 手机号脱敏处理：中间四位显示为 ****
const maskPhone = (phone) => {
	if (!phone) return '';
	const phoneStr = String(phone);
	// 如果是11位手机号，将中间4位替换为****
	if (phoneStr.length === 11) {
		return phoneStr.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
	}
	// 如果已经是脱敏格式（包含*），直接返回
	if (phoneStr.includes('*')) {
		return phoneStr;
	}
	// 其他情况返回原值
	return phoneStr;
};

// 将时间戳转为 "2025.09.11 23:23:23" 格式
const formatTimestamp = (timestamp) => {
	if (!timestamp) return '';
	
	const date = new Date(timestamp);
	
	// 检查日期是否有效
	if (isNaN(date.getTime())) return '';
	
	// 获取年月日时分秒
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0');
	const day = String(date.getDate()).padStart(2, '0');
	const hours = String(date.getHours()).padStart(2, '0');
	const minutes = String(date.getMinutes()).padStart(2, '0');
	const seconds = String(date.getSeconds()).padStart(2, '0');
	
	// 返回格式：2025.09.11 23:23:23
	return `${year}.${month}.${day} ${hours}:${minutes}:${seconds}`;
};

onMounted(async () => {
	showLoading('加载中...');
	try {
		await integralStore.getInvitationList();
		if (integralStore.invitationList?.success && integralStore.invitationList?.data) {
			records.value = integralStore.invitationList.data;
		}
	} catch (error) {
		console.error('获取邀请记录失败:', error);
	} finally {
		hideLoading();
	}
});
</script>

<style lang="less" scoped>
@import './invaterecords.less';
</style>
