<template>
	<view>
		<u-navbar title="冰排追溯管理" autoBack placeholder safeAreaInsetTop></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>
		</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">
				<view class="top_title space_between">
					<view class="title_Icecode">编号： {{item.code}}</view>
					<view :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 class="title_itemIce" v-if="item.iceRaftRecord.status != ''">冷冻时间：
					<span>{{formatMinutes(item.iceRaftRecord.freezeDuration)}}</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>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				typeList: [{
					bgcolor: '#606266',
					label: '未入库',
					value: '',
				}, {
					bgcolor: '#409EFF',
					label: '冷冻中',
					value: '1',
				}, {
					bgcolor: '#E6A23C',
					label: '待使用',
					value: '2',
				}, {
					bgcolor: '#67C23A',
					label: '使用中',
					value: '3',
				}, {
					bgcolor: '#909399',
					label: '已结束',
					value: '4',
				}, {
					bgcolor: '#ff9900',
					label: '释冷中',
					value: '4',
				}],
				PageIndex: 1,
				PageSize: 15,
				loadStatus: 'loadmore', //loading 、nomore
				loadingMore: true,
				keyword: '',
			}
		},
		onReachBottom() {
			if (!this.loadingMore) {
				this.getIceraftList()
			}
		},
		mounted() {
			this.getIceraftList()
		},
		methods: {
			// 搜索
			searchChange(value) {
				this.PageIndex = 1
				this.list = []
				this.loadingMore = true
				this.getIceraftList()
			},
			// 获取冰排列表
			getIceraftList() {
				this.loadStatus = 'loading'
				this.loadingMore = true
				this.$api.get('/api/ice-raft/newest-record', {
					page: this.PageIndex,
					pageSize: this.PageSize,
					code: this.keyword,
				}).then(res => {
					if (res.code == 200) {
						const data = res.data.list
						if (this.loadingMore == true && data) {
							this.list = this.list.concat(data);
						}
						if (data.length < this.PageSize) {
							this.loadingMore = true
							this.loadStatus = 'nomore'
						} else {
							this.loadStatus = 'loading'
							this.loadingMore = false
							this.PageIndex++
						}
					}
				})
			},
			// 冷冻要求
			getFreeze(event) {
				let title = event
				let arrList = []
				title.forEach(item => {
					let arr = '≥' + item + 'h'
					arrList.push(arr)
				})
				return arrList.toString()
			},
			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('未识别到二维码');
					}
				})
			},
			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>
	.search_card {
		display: flex;
		align-items: center;
		background-color: #fff;
		padding: 20rpx;
	}

	.card_item_iceraft {
		padding: 20rpx;
		margin: 20rpx;
		background-color: #fff;
		border-radius: 10rpx;
	}

	.top_title {
		display: flex;
	}

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

	.title_itemIce {
		color: #000000;
		font-size: 30rpx;
		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;
	}
</style>