<template>
	<!-- 冷冻柜冰排 -->
	<view>
		<u-navbar :title="headline" 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 style="margin-left: 10rpx;" @click="sweep">
					<u-icon name="scan" color="#606266" size="35"></u-icon>
				</view>
			</view>
			<view class="card_tab_freezer">
				<view :style="{width:isIceInfo.isIceReleaseCold ? '70%':'100%'}">
					<view class="card_subsection">
						<view :class="item.id == current ? 'activate_subsection' : 'item_subsection'"
							v-for="(item,index) in iceStateList" :key="index" @click="sectionChange(item)">
							{{item.title}}
						</view>
					</view>
				</view>
				<view class="select_freezer" v-if="isIceInfo.isIceReleaseCold">
					<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 style="display: flex;align-items: center;margin-bottom: 10rpx;">
						<view class="title_num blue_color">总数:{{Total}}</view>
						<view class="title_num green_color">已选:{{selectedList.length}}</view>
					</view>
				</view>
			</view>
		</u-sticky>
		<view style="display: flex;flex-direction: column;" v-if="list.length > 0">
			<view class="card_item_iceraft" v-for="(item,index) in list" :key="index" @click="choiceIce(item)">
				<view class="top_title space_between">
					<view class="title_Icecode">编号： {{item.code}}</view>
					<view style="font-size: 30rpx;" :style="{color: filterColor(item.iceRaftRecord.status)}">
						{{matchingType(item.iceRaftRecord.status)}}
					</view>
				</view>
				<view class="title_itemIce">冰排备注：{{item.label}}</view>
				<view class="title_itemIce" v-if="item.iceRaftRecord.inStorageTime">入库时间：
					{{item.iceRaftRecord.inStorageTime}}
				</view>
				<view class="title_itemIce" v-if="item.iceRaftRecord.outStorageTime">出库时间：
					{{item.iceRaftRecord.outStorageTime}}
				</view>
				<view class="title_itemIce"
					v-if="item.iceRaftRecord.outStorageTime == '' && item.iceRaftRecord.iceLocker.name">所在位置：
					{{item.iceRaftRecord.iceLocker.name}}
				</view>
				<view class="title_itemIce"
					v-else-if="item.iceRaftRecord.outStorageTime != '' && item.iceRaftRecord.coolerBox.name">所在位置：
					{{item.iceRaftRecord.coolerBox.name}}
				</view>
				<view class="title_itemIce" v-if="item.iceRaftRecord.status != ''">冷冻要求：
					<span>{{getFreeze(item.iceRaftRecord.freezeClaim)}}</span>
				</view>
				<view style="display: flex;align-items: center;">
					<view class="title_itemIce" style="margin-right: 10rpx;" v-if="item.iceRaftRecord.status != ''">
						冷冻时间：
						<span>{{formatMinutes(item.iceRaftRecord.freezeDuration)}}</span>
					</view>
					<view class="title_itemIce" v-if="item.iceRaftRecord.status != ''">释冷时间：
						<span>{{formatMinutes(item.iceRaftRecord.forColdTimeActual)}}</span>
					</view>
				</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="isIceInfo.isIceReleaseCold"></view>
		<view class="card_btn_freezer" v-if="isIceInfo.isIceReleaseCold">
			<u-button style="margin-bottom: 20rpx;" type="primary" @click="coolingRelease">出库释冷</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				headline: '',
				list: [],
				typeList: [{
					bgcolor: '#2979ff',
					label: '冷冻中',
					value: '1',
				}, {
					bgcolor: '#19be6b',
					label: '待使用',
					value: '2',
				}, {
					bgcolor: '#ff9900',
					label: '释冷中',
					value: '5',
				}, {
					bgcolor: '#67C23A',
					label: '释冷完成',
					value: '6',
				}],
				iceState: null,
				iceStateList: [],
				icelist: [{
					id: 0,
					title: '全部',
				}, {
					id: 1,
					title: '冷冻中',
				}, {
					id: 2,
					title: '待使用',
				}],
				icelistil: [{
					id: 0,
					title: '全部',
				}, {
					id: 1,
					title: '冷冻中',
				}, {
					id: 2,
					title: '待使用',
				}, {
					id: 5,
					title: '释冷中',
				}, {
					id: 6,
					title: '释冷完成',
				}],
				current: 0,
				PageIndex: 1,
				PageSize: 15,
				loadStatus: 'loadmore', //loading 、nomore
				loadingMore: true,
				keyword: '',
				freezerId: null,
				Total: 0,
				selectedNum: 0,
				selectedList: [],
				isIceInfo: {},
				searchShow: true,
			}
		},
		onReachBottom() {
			if (!this.loadingMore) {
				this.getIceraftList()
			}
		},
		onLoad(option) {
			this.headline = option.name
			this.freezerId = option.freezerId
		},
		mounted() {
			var userInfo = this.$cache.getCache('userInfo')
			this.isIceInfo = userInfo.dept
			if (this.isIceInfo.isIceReleaseCold) {
				this.iceStateList = this.icelistil
			} else {
				this.iceStateList = this.icelist
			}
			this.getIceraftList()
		},
		methods: {
			// 搜索
			searchChange(value) {
				var that = this
				if (that.searchShow) {
					that.searchShow = false
					const timer = setTimeout(function() {
						clearTimeout(timer);
						that.PageIndex = 1
						that.list = []
						that.loadingMore = true
						that.getIceraftList()
					}, 300);
				}
			},
			// 获取冰排列表
			getIceraftList() {
				this.loadStatus = 'loading'
				this.loadingMore = true
				this.$api.get('/api/ice-locker/geticelocker', {
					page: this.PageIndex,
					pageSize: this.PageSize,
					code: this.keyword,
					status: this.iceState,
					LockerId: this.freezerId,
				}).then(res => {
					if (res.code == 200) {
						const data = res.data.list
						this.Total = res.data.count
						if (this.loadingMore == true && data) {
							this.list = this.list.concat(data);
						}
						this.getRecord()
						if (data.length < this.PageSize) {
							this.loadingMore = true
							this.loadStatus = 'nomore'
						} else {
							this.loadStatus = 'loading'
							this.loadingMore = false
							this.PageIndex++
						}
					}
					this.searchShow = true
				})
			},
			// 判断冰排是否选择
			getRecord() {
				this.list.forEach(item => {
					let index = this.selectedList.findIndex((event) => event === item.iceRaftRecord.id);
					if (index !== -1) {
						item.whetherFlag = true
					} else {
						item.whetherFlag = false
					}
				})
				this.$forceUpdate()
			},
			// 选择冰排
			choiceIce(event) {
				let index = this.selectedList.findIndex((item) => item === event.iceRaftRecord.id);
				// 如果有就替换，没有就添加
				if (index !== -1) {
					this.selectedList.splice(index, 1);
				} else {
					this.selectedList.push(event.iceRaftRecord.id);
				}
				if (this.isIceInfo.isIceReleaseCold) {
					this.getRecord()
				}
			},
			// 全选
			checkAll() {
				this.selectedList = []
				for (let i = 0; i < this.list.length; i++) {
					this.$set(this.list[i], 'whetherFlag', true)
				}
				this.list.forEach(item => {
					if (item.whetherFlag) {
						this.selectedList.push(item.iceRaftRecord.id)
					}
				})
				this.getRecord()
			},
			// 反选
			Inverse() {
				this.selectedList = []
				for (let i = 0; i < this.list.length; i++) {
					let flag = this.list[i].whetherFlag;
					this.$set(this.list[i], 'whetherFlag', !flag)
				}
				this.list.forEach(item => {
					if (item.whetherFlag) {
						this.selectedList.push(item.iceRaftRecord.id)
					}
				})
				this.getRecord()
			},
			// 出库释冷
			coolingRelease() {
				if (this.selectedList.length > 0) {
					uni.showLoading({
						mask: true
					});
					let arrID = this.selectedList.map(item => item.toString())
					this.$api.post('/api/ice-raft-record/start-for-cold', {
						id: arrID,
					}).then(res => {
						if (res.code == 200) {
							uni.$u.toast('操作成功')
							this.PageIndex = 1
							this.loadingMore = true
							this.list = []
							this.list.forEach(item => {
								item.whetherFlag = false
							})
							this.selectedList = []
							this.getIceraftList()
						} else {
							uni.$u.toast(res.msg)
						}
						uni.hideLoading();
					}).catch(() => {
						uni.hideLoading();
					})
				} else {
					uni.$u.toast('请先选择释冷冰排')
				}
			},
			matchingType(type) {
				let list = this.typeList
				let name = ''
				if (list) {
					list.forEach(item => {
						if (type === item.value) {
							name = item.label
						}
					})
				}
				return name
			},
			// tag颜色获取
			filterColor(type) {
				let list = this.typeList
				let color = ''
				if (list) {
					list.forEach(item => {
						if (type === item.value) {
							color = item.bgcolor
						}
					})
				}
				return color
			},
			// 总分钟格式化
			formatMinutes(totalMinutes) {
				const hours = Math.floor(totalMinutes / 60); // 计算小时
				const minutes = totalMinutes % 60; // 计算分钟（余数）
				return `${hours}h${minutes}m`;
			},
			// 扫一扫
			sweep() {
				// 允许从相机和相册扫码
				uni.scanCode({
					scanType: ['barCode'],
					// scanType: ['qrCode'],
					autoZoom: false,
					success: (res) => {
						// console.log(res);
						if (res.result) {
							let url = res.result;
							this.keyword = url
						} else {
							console.log('请重新扫描');
							return false;
						}
					},
					fail: (res) => {
						console.log('未识别到二维码');
					}
				})
			},
			// 冷冻要求
			getFreeze(event) {
				let title = event
				let arrList = []
				title.forEach(item => {
					let arr = '≥' + item + 'h'
					arrList.push(arr)
				})
				return arrList.toString()
			},
			sectionChange(event) {
				this.current = event.id
				if (event.id == 0) {
					this.iceState = null
				} else {
					this.iceState = String(event.id)
				}
				this.searchChange()
			},
			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">
	.search_card {
		display: flex;
		align-items: center;
		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;
	}

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

	.card_item_iceraft {
		position: relative;
		padding: 20rpx;
		margin: 20rpx;
		background-color: #fff;
		border-radius: 10rpx;
		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%;
	}

	.top_title {
		display: flex;
	}

	.title_Icecode {
		color: #000000;
		font-size: 32rpx;
	}

	.title_itemIce {
		color: #000000;
		font-size: 28rpx;
		margin-top: 10rpx;

		span {
			color: #19be6b;
		}
	}

	.card_input_fa {
		margin: 20rpx;
		padding: 12rpx 20rpx;
		line-height: 48rpx;
		border: 1rpx solid #e7e6e4;
		border-radius: 40rpx;
		background-color: #fff;
	}

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

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

	.card_subsection {
		display: flex;
		align-items: center;
		background-color: rgb(238, 238, 239);
		padding: 8rpx;
		border-radius: 6rpx;
	}

	.item_subsection {
		flex: 1;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 24rpx;
		padding: 10rpx 0rpx;
	}

	.activate_subsection {
		flex: 1;
		display: flex;
		padding: 10rpx 0rpx;
		border-radius: 6rpx;
		justify-content: center;
		align-items: center;
		font-size: 24rpx;
		font-weight: bold;
		background-color: #fff;
		color: #2979ff;
	}
</style>