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

		<scroll-view scroll-y class="list-scroll-view" :show-scrollbar="false" @scrolltolower="onReachEnd">
			<view class="list-inner">
				<view class="complaint-card" v-for="item in displayList" :key="item.id" @click="goDetail(item)">
					<view class="card-header">
						<view class="type-badge">{{ item.feedbackTypeLabel }}</view>
						<view class="status-text" :class="item.status === '1' ? 'done' : 'pending'">
							{{ item.status === '1' ? '已处理' : '待处理' }}
						</view>
					</view>
					<view class="card-content">
						<text class="content-text">{{ item.content }}</text>
					</view>
					<view class="card-footer">
						<text class="time-text">{{ item.createTime }}</text>
						<view class="arrow-icon"></view>
					</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,
			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}` });
		},
		refresh() {
			this.displayList = [];
			this.noMore = false;
			this.pageNum = 1;
			this.loadData();
		},
		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,
					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: #f8fafb;
	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: 16rpx;
	padding: 30rpx;
	margin-bottom: 24rpx;
	box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.03);
}

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

.type-badge {
	font-size: 26rpx;
	color: #C1001C;
	background: rgba(193, 0, 28, 0.06);
	padding: 6rpx 16rpx;
	border-radius: 8rpx;
}

.status-text {
	font-size: 26rpx;
}

.status-text.pending {
	color: #ff9800;
}

.status-text.done {
	color: #4caf50;
}

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

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

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

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

.arrow-icon {
	width: 14rpx;
	height: 14rpx;
	border-right: 3rpx solid #ccc;
	border-top: 3rpx solid #ccc;
	transform: rotate(45deg);
	margin-left: 10rpx;
}

.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: 30rpx 40rpx calc(30rpx + env(safe-area-inset-bottom));
	flex-shrink: 0;
	border-top: 1rpx solid #f0f0f0;
}

.submit-btn {
	width: 100%;
	height: 96rpx;
	background: #C1001C;
	color: #fff;
	border-radius: 48rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 32rpx;
	font-weight: bold;
	border: none;
}

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