<template>
	<view class="page" :style="{'min-height':(h)+'px','padding-top':mt+'px'}">
		<c-nav-bar title="核销" @goBack="back()"></c-nav-bar>
		<view class="types">
			<view class="item bg2" @tap="scanCode(0)">
				<view class="n">扫码核销</view>
				<u-icon name="arrow-right" color="#fff" size="26" label="点击扫描" label-color="#fff" label-size="28" cu
					label-pos="left"></u-icon>
			</view>
			<view class="item bg1" @tap="scanCode(1)">
				<view class="n">扫身份证</view>
				<u-icon name="arrow-right" color="#fff" size="26" label="点击扫描" label-color="#fff" label-size="28"
					label-pos="left"></u-icon>
			</view>
		</view>
		<view class="orderInfo">
			<view class="tit">
				订单详情
			</view>
			<view class="time">{{orderInfo.playDate}} {{orderInfo.playTime}}出发</view>
			<view class="bottom">
				<text>{{orderInfo.num}}人</text>
				<text>出游·{{orderInfo.playLength||0}}小时</text>
				<text>¥{{orderInfo.realPrice}}</text>
			</view>
		</view>
		<view class="list">
			<view class="tit">{{boatNo}} 共({{data.length}})人</view>
			<view class="item" v-for="(i,index) in data" :key="index">
				<text class="type" :class="i.remark=='book'?'t1':'t2'">{{i.remark=='book'?'预订单':'现场单'}}</text>
				<view class="personInfo">
					<view class="name">{{i.touristName|handle(1)}}</view>
					<view class="code">
						身份证 {{i.touristCode|handle(3)}}
					</view>
				</view>
				<u-icon name="minus-circle" size="36" color="#FEA400" @tap="jian(index)"
					v-if="i.remark!='book'"></u-icon>
			</view>
		</view>
		<view class="list">
			<view class="tit">选择渔船</view>
			<view class="typeItem">
				<view class="txt" v-for="(t,i) in types" :key="i" @click="typeIndex=i" :class="typeIndex==i?'on':''">
					{{t.boatNo}}
					<image v-if="typeIndex==i"
						src="https://i.ringzle.com/file/20240320/b458b03f8f654a51a921656b8aa955de.png"></image>
				</view>

			</view>
		</view>
		<view class="btn">
			<text @click="hexiao">开始发船</text>
		</view>
	</view>
</template>

<script>
	import {
		encrypt,
		decrypt,
		tuomin
	} from '../../utils/aes.js'
	var that;
	export default {
		data() {
			return {
				typeIndex: null,
				types: [],
				date: new Date().Format('yyyy-MM-dd'),
				show: false,
				data: [],
				merchantId: uni.getStorageSync('merchantId'),
				playTime: '',
				boatNo: '',
				type: '',
				orderInfo: {},
				keyName: ''
			}
		},
		onLoad(opt) {
			that = this;
			this.playTime = opt.playTime || '';
			this.boatNo = opt.boatNo || '';
			this.getOrderInfo();
			this.getTypes();
		},
		filters: {
			handle(val, type) {
				return tuomin(val, type);
			}
		},
		methods: {
			getTypes() {
				this.$api.get('/scenic/api/boat/page', {
					page: -1
				}).then(res => {
					if (res.data.code == 0) {
						this.types = res.data.data.list;
					}
				})
			},
			back() {
				this.keyName = this.orderInfo.playDate + this.orderInfo.playTime + this.boatNo;
				uni.setStorageSync(this.keyName, JSON.stringify(this.data));
				//decodeURIComponent
			},
			getOrderInfo() {
				this.$api.post('/scenic/api/order/queryStartOrderInfo', {
					merchantId: this.merchantId,
					boatNo: this.boatNo
				}).then(res => {
					if (res.data.code == 0) {
						this.orderInfo = res.data.data;
						this.keyName = this.orderInfo.playDate + this.orderInfo.playTime + this.boatNo;
						if (uni.getStorageSync(this.keyName)) {
							this.data = JSON.parse(uni.getStorageSync(this.keyName)) || [];
						}
					}
				})
			},
			scanCode(type) {
				uni.scanCode({
					onlyFromCamera: true,
					success: (res) => {
						console.log('扫码结果：' + res.result);
						// 扫码成功后的操作，例如：
						that.handleScanResult(res.result, type, '');
					},
					fail: (err) => {
						console.error('扫码失败：' + err);
					}
				});
			},
			// 处理扫码结果的函数，可以根据实际需求进行扩展
			handleScanResult(result, type, name) {
				// 例如：导航到某个页面，或者处理其他业务逻辑
				let sendData = {};
				sendData.playDate = this.orderInfo.playDate;
				sendData.playTime = this.orderInfo.playTime;
				sendData.playLength = this.orderInfo.playLength || 1;
				sendData.merchantId = this.orderInfo.merchantId;
				sendData.orderType = this.orderInfo.orderType;
				sendData.type = type;
				sendData.boatNo = this.boatNo;
				if(type==0) sendData.writeOffCode = result;
				else if(type==1){
					sendData.touristCode = encrypt(result);
					sendData.touristName = encrypt(name);
				} 
				this.$api.post('/scenic/api/order/scanCode', sendData).then(res => {
					if (res.data.code == 0) {
						if (this.data.findIndex((item) => {
								return item.touristCode == res.data.data.touristCode
							}) > -1) {
							this.$showToast('该核验码已扫')
						} else {
							this.data.push(res.data.data);
						}
					} else {
						this.$showToast(res.data.msg)
					}
				})
			},
			check() {
				uni.navigateTo({
					url: '/pagesHouse/Verification/check'
				})
			},
			hexiao() {
				if (this.data.length == 0) {
					return this.$showToast('请选择核销人')
				}
				if (this.typeIndex == null) {
					return this.$showToast('请选择渔船')
				}
				let boatCode = this.types[this.typeIndex].boatNo;
				let newData = JSON.parse(JSON.stringify(this.data));
				newData.forEach(d=>{
					d.touristCode = encrypt(d.touristCode);
					d.touristName = encrypt(d.touristName);
				})
				this.$api.post('/scenic/api/order/sailWriteOff', {
					playDate:this.orderInfo.playDate,
					playTime:this.orderInfo.playTime,
					playLength:this.orderInfo.playLength,
					orderType: this.orderInfo.orderType,
					touristList: newData,
					boatCode: boatCode
				}).then(res => {
					if (res.data.code == 0) {
						uni.navigateTo({
							url: "/pagesHouse/Verification/success?boatNo=" + this.boatNo + '&price=' +
								this.orderInfo.realPrice
						})
					}else this.$showToast(res.data.msg)
				})
				uni.removeStorageSync(this.keyName);
			},
			jian(index){
				if(this.data.length>index) this.data.splice(index,1);
			}
		}
	}
