<style lang="scss">
	.container{
		min-height:100%;
		background:#f6f6f6;
		padding:30rpx 0;

		.info{
			margin-top:0;
			.name{
				display:flex;
				align-items: center;
				text{
					&:first-child{
						color:#fff;
						background:#10C176;
						border-radius: 0 20rpx 0 20rpx;
						padding:0 6rpx;
						font-size:20rpx;
						height:40rpx;
						line-height: 40rpx;
						margin-right: 20rpx;
					}
				}
			}
			.tip{
				display:flex;
				align-items: center;
				margin-top:6rpx;
				text{
					&:first-child{
						color:#FBA83C;
						font-size:24rpx;
						margin-right:auto;
					}
					&:nth-child(2){
						color:#999;
						font-size:20rpx;
					}
				}
			}
		}
		.card{
			.d-form /deep/{
				&>.u-form-item{
					position:relative;
					.u-form-item__body__right__message{
						position:absolute;
						bottom:-10rpx;
					}
				}
			}
		}
	}
</style>
<template>
	<view class="container">
		<view class="card info">
			<view class="name">
				<text>{{detail.plantTypeName}}</text> <text>{{detail.plantName}}</text>
			</view>
			<view class="tip">
				<text>提示：当前可交易重量{{detail.harvestNum||0}}kg</text> <text>采收日期：{{detail.harvestTime}}</text>
			</view>
		</view>
		<view class="card">
			<u-form class="inline" :model="formData" ref="form" :rules="rules" labelWidth="auto">
				<d-form label="交易重量" required field="tradeWeight" type="digit" suffix="kg" :value="formData.tradeWeight" @input="formData.tradeWeight=$event"/>
				<view style="display:flex;align-items: center;">
					<d-form label="规格" required field="specification" type="digit" suffix="kg" :value="formData.specification" @input="formData.specification=$event"/>
					<text style="margin:45rpx 20rpx 0 20rpx;">/</text>
					<d-form style="width:max-content;margin-top:40rpx;" type="select" placeholder="　" @select="formData.packUnit=$event[0].value" :columns="[packWeightList]" :value="[formData.packUnit]"/>
				</view>
				<d-form label="交易数量" disabled type="digit" :suffix="formData.packUnit" :value="tradeNum"/>
				<d-form label="交易日期" type="calendar" v-model="formData.tradeTime"/>
				<d-form label="买方名称" required field="buyerName" btnIcon="plant-sousuo" :btnFun="searchBuy" :value="formData.buyerName" @input="formData.buyerName=$event"/>
				<d-form label="买方类型" required field="buyerType" type="select" :columns="[buyerType.list]" :value="buyerType.value" @select="buyerType.value=$event.map(v=>v.value);formData.buyerType=buyerType.value[1]"/>
				<d-form label="收货地址" required field="buyerAddress" type="textarea" :value="formData.buyerAddress" @input="formData.buyerAddress=$event"/>
			</u-form>
		</view>
		
		<button class="cu-btn round bg-green shadow submit" @click="sumbit">保存</button>
	</view>
</template>
<script>
import request from '@/common/request'
export default {
	data(){
		return{
			buyerType:{list:[],value:[]},
			packWeightList:[],
			detail:{},

			formData:{
				harvestId:"",
				tradeWeight:"",
				specification:"",
				packUnit:"",
				tradeNum:"",
				tradeTime:"",
				buyerName:"",
				buyerType:"",
				buyerAddress:"",
			},
			rules:{
				tradeWeight:{required:true,validator:(rule,val,call)=>{
					if(!(val>0))call(new Error('交易重量必填！'));
					else if(val>this.detail.harvestNum)call(new Error(`交易重量不能大于可交易重量${this.detail.harvestNum}！`));
					else call()
				},trigger:["blur","change"]},
				specification:{required:true,type:'number',message:"规格必填！",trigger:["blur","change"]},
				tradeTime:{required:true,message:"请选择交易日期！",trigger:['blur','change']},
				buyerName:{required:true,message:"买方名称必填！",trigger:["blur","change"]},
				buyerType:{required:true,message:"交易类型必选！",trigger:["blur","change"]},
				buyerAddress:{required:true,message:"收货地址必填！",trigger:["blur","change"]},
			},
			eventChannel:null,
		}
	},
	computed:{
		tradeNum(){
			return Math.round((parseFloat(this.formData.tradeWeight)/parseFloat(this.formData.specification)||0)*100)/100
		},
	},
	onLoad(options){
		this.eventChannel=this.getOpenerEventChannel()
		this.eventChannel.on("detail", e=>{
			this.detail=e
			this.formData.harvestId=e.id
		})
		this.init()
	},
	methods:{
		async init(){
			//规格
			var res = await request("/common/api/dict/queryDictItemByDictCode",{
				method:"post",
				params:{dictCode:"dressing_unit"}
			})
			if(res.statu){
				this.packWeightList=res.data
				this.formData.packUnit=res.data[0].value
			}else{
				uni.showModal({
					title:"提示",
					content:res.msg||"获取规格选项失败！",
					showCancel:false,
				})
			}
			//买方类型
			var res = await request("/common/api/dict/categoryValue",{
				method:"post",
				params:{code:"B08"}
			})
			if(res.statu){
				this.buyerType.list=res.data
			}else{
				uni.showModal({
					title:"提示",
					content:res.msg||"获取买方类型选项失败！",
					showCancel:false,
				})
			}
		},
		sumbit(){
			this.$refs.form.validate().then(async valid=>{
				if(valid){
					var res=await request("/api/plantTrade/add",{
						method:"post",
						body: {
							...this.formData,
							tradeNum: this.tradeNum
						}
					})
					if(res.statu){
						uni.navigateBack()
						this.eventChannel.emit("update")
					}else{
						uni.showModal({
							title:"提示",
							content:res.msg||"保存失败！",
							showCancel:false,
						})
					}
				}
			})
		},
		searchBuy(){
			uni.navigateTo({
				url:`/pages/massif/search?data=${this.formData.buyerName||''}`,
				events:{update:e=>{
					this.formData.buyerName=e.buyerName
					this.formData.buyerAddress=e.address
					this.buyerType.value=e.buyerType
					this.formData.buyerType=e.buyerType[1]
				}}
			})
		},
	}
}
</script>