<template>
	<view class="container">
		<view class="card" v-for="item in mchList" :key="item.iccid">
			<view class="flex-row-sb mt-30">
				<view class="flex-row-g20">
					<view class="logo">
						<image :src="getLogo(item.category).logo" mode="aspectFit"></image>
					</view>
					<view class="flex-col-g20">
						<view class="flex-row-g20">
							<view class="iccid">{{ item.iccid }}</view>
							<view class="operator">
								<up-tag type="success" size="mini">{{ getLogo(item.category).name }}</up-tag>
							</view>
						</view>
						<view class="flex-row-g20">
							<view class="operator">
								<up-tag type="primary" size="mini" v-if="item.is_current">当前使用</up-tag>
							</view>
							<view class="operator">
								<up-tag :type="item.real_name_status === 1 ? 'primary' : 'success'" size="mini">{{ item.real_name_status === 1 ? '已实名' : '未实名' }}</up-tag>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="btn flex-row-g20 mt-30">
				<up-button class="btn-apple btn-success" v-if="!item.is_current" type="success" @tap="switchOperator(item)" :loading="switching">
					切换此运营商
				</up-button>
			</view>
		</view>
	</view>
</template>

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

	const userStore = useUserStore();

	let mchList = reactive([]);
	let switching = ref(false);

	let opratorList = reactive([
		{ category: '124', logo: 'https://img2.baidu.com/it/u=139558247,3893370039&fm=253&fmt=auto?w=529&h=500', name: '中国电信' },
		{ category: '125', logo: 'https://img1.baidu.com/it/u=2816777816,1756344384&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500', name: '中国联通' },
		{ category: '126', logo: 'https://img2.baidu.com/it/u=915783975,1594870591&fm=253&fmt=auto&app=120&f=PNG?w=182&h=182', name: '中国移动' }
	]);

	const getLogo = (category) => {
		const operator = opratorList.find(item => item.category === category);
		return operator || opratorList[0];
	};

	const loadCards = async () => {
		try {
			const data = await deviceApi.getCards(userStore.state.identifier);
			if (data.cards && data.cards.length > 0) {
				mchList.splice(0, mchList.length, ...data.cards.map(card => ({
					iccid: card.iccid,
					carrier_name: card.carrier_name,
					is_current: card.is_active,
					real_name_status: card.real_name_status,
					slot_position: card.slot_position,
					category: getCarrierCategory(card.carrier_name)
				})));
			}
		} catch (e) {
			console.error('加载卡列表失败', e);
		}
	};

	const getCarrierCategory = (carrierName) => {
		if (!carrierName) return '126';
		if (carrierName.includes('电信')) return '124';
		if (carrierName.includes('联通')) return '125';
		if (carrierName.includes('移动')) return '126';
		return '126';
	};

	const switchOperator = async (item) => {
		switching.value = true;
		try {
			await deviceApi.switchCard(userStore.state.identifier, item.iccid);
			uni.showToast({ title: '切换成功，3-5分钟后生效', icon: 'success' });
			loadCards();
		} catch (e) {
			console.error('切换运营商失败', e);
		}
		switching.value = false;
	};

	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-success {
			background: var(--success);
			color: var(--text-inverse);
		}
	}
</style>