<template>
	<!-- 出入库 -->
	<view>
		<u-navbar :title="navTitle" autoBack placeholder @leftClick="leftClick"></u-navbar>
		<view class="card_order_details">
			<view class="card_search" v-if="selectiveType == 'enter'">
				<view class="details_title">冷冻柜 <span class="line_title">*</span></view>
				<view class="card_input">
					<view class="card_search_gray frame" @click="focus(true)">
						<span v-if="coldCupboard.name">{{coldCupboard.name}}</span>
						<span class="title_gray" v-else>请选择冷冻柜</span>
						<u-icon name="arrow-down" color="#c8c9cc" size="18"></u-icon>
					</view>
					<!-- <u-input placeholder="请选择" border="surround" v-model="coldCupboard.name" suffixIcon="arrow-down"
						@focus="focus(true)"></u-input> -->
				</view>
			</view>
			<view class="card_search" v-if="selectiveType == 'out'">
				<view class="details_title">保温箱 <span class="line_title">*</span></view>
				<view class="card_input">
					<view class="card_search_gray frame" @click="focus(false)">
						<span v-if="incubator.name">{{incubator.name}}</span>
						<span class="title_gray" v-else>请选择保温箱</span>
						<u-icon name="arrow-down" color="#c8c9cc" size="18"></u-icon>
					</view>
					<!-- <u-input placeholder="请选择" border="surround" v-model="incubator.name" suffixIcon="arrow-down"
						@focus="focus(false)"></u-input> -->
				</view>
			</view>
			<!-- <view class="card_search" v-if="selectiveType == 'out'">
				<view class="details_title">冰排适冷温度 <span class="line_title">*</span></view>
				<view class="card_input">
					<u-input placeholder="请输入冰排适冷温度" border="surround" v-model="suitableForCold"></u-input>
				</view>
				<view class="title_hour"> ℃ </view>
			</view> -->
			<!-- <view class="card_search" v-if="selectiveType == 'enter'">
				<view class="details_title">冷冻要求 <span class="line_title">*</span></view>
				<view class="card_input">
					<u-input placeholder="请输入冷冻要求" border="surround" v-model="freezingDuration"></u-input>
				</view>
				<view class="title_hour">小时</view>
			</view> -->
			<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 class="deleteCurrent" @click="removeWaybill(frequencyCoding)">
					<u-icon class="icon_current" name="backspace" height="10" size="20"></u-icon>
					<view class="title_nape">删除</view>
				</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>
		<u-popup :show="courierShow" mode="bottom" closeable :round="10" :closeOnClickOverlay="false" @close="close">
			<view class="card_courier">
				<view class="headline">{{courierHeadline}}</view>
				<view class="search_card">
					<u-search :showAction="false" v-model="keyword" @change="searchChange"
						placeholder="输入关键字快速查找"></u-search>
				</view>
				<scroll-view class="scroll-view" scroll-y="true" @scrolltolower="loadMore">
					<view class="courier" v-if="courieList.length > 0">
						<view class="courier_item space_between" v-for="(item,index) in courieList" :key="index"
							@click="bindingDistributor(item)">
							<view class="courier_name">
								<view class="title_name">{{item.name}}</view>
							</view>
							<view class="xd_title center_in">选择</view>
						</view>
					</view>
					<view style="margin-top: 30rpx;" v-else>
						<u-empty mode="list" :text="textEmpty"></u-empty>
					</view>
				</scroll-view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	const ENV = require('@/.env.js')
	export default {
		data() {
			return {
				frequencyCoding: '',
				navTitle: '',
				selectiveType: '',
				cmpCode: '',
				list: [],
				orderId: '',
				freezingDuration: '',
				suitableForCold: null,

				keyword: '',
				courierHeadline: '选择冷冻柜',
				textEmpty: '暂无冷冻柜',
				courieList: [],
				courierShow: false,
				iceLockerId: '',
				freezeClaim: '',
				typeFlag: true,

				incubatorMore: true,
				Pagination: 1,
				pageSize: 10,
				coldCupboard: {
					id: null,
					name: '',
				},
				incubator: {
					id: null,
					name: '',
				},
			}
		},
		onUnload() {
			uni.removeStorageSync('incubatorValue');
		},
		onLoad(receive) {
			this.navTitle = receive.title
			this.selectiveType = receive.id
		},
		onShow() {
			let incubator = uni.getStorageSync('incubatorValue')
			if (incubator) {
				if (incubator.id != this.incubator.id) {
					this.incubator.id = incubator.id
					this.incubator.name = incubator.name
				}
			}
		},
		methods: {
			focus(flag) {
				this.typeFlag = flag
				this.courieList = []
				this.Pagination = 1
				this.incubatorMore = true
				if (flag) {
					this.courierHeadline = '选择冷冻柜'
					this.textEmpty = '暂无冷冻柜'
					this.getFreezer()
					this.courierShow = true
				} else {
					uni.navigateTo({
						url: '/pages/order/Incubator'
					});
					// this.courierHeadline = '选择保温箱'
					// this.textEmpty = '暂无保温箱'
					// this.getIncubator()
				}
			},
			// 关键字搜索
			searchChange(value) {
				this.Pagination = 1
				this.incubatorMore = true
				this.courieList = []
				if (this.typeFlag) {
					this.getFreezer()
				} else {
					this.getIncubator()
				}
			},
			// 获取冷冻柜
			getFreezer() {
				this.$api.get('/api/ice-locker', {
					page: this.Pagination,
					pageSize: this.pageSize,
					name: this.keyword,
					status: '2',
				}).then(res => {
					if (res.code == 200) {
						const data = res.data.list
						if (this.incubatorMore == true) {
							this.courieList = this.courieList.concat(data);
						}
						if (data.length < this.pageSize) {
							this.incubatorMore = false
						} else {
							this.incubatorMore = true
						}
					}
				})
			},
			// 获取保温箱
			getIncubator() {
				this.$api.get('/api/cooler-box', {
					page: this.Pagination,
					pageSize: this.pageSize,
					name: this.keyword,
					status: '2',
				}).then(res => {
					if (res.code == 200) {
						const data = res.data.list
						if (this.incubatorMore == true) {
							this.courieList = this.courieList.concat(data);
						}
						if (data.length < this.pageSize) {
							this.incubatorMore = false
						} else {
							this.incubatorMore = true
						}
					}
				})
			},
			// 选择保温箱.冷冻柜
			bindingDistributor(value) {
				if (this.typeFlag) {
					this.coldCupboard.id = value.id
					this.coldCupboard.name = value.name
				} else {
					this.incubator.id = value.id
					this.incubator.name = value.name
				}
				this.courierShow = false
			},
			// 滚动加载更多
			loadMore() {
				if (this.incubatorMore) {
					this.Pagination++
					if (this.typeFlag) {
						this.getFreezer()
					} else {
						this.getIncubator()
					}
				}
			},
			close() {
				this.courierShow = false
			},
			leftClick() {
				uni.$emit('refresh');
			},
			async submit() {
				await this.getEntering()
				if (this.list.length > 0) {
					if (this.typeFlag) {
						// 入库
						this.$api.post('/api/ice-raft/in-storage', {
							code: this.list,
							iceLockerId: this.coldCupboard.id,
							freezeClaim: Number(this.freezingDuration),
						}).then(res => {
							if (res.code == 200) {
								this.list = []
								this.freezingDuration = ''
								this.frequencyCoding = ''
								this.coldCupboard.id = null
								this.coldCupboard.name = ''
								uni.showToast({
									icon: 'none',
									title: res.msg,
									duration: 3000
								});
							} else {
								uni.showToast({
									icon: 'none',
									title: res.msg,
									duration: 3000
								});
							}
						})
					} else {
						// 出库
						this.$api.post('/api/ice-raft/out-storage', {
							code: this.list,
							coolerBoxId: this.incubator.id,
							suitableForCold: Number(this.suitableForCold),
						}).then(res => {
							if (res.code == 200) {
								this.list = []
								this.suitableForCold = null
								this.frequencyCoding = ''
								this.incubator.id = null
								this.incubator.name = ''
								uni.showToast({
									icon: 'none',
									title: res.msg,
									duration: 3000
								});
							} else {
								uni.showToast({
									icon: 'none',
									title: res.msg,
									duration: 3000
								});
							}
						})
					}
				} else {
					uni.$u.toast('请先录入冰排编号')
				}
			},
			// 扫码录入
			async getEntering() {
				if (this.frequencyCoding) {
					await this.$api.get('/api/ice-raft/code/' + this.frequencyCoding).then(res => {
						if (res.code == 200) {
							this.list.push(this.frequencyCoding)

							function methods1(arr) {
								return Array.from(new Set(arr));
							}
							this.list = methods1(this.list)
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg,
								duration: 3000
							});
						}
					})
				}
			},
			// 扫一扫
			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;
	}

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

	.title_hour {
		flex: none;
		font-size: 30rpx;
		margin-left: 15rpx;
	}

	.card_time {
		width: 100%;
		border: 1rpx solid #dadbde;
		border-radius: 10rpx;
		padding: 6px 9px;
		margin-left: 10rpx;
		line-height: 48rpx;
	}

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

	.card_sign_in {
		margin-top: 30rpx;
		display: flex;
	}

	.scan_title {
		font-size: 20rpx;
	}

	.card_sweep {
		display: flex;
		flex-direction: column;
		align-items: center;
		flex: none;
	}

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

	.line_title {
		color: red;
	}

	.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_empty {
		width: 100%;
		margin-top: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.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;
	}

	.deleteCurrent {
		display: flex;
		align-items: center;
		flex-direction: column;
		justify-content: center;
		border: 1px solid #e5e5e5;
		padding: 5rpx 0rpx 5rpx 5rpx;
		margin-left: 10rpx;
		border-radius: 8rpx;
		height: 66rpx;

		.icon_current {
			height: 15px;
		}

		.title_nape {
			margin: 0rpx 10rpx;
			display: flex;
			font-size: 20rpx;
			width: 40rpx;
		}
	}

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

	::v-deep .u-border {
		border-width: 1px !important;
		border-color: #e5e5e5 !important;
		border-style: solid;
	}

	.card_courier {
		min-height: 600rpx;
		padding-bottom: 20rpx;
	}

	.scroll-view {
		max-height: 60vh;
		overflow: hidden;
	}

	.headline {
		padding: 30rpx 30rpx 0rpx 30rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		padding-bottom: 30rpx;
		font-size: 32rpx;
		font-weight: 600;
	}

	.search_card {
		padding: 0rpx 30rpx 10rpx 30rpx;
	}

	.courier_item {
		margin: 20rpx 20rpx;
		width: calc(100% - 80rpx);
		padding: 20rpx;
		border-radius: 16rpx;
		border: 1rpx solid #e7e6e4;
		// box-shadow: 0 2rpx 10rpx 0 rgba(0, 0, 0, .1);
	}

	.courier_name {
		display: flex;
		flex-direction: column;
	}

	.title_name {
		font-weight: 600;
		font-size: 32rpx;
		margin-bottom: 10rpx;
	}

	.title_phone {
		font-size: 28rpx;
	}

	.xd_title {
		width: auto;
		font-size: 28rpx;
		font-weight: 500;
		padding: 15rpx 30rpx;
		border-radius: 35rpx;
		// border: 3rpx solid #82848a;
		background-color: #E4E7ED;
	}

	.card_search_gray {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		flex: 1;
		padding: 12rpx 18rpx;
		border-radius: 8rpx;
		line-height: 48rpx;
	}

	.title_gray {
		font-size: 30rpx;
		color: #c8c9cc;
	}

	// 伪元素1rpx边框
	.frame {
		position: relative; //重要
	}

	.frame::after {
		position: absolute;
		content: '';
		border: 4rpx solid #e7e6e4;
		border-radius: 16rpx;
		width: 200%;
		height: 200%;
		top: 0;
		left: 0;
		transform: scale(0.5);
		transform-origin: 0 0;
		pointer-events: none;
		/* 使伪元素不会阻止鼠标事件 */
	}
</style>