<template>
	<view class="container">
		<u-navbar title="我的批次" placeholder :autoBack="true" :rightText="`今日待办:${todayTaskCount}条`" @rightClick="jumpToTaskList">
		</u-navbar>
		<view class="search">
			<u-search v-model="queryParams.batchName" placeholder="请输入批次名称" @search="handleSearch"
				@custom="handleSearch"></u-search>
		</view>
		<template v-if="batchList.length>0">
			<view class="item" v-for="(item,index) in batchList" :key="index">
				<view class="top">
					<view class="left">
						<u-image width="170rpx" height="170rpx" :src="`${baseUrl}${item.germplasmImg}`"></u-image>
					</view>
					<view class="center">
						<view class="cell">
							批次名称：{{item.batchName}}
						</view>
						<view class="cell">
							种质名称：{{item.germplasmName}}
						</view>
						<view class="cell">
							负责人：{{item.headName}}
						</view>
					</view>
					<view class="right" @click="handleJump(item.batchId)">
						查看任务
					</view>
				</view>
				<view class="bottom" @click="showDetail(item)">
					<text>批次详情</text>
					<u-icon v-if="!item.detailVisiable" name="arrow-down"></u-icon>
					<u-icon v-else name="arrow-up"></u-icon>
				</view>
				<view v-if="item.detailVisiable" class="detail">
					<u-row gutter="16" class="row">
						<u-col span="6">
							种植面积：{{item.cropArea}}亩
						</u-col>
						<u-col span="6">
							开始时间：{{item.startTime}}
						</u-col>
					</u-row>
					<u-row gutter="16" class="row">
						<u-col span="6">
							已完成：{{item.finishedTaskCount}}条
						</u-col>
						<u-col span="6">
							总数：{{item.allTaskCount}}条
						</u-col>
					</u-row>
				</view>
			</view>
		</template>
		<u-empty v-else mode="data"></u-empty>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryParams: {
					pageNum: null,
					pageSize: null,
					batchName: '',
					baseId: null
				},
				batchList: [],
				baseUrl: uni.$u.http.config.baseURL,
				todayTaskCount: '0'
			}
		},
		onShow() {
			this.queryParams.baseId = uni.getStorageSync("baseId");
			this.getBatchList();
			this.getToadyTaskCountByBatchHead();
		},
		async onPullDownRefresh() {
			await this.getBatchList();
			await this.getToadyTaskCountByBatchHead();
			uni.stopPullDownRefresh();
		},
		methods: {
			//请求批次列表
			async getBatchList() {
				let res = await uni.$u.http.get('/agriculture/batch/mobilelist', {
					params: this.queryParams
				});
				this.batchList = res.rows.map(item => ({
					...item,
					detailVisiable: false
				}));
			},
			//处理搜索按钮
			handleSearch() {
				this.getBatchList();
			},
			//处理页面跳转
			handleJump(batchId) {
				uni.$u.route({
					url: 'pages/farm/taskList',
					params: {
						batchId
					}
				});
			},
			//获取今日待完成任务
			async getToadyTaskCountByBatchHead() {
				const {
					data
				} = await uni.$u.http.get('/agriculture/statistics/selectToadyTaskCountByTaskHead');
				this.todayTaskCount = data.todayTaskCount+'';
			},
			showDetail(item){
				item.detailVisiable=!item.detailVisiable
			},
			//跳转到任务详情
			jumpToTaskList(){
				uni.$u.route({
					url: 'pages/farm/taskList',
					params: {
						//任务的状态值为0
						status:0,
						//任务的计划开始时间为当天
						planStart:this.$date.getDate(),
						//页面标题
						title:'待处理任务',
						jumpFrom:2
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;

		.search {
			width: 750rpx;
			background-color: #fff;
			padding: 20rpx;
			box-sizing: border-box;
		}

		.item {
			background-color: #fff;
			margin-top: 20rpx;
			width: 710rpx;
			padding: 20rpx 20rpx 0;
			box-sizing: border-box;
			border-radius: 10rpx;

			.top {
				display: flex;

				.left {
					width: 170rpx;
					height: 170rpx;
				}

				.center {
					display: flex;
					flex-direction: column;
					justify-content: space-around;
					width: 360rpx;
					padding-left: 20rpx;
					font-size: 27rpx;
				}

				.right {
					width: 140rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 28;
					color: #2b7;
					font-weight: bold;
				}
			}

			.bottom {
				border-top: 1px solid #eee;
				padding: 7px 0;
				font-size: 29rpx;
				margin-top: 10rpx;
				display: flex;
				justify-content: space-between;

				text {
					font-weight: bold;
				}
			}

			.detail {
				font-size: 29rpx;

				.row {
					margin-bottom: 10rpx;
				}
			}
		}

	}
</style>