<template>
	<!-- <div>交易明细</div> -->
	<view class="box">
		<view class="top flex" :style="{ borderRadius: '31.5rpx' }">
			<view @click="changeTab(0)" :class="{ active : type == 0 }" class="income flex element"
				:style="{ borderRadius: '31.5rpx' }">
				收入明细
			</view>
			<view @click="changeTab(1)" :class="{ active : type == 1 }" class="income flex element"
				:style="{ borderRadius: '31.5rpx' }">
				支出明细
			</view>
		</view>

		<view v-for="(item,index) in list" :key="index" class="Recharge">
			<view class="flex" style="flex-shrink: 0;">
				<image style="width: 80rpx;height: 80rpx" src="https://img1.baidu.com/it/u=3034232350,1041791648&fm=253&fmt=auto&app=138&f=PNG?w=500&h=500"
					mode="aspectFill"></image>
				<view class="text1">
					<view class="text2">
						{{ item.title }}
						<!-- 金额类型标签 -->
						<!-- <text v-if="item.moneyType !== undefined" class="money-type-tag" :class="getMoneyTypeClass(item.moneyType)">
							{{ getMoneyTypeText(item.moneyType) }}
						</text> -->
					</view>
					<view>
						{{ item.createTime }}
					</view>
					<!-- 审核状态显示 - 只在支出明细中显示 -->
					<view v-if="type === 1 && item.auditStatus !== undefined" class="audit-status">
						<text v-if="item.auditStatus === 0" class="status-pending">待审核</text>
						<text v-if="item.auditStatus === 1" class="status-approved">审核通过</text>
						<text v-if="item.auditStatus === 2" class="status-rejected">审核不通过</text>
					</view>
					<!-- 审核不通过时显示备注 - 只在支出明细中显示 -->
					<view v-if="type === 1 && item.auditStatus === 2 && item.remark" class="remark">
						备注：{{ item.remark }}
					</view>
				</view>
			</view>
			<view class="right-content" style="flex-shrink: 0;">
				<view class="text3">
					￥{{ item.amount }}
				</view>
				<!-- 审核通过的提现显示领取按钮 -->
				<view v-if="item.auditStatus === 1 && type === 1 && item.state == 0" class="receive-btn" @click="receiveWithdrawal(item)">
					领取
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue"
	import { useStore } from 'vuex'
	import {
		amountLogList
	} from "@/api/amount/index.js"
	import {
		withdrawalSuccessful
	} from "@/api/amount/index.js"

	import {
		onShow,
		onLoad
	} from "@dcloudio/uni-app"

	onLoad((options) => {
		console.log(options);
		// 如果有type参数，设置默认选中的tab
		if (options.type) {
			type.value = parseInt(options.type)
		}
		if (options.moneyType) {
			moneyType.value = parseInt(options.moneyType)
		}
	})

	onShow(() => {
		getRunningWater()
	})

	const store = useStore()
	const list = ref([]);
	const type = ref(0);
	const moneyType = ref(1);

	// 获取流水
	const getRunningWater = async () => {
		let response = await amountLogList({
			type: type.value,
			moneyType : moneyType.value,
			userId: store.state.user.userInfo.userId
		});
		if (response.code == 200) {
			list.value = response.data
		}
	}

	const changeTab = (index) => {
		type.value = index;
		getRunningWater();
	}

	// 获取金额类型文本
	const getMoneyTypeText = (moneyType) => {
		switch (moneyType) {
			case 0:
				return '合伙人余额'
			case 1:
				return '伴宠师余额'
			case 2:
				return '保证金余额'
			default:
				return ''
		}
	}

	// 获取金额类型样式类名
	const getMoneyTypeClass = (moneyType) => {
		switch (moneyType) {
			case 0:
				return 'money-type-partner'
			case 1:
				return 'money-type-sitter'
			case 2:
				return 'money-type-deposit'
			default:
				return ''
		}
	}

	// 领取提现
	const receiveWithdrawal = async (item) => {
		try {
			console.log('开始领取提现，item:', item)
			
			// 检查是否支持微信转账
			if (typeof wx === 'undefined' || !wx.canIUse('requestMerchantTransfer')) {
				console.log('微信API不可用')
				uni.hideLoading()
				if (result.code === 200) {
					uni.showToast({
						title: '领取失败，请将微信升级到最新版本',
						icon: 'icon',
						duration: 1500,
						success() {
							// 刷新列表
							getRunningWater()
						}
					})
				} else {
					uni.showToast({
						title: result.msg || '领取失败',
						icon: 'error',
						duration: 2000
					})
				}
				return
			}

			// 调用微信商户转账API
			wx.requestMerchantTransfer({
				mchId: '1665639691', // 商户ID
				appId: wx.getAccountInfoSync().miniProgram.appId,
				package: item.packageInfo || '', // 从后端获取的转账包信息
				success: async (res) => {
					console.log('微信转账成功:', res)
					// 转账成功后调用成功接口
					try {
						const result = await withdrawalSuccessful({ 
							id: item.id,
							userId: store.state.user.userInfo.userId 
						})
						console.log('withdrawalSuccessful result:', result)
						
						uni.hideLoading()
						
						if (result.code === 200) {
							uni.showToast({
								title: '领取成功',
								icon: 'success',
								duration: 1500,
								success() {
									// 刷新列表
									getRunningWater()
								}
							})
						} else {
							uni.showToast({
								title: result.msg || '领取失败',
								icon: 'error',
								duration: 2000
							})
						}
					} catch (error) {
						console.error('调用withdrawalSuccessful失败:', error)
						uni.hideLoading()
						uni.showToast({
							title: '网络异常，请重试',
							icon: 'error',
							duration: 2000
						})
					}
				},
				fail: (res) => {
				}
			})
		} catch (error) {
			uni.hideLoading()
			console.error('领取提现异常:', error)
			uni.showToast({
				title: '网络异常，请重试',
				icon: 'error',
				duration: 2000
			})
		}
	}
