<template>
	<view class="container">
		<view class="maintenance-box">
			<view class="tab">
				<view class="item" v-for="item in maintenanceTabs" :key="item.status" :class="{'active': activeStatus === item.status}" @click="onChangeTab(item.status)">
					<view class="item-box">
						<view class="label">{{item.label}}</view>
						<view class="number">{{item.number}}</view>
					</view>
					<image src="../../static/images/icon_press02.png" class="icon"></image>
				</view>
			</view>
			
			<view class="list-box" v-if="maintenanceList.length">
				<view class="list" v-for="item in maintenanceList" :key="item.id">
					<view class="content"  @click="onJump(`/pages/repairDetails/index?id=${item.id}`)">
						<view class="item">
							<view class="name">报修位置</view>
							<view class="detail">{{item.repairAddress}}</view>
						</view>
						<view class="item">
							<view class="name">报修物品</view>
							<view class="detail">{{item.repairArticle}}</view>
						</view>
						<view class="item">
							<view class="name">保修时间</view>
							<view class="detail">{{item.reservationTime}}</view>
						</view>
					</view>
					<view class="handle-box" v-if="activeStatus === 'waitReceive'">
						<view class="btn" @click="onJump(`/pages/personnel/index?id=${item.id}`)">转单</view>
						<view class="btn" @click="onReceivingOrders(item)">接单</view>
					</view>
				</view>
			</view>
			<no-data :show="maintenanceList.length === 0" :isFull="false" text="暂无维修信息"></no-data>
		</view>
	</view>
</template>

<script>
	import {getommunityRepairApi, editCommunityComplaintApi} from '@/config/api.js'
	import noData from '@/components/no-data/no-data'
	export default {
		components: {noData},
		data() {
			return {
				activeStatus: 'waitReceive',
				maintenanceTabs: [
					{label: '待接单', status: 'waitReceive', number: 0},
					{label: '维修中', status: 'inRepair', number: 0},
					{label: '已完成', status: 'finishAuditPass', number: 0}
				],
				maintenancePage: 1,
				maintenanceList: []
			}
		},
		methods: {
			onChangeTab(status) {
				this.maintenancePage = 1
				this.activeStatus = status
				this.maintenanceList = []
				this.getMaintenanceList()
			},
			async getMaintenanceList() {
				let {result} = await getommunityRepairApi({
					pageNumber: this.maintenancePage,
					pageSize: 10,
					repairStatus: this.activeStatus
				})
				this.maintenanceTabs[0].number = result.waitCount
				this.maintenanceTabs[1].number = result.runCount
				this.maintenanceTabs[2].number = result.finishCount
				this.maintenanceList = result.pageList.records
			},
			onJump(url) {
				if(!url) return
				uni.navigateTo({
					url
				});
			},
			onReceivingOrders(row) {
				let _this = this
				uni.showModal({
					title: '提示',
					content: '确认接单嘛？',
					success: function (res) {
						if (res.confirm) {
							_this.onTransferOrder(row)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			async onTransferOrder(row) {
				let data = await editCommunityComplaintApi({
					id: row.id,
					repairStatus: 'inRepair'
				})
				if(timer) clearTimeout(timer)
				timer = null
				timer = setTimeout(()=> {
					uni.showToast({
						title: '接单成功！'
					});
					setTimeout(()=> {
						this.maintenancePage = 1
						this.getMaintenanceList()
					}, 1500)
				}, 30)
			},
		},
		onLoad() {
			this.getMaintenanceList()
		}
	}
</script>
<style>
	page {
		background-color: #F8F6F9;
	}
</style>
<style lang="scss" scoped>
	.maintenance-box {
		/deep/ .nodate-box {
			margin-top: 30rpx;
			padding: 100rpx 0;
		}
		.tab {
			padding: 30rpx 0;
			display: flex;
			justify-content: space-around;
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 25rpx 0rpx rgba(81,89,234,0.06);
			border-radius: 16rpx;

			.item {
				position: relative;
				&.active {
					.item-box .label {
						color: #373737;
					}

					.icon {
						opacity: 1;
					}
				}
				.item-box {
					display: flex;
					align-items: center;
					.label {
						font-size: 28rpx;
						color: #9D9CA6;
						transition: color .5s;
					}
					.number {
						min-width: 30rpx;
						height: 30rpx;
						background-color: #F6C864;
						border-radius: 15rpx;
						font-size: 22rpx;
						color: #FFFFFF;
						padding: 0 8rpx;
						line-height: 30rpx;
						text-align: center;
						margin-left: 10rpx;
					}
				}

				.icon {
					width: 40rpx;
					height: 8rpx;
					position: absolute;
					bottom: -10px;
					left: 50%;
					transform: translateX(-50%);
					opacity: 0;
					transition: opacity .5s;
				}
			}
		}

		.list-box {
			padding: 0 30rpx;
			.list {
				margin-top: 30rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 0rpx 25rpx 0rpx rgba(81,89,234,0.06);
				border-radius: 16rpx;
				padding: 0 30rpx;
				.content {
					// margin-top: 30rpx;
					// background: #FFFFFF;
					// box-shadow: 0rpx 0rpx 25rpx 0rpx rgba(81,89,234,0.06);
					// border-radius: 16rpx;
					padding: 30rpx 0;
		
					.item {
						display: flex;
						&:not(:last-child) {
							margin-bottom: 20rpx;
						}
						// height: 70rpx;
						// line-height: 70rpx;
		
						.name {
							width: 25%;
							font-size: 28rpx;
							color: #CECED3;
						}
		
						.detail {
							width: 75%;
							font-size: 28rpx;
							color: #373737;
							&.waitPending .tag {
								color: #F1BD4D;
								border: 1px solid #F1BD4D;
								background-color: #FFFCF6;
							}
							&.pending .tag {
								color: #6A59F2;
								border: 1px solid #6A59F2;
								background-color: #F6F5FF;
							}
							&.finish .tag {
								color: #B6B6BA;
								border: 1px solid #B6B6BA;
								background-color: #F8F8F8;
							}
							.tag {
								height: 40rpx;
								line-height: 40rpx;
								font-size: 22rpx;
								padding: 2rpx 20rpx;
								border-radius: 30rpx;
								display: inline-block;
							}
						}
					}
		
					.text {
						height: auto;
		
						.detail {
							line-height: 52rpx;
						}
		
						.image {
							display: flex;
		
							.icon {
								width: 120rpx;
								height: 120rpx;
								border-radius: 10rpx;
								margin-right: 10rpx;
							}
						}
					}
				}
				.handle-box {
					border-top: 1rpx solid #F0F0F2;
					height: 95rpx;
					display: flex;
					align-items: center;
					justify-content: flex-end;
					.btn {
						width: 200rpx;
						height: 50rpx;
						border: 1rpx solid #B5B8ED;
						border-radius: 25rpx;
						margin-left: 30rpx;
						font-size: 28rpx;
						color: #6A59F2;
						line-height: 50rpx;
						text-align: center;
					}
				}
			}
		}
	}
</style>