<template>
	<view class="container">
		<view class="page">
			<view class="complaints" v-if="maintenanceList.length !== 0">
				<view class="information" v-for="item in maintenanceList" :key="item.id" @click="toDetail(item.id)">
					<view class="title" :class="item.handleStatus">{{item.complaintTheme}}<view class="tag">{{item.statusName}}</view>
					</view>
					<view class="content">
						<view class="item">
							<view class="name">投诉人</view>
							<view class="detail">{{item.userName}} {{item.userPhone}}</view>
						</view>
						<view class="item">
							<view class="name">投诉时间</view>
							<view class="detail">{{item.complaintTime}}</view>
						</view>
					</view>
				</view>
			</view>
			<no-data :show="maintenanceList.length === 0" text="暂无投诉信息"></no-data>
		</view>
		<view class="form-submit-fixed">
			<view class="form-submit" @click="toEditor()">新增投诉</view>
		</view>
	</view>
</template>

<script>
	import {
		getCommunityComplaintListApi,
	} from '@/config/api.js'
	import {Moment} from '@/utils/moment.js'
	import noData from '@/components/no-data/no-data'
	const handleStatus = {
		waitPending: '待处理',
		pending: '处理中',
		finish: '处理完成'
	}
	export default {
		components: {noData},
		data() {
			return {
				maintenanceList: [],
				pageNumber: 1,
				isFinish: false
			}
		},
		methods: {
			toDetail(id) {
				uni.navigateTo({
					url: '/pages/complaintsDetail/index?id=' + id
				});
			},
			toEditor() {
				uni.navigateTo({
					url: '/pages/complaintsAdd/index',
				});
			},
			async getList(flag) {
				let {result} = await getCommunityComplaintListApi({
					pageNo: this.pageNumber,
					pageSize: 10
				})
				let data = result.records.map(item=> {
					return {
						...item,
						statusName: handleStatus[item.handleStatus],
						complaintTime: new Moment(new Date(item.createTime).getTime()).format('YYYY-MM-DD HH:mm'),
					}
				})
				if(this.pageNumber === 1) {
					this.maintenanceList = [].concat(data)
				} else {
					this.maintenanceList = this.maintenanceList.concat(data)
				}
				if(flag) { uni.stopPullDownRefresh(); }
				if(this.maintenanceList.length >= result.total) {
					this.isFinish = true
				} else {
					this.pageNumber += 1
				}
			}
		},
		onLoad() {
			this.getList()
			uni.$on('update-list',async ()=>{
				this.pageNumber = 1
				this.isFinish = false
				this.getList()
			})
		},
		onUnload() {  
			// 移除监听事件  
			uni.$off('update-list');
		},
		onPullDownRefresh() {
			this.pageNumber = 1
			this.isFinish = false
			this.getList(true)
		},
		onReachBottom() {
			if(this.isFinish) return
			this.getList()
		}
	}
</script>
<style>
	page {
		background-color: #F8F6F9;
	}
</style>
<style lang="scss" scoped>
	.complaints {
		padding: 30rpx 30rpx 90rpx;
		background-color: #F8F6F9;
		.information {
			background-color: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;
			margin-bottom: 30rpx;

			.title {
				height: 40rpx;
				line-height: 40rpx;
				padding-left: 20rpx;
				font-size: 32rpx;
				color: #373737;
				font-weight: bold;
				margin-bottom: 30rpx;

				.tag {
					font-size: 22rpx;
					padding: 2rpx 20rpx;
					border-radius: 30rpx;
					display: inline-block;
					float: right;
				}
			}

			.waitPending {
				border-left: 2px solid #F1BD4D;

				.tag {
					color: #F1BD4D;
					border: 1px solid #F1BD4D;
					background-color: #FFFCF6;
				}
			}

			.pending {
				border-left: 2px solid #6A59F2;

				.tag {
					color: #6A59F2;
					border: 1px solid #6A59F2;
					background-color: #F6F5FF;
				}
			}

			.finish {
				border-left: 2px solid #B6B6BA;

				.tag {
					color: #B6B6BA;
					border: 1px solid #B6B6BA;
					background-color: #F8F8F8;
				}
			}

			.content {
				border-top: 1px solid #F0F0F2;
				padding-top: 20rpx;

				.item {
					display: flex;
					height: 70rpx;
					line-height: 70rpx;

					.name {
						width: 35%;
						font-size: 28rpx;
						color: #CECED3;
					}

					.detail {
						font-size: 28rpx;
						color: #373737;
					}
				}
			}

		}
	}

	.form-submit-fixed {
		position: fixed;
		width: 100%;
		bottom: 10rpx;
	}

	.form-submit {
		margin: 0 30rpx;
		box-sizing: border-box;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #6A59F2;
		border-radius: 80rpx;
		text-align: center;
		font-size: 28rpx;
		color: #FFFFFF;
	}
</style>