<template>
	<view class="complaint-list-page">
		<nav-bar title="投诉管理"></nav-bar>
		<view class="list-container">
			<view class="empty-state" v-if="historyList.length === 0">
				<text>暂无投诉记录</text>
			</view>
			<view v-else class="history-card" v-for="item in historyList" :key="item.id">
				<view class="card-header">
					<view class="left-box">
						<text class="order-no">单号：{{ item.orderCode || '-' }}</text>
						<text class="submitted-tag">已提交</text>
					</view>
					<view :class="['praise-badge', item.praiseFlag ? 'agree' : 'disagree']">
						<image class="badge-icon"
							:src="item.praiseFlag ? '/static/images/complaint-agree.png' : '/static/images/complaint-disagree.png'"
							mode="aspectFit"></image>
						<text class="badge-text">{{ item.praiseFlag ? '赞' : '不赞' }}</text>
					</view>
				</view>
				<view class="card-time">{{ item.createTime || '-' }}</view>
				<view class="card-body">
					<view class="reason-row">
						<text class="reason-content">{{ item.reason || '未填写内容' }}</text>
					</view>

					<!-- 凭证图片展示 @Author: Antigravity -->
					<view class="photo-grid" v-if="item.photoUrls">
						<image v-for="(url, index) in item.photoUrls.split(',')" :key="index" :src="url"
							mode="aspectFill" class="photo-item"
							@click="previewImage(item.photoUrls.split(','), index)"></image>
					</view>
				</view>
			</view>


			<view v-if="historyList.length > 0 && !hasMore" class="no-more">没有更多了</view>
		</view>
	</view>
</template>

<script setup>
// @Author: Antigravity
import { ref } from 'vue'
import { onShow, onReachBottom, onPullDownRefresh } from '@dcloudio/uni-app'
import navBar from '@/components/nav-bar/index.vue'
import { listMyComplaint } from '@/api/fulfiller/complaint'
import { getInfo } from '@/api/system/user'

const historyList = ref([])
const pageNum = ref(1)
const pageSize = ref(10)
const hasMore = ref(true)

const loadData = async (isLoadMore = false) => {
	try {
		uni.showNavigationBarLoading()
		const res = await listMyComplaint({
			pageNum: pageNum.value,
			pageSize: pageSize.value
		})

		const rows = res.rows || []
		if (isLoadMore) {
			historyList.value = [...historyList.value, ...rows]
		} else {
			historyList.value = rows
		}
		hasMore.value = historyList.value.length < (res.total || 0)
	} catch (error) {
		console.error('加载投诉记录失败', error)
		uni.showToast({ title: typeof error === 'string' ? error : '加载投诉记录失败', icon: 'none' })
	} finally {
		uni.hideNavigationBarLoading()
		uni.stopPullDownRefresh()
	}
}

onShow(() => {
	pageNum.value = 1
	loadData()
})

onPullDownRefresh(() => {
	pageNum.value = 1
	loadData()
})

onReachBottom(() => {
	if (hasMore.value) {
		pageNum.value++
		loadData(true)
	}
})

const previewImage = (urls, index) => {

	uni.previewImage({
		urls: urls,
		current: index
	})
}
</script>

<style lang="scss" scoped>
.complaint-list-page {
	min-height: 100vh;
	background: #f7f8fa;
	padding-bottom: constant(safe-area-inset-bottom);
	padding-bottom: env(safe-area-inset-bottom);
}

.list-container {
	padding: 24rpx;
}

.empty-state {
	text-align: center;
	padding: 100rpx 0;
	color: #999;
	font-size: 28rpx;
}

.history-card {
	background: #fff;
	border-radius: 20rpx;
	padding: 28rpx;
	margin-bottom: 24rpx;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.03);
}

.card-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.left-box {
	display: flex;
	align-items: center;
	gap: 12rpx;
}

.order-no {
	font-size: 26rpx;
	color: #333;
	font-weight: 500;
}

.submitted-tag {
	font-size: 20rpx;
	color: #999;
	background: #f0f0f0;
	padding: 2rpx 12rpx;
	border-radius: 6rpx;
	font-weight: 500;
}

.praise-badge {
	display: flex;
	align-items: center;
	gap: 6rpx;
	padding: 4rpx 14rpx;
	border-radius: 8rpx;
	font-size: 22rpx;
	font-weight: 600;
	flex-shrink: 0;

	&.agree {
		color: #52c41a;
		background: #f6ffed;
	}

	&.disagree {
		color: #ff4d4f;
		background: #fff1f0;
	}
}

.badge-icon {
	width: 28rpx;
	height: 28rpx;
	flex-shrink: 0;
}

.badge-text {
	line-height: 1;
}

.card-time {
	font-size: 24rpx;
	color: #999;
	margin-top: 12rpx;
	padding-bottom: 16rpx;
	border-bottom: 2rpx solid #EEEEEE;
}

.reason-row {
	display: flex;
	flex-direction: column;
	gap: 12rpx;
}

.reason-content {
	font-size: 28rpx;
	color: #333;
	line-height: 1.6;
}

.photo-grid {
	display: grid;
	grid-template-columns: repeat(3, 1fr);
	gap: 12rpx;
	margin-top: 20rpx;
}

.photo-item {
	width: 100%;
	height: 200rpx;
	border-radius: 12rpx;
	background: #f5f5f5;
}

.no-more {
	text-align: center;
	font-size: 24rpx;
	color: #ccc;
	padding: 20rpx 0;
}
</style>
