<template>
	<scroll-view class="container" :scroll-y="scrollY" @scrolltolower="scrolltolower">
		<view class="top_bg" v-if="dataList.length"></view>
		<view class="body" v-if="dataList.length">
			<!-- 每一个订单区域 -->
			<view class="body_item" v-for="item in dataList" :key="item.id">
				<!-- 工单编号 -->
				<view class="item_title">
					<img src="../../static/images/repairsImg/order.png" />
					<view class="title_info">工单编号：{{ item.recordNo }}</view>
				</view>
				<!-- 报修时间 -->
				<view class="item_time">
					<view class="time_msg">
						报修时间：
						<text>{{ item.reportTime }}</text>
					</view>
					<view class="time_type">{{ item.state }}</view>
				</view>

				<!-- 报修姓名 -->
				<view class="item_box">
					<view class="box_key">报修姓名：</view>
					<view class="box_value">{{ item.userName }}</view>
				</view>

				<!-- 报修电话 -->
				<view class="item_box">
					<view class="box_key">报修电话：</view>
					<view class="box_value phone" @click="handleCallPhone(item.userPhone)">
						{{ item.userPhone }}
						<img src="../../static/images/repairsImg/phone.png" />
					</view>
				</view>

				<!-- 报修区域 -->
				<view class="item_box">
					<view class="box_key">报修区域：</view>
					<view class="box_value">{{ item.areaName }}</view>
				</view>

				<!-- 详细地址 -->
				<view class="item_box">
					<view class="box_key">详细地址：</view>
					<view class="box_value">{{ item.address }}</view>
				</view>

				<!-- 报修物品 -->
				<view class="item_box">
					<view class="box_key">报修物品：</view>
					<view class="box_value">{{ item.articleName }}</view>
				</view>

				<!-- 故障描述 -->
				<view class="item_box" v-if="item.description">
					<view class="box_key">故障描述：</view>
					<view class="box_value">{{ item.description }}</view>
				</view>

				<!-- 上传图片 -->
				<view class="item_img">
					<view class="img_key">上传图片：</view>
					<img class="img_value" mode="aspectFill" :src="item.images[0]" @click="handleLookImgs(item.images)" />
				</view>

				<!-- 按钮区域 -->
				<view class="item_btn">
					<img src="../../static/images/repairsImg/close.png" @click="handleClose(item)" />
					<view class="btn_box type" v-if="btns.includes('延时')" @click="handleDelay(item)">延时</view>
					<view class="btn_box type" v-if="btns.includes('接单')" @click="handleOrderReceiving(item)">接单</view>
					<view class="btn_box type2" v-if="btns.includes('派单')" @click="handleSendOrder(item)">派单</view>
				</view>

				<!-- 关闭按钮弹窗 -->
				<uni-popup ref="popup_close" :is-mask-click="false">
					<view class="popup_close">
						<view
							:class="[{ active: activeIndex === index }, { radius: index === 0 }]"
							class="close_box"
							v-for="(ele, index) in closeList"
							:key="ele.id"
							@click="handleChangeItem(index)"
						>
							{{ ele.name }}
						</view>
						<view class="close_btn">
							<view class="btn_box cancel" @click="$refs.popup_close[0].close()">取消</view>
							<view class="btn_box confirm" @click="handleCloseConfirm">确定</view>
						</view>
					</view>
				</uni-popup>
			</view>
		</view>
		<!-- 没有数据时展示的图片 -->
		<view class="no_data" v-if="dataList.length === 0">
			<img src="../../pagesClockIn/static/imgs/nodata.png" />
			<view>暂无数据</view>
		</view>
	</scroll-view>
</template>

