<template>
	<view class="contrin">
		<!-- 头部切换 -->
		<view class="head_case">
			<view class="head_case_item" @click="caseheaditem(index)" v-for="(item, index) in typeset" :key="index"
				:class="[senumber === index ? 'head_case_item_back' : '']">
				<u-icon :name="item.iconname" :color="setbackcolor(index)" size="45"></u-icon>
				<view class="head_case_title">
					{{ item.casetitle }}
					<text v-if="index === 0">{{ landareanum }}㎡</text>
				</view>
			</view>
		</view>
		<!-- 种子列表 -->
		<view class="seedlist" v-if="senumber === 0">
			<view class="seed_item" v-for="(item, index) in orderseedlist" :key="index">
				<image class="seeditem_thumbnail" :src="item.seeditem.ripe_pic" mode="aspectFill"></image>
				<view class="seeditem_deti">
					<view class="seeditem_deti_title">{{ item.seeditem.seed_name }}</view>
					<view class="seeditem_deti_subtitle">
						最低种植：{{ item.seeditem.min_plant_area }}㎡；产量：{{ item.seeditem.seed_yield }}kg/㎡</view>
					<view class="priceanum">
						<view class="sedprice">￥{{ item.seeditem.price }}</view>
						<view class="unumber_box">
							<!-- <u-number-box v-model="item.seednum" @plus="plus(item,index)" @minus="mixin(item,index)" :max="num"></u-number-box> -->
							<u-icon size="35" color="#222" name="minus" @click="changeseednum('minus', index)"></u-icon>
							<view class="unumnum">{{ item.seednum }}</view>
							<u-icon size="35" color="#222" name="plus" @click="changeseednum('plus', index)"></u-icon>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class=""></view>
		<!-- 待播种 -->
		<view v-if="senumber === 1" class="seedlist">待播种</view>
		<!-- 种植中 -->
		<view v-if="senumber === 2" class="seedlist">种植中</view>
		<!-- 已成熟 -->
		<view v-if="senumber === 3" class="seedlist">已成熟</view>
		<view class="vacantplot">
			<view class="title_price">
				<view class="footer_case_title">空闲地块</view>
				<view class="footer_case_price">已选:{{ seedarea() }}/{{ landareanum }}㎡,总计:{{ seedtariff() }}元</view>
			</view>
			<u-button type="success" shape="square" size="medium" @click="configseed_add()">追加种子</u-button>
		</view>
		<!-- 通知 -->
		<view class="load_window">
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	import Landapi from '@/api/land/index.js';
	export default {
		data() {
			return {
				primary: this.$theme.primary,
				settingFile: getApp().globalData.siteinfo,
				// 订单信息
				ordermess: {},
				// 空闲土地面积
				landareanum: 0,
				//当前选中类型下标
				senumber: 0,
				typeset: [{
					casetitle: '空闲土地',
					iconname: 'hourglass-half-fill'
				}],
				//种子列表
				seeddatalist: [],
				//选中种子
				orderseedlist: [],
				// 选择完毕种子后订单信息
				seedordermy: {},
				//追加种子订单
				addseedparams: null
			};
		},
		onLoad(options) {
			if (options.params !== undefined && options.params !== null) {
				//如果传导的seed有值，则使用传导过来的seed集合
				this.vueinitadded(options.params);
				this.getList();
			}
		},
		methods: {
			//如果传导的seed有值，则使用传导过来的seed集合
			vueinitadded(params) {
				let that = this;
				that.addseedparams = JSON.parse(params);
				//添加种子初始化页面所需数据
				that.landareanum = parseInt(that.addseedparams.subdivision_area);
				if (that.addseedparams.seeds !== null && that.addseedparams.seeds !== undefined) {
					let alluseredarea = 0;
					that.addseedparams.seeds.forEach(item => {
						alluseredarea = item.seednum;
					});
					that.landareanum = that.landareanum - alluseredarea;
				}
			},
			//进入页面获取种子数据
			getList() {
				var that = this;
				Landapi.seed_list({
					type: that.senumber + 1,
					p: 1
				}).then(res => {
					if (res.status == 1) {

						that.orderseedlist = [];
						that.seeddatalist = res.data;
						that.seeddatalist.forEach((item, index) => {
							that.orderseedlist.push({
								//种子详情
								seeditem: item,
								//种植面积
								seednum: 0
							});
						});
					} else {
						that.$api.msg('获取种子数据失败!');
					}
				});
			},
			//设置图标颜色
			setbackcolor(senumber) {
				if (senumber === this.senumber) {
					return '#FFFFFF';
				} else {
					return '#56AB2F';
				}
			},
			//切换选中模块
			caseheaditem(senumber) {
				this.senumber = senumber;
				this.getList();
			},
			//更改步进器值
			changeseednum(changtype, index) {
				var that = this;
				if (that.orderseedlist.length > 0) {
					if (changtype === 'minus') {
						if (that.orderseedlist[index].seednum > that.orderseedlist[index].seeditem.min_plant_area) {
							that.orderseedlist[index].seednum = that.orderseedlist[index].seednum - 1;
						} else {
							if (that.orderseedlist[index].seednum - that.orderseedlist[index].seeditem.min_plant_area >=
								0) {
								that.orderseedlist[index].seednum = that.orderseedlist[index].seednum - that.orderseedlist[
									index].seeditem.min_plant_area;
							}
						}
					} else if (changtype === 'plus') {
						let ocedlandarea = 0;
						that.orderseedlist.forEach((item, index) => {
							ocedlandarea = ocedlandarea + item.seednum;
						});
						//判断当前可用面积是否有剩余
						if (ocedlandarea < that.landareanum) {
							//判断是否满足最低种植面积，满足则每次+1，否则补充为最低种植面积
							if (that.orderseedlist[index].seednum >= that.orderseedlist[index].seeditem.min_plant_area) {
								//判断土地是否满足增加种植量
								if (that.landareanum - ocedlandarea >= 1) {
									that.orderseedlist[index].seednum = that.orderseedlist[index].seednum + 1;
								} else {
									that.$refs.uToast.show({
										title: '该地块可用面积不足了~',
										type: 'error'
									});
								}
							} else {
								//判断土地是否满足增加最低种植量
								if (that.landareanum - ocedlandarea >= that.orderseedlist[index].seeditem.min_plant_area) {
									that.orderseedlist[index].seednum = that.orderseedlist[index].seednum + that
										.orderseedlist[index].seeditem.min_plant_area;
								} else {
									// ...
									that.$refs.uToast.show({
										title: '该地块可用面积不足了~',
										type: 'error'
									});
								}
							}
						} else {
							that.$refs.uToast.show({
								title: '该地块选择的种子已经够了，可以下单了~',
								type: 'success'
							});
						}
					}
				}
			},
			//反馈信息
			showToast(type, promptstatement) {
				var that = this;
				that.$refs.uToast.show({
					type: type,
					title: promptstatement
				});
			},

			// 实时计算已选种子占用土地面积
			seedarea() {
				var comattribute = 0;
				this.orderseedlist.forEach(item => {
					comattribute = comattribute + item.seednum;
				});
				return comattribute;
			},
			// 实时计算已选种子总金额
			seedtariff() {
				var comattribute = 0;
				this.orderseedlist.forEach(item => {
					comattribute += item.seednum * parseInt(item.seeditem.price);
				});
				return comattribute;
			},
			//确定选择完毕种子
			configseed() {
				var that = this;
				let orderlist = that.orderseedlist;
				that.orderseedlist = [];
				orderlist.forEach((item, index) => {
					if (item.seednum !== 0) {
						that.orderseedlist.push(item);
					}
				});
				that.seedordermy = {
					seedtariff: that.seedtariff(),
					seedarea: that.seedarea(),
					allarea: that.landareanum,
					orderseedlist: that.orderseedlist
				};
				that.ordermess.seedordermy = that.seedordermy;
				that.$store.commit('setlandleaseorder', that.ordermess);
				uni.navigateBack({
					delta: 1
				});
			},
			//添加种子模式下选择种子完毕
			configseed_add() {
				var that = this;
				let orderlist = that.orderseedlist;
				that.orderseedlist = [];
				orderlist.forEach((item, index) => {
					if (item.seednum !== 0) {
						that.orderseedlist.push(item);
					}
				});
				that.seedordermy = {
					seedtariff: that.seedtariff(),
					seedarea: that.seedarea(),
					allarea: that.landareanum,
					orderseedlist: that.orderseedlist
				};
				that.orderseedlist.forEach((item, index) => {
					if (that.addseedparams.seeds !== null && that.addseedparams.seeds !== undefined) {
						that.addseedparams.seeds.forEach((item2, index2) => {
							if (item.seeditem.id === item2.seeditem.id) {
								that.addseedparams.seeds[index2].seednum = that.addseedparams.seeds[index2]
									.seednum + that.orderseedlist[index].seednum;
							}
						});
					} else {
						that.addseedparams.seeds = that.orderseedlist;
					}
				});
				//结合完毕,发起支付并提交更新
				let params2 = {
					addseedparams: that.addseedparams,
					payprice: that.seedtariff(),
					payType: 1,
					addseedon: true
				};
				uni.redirectTo({
					url: '/pagesB/pages/creatorder/pay?parms=' + JSON.stringify(params2)
				});
			}
		}
	};
