<style lang="scss" scoped>
	.plant_plan{
		height:100%;
		display:flex;
		flex-direction: column;
		scroll-view{
			flex-grow: 1;
			height:1rpx;
			padding-top:30rpx;
			.card{
				margin-top:0;
				&>.info{
					display:flex;
					&>.u-transition{
						position:relative;
						margin-right:20rpx;
						&:before{
							content:attr(data-type);
							position:absolute;
							z-index: 30;
							top:0;
							left:0;
							border-radius: 20rpx 20rpx 20rpx 0;
							background:rgba(60, 84, 44, 0.5);
							color:#fff;
							padding:6rpx 2rpx;
							min-width:80rpx;
							text-align: center;
						}
					}
					&>.detail{
						flex-grow: 1;
						width:1rpx;
						.name{
							display:flex;
							margin-bottom: 6rpx;
							text{
								&:first-child{
									flex-shrink: 0;
									font-size:20rpx;
									color:#fff;
									background:#10C176;
									height:40rpx;
									width:80rpx;
									line-height: 40rpx;
									text-align: center;
									border-radius: 6rpx;
									margin-right:12rpx;
								}
								&:nth-child(2){
									font-weight: bold;
									flex-grow: 1;
								}
							}
						}
						.batch,.recovery{
							font-size:24rpx;
							color:var(--c);
							margin-bottom:6rpx;
						}
						.mu{
							display:flex;
							align-items: center;
							&>text.area{
								color:#10C176;
								font-size:32rpx;
								font-weight: bold;
								&:after{
									content:"亩";
								}
							}
							&>.farm{
								font-size:22rpx;
								color:#10C176;
								height:40rpx;
								line-height: 40rpx;
								margin-left:18rpx;
								background: rgba(16, 193, 119, 0.1);
								padding:0 6rpx;
							}
							button{
								margin:-20rpx 0 -20rpx auto!important;
								padding:20rpx;
								height: auto;
    							background: transparent;
							}
						}
					}
				}
				&>.date{
					border-top:2rpx solid rgba(216, 216, 216, 0.5);
					margin-top:6rpx;
					padding-top:12rpx;
					display:flex;
					align-items: center;
					text{
						flex-shrink: 0;
						color:#999;
						font-size:24rpx;
						margin-right:auto;
						&.over{
							background:#F7F7F7;
							border-radius: 8rpx;
							height:48rpx;
							line-height: 48rpx;
							padding:0 20rpx;
							margin:0 0 0 auto;
						}
					}
					button{
						margin:0 0 0 20rpx;
						color:rgba(var(--c),1);
						background:rgba(var(--c),0.15);
						height:48rpx;
						font-size:24rpx;
						padding:0 16rpx;
						min-width:100rpx;
					}
				}
			}
		}
		&>button{
			flex-shrink: 0;
			margin:20rpx auto!important;
		}
	}
</style>
<template>
	<view class="plant_plan">
		<scroll-view scroll-y @scrolltolower="search">
			<view class="card" v-for="(v,k) in list" :key="k">
				<view class="info" @click="v.status==1 && reDetail(v.id)">
					<u-image :data-type="v.plantType" radius="20rpx" width="170rpx" height="140rpx" mode="aspectFill" :src="`${host}/${v.images}`">
						<text slot="error" style="font-size:45rpx;" class="plant-xiaopangchetupianjiazaishibai"/>
					</u-image>
					<view class="detail">
						<view class="name">
							<text>{{v.plantStandard}}</text> <text class="over">{{v.varietyName}}</text> <text class="plant-youbian"/>
						</view>
						<view class="batch over" style="--c:#999;">种植批次号:{{v.plantBatch}}</view>
						<view class="recovery" style="--c:#FFC760;">已采收:{{v.harvested}}kg</view>
						<view class="mu">
							<text class="area">{{v.plantArea}}</text>
							<text class="farm" @click.stop="v.farmNum>0 && $emit('farm',v.id)">农事操作{{v.farmNum||0}}次</text>
							<button class="cu-btn bg-white sm plant-shanchu" v-if="v.status!=2" @click.stop="del(k)">删除</button>
						</view>
					</view>
				</view>
				<view class="date">
					<text>预估采收日期:{{v.harvestTime}}</text>
					<template v-if="v.status==1">
						<button class="cu-btn" style="--c:255, 196, 87;" @click.stop="finishPlant(k)">结束种植</button>
						<button class="cu-btn" style="--c:153, 153, 153;" @click.stop="toAdd(v.id)">采收</button>
					</template>
					<template v-else-if="v.status==2">
						<button v-if="v.plantType=='水果'" class="cu-btn" style="--c:16, 193, 118;" @click.stop="copy(v.id)">复制当前计划</button>
						<text class="over">已结束</text>
					</template>
				</view>
			</view>
			<u-empty v-if="list.length==0" text="为查询到相关信息" width="70%" icon="/static/noData.png"/>
		</scroll-view>

		<button class="cu-btn round bg-green shadow submit" @click="addPlant">+新增种植计划</button>
	</view>