<script>
export default {
	data() {
		return {
			scrollY: true,
			// 按钮权限
			btns: [],
			// 关闭按钮弹窗当前索引
			activeIndex: 0,
			// 关闭按钮弹窗选项数组
			closeList: [],
			// 工单数据数组
			dataList: [],
			// 当前页
			currentPage: 1,
			// 每页多少条
			pageCount: 4,
			// 总条数
			total: null,
			// 用户ID
			userId: '',
			// 订单id
			orderId: ''
		}
	},
	mounted() {
		console.log('待处理池页面加载')
		const repairsUserInfo = uni.getStorageSync('repairsUserInfo')
		this.userId = repairsUserInfo.userId
		this.btns = repairsUserInfo.btns
		console.log(this.btns)
		this.getData()
		this.getCloseList()
	},
	methods: {
		// 滚动到底部触发回调
		scrolltolower() {
			if (this.total > this.dataList.length) {
				this.currentPage++
				this.getData()
			} else {
				uni.showToast({
					title: '没有更多数据了',
					icon: 'none'
				})
			}
		},
		// 获取关闭订单原因数组
		async getCloseList() {
			const res = await this.$myRequest_repairs({
				url: '/repairClose/queryRepairCloses'
			})
			// console.log(res)
			if (res.code === '200') {
				this.closeList = res.data
			}
		},
		// 获取待处理订单数据
		async getData() {
			const res = await this.$myRequest_repairs({
				url: '/repairRecord/queryPendingOrder',
				data: {
					currentPage: this.currentPage,
					pageCount: this.pageCount
				}
			})
			// console.log(res)
			if (res.code === '200') {
				this.total = res.data.totalCount
				this.dataList = [...this.dataList, ...res.data.list]
			}
		},
		// 接单按钮回调
		handleOrderReceiving(item) {
			uni.showModal({
				content: '确定接单吗？',
				title: '提示',
				success: async (res) => {
					if (res.confirm) {
						const res = await this.$myRequest_repairs({
							url: '/repairRecord/receiveOrders',
							data: {
								recordId: item.id,
								userId: this.userId
							}
						})
						// console.log(res)
						if (res.code === '200') {
							uni.showToast({
								title: '接单成功',
								icon: 'success'
							})
							setTimeout(() => {
								this.dataList = []
								this.currentPage = 1
								this.getData()
							}, 1500)
						}
					}
				}
			})
		},
		// 关闭图标点击回调
		handleClose(item) {
			this.orderId = item.id
			this.activeIndex = 0
			this.$refs.popup_close[0].open('center')
		},
		// 关闭弹窗确定按钮回调
		handleCloseConfirm() {
			uni.showModal({
				title: '提示',
				content: '确定关闭订单吗？',
				success: async (res) => {
					if (res.confirm) {
						const res = await this.$myRequest_repairs({
							url: '/repairRecord/closeOrder',
							data: {
								recordId: this.orderId,
								userId: this.userId,
								reason: this.closeList[this.activeIndex].name
							}
						})
						// console.log(res)
						if (res.code === '200') {
							uni.showToast({
								title: '关单成功',
								icon: 'success'
							})
							this.$refs.popup_close[0].close()
							setTimeout(() => {
								this.dataList = []
								this.currentPage = 1
								this.getData()
							}, 1500)
						}
					}
				}
			})
		},
		// 关闭按钮弹窗选中按钮时回调
		handleChangeItem(val) {
			this.activeIndex = val
		},
		// 延时按钮回调
		handleDelay(item) {
			uni.navigateTo({
				url: `/pagesRepairs/delay/delay?id=${item.id}`
			})
		},
		// 派单按钮回调
		handleSendOrder(item) {
			uni.navigateTo({
				url: `/pagesRepairs/helpPeople/helpPeople?type=1&id=${item.id}`
			})
		},
		// 点击电话号码回调
		handleCallPhone(phone) {
			// console.log(phone)
			uni.makePhoneCall({
				phoneNumber: phone
			})
		},
		// 点击图片回调
		handleLookImgs(img) {
			// console.log(img)
			uni.previewImage({
				urls: img
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	width: 100vw;
	height: calc(100vh - 152rpx);
	font-size: 32rpx;
	overflow-y: auto;

	.top_bg {
		height: 165rpx;
		background-color: #6fb6b8;
	}

	.body {
		box-sizing: border-box;
		margin: auto;
		margin-top: -100rpx;
		padding: 2rpx;
		width: 690rpx;
		border-radius: 11rpx 11rpx 0 0;
		background-color: #fff;
		overflow-y: auto;

		.body_item {
			margin-bottom: 20rpx;
			border-radius: 11rpx;
			box-shadow: 0 0 8rpx #d9d9d9;

			.item_title {
				display: flex;
				align-items: center;
				padding: 0 30rpx;
				height: 87rpx;
				border-bottom: 1rpx solid #e6e6e6;

				img {
					width: 30rpx;
					height: 30rpx;
				}

				.title_info {
					margin: 0 21rpx 0 13rpx;
					// font-size: 28rpx;
				}
			}

			.item_time {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 30rpx;
				height: 79rpx;

				.time_msg {
					// font-size: 28rpx;
					color: #808080;

					text {
						color: #000000;
					}
				}

				.time_type {
					font-size: 32rpx;
					color: #ff5733;
				}
			}

			.item_box {
				display: flex;
				padding: 0 30rpx;
				height: 60rpx;
				// font-size: 28rpx;

				.box_key {
					color: #808080;
				}

				.box_value {
					display: flex;

					img {
						margin-left: 10rpx;
						width: 45rpx;
						height: 45rpx;
					}
				}

				.phone {
					color: #6fb6b8;
				}
			}

			.item_img {
				display: flex;
				align-items: center;
				padding: 0 30rpx;
				height: 120rpx;
				color: #808080;
				// font-size: 28rpx;

				.img_key {
				}

				.img_value {
					width: 120rpx;
					height: 120rpx;
					border-radius: 14rpx;
				}
			}

			.item_btn {
				display: flex;
				align-items: center;
				justify-content: flex-end;
				padding: 0 30rpx;
				height: 153rpx;

				img {
					margin-right: auto;
					width: 56rpx;
					height: 56rpx;
				}

				.btn_box {
					display: flex;
					justify-content: center;
					align-items: center;
					margin-left: 16rpx;
					width: 174rpx;
					height: 68rpx;
					// font-size: 28rpx;
					border-radius: 11rpx;
				}

				.type {
					color: #fff;
					background-color: #6fb6b8;
				}

				.type2 {
					color: #6fb6b8;
					background-color: #fff;
					border: 1rpx solid #6fb6b8;
				}
			}

			.popup_close {
				width: 690rpx;
				border-radius: 19rpx;
				background-color: #fff;

				.close_box {
					height: 100rpx;
					line-height: 100rpx;
					text-align: center;
					font-size: 32rpx;
					border: 1rpx solid #e6e6e6;
				}

				.radius {
					border-radius: 19rpx 19rpx 0 0;
				}

				.active {
					color: #fff;
					background-color: #6fb6b8;
				}

				.close_btn {
					display: flex;
					align-items: center;
					justify-content: space-evenly;
					margin-top: 100rpx;
					height: 121rpx;

					.btn_box {
						display: flex;
						justify-content: center;
						align-items: center;
						width: 203rpx;
						height: 72rpx;
						border-radius: 9rpx;
						font-size: 32rpx;
					}

					.cancel {
						background-color: #e5e5e5;
						color: #6fb6b8;
					}

					.confirm {
						background-color: #6fb6b8;
						color: #fff;
					}
				}
			}
		}
	}

	.no_data {
		text-align: center;
		color: #b3b3b3;
		font-size: 24rpx;

		img {
			margin: 215rpx auto 0;
			width: 480rpx;
			height: 508rpx;
		}
	}
}
</style>
