<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>
			<view class="card_tab_freezer" v-if="detailsFlag && isCoolerInfo.isCoolerReleaseCold">
				<view class="select_freezer">
					<view style="display: flex;align-items: center;margin-right: 10rpx;">
						<view class="title_num blue_color">总数:{{Total}}</view>
						<view class="title_num green_color">已选:{{selectedList.length}}</view>
					</view>
					<view style="display: flex;align-items: center;">
						<view class="select_title blue_color" @click="checkAll">全选</view>
						<view class="select_title orange_color" @click="Inverse">反选</view>
					</view>
				</view>
			</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 class="position-triangle" v-if="item.whetherFlag">
					<span>已选</span>
				</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 style="width: 100%;height: 120rpx;" class="env_padding" v-if="detailsFlag"></view>
		<view class="card_btn_freezer" v-if="detailsFlag && isCoolerInfo.isCoolerReleaseCold">
			<u-button style="margin-bottom: 20rpx;" type="primary" @click="coolingRelease">保温箱预冷</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword: '',
				currentPage: 1,
				pageSize: 15,
				loadingMore: true,
				loadStatus: 'loadmore', //loading 、nomore
				incubatorValue: '',
				orderList: [],
				detailsFlag: false,
				showFlag: true,
				// 是否释冷
				iceColdFlag: true,
				Total: 0,
				selectedList: [],
				isCoolerInfo: {},
				searchShow: true,
			}
		},
		onReachBottom() {
			if (!this.loadingMore) {
				this.getIncubator()
			}
		},
		onLoad(option) {
			if (option.detailsFlag) {
				this.detailsFlag = true
			}
		},
		mounted() {
			var userInfo = this.$cache.getCache('userInfo')
			this.isCoolerInfo = userInfo.dept
			this.getIncubator()
		},
		methods: {
			// 搜索
			searchChange(value) {
				var that = this
				if (that.searchShow) {
					that.searchShow = false
					const timer = setTimeout(function() {
						clearTimeout(timer);
						that.currentPage = 1
						that.orderList = []
						that.loadingMore = true
						that.getIncubator()
					}, 300);
				}
			},
			// 获取保温箱
			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
						this.Total = res.data.count
						if (this.loadingMore == true && data) {
							this.orderList = this.orderList.concat(data);
							// console.log(this.orderList, 245)
						}
						this.getRecord()
						if (data.length < this.pageSize) {
							this.loadingMore = true
							this.loading = '没有更多了'
							this.loadStatus = 'nomore'
						} else {
							this.loading = '加载中'
							this.loadStatus = 'loading'
							this.loadingMore = false
							this.currentPage++
						}
					}
					this.searchShow = true
				})
			},
			// 保温箱预冷
			coolingRelease() {
				console.log(this.selectedList, 24)
				if (this.selectedList.length > 0) {
					let arrID = this.selectedList.map(item => item.toString())
					this.$api.post('/api/cooler-box/coolerboxstarttime', {
						id: arrID,
					}).then(res => {
						if (res.code == 200) {
							uni.$u.toast('操作成功')
						}
					})
				} else {
					uni.$u.toast('请先选择释冷冰排')
				}
			},
			// 判断冰排是否选择
			getRecord() {
				this.orderList.forEach(item => {
					let index = this.selectedList.findIndex((event) => event === item.id);
					if (index !== -1) {
						item.whetherFlag = true
					} else {
						item.whetherFlag = false
					}
				})
				this.$forceUpdate()
			},
			// 全选
			checkAll() {
				this.selectedList = []
				for (let i = 0; i < this.orderList.length; i++) {
					this.$set(this.orderList[i], 'whetherFlag', true)
				}
				this.orderList.forEach(item => {
					if (item.whetherFlag) {
						this.selectedList.push(item.id)
					}
				})
				this.getRecord()
			},
			// 反选
			Inverse() {
				this.selectedList = []
				for (let i = 0; i < this.orderList.length; i++) {
					let flag = this.orderList[i].whetherFlag;
					this.$set(this.orderList[i], 'whetherFlag', !flag)
				}
				this.orderList.forEach(item => {
					if (item.whetherFlag) {
						this.selectedList.push(item.id)
					}
				})
				this.getRecord()
			},
			// 结束释冷
			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 => {
					if (res.code == 200) {
						uni.$u.toast('操作成功')
						this.currentPage = 1
						this.orderList = []
						this.getIncubator()
					}
				})
			},
			// 选择保温箱
			selectIncubator(value) {
				if (this.detailsFlag) {
					// console.log('详情')
					let index = this.selectedList.findIndex((item) => item === value.id);
					// 如果有就替换，没有就添加
					if (index !== -1) {
						this.selectedList.splice(index, 1);
					} else {
						this.selectedList.push(value.id);
					}
					if (this.isCoolerInfo.isCoolerReleaseCold) {
						this.getRecord()
					}
				} 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;
	}

	.card_tab_freezer {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0rpx 20rpx 20rpx 20rpx;
		background-color: #fff;
	}

	.card_subsection {
		width: 50%;
	}

	.select_freezer {
		display: flex;
		align-items: flex-end;
		// flex-direction: column;
	}

	.title_num {
		font-size: 26rpx;
		margin-left: 10rpx;
	}

	.select_title {
		font-size: 30rpx;
		margin-left: 15rpx;
	}

	.blue_color {
		color: #2979ff;
	}

	.green_color {
		color: #19be6b;
	}

	.orange_color {
		color: #ff9900;
	}

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

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

	.position-triangle {
		position: absolute;
		bottom: 0;
		right: 0;
		overflow: hidden;
		height: 35px;
		width: 35px;
	}

	.position-triangle span {
		position: absolute;
		font-size: 20rpx;
		bottom: 8rpx;
		right: 3rpx;
		z-index: 1;
		color: #fff;
		transform: rotate(-45deg);
	}

	.position-triangle::after {
		content: "";
		font-size: 20rpx;
		position: absolute;
		width: 70px;
		height: 70px;
		background-color: #19be6b;
		transform: rotate(45deg);
		transform-origin: center;
		top: 50%;
	}

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

	.card_btn_freezer {
		position: fixed;
		z-index: 2;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20rpx 30rpx 20rpx 30rpx;
		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;
	}

	.env_padding {
		padding-bottom: constant(safe-area-inset-bottom); //兼容 IOS<11.2
		padding-bottom: env(safe-area-inset-bottom); //兼容 IOS>11.2
	}
</style>