<template>
	<view class="complaint-list-root">
		<erp-nav-bar title="投诉与建议" />

		<scroll-view scroll-y class="list-scroll-view" :show-scrollbar="false" @scrolltolower="onReachEnd"
			:refresher-enabled="true" :refresher-triggered="refresherTriggered" @refresherrefresh="onPullDownRefresh">
			<view class="list-inner">
				<view class="complaint-card" v-for="item in displayList" :key="item.id" @click="goDetail(item)">
					<view class="card-head">
						<view class="head-left">
							<view class="type-tag">
								<view class="tag-dot"></view>
								<text>{{ item.feedbackTypeLabel }}</text>
							</view>
						</view>
						<view class="head-right" :class="item.status === '1' ? 'done' : 'pending'">
							<view class="status-dot"></view>
							<text>{{ item.status === '1' ? '已处理' : '待处理' }}</text>
						</view>
					</view>

					<view class="card-content">
						<text class="content-text">{{ item.content }}</text>
					</view>

					<view class="card-images" v-if="item.imageUrls && item.imageUrls.length > 0">
						<view class="img-cell" v-for="(url, idx) in item.imageUrls" :key="idx"
							@click.stop="previewImage(item.imageUrls, idx)">
							<image :src="url" mode="aspectFill"></image>
						</view>
					</view>

					<view class="card-foot">
						<text class="time-text">{{ item.createTime }}</text>
						<text class="arrow">›</text>
					</view>
				</view>

				<view class="list-status-info" v-if="displayList.length > 0">
					<view class="loading-wrap" v-if="loading">
						<text>加载中...</text>
					</view>
					<view class="nomore-wrap" v-if="noMore">
						<text class="nomore-text">没有更多了</text>
					</view>
				</view>

				<view class="empty-state" v-if="displayList.length === 0 && !loading">
					<image src="https://img.icons8.com/clouds/200/comments.png" mode="aspectFit"></image>
					<text class="empty-txt">暂无反馈记录</text>
				</view>

				<view class="safe-bottom"></view>
			</view>
		</scroll-view>

		<view class="footer-bar">
			<button class="submit-btn" @click="goSubmit">提交反馈</button>
		</view>
	</view>
</template>

<script>
import ErpNavBar from '@/components/erp-nav-bar.vue';
import { getMyComplaintList } from '@/api/system/complaint.js';
import { getDictByType } from '@/api/system/dict.js';

export default {
	components: { ErpNavBar },
	data() {
		return {
			loading: false,
			noMore: false,
			refresherTriggered: false,
			pageNum: 1,
			displayList: [],
			typeMap: {}
		}
	},
	onLoad() {
		this.loadDict();
		this.refresh();
	},
	onShow() {
		if (this.displayList.length > 0) {
			this.refresh();
		}
	},
	methods: {
		async loadDict() {
			try {
				const res = await getDictByType('sys_complaint_type');
				if (res && res.data) {
					const map = {};
					res.data.forEach(d => { map[d.dictValue] = d.dictLabel; });
					this.typeMap = map;
				}
			} catch (e) { /* 忽略字典加载失败 */ }
		},
		goBack() { uni.navigateBack(); },
		goSubmit() {
			uni.navigateTo({ url: '/pages/mine/complaint/submit/index' });
		},
		goDetail(item) {
			uni.navigateTo({ url: `/pages/mine/complaint/detail/index?id=${item.id}` });
		},
		previewImage(urls, current) {
			uni.previewImage({ urls, current });
		},
		refresh() {
			this.displayList = [];
			this.noMore = false;
			this.pageNum = 1;
			this.loadData();
		},
		onPullDownRefresh() {
			this.refresherTriggered = true;
			this.displayList = [];
			this.noMore = false;
			this.pageNum = 1;
			this.loadData().finally(() => {
				this.refresherTriggered = false;
			});
		},
		onReachEnd() {
			if (!this.loading && !this.noMore) this.loadData();
		},
		async loadData() {
			if (this.loading || this.noMore) return;
			this.loading = true;
			try {
				const params = {
					pageNum: this.pageNum,
					pageSize: 10
				};
				const res = await getMyComplaintList(params);
				const rows = (res && res.rows) ? res.rows : [];

				const formatted = rows.map(item => ({
					id: item.id,
					feedbackType: item.feedbackType,
					feedbackTypeLabel: this.typeMap[item.feedbackType] || item.feedbackType || '未知类型',
					content: item.content,
					imageUrls: item.imageUrls ? item.imageUrls.split(',').filter(u => u) : [],
					status: item.status || '0',
					createTime: item.createTime
				}));

				this.displayList = [...this.displayList, ...formatted];
				this.pageNum++;
				this.noMore = rows.length === 0 || this.displayList.length >= (res.total || 0);
			} catch (e) {
				console.error('加载反馈列表失败', e);
				uni.showToast({ title: e || '加载反馈列表失败', icon: 'none' });
			} finally {
				this.loading = false;
			}
		}
	}
}
</script>

