<template>
	<!-- 选择保温箱、 -->
	<view>
		<u-navbar autoBack placeholder></u-navbar>
		<u-sticky :customNavHeight="navbarHeight()">
			<view class="search_card">
				<u-search :showAction="false" v-model="keyword" @change="searchChange"
					placeholder="输入关键字快速查找"></u-search>
			</view>
		</u-sticky>
		<view class="card_incubator" v-if="orderList.length > 0">
			<view :class="iceColdFlag ? 'item_bator' : 'item_bator_cold'" v-for="(item,index) in orderList" :key="index"
				@click.stop="selectIncubator(item)">
				<view style="display: flex;align-items: center;">
					<span class="iconfont icon-incubator" :class="iceColdFlag ? 'imagebwx' : 'imagebwx_cold'"></span>
					<view>{{item.name}}</view>
				</view>
				<view class="floe_card" v-if="item.ice_raft.length > 0 && iceColdFlag">
					<view class="card_ice_list">
						<view class="card_ice_item" v-for="(item1,index) in item.ice_raft">
							<view style="display: flex;align-items: center;height: 60rpx;">
								<span class="iconfont icon-bingpaiguanli icon_ice"></span>
								<view style="margin-left: 5rpx;">
									<view class="ice_title">{{item1.label}}</view>
									<view class="ice_code">{{item1.code}}</view>
								</view>
							</view>
							<view style="display: flex;align-items: center;">
								<view class="title_cryophilic" style="margin-right: 5rpx;">
									适冷温度:<span v-if="item1.iceRaftRecord">{{item1.iceRaftRecord.suitableForCold || 0}}℃
									</span></view>
								<view class="title_cryophilic">适冷时间:<span>≥{{item1.suitableForColdTime}}min</span>
								</view>
							</view>
							<view class="title_cryophilic" v-if="item1.iceRaftRecord">
								出库时间:{{item1.iceRaftRecord.outStorageTime}}</view>
						</view>
						<view class="card_ice_item" style="border: unset;flex: 1;padding: 0rpx;margin: 0rpx;"></view>
					</view>
					<view class="card_ice_list"></view>
				</view>
				<view class="btn_printil markd10" v-if="item.ice_raft.length > 0 && getIceState(item.ice_raft)"
					@click.stop="endCooling(item.ice_raft)">
					<u-button size="small" type="warning" text="结束适冷"></u-button>
				</view>
			</view>
			<view style="width: 100%;">
				<u-loadmore lineColor="#ffffff" :status="loadStatus" :key="Math.random()" />
			</view>
		</view>
		<view style="margin-top: 20%;" v-else>
			<u-empty mode="list" text="暂无保温箱"></u-empty>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword: '',
				currentPage: 1,
				pageSize: 15,
				loadingMore: true,
				loadStatus: 'loadmore', //loading 、nomore
				incubatorValue: '',
				orderList: [],
				detailsFlag: false,
				iceData: [{
					label: '冰排1',
					code: '202409091350938276'
				}, {
					label: '冰排1',
					code: '202409091350938276'
				}, {
					label: '冰排1',
					code: '202409091350938276'
				}],
				showFlag: true,
				// 是否释冷
				iceColdFlag: true,
			}
		},
		onReachBottom() {
			if (!this.loadingMore) {
				this.getIncubator()
			}
		},
		onLoad(option) {
			if (option.detailsFlag) {
				this.detailsFlag = true
			}
			console.log(option, 25)
		},
		mounted() {
			this.getIncubator()
		},
		methods: {
			// 搜索
			searchChange(value) {
				this.currentPage = 1
				this.orderList = []
				this.loadingMore = true
				this.getIncubator()
			},
			// 获取保温箱
			getIncubator() {
				this.loadStatus = 'loading'
				this.loadingMore = true
				this.$api.get('/api/cooler-box', {
					page: this.currentPage,
					pageSize: this.pageSize,
					name: this.keyword,
					status: '2',
				}).then(res => {
					if (res.code == 200) {
						const data = res.data.list
						if (this.loadingMore == true && data) {
							this.orderList = this.orderList.concat(data);
							// console.log(this.orderList, 245)
						}
						if (data.length < this.pageSize) {
							this.loadingMore = true
							this.loading = '没有更多了'
							this.loadStatus = 'nomore'
						} else {
							this.loading = '加载中'
							this.loadStatus = 'loading'
							this.loadingMore = false
							this.currentPage++
						}
					}
				})
			},
			// 结束适冷
			endCooling(event) {
				let list = event.filter(item => item.iceRaftRecord.isSuitableForCold == 0);
				const idsString = list.map(item => item.iceRaftRecordId);
				this.$api.post('/api/ice-raft-record/end-for-cold', {
					iceRaftRecordId: idsString,
				}).then(res => {
					console.log(res, 235)
					if (res.code == 200) {
						uni.$u.toast('操作成功')
						this.currentPage = 1
						this.orderList = []
						this.getIncubator()
					}
				})
			},
			// 选择保温箱
			selectIncubator(value) {
				if (this.detailsFlag) {
					console.log('详情')
				} else {
					uni.setStorageSync('incubatorValue', value)
					uni.navigateBack({
						delta: 1
					});
				}
			},
			// 保温箱下所有冰排适冷状态
			getIceState(event) {
				let arrList = event
				let bol = true
				// isSuitableForCold 0 未适冷 1 已适冷
				for (let i = 0; i < arrList.length; i++) {
					if (arrList[i].iceRaftRecord.isSuitableForCold == 1) {
						bol = false
						break //减少循环次数
					}
				}
				return bol
			},
			navbarHeight() {
				let systemInfo = uni.getSystemInfoSync();
				/* (750 / systemInfo.windowWidth) */
				/* 在uview navBar组件中有一个默认高度，当这个默认高度加上状态栏高度后就是吸顶的位置，由于这两者相加是px，所以最后还需要转为rpx */
				let topHeight = 0
				// #ifdef APP-PLUS
				topHeight = 44 + systemInfo.statusBarHeight;
				// #endif
				// #ifdef MP
				let height = systemInfo.platform == 'ios' ? 44 : 48;
				topHeight = height + systemInfo.statusBarHeight
				// #endif
				/* 最后一步将px转为rpx */
				return topHeight * (750 / systemInfo.windowWidth) / 2
			},
		},
	}