</script>

<style scoped lang="scss">
	.box {
		width: 750rpx;
		min-height: 100vh;
		background-color: #FFFFFF;
		padding: 32rpx 24rpx 0 24rpx;
		box-sizing: border-box;
	}

	.top {
		width: 722rpx;
		height: 63rpx;
		background-color: #F3F3F3;
	}

	.active {
		background-color: #FFBF60;
		color: white;
		font-weight: 600;
		box-shadow: 0 2rpx 8rpx rgba(255, 191, 96, 0.3);
	}

	.income {
		width: 361rpx;
		height: 63rpx;
		line-height: 63rpx;
		font-size: 28rpx;
		font-weight: 500;
		justify-content: center;
		transition: all 0.3s ease;
	}



	.Recharge {
		padding: 32rpx 24rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		align-items: flex-start;
		border-bottom: 1rpx solid #f5f5f5;
		
		&:last-child {
			border-bottom: none;
		}
	}

	.flex {
		display: flex;
	}

	.text1 {
		font-size: 26rpx;
		color: #666666;
		line-height: 1.4;
		margin-left: 20rpx;
	}

	.Recharge image {
		width: 80rpx;
		height: 80rpx;
		border-radius: 8rpx;
		margin-top: 0;
	}

	.text2 {
		font-weight: 500;
		color: #333333;
		font-size: 28rpx;
		line-height: 40rpx;
		margin-bottom: 8rpx;
		display: flex;
		align-items: center;
	}

	.text3 {
		color: #FF2A2A;
		font-size: 30rpx;
		font-weight: 600;
		line-height: 44rpx;
	}

	.right-content {
		display: flex;
		flex-direction: column;
		align-items: flex-end;
		// min-width: 120rpx;
	}

	.right-content > .text3 {
		margin-bottom: 16rpx;
	}

	.audit-status {
		margin-top: 12rpx;
		font-size: 22rpx;
	}

	.status-pending {
		color: #FF9800;
		background-color: #FFF3E0;
		padding: 6rpx 12rpx;
		border-radius: 12rpx;
		font-size: 20rpx;
		font-weight: 500;
		display: inline-block;
	}

	.status-approved {
		color: #4CAF50;
		background-color: #E8F5E8;
		padding: 6rpx 12rpx;
		border-radius: 12rpx;
		font-size: 20rpx;
		font-weight: 500;
		display: inline-block;
	}

	.status-rejected {
		color: #F44336;
		background-color: #FFEBEE;
		padding: 6rpx 12rpx;
		border-radius: 12rpx;
		font-size: 20rpx;
		font-weight: 500;
		display: inline-block;
	}

	.remark {
		margin-top: 12rpx;
		color: #666;
		font-size: 24rpx;
		line-height: 36rpx;
		background-color: #f8f8f8;
		padding: 12rpx;
		border-radius: 8rpx;
		border-left: 4rpx solid #F44336;
		width: 460rpx;
	}

	.receive-btn {
		background-color: #FFBF60;
		color: #FFFFFF;
		padding: 12rpx 24rpx;
		border-radius: 24rpx;
		font-size: 26rpx;
		font-weight: 500;
		text-align: center;
		min-width: 80rpx;
		cursor: pointer;
		box-shadow: 0 4rpx 12rpx rgba(255, 191, 96, 0.3);
		transition: all 0.3s ease;
	}

	.receive-btn:active {
		background-color: #E6A84D;
		transform: translateY(2rpx);
		box-shadow: 0 2rpx 8rpx rgba(255, 191, 96, 0.2);
	}

	/* 金额类型标签样式 */
	.money-type-tag {
		font-size: 20rpx;
		font-weight: 500;
		padding: 4rpx 8rpx;
		border-radius: 8rpx;
		margin-left: 12rpx;
		display: inline-block;
		line-height: 1;
		flex-shrink: 0;
	}

	.money-type-partner {
		color: #1976D2;
		background-color: #E3F2FD;
	}

	.money-type-sitter {
		color: #388E3C;
		background-color: #E8F5E8;
	}

	.money-type-deposit {
		color: #F57C00;
		background-color: #FFF3E0;
	}
</style>