<style scoped>
/deep/ ::-webkit-scrollbar {
	display: none !important;
	width: 0 !important;
	height: 0 !important;
}

.complaint-list-root {
	width: 100vw;
	height: 100vh;
	background: #F4F6F9;
	display: flex;
	flex-direction: column;
	overflow: hidden;
}

.list-scroll-view {
	flex: 1;
	height: 0;
	width: 100%;
}

.list-inner {
	padding: 30rpx;
}

.complaint-card {
	background: #fff;
	border-radius: 24rpx;
	padding: 32rpx;
	margin-bottom: 24rpx;
	box-shadow: 0 2rpx 20rpx rgba(0, 0, 0, 0.03);
	transition: transform 0.15s ease;
}

.complaint-card:active {
	transform: scale(0.985);
}

.card-head {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 20rpx;
}

.head-left {
	flex-shrink: 0;
}

.type-tag {
	display: flex;
	align-items: center;
	gap: 10rpx;
	font-size: 24rpx;
	color: #5A6577;
	background: #F4F6F9;
	padding: 8rpx 18rpx;
	border-radius: 20rpx;
}

.tag-dot {
	width: 10rpx;
	height: 10rpx;
	background: #C1001C;
	border-radius: 50%;
}

.head-right {
	display: flex;
	align-items: center;
	gap: 8rpx;
	font-size: 24rpx;
	font-weight: 500;
}

.head-right .status-dot {
	width: 12rpx;
	height: 12rpx;
	border-radius: 50%;
}

.head-right.pending {
	color: #E8900C;
}

.head-right.pending .status-dot {
	background: #E8900C;
	box-shadow: 0 0 0 4rpx rgba(232, 144, 12, 0.12);
}

.head-right.done {
	color: #34A853;
}

.head-right.done .status-dot {
	background: #34A853;
	box-shadow: 0 0 0 4rpx rgba(52, 168, 83, 0.12);
}

.card-content {
	margin-bottom: 18rpx;
}

.content-text {
	font-size: 28rpx;
	color: #1A1A2E;
	line-height: 1.7;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
	overflow: hidden;
}

.card-images {
	display: flex;
	gap: 12rpx;
	margin-bottom: 20rpx;
	overflow-x: auto;
	padding-bottom: 4rpx;
}

.card-images::-webkit-scrollbar {
	display: none;
}

.img-cell {
	width: 160rpx;
	height: 160rpx;
	border-radius: 12rpx;
	overflow: hidden;
	flex-shrink: 0;
	background: #F5F6F8;
}

.img-cell image {
	width: 100%;
	height: 100%;
}

.card-foot {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding-top: 16rpx;
	border-top: 1rpx solid #F0F0F3;
}

.time-text {
	font-size: 24rpx;
	color: #B0B8C5;
}

.arrow {
	font-size: 36rpx;
	color: #CBD2DB;
	font-weight: 300;
	line-height: 1;
	margin-right: -4rpx;
}

.list-status-info {
	padding: 30rpx 0;
	display: flex;
	justify-content: center;
}

.loading-wrap {
	font-size: 26rpx;
	color: #999;
}

.nomore-wrap {
	display: flex;
	align-items: center;
}

.nomore-text {
	font-size: 26rpx;
	color: #ccc;
}

.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding-top: 150rpx;
}

.empty-state image {
	width: 200rpx;
	height: 200rpx;
	margin-bottom: 30rpx;
}

.empty-txt {
	font-size: 28rpx;
	color: #999;
	margin-bottom: 40rpx;
}

.safe-bottom {
	height: 40rpx;
}

.footer-bar {
	background: #fff;
	padding: 24rpx 40rpx;
	padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
	flex-shrink: 0;
	box-shadow: 0 -2rpx 16rpx rgba(0, 0, 0, 0.04);
}

.submit-btn {
	width: 100%;
	height: 92rpx;
	background: #C1001C;
	color: #fff;
	border-radius: 46rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 30rpx;
	font-weight: 600;
	border: none;
	box-shadow: 0 4rpx 16rpx rgba(193, 0, 28, 0.2);
	transition: opacity 0.2s;
}

.submit-btn:active {
	opacity: 0.9;
}

.submit-btn::after {
	border: none;
}
</style>