</script>

<style lang="scss">
	.contrin {
		background-color: #fafafa;
		min-height: 100vh;
	}

	.head_case {
		display: flex;
		justify-content: center;
		margin-top: 20rpx;
	}

	.head_case_item {
		width: 100%;
		text-align: center;
		padding: 25rpx 0;
	}

	.head_case_title {
		font-size: 24rpx;
		margin-top: 15rpx;
	}

	.head_case_item_back {
		background-color: #56ab2f;
		color: #ffffff;
	}

	.seedlist {
		margin: 0 2.5%;
		width: 95%;
	}

	.seed_item {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		margin: 25rpx 0;
		background-color: #ffffff;
		border-radius: 15rpx;
		box-shadow: 2px 4px 12px rgba(0, 0, 0, 0.08);
	}

	.seeditem_thumbnail {
		width: 220rpx;
		height: 220rpx;
		border-radius: 10rpx;
	}

	.seeditem_deti {
		margin-left: 35rpx;
	}

	.seeditem_deti_title {
		color: #000000;
		font-size: 30rpx;
	}

	.seeditem_deti_subtitle {
		font-size: 26rpx;
		color: #999999;
		margin: 25rpx 0 50rpx 0;
	}

	.priceanum {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.sedprice {
		color: red;
		font-size: 32rpx;
		font-weight: 800;
	}

	.load_window {
		position: fixed;
		top: 50%;
		left: 50%;
		width: 100%;
		transform: translate(-50%, -50%);
	}

	.unumber_box {
		display: flex;
		justify-content: flex-start;
		align-items: center;
	}

	.unumnum {
		font-size: 28rpx;
		color: rgb(50, 50, 51);
		margin: 0 25rpx;
	}

	.vacantplot {
		position: fixed;
		width: 100%;
		padding: 10rpx 20rpx;
		bottom: env(safe-area-inset-bottom);
		background-color: #000;
		background: transparent;
		display: flex;
		justify-content: space-between;
	}

	.title_price {}

	.footer_case_title {
		font-size: 30rpx;
		color: #000000;
	}

	.footer_case_price {
		font-size: 24rpx;
		color: #666;
		margin-top: 10rpx;
	}
</style>
