<template>
	<view class="container">
		<view v-if="list.length === 0 && !loading" class="empty-state">
			<view class="empty-icon">📇</view>
			<view class="empty-title">认证列表为空</view>
			<view class="empty-desc">当前账号下暂无可实名的卡片</view>
		</view>

		<view class="card" v-for="item in list" :key="item.iccid">
			<view class="flex-row-g20">
				<view class="logo">
					<image :src="getCarrier(item.carrier_type).logo" mode="aspectFit"></image>
				</view>
				<view class="flex-col-g20">
					<view class="iccid">ICCID: {{ item.iccid }}</view>
					<view class="operator">运营商: {{ getCarrier(item.carrier_type).name }}</view>
					<view v-if="item.isDevice" class="slot">卡槽位: {{ item.slot_position || '-' }}</view>
				</view>
			</view>

			<view class="btn mt-30 flex-col-g20">
				<up-button class="btn-apple btn-primary" v-if="item.isRealName" type="primary">已实名</up-button>
				<up-button class="btn-apple btn-success" v-else type="success" @tap="toReal(item)">去实名</up-button>
			</view>
		</view>

		<view class="modal-overlay" v-if="showIccidModal" @tap="closeModal">
			<view class="modal-content" @tap.stop>
				<view class="modal-close" @tap="closeModal">✕</view>
				<view class="modal-body">
					<view class="modal-title">实名认证</view>
					<view class="iccid-value">{{ currentModalIccid }}</view>
					<view class="modal-button" @tap="doRealName">点击复制ICCID并跳转实名</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref, reactive, onMounted } from 'vue';
	import { assetApi, realnameApi } from '@/api/index.js';
	import { useUserStore } from '@/store/index.js';

	const userStore = useUserStore();

	let list = reactive([]);
	let loading = ref(false);
	let showIccidModal = ref(false);
	let currentModalIccid = ref('');
	let currentCard = ref(null);

	const carrierMap = {
		CMCC: '中国移动',
		CUCC: '中国联通',
		CTCC: '中国电信',
		CBN: '中国广电'
	};

	const carrierLogoMap = {
		CMCC: 'https://img2.baidu.com/it/u=915783975,1594870591&fm=253&fmt=auto&app=120&f=PNG?w=182&h=182',
		CUCC: 'https://img1.baidu.com/it/u=2816777816,1756344384&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
		CTCC: 'https://img2.baidu.com/it/u=139558247,3893370039&fm=253&fmt=auto?w=529&h=500',
		CBN: 'https://img1.baidu.com/it/u=3160680953,3401650303&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'
	};

	const getCarrier = (carrierType) => {
		return {
			name: carrierMap[carrierType] || '-',
			logo: carrierLogoMap[carrierType] || carrierLogoMap.CMCC
		};
	};

	const loadCards = async () => {
		loading.value = true;
		try {
			const assetData = await assetApi.getInfo(userStore.state.identifier);

			if (assetData.asset_type === 'device') {
				list.splice(0, list.length, ...(assetData.cards || []).map(card => ({
					iccid: card.iccid,
					carrier_type: card.carrier_type,
					slot_position: card.slot_position,
					isDevice: true,
					isRealName: card.real_name_status === 1
				})));
			} else if (assetData.iccid) {
				list.splice(0, list.length, {
					iccid: assetData.iccid,
					carrier_type: assetData.carrier_type,
					isDevice: false,
					isRealName: assetData.real_name_status === 1
				});
			} else {
				list.splice(0, list.length);
			}
		} catch (e) {
			console.error('加载卡列表失败', e);
		}
		loading.value = false;
	};

	const toReal = async (card) => {
		currentCard.value = card;
		currentModalIccid.value = card.iccid;
		showIccidModal.value = true;
	};

	const closeModal = () => {
		showIccidModal.value = false;
	};

	const doRealName = async () => {
		const iccid = currentModalIccid.value;
		closeModal();

		try {
			const data = await realnameApi.getLink(userStore.state.identifier, iccid, { showError: false });
			if (data.realname_url) {
				uni.setClipboardData({
					data: iccid,
					success: () => {
						uni.showToast({ title: 'ICCID已复制', icon: 'success' });
					}
				});
				setTimeout(() => {
					window.location.href = data.realname_url;
				}, 1500);
			}
		} catch (e) {
			console.error('获取实名链接失败', e);
			uni.showToast({ title: e?.msg || '获取实名链接失败', icon: 'none' });
		}
	};

	onMounted(() => {
		loadCards();
	});
</script>

<style lang="scss" scoped>
	.container {
		.card {
			.logo {
				width: 80rpx;
				height: 80rpx;
				border-radius: 120rpx;
				border: 1rpx solid var(--primary);
				overflow: hidden;
				image { width: 100%; height: 100%; }
			}
		}
	}

	.btn-apple {
		display: inline-flex;
		align-items: center;
		justify-content: center;
		border: none;
		border-radius: var(--radius-small);
		font-weight: 500;
		font-size: 26rpx;
		line-height: 1;
		cursor: pointer;
		padding: 20rpx;
		background: var(--gray-100);
		color: var(--text-primary);

		&.btn-primary { background: var(--primary); color: var(--text-inverse); }
		&.btn-success { background: var(--success); color: var(--text-inverse); }
	}

	.modal-overlay {
		position: fixed;
		top: 0; left: 0; right: 0; bottom: 0;
		background: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 9999;
	}

	.modal-content {
		background: var(--bg-primary);
		border-radius: var(--radius-large);
		width: 620rpx;
		max-width: 90%;
		overflow: hidden;
		position: relative;
	}

	.modal-close {
		position: absolute;
		top: 24rpx; left: 24rpx;
		width: 48rpx; height: 48rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 36rpx;
		color: var(--gray-600);
		cursor: pointer;
	}

	.modal-body {
		padding: 60rpx 32rpx 32rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: 32rpx;
	}

	.modal-title {
		font-size: 32rpx;
		font-weight: 600;
		color: var(--text-primary);
		text-align: center;
	}

	.iccid-value {
		font-size: 40rpx;
		font-weight: bold;
		color: var(--primary);
		text-align: center;
		word-break: break-all;
		line-height: 1.4;
	}

	.modal-button {
		padding: 24rpx 32rpx;
		background: var(--success);
		color: var(--text-inverse);
		font-size: 28rpx;
		font-weight: 500;
		text-align: center;
		border-radius: var(--radius-small);
		cursor: pointer;
	}

	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 120rpx 40rpx;
		min-height: 400rpx;
		.empty-icon { font-size: 120rpx; margin-bottom: 30rpx; opacity: 0.6; }
		.empty-title { font-size: 32rpx; font-weight: 600; color: var(--text-primary); margin-bottom: 16rpx; }
		.empty-desc { font-size: 26rpx; color: var(--text-tertiary); text-align: center; }
	}
</style>
