<template>
	<view class="mine-page">
		<view class="score-card">
			<text class="score-label">平台积分</text>
			<text class="score-value">¥ {{ platformScoreText }}</text>
		</view>

		<view class="fund-card" hover-class="fund-card-hover" @tap="goPropertyFund">
			<view class="fund-card-left">
				<image class="fund-icon-image" :src="fundIconUrl" mode="aspectFit" />
				<view class="fund-text">
					<text class="fund-title">物业基金</text>
					<text class="fund-desc">{{ propertyFundDesc }}</text>
				</view>
			</view>
			<view class="fund-action">
				<text class="fund-action-text">去查看</text>
				<text class="ri-arrow-right-s-line fund-action-arrow"></text>
			</view>
		</view>

		<view class="menu-card">
			<view class="menu-item" hover-class="menu-item-hover" @tap="goRecords">
				<text class="menu-label">付款记录</text>
				<text class="ri-arrow-right-s-line menu-arrow"></text>
			</view>
			<view class="menu-item no-border" hover-class="menu-item-hover" @tap="goScoreRecords">
				<text class="menu-label">积分流水</text>
				<text class="ri-arrow-right-s-line menu-arrow"></text>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { onPullDownRefresh, onShow } from '@dcloudio/uni-app';
import { platformScore } from '@/api/cashier';
import { resolveLoginPlatform } from '@/api/auth';
import { APP_CONFIG, PROPERTY_APP } from '@/config';

const IS_ALIPAY = resolveLoginPlatform() === 'mp-alipay';

const platformScoreText = ref('0.00');
const loading = ref(false);
const fundIconUrl = `${APP_CONFIG.PROPERTY_IMG_URL}property/new/cashier/fangwu.png`;
const propertyFundDesc = IS_ALIPAY
	? `请前往微信「${PROPERTY_APP.WECHAT_NAME}」查看`
	: '查看我的物业基金余额';

const loadPlatformScore = async () => {
	const response = await platformScore();
	if (Number(response?.code) !== 200) {
		throw new Error(response?.msg || '获取平台积分失败');
	}
	const score = Number(response?.data?.score || 0);
	platformScoreText.value = Number.isFinite(score) ? score.toFixed(2) : '0.00';
};

const refreshPage = async () => {
	loading.value = true;
	try {
		await loadPlatformScore();
	} catch (error: any) {
		uni.showToast({
			title: error?.message || '加载失败',
			icon: 'none'
		});
	} finally {
		loading.value = false;
		uni.stopPullDownRefresh();
	}
};

const copyPropertyWechatName = () => {
	uni.setClipboardData({
		data: PROPERTY_APP.WECHAT_NAME,
		success: () => {
			uni.showToast({
				title: '已复制，请到微信搜索',
				icon: 'none'
			});
		},
		fail: () => {
			uni.showToast({
				title: '复制失败，请手动搜索',
				icon: 'none'
			});
		}
	});
};

const showPropertyFundWechatTip = () => {
	uni.showModal({
		title: '查看物业基金',
		content: `请打开微信，搜索并进入「${PROPERTY_APP.WECHAT_NAME}」小程序，在「我的」中查看物业基金。`,
		cancelText: '知道了',
		confirmText: '复制名称',
		success: (res) => {
			if (res.confirm) {
				copyPropertyWechatName();
			}
		}
	});
};

const goPropertyFund = () => {
	if (IS_ALIPAY) {
		if (PROPERTY_APP.ALIPAY_APP_ID) {
			uni.navigateToMiniProgram({
				appId: PROPERTY_APP.ALIPAY_APP_ID,
				path: PROPERTY_APP.MINE_PATH,
				extraData: { from: 'cashier' },
				envVersion: APP_CONFIG.API_BASE_URL.includes('dev.') ? 'trial' : 'release',
				fail: () => {
					showPropertyFundWechatTip();
				}
			});
			return;
		}
		showPropertyFundWechatTip();
		return;
	}

	uni.navigateToMiniProgram({
		appId: PROPERTY_APP.APP_ID,
		path: PROPERTY_APP.MINE_PATH,
		extraData: { from: 'cashier' },
		envVersion: APP_CONFIG.API_BASE_URL.includes('dev.') ? 'trial' : 'release',
		fail: () => {
			uni.showToast({ title: '跳转失败，请稍后重试', icon: 'none' });
		}
	});
};

const goRecords = () => {
	uni.navigateTo({ url: '/pages/records/index' });
};

const goScoreRecords = () => {
	uni.navigateTo({ url: '/pages/mine/score-records' });
};

onShow(() => {
	refreshPage();
});

onPullDownRefresh(() => {
	refreshPage();
});
</script>

<style lang="scss" scoped>
.mine-page {
	min-height: 100vh;
	padding: 24rpx;
	background: #f4f6f8;
}

.score-card {
	padding: 40rpx 32rpx;
	border-radius: 20rpx;
	background: linear-gradient(135deg, #1f5bd8 0%, #3d7cf5 100%);
	box-shadow: 0 12rpx 32rpx rgba(31, 91, 216, 0.24);
}

.score-label {
	color: rgba(255, 255, 255, 0.88);
	font-size: 28rpx;
}

.score-value {
	display: block;
	margin-top: 16rpx;
	color: #ffffff;
	font-size: 64rpx;
	font-weight: 800;
	line-height: 1.1;
}

.fund-card {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 20rpx;
	margin-top: 24rpx;
	padding: 28rpx 28rpx 28rpx 24rpx;
	border-radius: 20rpx;
	background: linear-gradient(135deg, #f8faff 0%, #eef4ff 100%);
	border: 1rpx solid rgba(31, 91, 216, 0.12);
	box-shadow: 0 8rpx 22rpx rgba(31, 91, 216, 0.08);
}

.fund-card-hover {
	opacity: 0.92;
}

.fund-card-left {
	display: flex;
	flex: 1;
	align-items: center;
	min-width: 0;
	gap: 20rpx;
}

.fund-icon-image {
	flex-shrink: 0;
	width: 72rpx;
	height: 72rpx;
}

.fund-text {
	display: flex;
	flex: 1;
	flex-direction: column;
	min-width: 0;
	gap: 8rpx;
}

.fund-title {
	color: #111827;
	font-size: 32rpx;
	font-weight: 800;
	line-height: 1.2;
}

.fund-desc {
	color: #6b7280;
	font-size: 24rpx;
	line-height: 1.4;
}

.fund-action {
	display: flex;
	flex-shrink: 0;
	align-items: center;
	gap: 2rpx;
}

.fund-action-text {
	color: #1f5bd8;
	font-size: 26rpx;
	font-weight: 700;
}

.fund-action-arrow {
	color: #1f5bd8;
	font-size: 28rpx;
	line-height: 1;
}

.menu-card {
	margin-top: 24rpx;
	padding: 8rpx 0;
	border-radius: 20rpx;
	background: #ffffff;
	box-shadow: 0 8rpx 22rpx rgba(17, 24, 39, 0.04);
}

.menu-item {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 32rpx;
	border-bottom: 1rpx solid #f3f4f6;
}

.menu-item.no-border {
	border-bottom: none;
}

.menu-item-hover {
	background: #f9fafb;
}

.menu-label {
	color: #1f2937;
	font-size: 30rpx;
	font-weight: 600;
}

.menu-arrow {
	color: #c2c7d0;
	font-size: 30rpx;
}
</style>