</template>
<script>
import request,{host} from '@/common/request'
export default {
	name:"plantPlan",
	props:{
		plotId:{
			type:String,
			default:""
		}
	},
	data(){
		return{
			searchVal:{
				pageNo:1,
				pageSize:4
			},
			total:0,
			list:[],
			host:host("imgUrl"),
		}
	},
	created(){
		this.search()
		this.$provise.on('addFarming',()=>{
			this.clean()
			this.search()
		})
	},
	methods:{
		clean(){
			this.list=[]
			this.total=0
			this.searchVal.pageNo=1
		},
		async search(){
			if(this.total==0||this.list.length<this.total){
				var res=await request('/api/plantPlan/list',{
					params: {
						...this.searchVal,
						plotId: this.plotId
					}
				})
				if(res.statu){
					this.list=this.list.concat(res.data.records)
					this.total=res.data.total
					this.searchVal.pageNo++
					if(this.list.length==this.total)uni.showToast({title:"加载完成!",icon:"none"})
				}else{
					uni.showModal({
						title:"提示",
						content:res.msg||"获取地块列表失败!",
						showCancel:false,
					})
				}
			}
		},
		addPlant(){
			uni.navigateTo({
				url:`/pages/massif/plant_plan?plotId=${this.plotId}`,
				events:{ update:()=> {this.clean();this.search()} }
			})
		},
		reDetail(id){
			uni.navigateTo({
				url:`/pages/massif/plant_plan?id=${id}`,
				events:{update:()=> {this.clean();this.search()} }
			})
		},
		//结束种植
		finishPlant(index){
			uni.showModal({
				title:"提示",
				content:"确定结束种植!",
				success: async e=>{
					if(e.confirm){
						var res=await request("/api/plantPlan/finishPlant",{
							params:{plantPlanId:this.list[index].id}
						})
						if(res.statu){
							uni.showToast({title:"结束成功!",duration:1500})
							this.list[index].status=2
						}else{
							uni.showModal({
								title:"提示",
								content:res.msg||"结束种植失败!",
								showCancel:false,
							})
						}
					}
				}
			})
		},
		//跳转新增采收页面
		toAdd(plantId){
			uni.navigateTo({
				url:`/pages/massif/plant_info?plantId=${plantId}&plotId=${this.plotId}`,
				events:{ toRecovery:()=>{
					this.clean()
					this.search()
					this.$emit("trigger", plantId)
				} }
			})
		},
		del(index){
			uni.showModal({
				title:"提示",
				content:"确定删除!",
				success: async e=>{
					if(e.confirm){
						var res=await request("/api/plantPlan/deletePlantPlanSchedule",{
							method:"delete",
							params:{id: this.list[index].id}
						})
						if(res.statu){
							uni.showToast({title:"删除成功!",icon:"success"})
							if(this.list.length>this.searchVal.pageNo+3)this.list.splice(index,1);
							else{
								this.list=[]
								this.total=0
								this.searchVal.pageNo=1
								this.search()
							}
						}else{
							uni.showModal({
								title:"提示",
								content:res.msg||"删除失败!",
								showCancel:false,
							})
						}
					}
				}
			})
		},
		async copy(id){
			uni.showModal({
				title:"提示",
				content:"确定复制当前计划?",
				success: async e=>{
					if(e.confirm){
						var res=await request("/api/plantPlan/copyPlan",{
							params:{id}
						})
						if(res.statu){
							uni.navigateTo({
								url:`/pages/massif/plant_plan`,
								event:{update:()=> {this.clean();this.search()} },
								success(e){
									e.eventChannel.emit("copy", res.data)
								}
							})
						}else{
							uni.showModal({
								title:"提示",
								content:res.msg||"复制失败!",
								showCancel:false,
							})
						}
					}
				}
			})
		},
	},
	destroyed(){
		this.$provise.off('addFarming')
	}
}
</script>