</script>

<style lang="scss" scoped>
	.card_incubator {
		display: flex;
		flex-direction: column;
		margin-top: 10rpx;
	}

	.search_card {
		background-color: #fff;
		padding: 20rpx;
	}

	.item_bator {
		display: flex;
		flex-direction: column;
		background-color: #fff;
		border-radius: 10rpx;
		padding: 15rpx;
		margin: 10rpx 20rpx;
	}

	.item_bator_cold {
		display: flex;
		flex-direction: column;
		background-color: #fff;
		border-radius: 10rpx;
		padding: 30rpx;
		margin: 10rpx 20rpx;
	}

	.imagebwx {
		color: #2b85e4;
		font-size: 60rpx;
		margin-right: 20rpx;
	}

	.imagebwx_cold {
		color: #2b85e4;
		font-size: 70rpx;
		margin-right: 20rpx;
	}

	.floe_card {
		padding: 15rpx 0rpx;
		display: flex;
		flex-direction: column;
	}

	.card_ice_list {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		justify-content: space-between;
	}

	.card_ice_item {
		flex: none;
		margin: 0rpx 10rpx;
		margin-bottom: 10rpx;
		padding: 6rpx 10rpx;
		border-radius: 6rpx;
		border: 1rpx solid #e7e6e4;
	}

	.ice_title {
		font-size: 28rpx;
	}

	.icon_ice {
		font-size: 44rpx;
		color: #19be6b;
		margin-right: 5rpx;
	}

	.ice_code {
		font-size: 24rpx;
	}

	.title_cryophilic {
		margin-top: 5rpx;
		font-size: 23rpx;

		span {
			color: #19be6b;
		}
	}
</style>