<template>
	<view class="uni-column" style="padding: 24rpx">
		<view class="box-bg uni-row">
			<view class="input-view uni-row">
				<uni-icons class="input-uni-icon" type="search" size="18" color="#999" />
				<input class="nav-bar-input" type="text" v-model="keywords" placeholder="输入搜索关键词" />
			</view>
			<view class="search" @click="handleSearch">
				搜索
			</view>
		</view> 
		<view v-if="listData.length == 0" style="color: #999;margin: 50% auto;">
			<text>暂无生产计划</text>
		</view>
		<view v-else style="height: calc(100% - 100rpx); overflow: auto;">
			<view  v-for="(item, index) in listData" :key="index" @click="handleToBatchReporting(item)" class="list-item">
				<view class="title-container uni-row">
					<view class="title uni-row">
						<text class="label">生产计划单号</text>
						<text class="label code">{{ item['productionPlanNo'] }}</text>
					</view>
					<view v-if="item['dayWorkList'].length > 0" class="tag"><text class="label">进行中</text></view>
					<view v-else type="default" class="tag not-start"><text class="label">未开始</text></view>
				</view>
				<view class="item-info uni-row">
					<text class="label">产品描述</text>
					<text class="label right">{{ item['productDescription'] }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">总批数</text>
					<text class="label right">{{ item['totalLotNumber'] }}</text>
				</view>
				<!-- <view class="item-info uni-row">
					<text class="label">完成批数</text>
					<text class="label right">{{ item['equiment'] }}</text>
				</view> -->
				<view class="item-info uni-row">
					<text class="label">投产数</text>
					<text class="label right">{{ item['productionQuantity'] }}</text>
				</view>
				<view class="item-info uni-row">
					<text class="label">单批量</text>
					<text class="label right">{{ item['oneLotQuantity'] }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { getPlanDetailsList } from '@/api/business/planDetails.js'
	import {
		ref
	} from 'vue'
	import {
		onReady,
		onLoad,
		onUnload,
		onPullDownRefresh,
		onShow
	} from '@dcloudio/uni-app'
	import {
		getToken
	} from '@/utils/auth'
	import {
		store
	} from '@/store/index.js'
	import {
		getUserProcess
	} from '@/api/process/process.js'


	const listData = ref([])
	const keywords = ref('')
	const initReqParam = ref({})

	onLoad(() => {
		console.log(store.curDeptDetails.deptId)
		initReqParam.value = {
			deptId: store.curDeptDetails.deptId
		}
		init(initReqParam.value);
		// dayworkItem数据更改后刷新数据
		// uni.$on('dayworkItemUpdate', reflush);
	})
	
	onShow(() => {
		reflush();
	})

	onUnload(() => {
		console.log(store.curDeptDetails)
		// uni.$off('dayworkItemUpdate', reflush)
	})

	/**	
	 * 监听下拉刷新
	 */
	// onPullDownRefresh(() => {
	// 	init(initReqParam.value);
	// 	setTimeout(function() {
	// 		uni.stopPullDownRefresh();
	// 	}, 3000);
	// })

	function reflush() {
		init(initReqParam.value);
	}

	function init(data) {
		uni.showLoading({
			title: '加载中'
		});
		getPlanDetailsList({
			deptId: Number(store.curDeptDetails.deptId),
			keywords:keywords.value
		}).then(res => {
			if(res.code == 200){
				listData.value = res.data;
				uni.hideLoading();
			}
			uni.hideLoading();
		})
	}

	function handleToBatchReporting(item) {
		store.planDetails = item
		uni.navigateTo({
			url: '/pages/batchReporting/index'
		})
	}

	function handleSearch() {
		let reqParam = {
			keywords: keywords.value
		}
		reqParam.tenantId = !store.tenantId ? store.userInfo.tenantId : store.tenantId;
		init(reqParam)
	}
</script>

<style lang="scss">
	$nav-height: 60rpx;

	.box-bg {
		width: 100%;
		background-color: #F5F5F5;
		padding: 5rpx 0;
		justify-content: space-around;
		align-items: center;

		.input-view {
			width: 100%;
			flex: 4;
			background-color: #f8f8f8;
			height: $nav-height;
			border: 1rpx solid #999;
			border-radius: 15rpx;
			padding: 0 15rpx;
			flex-wrap: nowrap;
			margin:0 10rpx 20rpx;
			line-height: $nav-height;

			.input-uni-icon {
				line-height: $nav-height;
			}

			.nav-bar-input {
				width: 80%;
				height: $nav-height;
				line-height: $nav-height;
				padding: 0 5rpx;
				background-color: #f8f8f8;
			}
		}

		.search {
			width: 20%;
			text-align: center;
			color: #808080;
			margin-top: -20rpx;
		}
	}

	.uni-column {
		background-color: rgba(245, 245, 245, 1);
		height: calc(100% - 40rpx);
	}

	.list-item {
		background-color: #fff;
		position: relative;
		padding: 16rpx;
		padding-bottom: 24rpx;
		border-radius: 24rpx;
		margin-bottom: 24rpx;

		.title-container {
			justify-content: space-between;
			margin-top: 8rpx;
			margin-bottom: 16rpx;

			.title {
				height: 48rpx;
				align-items: center;

				.label {
					font-size: 32rpx;
					font-weight: bold;

					&.code {
						margin-left: 8rpx;
					}
				}
			}

			.tag {
				border: 1px solid #1ce5b0;
				background-color: #f6fffd;
				padding: 8rpx;
				border-radius: 8rpx;

				.label {
					color: #1ce5b0;
					font-size: 24rpx;
				}

				&.not-start {
					border: 1px solid #bbbbbb;
					background-color: #f5f5f5;

					.label {
						color: #bbbbbb;
					}
				}
			}
		}

		.item-info {
			margin-bottom: 8rpx;

			.label {
				font-size: 28rpx;
				width: 220rpx;
				color: #808080;

				&.right {
					flex: 1;
					color: #000000;
				}
			}
		}
	}
</style>