<template>
	<!-- 选择保温箱、 -->
	<view>
		<u-navbar title="保温箱管理" 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 @click="endOfUse">
					<u-button size="small" type="error" text="结束使用"></u-button>
				</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 class="space_between">
					<view style="display: flex;align-items: center;margin-bottom: 10rpx;">
						<span class="iconfont icon-incubator"
							:class="iceColdFlag ? 'imagebwx' : 'imagebwx_cold'"></span>
						<view>{{item.name}}</view>
					</view>
					<view class="card_precooling" :style="{color: filterColor(item.useStatus)}">
						{{matchingType(item.useStatus)}}
					</view>
				</view>
				<view v-if="isCoolerInfo.isCoolerReleaseCold && item.useStatus == 1 || item.useStatus == 3">
					<view class="card_precool" v-if="item.cold_temperatures">预冷温度(℃): {{item.cold_temperatures}}</view>
					<view class="card_precool" v-if="item.cold_spots">预冷地点: {{item.cold_spots}}</view>
					<view style="display: flex;align-items: center;">
						<view class="card_precool" v-if="item.for_cold_cooler_time">预冷要求:
							<span>{{item.for_cold_cooler_time}}h</span>
						</view>
						<view class="card_precool" v-if="item.cooler_box_record.for_cold_duration">预冷时间:
							<span>{{item.cooler_box_record.for_cold_duration}}</span>
						</view>
					</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_code">{{item1.code}}</view>
									<view class="ice_title">{{item1.label}}</view>
								</view>
							</view>
							<view style="display: flex;flex-direction: column;">
								<view class="title_cryophilic" style="margin-right: 5rpx;">
									释冷温度:<span v-if="item1.iceRaftRecord">{{item1.iceRaftRecord.suitableForCold || 0}}℃
									</span></view>
								<view class="title_cryophilic">
									冷冻时间:<span>{{formatMinutes(item1.iceRaftRecord.freezeDuration)}}</span>
								</view>
							</view>
							<view class="title_cryophilic" v-if="item1.iceRaftRecord">
								出库时间:{{item1.iceRaftRecord.outStorageTime}}</view>
						</view>
						<view class="card_ice_item" style="border: unset;"></view>
					</view>
					<view class="card_ice_list"></view>
				</view>
				<view class="btn_printil markd10" v-if="detailsFlag">
					<view style="flex: 1;margin-right: 10rpx;"
						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 style="flex: 1;" v-if="item.useStatus && item.useStatus == 2" @click.stop="endOfUseil(item)">
						<u-button size="small" type="error" text="结束使用"></u-button>
					</view>
				</view>
				<view class="position-triangle" v-if="item.whetherFlag">
					<span>已选</span>
				</view>
				<!-- <view class="position-inuse center_in" v-if="item.monitorStatus == 1">
					<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,
				usageStatus: [{
					bgcolor: '#2979ff',
					label: '预冷中',
					value: 1,
				}, {
					bgcolor: '#67C23A',
					label: '使用中',
					value: 2,
				}, {
					bgcolor: '#67C23A',
					label: '预冷完成',
					value: 3,
				}]
			}
		},
		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);
						}
						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() {
				if (this.selectedList.length > 0) {
					uni.showLoading({
						mask: true
					});
					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('操作成功')
							this.searchChange()
							this.orderList.forEach(item => {
								item.whetherFlag = false
							})
							this.selectedList = []
						} else {
							uni.$u.toast(res.msg)
						}
						uni.hideLoading();
					}).catch(() => {
						uni.hideLoading();
					})
				} else {
					uni.$u.toast('请先选择保温箱')
				}
			},
			// 结束使用
			endOfUse() {
				if (this.selectedList.length > 0) {
					uni.showLoading({
						mask: true
					});
					let arrID = this.selectedList.map(item => item.toString())
					this.$api.put('/api/cooler-box/coolerBoxEndUse', {
						id: arrID,
					}).then(res => {
						if (res.code == 200) {
							uni.$u.toast('操作成功')
							this.currentPage = 1
							this.orderList = []
							this.loadingMore = true
							this.getIncubator()
						} else {
							uni.$u.toast(res.msg || '操作成功')
						}
						uni.hideLoading();
					}).catch(() => {
						uni.hideLoading();
					})
				} else {
					uni.$u.toast('请先选择保温箱')
				}
			},
			// 结束使用单个
			endOfUseil(event) {
				let incubatorId = String(event.id)
				uni.showLoading({
					mask: true
				});
				this.$api.put('/api/cooler-box/coolerBoxEndUse', {
					id: [incubatorId],
				}).then(res => {
					if (res.code == 200) {
						uni.$u.toast('操作成功')
						this.currentPage = 1
						this.orderList = []
						this.loadingMore = true
						this.getIncubator()
					}
					uni.hideLoading();
				}).catch(() => {
					uni.hideLoading();
				})
			},
			// 判断冰排是否选择
			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 = false
				// isSuitableForCold 0 未释冷 1 已释冷
				for (let i = 0; i < arrList.length; i++) {
					if (arrList[i].iceRaftRecord.isSuitableForCold === 0) {
						bol = true
						break //减少循环次数
					}
				}
				return bol
			},
			// 总分钟格式化
			formatMinutes(totalMinutes) {
				if (totalMinutes) {
					const hours = Math.floor(totalMinutes / 60); // 计算小时
					const minutes = totalMinutes % 60; // 计算分钟（余数）
					return `${hours}h${minutes}m`;
				} else {
					return ''
				}
			},
			matchingType(type) {
				let list = this.usageStatus
				let name = ''
				if (list) {
					list.forEach(item => {
						if (type === item.value) {
							name = item.label
						}
					})
				}
				return name
			},
			// tag颜色获取
			filterColor(type) {
				let list = this.usageStatus
				let color = ''
				if (list) {
					list.forEach(item => {
						if (type === item.value) {
							color = item.bgcolor
						}
					})
				}
				return color
			},
			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%;
	}

	.position-inuse {
		position: absolute;
		bottom: 0;
		top: 10rpx;
		right: 15rpx;
		width: 100rpx;
		height: 40rpx;
		font-size: 24rpx;
		border-radius: 6rpx;
		color: #E4E7ED;
		background-color: #19be6b;
	}

	.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-around;
		justify-content: space-between;
	}

	.btn_printil {
		display: flex;
		align-items: center;
	}

	.card_ice_item {
		flex: none;
		width: calc(50% - 20rpx);
		padding: 10rpx 7rpx;
		margin-bottom: 10rpx;
		border-radius: 6rpx;
		border: 1rpx solid #e7e6e4;
	}

	.ice_title {
		font-size: 20rpx;
	}

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

	.ice_code {
		font-size: 24rpx;
		font-weight: bold;
	}

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

	.card_precooling {
		color: #19be6b;
		font-size: 30rpx;
		flex: none;
		margin-right: 10rpx;
	}

	.card_precool {
		font-size: 28rpx;
		margin-right: 10px;

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