<template>
	<view class="service-page">
		<navigation title="设备维修"></navigation>
		<view class="tab-box">
			<u-subsection :list="list" :current="curNow" fontSize="16" @change="sectionChange"></u-subsection>
		</view>
		<view class="list-box" v-if="dataList.length>0">
			<view class="list-item" v-for="(item, index) in dataList" :key="index">
				<view class="item-text">工单编号：{{ item.num || '-' }}</view>
				<view class="item-text">设备名称：{{ item.equipmentName || '-' }}</view>
				<view class="item-text">工单描述：{{ item.orderMsg || '-' }}</view>
				<view class="item-text" v-if="curNow == 0">负责人：{{ item.ownerUserName || '-' }}</view>
				<view class="item-text" v-if="curNow == 0">认领时间：{{ item.receiveTime || '-' }}</view>
				<view class="item-text" v-if="curNow == 0">完成时间：{{ item.completeTime || '-' }}</view>
				<view class="item-text">发生时间：{{ item.happenTime || '-' }}</view>
				<view class="btn-box" v-if="curNow == 0">
					<view class="btn-item" @click="toDetail(item.id)">
						<view>详情</view>
					</view>
					<view class="btn-item" @click="confirmInfo(item.id, item.num)">
						<view>完工</view>
					</view>
					<view class="btn-item btn-gray" @click="cancelInfo(item.id)">
						<view>取消</view>
					</view>
				</view>
				<view class="btn-box" v-else>
					<view class="btn-item" @click="toDetail(item.id)">
						<view>详情</view>
					</view>
					<view class="btn-item" @click="claimInfo(item.id)">
						<view>认领</view>
					</view>
				</view>
			</view>
		</view>
		<empty v-else text="暂无数据"></empty>
		<!-- 取消确认框 -->
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="warn" cancelText="取消" confirmText="确定" title="提示" content="确认取消该任务吗？"
				@confirm="dialogConfirm"></uni-popup-dialog>
		</uni-popup>
		<!-- 认领确认框 -->
		<uni-popup ref="alertDialogClaim" type="dialog">
			<uni-popup-dialog type="warn" cancelText="取消" confirmText="确定" title="提示" content="确认要认领该任务吗？"
				@confirm="dialogConfirmClaim"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: ['我的', '待处理'],
				curNow: 0,
				dataList: [],
				id: '',
				userId: uni.getStorageSync('userInfo').id
			}
		},
		onShow() {
			this.getList()
		},
		methods: {
			sectionChange(index) {
				this.curNow = index;
				if (index == 0) {
					this.getList()
				} else if (index == 1) {
					this.getWaitReceiveList()
				}
			},
			getList() {
				uni.showLoading({
					title: '正在加载...'
				})
				let params = {
					ownerUserId: this.userId
				}
				this.$api.workOrderList(params).then(res => {
					this.dataList = res.rows
					uni.hideLoading()
				}).catch(err => {})
			},
			getWaitReceiveList() {
				uni.showLoading({
					title: '正在加载...'
				})
				this.$api.listWaitReceive().then(res => {
					this.dataList = res.rows
					uni.hideLoading()
				}).catch(err => {})
			},
			// 完工
			confirmInfo(id, num) {
				uni.navigateTo({
					url: '/subPackages/repair/finish?id=' + id + '&num=' + num
				});
			},
			// 取消
			cancelInfo(id) {
				this.id = id
				this.$refs.alertDialog.open()
			},
			dialogConfirm() {
				let that = this
				this.$api.cancelOrder(this.id).then(res => {
					uni.showToast({
						title: '任务取消成功！',
						duration: 2000
					});
					setTimeout(function() {
						that.getList()
					}, 2000);
				}).catch(err => {})
			},
			// 认领
			claimInfo(id) {
				this.id = id
				this.$refs.alertDialogClaim.open()
			},
			dialogConfirmClaim() {
				let that = this
				this.$api.receiveOrder(this.id).then(res => {
					uni.showToast({
						title: '任务认领成功！',
						duration: 2000
					});
					setTimeout(function() {
						that.curNow = 0;
						that.getList()
					}, 2000);
				}).catch(err => {})
			},
			toDetail(id) {
				uni.navigateTo({
					url: '/subPackages/repair/detail?id=' + id
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.service-page {
		.tab-box {
			// width: 100%;
			// position: fixed;
		}

		.list-box {
			padding: 0 20rpx;
			color: $uni-text-color-list;

			.list-item {
				background-color: $uni-bg-color;
				padding: 20rpx;
				border-radius: 20rpx;
				margin-top: 28rpx;

				.item-text {
					margin-top: 10rpx;
				}
			}

			.btn-box {
				display: flex;
				justify-content: flex-end;
				margin-top: 10rpx;

				.btn-item {
					width: 175rpx;
					height: 50rpx;
					line-height: 50rpx;
					text-align: center;
					border-radius: 24rpx;
					margin-left: 20rpx;
					border: 1px solid #43adfd;
					color: #43adfd;
				}

				.btn-gray {
					border: 1px solid #aaa;
					color: #aaa;
				}
			}
		}
	}
</style>
