<template>
	<view>
		<u-navbar title="冰排释冷" autoBack placeholder></u-navbar>
		<view class="card_order_details">
			<view class="card_search">
				<view class="details_title">冰排编号 <span class="line_title">*</span></view>
				<view class="card_input">
					<u-input border="surround" v-model="frequencyCoding">
						<template slot="suffix">
							<u-icon name="scan" size="24" @click="sweep"></u-icon>
						</template>
					</u-input>
				</view>
				<view class="add_card center_in" @click="addIce">
					<u-icon name="plus" size="18"></u-icon>
				</view>
			</view>
			<view class="card_frequency">
				<view class="card_high space_between">
					<view class="card_frequency_title">已录入冰排编号</view>
					<view class="card_bottle">已扫<span>{{list.length}}</span></view>
				</view>
				<view style="width: 100%;" v-if="list.length > 0">
					<view class="item_coding" v-for="(item,index) in list" :key="index">
						<view class="title_coding">{{item}}</view>
						<u-icon name="close-circle-fill" color="#c0c4cc" size="20"
							@click="removeWaybill(item)"></u-icon>
					</view>
				</view>
			</view>
			<view style="width: 100%;height: 120rpx;"></view>
			<view class="card_btn">
				<u-button style="margin-bottom: 20rpx;" type="primary" @click="submit">提交</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				frequencyCoding: '',
				list: [],
			}
		},
		methods: {
			submit() {

			},
			// 扫一扫
			sweep() {
				// 允许从相机和相册扫码
				uni.scanCode({
					scanType: ['barCode'],
					// scanType: ['qrCode'],
					autoZoom: false,
					success: (res) => {
						console.log(res);
						if (res.result) {
							let url = res.result;
							this.frequencyCoding = url
							this.$api.get('/api/ice-raft/code/' + this.frequencyCoding).then(res => {
								if (res.code == 200) {
									this.list.push(url)

									function methods1(arr) {
										return Array.from(new Set(arr));
									}
									this.list = methods1(this.list)
								} else {
									uni.showToast({
										icon: 'none',
										title: res.msg,
										duration: 3000
									});
								}
							})
						} else {
							console.log('请重新扫描');
							return false;
						}
					},
					fail: (res) => {
						console.log('未识别到条形码');
					}
				})
			},
			// 添加冰排 
			addIce() {
				if (this.frequencyCoding) {
					this.$api.get('/api/ice-raft/code/' + this.frequencyCoding).then(res => {
						if (res.code == 200) {
							this.list.push(this.frequencyCoding)
							this.list = this.uniqueArray(this.list)
							this.frequencyCoding = ''
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg,
								duration: 3000
							});
						}
					})
				}
			},
			// 去重
			uniqueArray(arr) {
				return [...new Set(arr)];
			},
			// 移除错误运单号
			removeWaybill(value) {
				if (this.frequencyCoding == value) {
					this.frequencyCoding = ''
				}
				const arr = deleteElementById(this.list, value)
				this.list = arr

				function deleteElementById(arr, key) {
					return arr.filter((item) => item !== key);
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff !important;
	}

	.card_order_details {
		margin: 10rpx 30rpx 30rpx 30rpx;
	}

	.card_search {
		display: flex;
		align-items: center;
		margin: 20rpx 0rpx;
	}

	.details_title {
		width: 150rpx;
		flex: none;
		color: #333;
		font-size: 32rpx;
		font-weight: 500;
		margin-bottom: 10rpx;
		// margin-right: 15rpx;
	}

	.line_title {
		color: red;
	}

	.card_input {
		width: 100%;
		margin-left: 30rpx;
	}

	.add_card {
		margin-left: 10rpx;
		width: 50rpx;
		height: 46rpx;
		border-radius: 8rpx;
		padding: 6px 9px;
		border: 1px solid #dadbde;
	}

	.card_high {
		margin-top: 20rpx;
	}

	.card_frequency {
		display: flex;
		flex-wrap: wrap;
	}

	.card_frequency_title {
		font-size: 32rpx;
	}

	.card_bottle {
		font-size: 30rpx;

		span {
			color: red;
		}
	}

	.item_coding {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
		margin-top: 10px;
		padding: 25rpx 0rpx;
		border-bottom: 2rpx solid #dfdfdf;
	}

	.title_coding {
		font-size: 30rpx;
	}

	.card_btn {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		padding-left: 30rpx;
		padding-right: 30rpx;
		padding-top: 20rpx;
		background-color: #fff;
		padding-bottom: constant(safe-area-inset-bottom); //兼容 IOS<11.2
		padding-bottom: env(safe-area-inset-bottom); //兼容 IOS>11.2
		border-top: 1rpx solid #E4E7ED;
	}
</style>