</script>

<style lang="less" scoped>
	/deep/.u-icon {
		justify-content: flex-end;
	}

	.btn {
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 1;
		padding: 16rpx 30rpx 36rpx 30rpx;
		width: 100%;
		box-sizing: border-box;
		background: #fff;

		text {
			display: block;
			background-color: #007A69;
			color: #007A69;
			color: #fff;
			height: 88rpx;
			line-height: 88rpx;
			border-radius: 44rpx;
			text-align: center;
			font-size: 32rp;
			font-weight: bold;
			box-sizing: border-box;


		}

	}

	.page {
		background: #F5F8FA;
		padding: 20rpx 24rpx 160rpx;
		box-sizing: border-box;

		.types {
			display: flex;
			gap: 36rpx;
			justify-content: space-between;


			.n {
				margin-bottom: 20rpx;
				color: #fff;
			}

			.item {
				width: 330rpx;
				height: 156rpx;
				padding: 34rpx 24rpx;
				font-size: 36rpx;
				font-weight: bold;
				box-sizing: border-box;
			}

			.bg2 {
				background: url('https://i.ringzle.com/file/20240316/3fc9f6a78557492e98caa50392411e3a.png') no-repeat center;
				background-size: 100%;
			}

			.bg1 {
				background: url('https://i.ringzle.com/file/20240316/8a927d5d4da84728b6bb0896541baccf.png') no-repeat center;
				background-size: 100%;
			}
		}

		.orderInfo {
			background-color: #fff;
			color: #333;
			padding: 0 24rpx 36rpx;
			margin: 20rpx 0 0;
			background: #fff;
			border-radius: 16rpx;

			.tit {
				padding: 32rpx 0 24rpx;
				font-size: 32rpx;
			}

			.time {
				font-size: 36rpx;
				font-weight: bold;
				margin-bottom: 32rpx;
			}

			.bottom {
				display: flex;
				align-items: center;

				text {
					&:first-child {
						width: 88rpx;
						height: 54rpx;
						background-color: #007A69;
						color: #fff;
						font-size: 34rpx;
						font-weight: bold;
						margin-right: 10rpx;
						text-align: center;
						border-radius: 12rpx;
					}

					&:nth-child(2) {
						width: 200rpx;
						flex: 1;
					}

					&:last-child {
						font-size: 30rpx;
						font-weight: bold;
					}
				}
			}
		}

	}

	.list {
		box-sizing: border-box;
		padding: 0 24rpx 0;
		border-radius: 16rpx;
		background-color: #fff;
		margin-top: 20rpx;

		.tit {
			font-size: 36rpx;
			font-weight: bold;
			color: #111;
			padding: 36rpx 0 17rpx;
		}

		.typeItem {
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;
			gap: 20rpx 0;
			padding-bottom: 50rpx;

			.txt {
				display: inline-block;
				width: 316rpx;
				height: 80rpx;
				background: #fff;
				border-radius: 16rpx;
				text-align: center;
				line-height: 80rpx;
				border: 2rpx solid #EFEFEF;
				color: #333;
				position: relative;

				&.on {
					color: #007A69;
					background: rgba(0, 122, 105, 0.06);
					border: 2rpx solid #007A69;

					image {
						position: absolute;
						bottom: 0;
						right: 0;
						width: 34rpx;
						height: 26rpx;
						z-index: 11;
					}
				}

			}
		}

		.item {
			display: flex;
			justify-content: space-between;
			padding: 24rpx 0;
			align-items: center;

			.type {
				width: 110rpx;
				height: 48rpx;
				text-align: center;
				display: inline-block;
				line-height: 48rpx;
				border-radius: 11rpx;
				font-size: 26rpx;
				font-weight: bold;
				color: #111;
				margin-right: 16rpx;
			}

			.t1 {
				background-color: #F0F8F6;
				color: #007A69;
			}

			.t2 {
				background-color: #E8EFFD;
				color: #326EE0;
			}

			.personInfo {
				flex: 1;

				.name {
					font-size: 30rpx;
					color: #111;
					margin-bottom: 16rpx;
				}

				.code {
					color: #666;
					font-size: 28rpx;
				}
			}

		}
	}
</style>