<style lang="scss">
	.container{
		min-height: 100%;
		background:#F6F6F6;
		padding:30rpx 0;
		.card{
			margin-top:0;
			&>.title{
				display:flex;
				align-items: center;
				justify-content: space-between;
				text:nth-child(2){
					color:#777;
				}
			}
			.d-form /deep/{
				&>.u-form-item{
					position:relative;
					.u-form-item__body__right__message{
						position:absolute;
						bottom:-10rpx;
					}
				}
			}
		}
		&>.fertilizer{
			&>.name{
				display:flex;
				align-items: center;
				margin-bottom:14rpx;
				text{
					&:first-child{
						font-size:20rpx;
						color:#fff;
						background:#10C176;
						border-radius: 7rpx;
						min-width:80rpx;
						padding:0 6rpx;
						text-align: center;
						height:40rpx;
						line-height: 40rpx;
						margin-right:16rpx;
						box-sizing: border-box;
					}
					&:nth-child(3){
						border-radius: 50%;
						background:#10C176;
						color:#fff;
						margin-left:auto;
						width:35rpx;
						height:35rpx;
						text-align: center;
						line-height: 35rpx;
					}
				}
			}
			&>.company{
				display:flex;
				align-items: center;
				text{
					&.type{
						flex-shrink: 0;
						font-size:20rpx;
						color:#FBA83C;
					}
					&.name{
						margin-left:20rpx;
						font-size:20rpx;
						color:#707070;
					}
					&.specs{
						margin-left:auto;
					}
				}
			}
		}
		&>.specs{
			.select{
				flex-grow: 1;
				height:80rpx;
				line-height: 80rpx;
				background:#F7F7F7;
				border-radius: 24rpx;
				padding:0 28rpx;
				display:flex;
				text{
					color:#999;
					&:first-child{
						flex-grow: 1;
					}
					&:nth-child(2){
						flex-shrink: 0;
						transform: rotate(90deg);
					}
				}
			}
			.plant-xinjian{
				color:#10C176;
				border-radius: 24rpx;
				margin-left:24rpx;
				background:#E7FCF3;
				height:80rpx;
				line-height: 80rpx;
				width:120rpx;
				text-align: center;
				box-shadow: 6rpx 6rpx 8rpx rgba(16,193,118,0.1);
			}
			.tip{
				color:#FBA83C;
				font-size:24rpx;
				margin-top:12rpx;
			}
		}
		&>.purchase{
			.input{
				display:flex;
				background:#F7F7F7;
				border-radius: 24rpx;
				padding:20rpx;
				input{
					flex-grow: 1;
				}
				text{
					color:#999;
					line-height: 40rpx;
					margin-left:20rpx;
				}
			}
		}
	}
</style>
<template>
	<view class="container">
		<view class="card fertilizer">
			<view class="name">
				<text>{{detail.agriculturalGoods}}</text>
				<text>{{detail.substanceName}}</text>
				<!-- <text class="plant-jiantou_zuoyouqiehuan" @click="$refs.dPicker.show=true"/> -->
			</view>
			<view class="company">
				<text class="type">{{detail.detailType}}</text>
				<text v-if="formData.substanceType==1" class="name">{{detail.supplierName}}</text>
				<text v-else-if="inInventoryBatch.list.length<=0" class="specs">{{detail.equivalentInventory}}{{detail.unit}}</text>
			</view>
		</view>

		<view class="card specs" v-if="formData.substanceType==1">
			<view class="title">
				<text>规格</text> <text>{{detail.specDescribe}}</text>
			</view>
			<view class="tip">库存:{{detail.equivalentInventory}}{{detail.packgeUnit}}({{detail.equivalentInventory*detail.unitNum}}{{detail.unit}})</view>
		</view>

		<view class="card">
			<u-form class="inline" :model="formData" ref="form" :rules="rules" labelWidth="auto">
				<d-form v-if="inInventoryBatch.list.length>0" label="采收批次" field="inInventoryBatch" required type="select" :columns="inInventoryBatch.list" custom :value="inInventoryBatch.value" @select="inInventoryBatch.value=[$event[0].value];inInventoryBatch.number=$event[0].surplus;formData.inInventoryBatch=inInventoryBatch.value.at(-1)">
					<view slot-scope="{row}" style="display:flex;">
						<text style="margin-left:10rpx;font-weight:bold;">{{row.batchNo}}</text>
						<text style="font-size:24rpx;color:#999;margin-left:10rpx;">{{row.substanceName}}</text>
						<text style="margin-left:auto;color:#999;">剩余:{{row.surplus}}</text>
					</view>
				</d-form>
				<d-form :label="`${({in:'入',out:'出'})[mode]}库类型`" field="inOutType" required type="select" :columns="inOutType.list" :value="inOutType.value" @select="inOutType.value=$event.map(v=>v.value);formData.inOutType=inOutType.value.at(-1)"/>
				<d-form :label="`${({in:'入',out:'出'})[mode]}库批次号`" field="inOutBatchNo" required :value="formData.inOutBatchNo" @input="formData.inOutBatchNo=$event" btnText="生成" :btnFun="()=>this.formData.inOutBatchNo=`${({in:'RK',out:'CK'})[this.mode]}${new Date().format('yyyyMMdd')}${new Date().getTime()}`"/>
				<d-form :label="({1:`${({in:'入',out:'出'})[mode]}库数量`,2:'交易重量'})[formData.substanceType]" field="number" required :suffix="number.suffix" :value="formData.number" @input="formData.number=$event" type="digit"/>
				<template v-if="formData.substanceType==2&&formData.inOutType=='out01'&&inInventoryBatch.list.length>0">
					<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="买方名称" 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"/>
				</template>
				<d-form :label="`${({in:'入',out:'出'})[mode]}库凭证`" type="upImg" :num="3" :value="formData.pic" @imgs="pic=$event"/>
			</u-form>
		</view>

		<button class="cu-btn round bg-green shadow submit" @click="submit">确定</button>

	</view>
</template>
<script>
import request from '@/common/request'
export default{
	data(){
		return{
			mode:"",
			detail:{},

			inInventoryBatch:{list:[],value:[],number:0},
			inOutType:{list:[],value:[]},
			pic:"",
			number:{suffix:""},
			buyerType:{list:[],value:[]},
			packWeightList:[],
			formData:{
				inInventoryBatch:"",
				inOutType:"",
				goodsId:"",
				inOutBatchNo:"",
				pic:[],
				number:"",
				substanceType:"",

				tradeWeight:"",
				specification:"",
				packUnit:"",
				tradeNum:"",
				buyerType:"",
				buyerAddress:"",
			},
			rules:{
				inInventoryBatch:{required:true,message:"请选择采收批次!",trigger:['blur','change']},
				inOutType:{required:true,validator:(rule,val,call)=>{
					if(!val)call(new Error(`${({in:'入',out:'出'})[this.mode]}库类型必填!`));
					else call()
				},trigger:['blur','change']},
				inOutBatchNo:{required:true,validator:(rule,val,call)=>{
					if(!val)call(new Error(`${({in:'入',out:'出'})[this.mode]}库批次号必填!`));
					else call()
				},trigger:['blur','change']},
				number:{required:true,validator:(rule,val,call)=>{
					var title=({1:({in:'入',out:'出'})[this.mode],2:'交易重量'})[this.formData.substanceType]
					if(!(val>0))call(new Error(`请输入${title}!`));
					else if(this.mode=='out' &&this.inInventoryBatch.list<=0&& val>this.detail.equivalentInventory)call(new Error(`${title}不能大于剩余数量${this.detail.equivalentInventory}!`));
					else if(this.mode=='out' &&this.inInventoryBatch.list>0&& val>this.inInventoryBatch.number)call(new Error(`${title}不能大于剩余数量${this.inInventoryBatch.number}!`));
					else call()
				},type:"number",trigger:['blur','change']},
				
				specification:{required:true,message:"规格必填!",type:"number",trigger:['blur','change']},
				tradeNum:{required:true,message:"交易数量必填!",type:"number",trigger:['blur','change']},
				buyerName:{required:true,message:"买方名称必填!",trigger:['blur','change']},
				buyerType:{required:true,message:"交易类型必选!",trigger:['blur','change']},
				buyerAddress:{required:true,message:"收货地址必填!",trigger:['blur','change']},
			},
			mode:"",
			eventChannel:null,
		}
	},
	computed:{
		tradeNum(){
			return Math.round((parseFloat(this.formData.number)/parseFloat(this.formData.specification)||0)*100)/100
		},
	},
	onLoad(options){
		this.formData.substanceType = ({nz:1, ncp:2})[options.type],
		this.mode=options.mode
		uni.setNavigationBarTitle({
			title:({out:"出库",in:"入库"})[this.mode]
		})
		this.eventChannel=this.getOpenerEventChannel()
		this.eventChannel.on("detail", e=>{
			this.detail=e

			if(this.formData.substanceType==1){
				this.number.suffix=(e.specDescribe.match(/\/(.+)/)||['',''])[1]
			}else{
				this.number.suffix="kg"
				this.searchStock(e.inventoryId)
			}
			this.formData.goodsId=e.goodsId
		})
		this.init()
	},
	methods:{
		async searchStock(inventoryId){
			var res=await request("/api/Inventory/queryInInventoryBatchRecord",{
				params:{inventoryId}
			})
			if(res.statu){
				this.inInventoryBatch.list=res.data.length>0?[res.data.map(v=>({...v,label:v.substanceName,value:v.batchNo}))]:[]
			}else{
				uni.showModal({
					title:"提示",
					content:res.msg||"获取库存失败!",
					showCancel:false,
				})
			}
		},
		async init(){
			//出库类型
			var res=await request("/common/api/dict/queryDictItemByDictCode",{
				method:"post",
				//农产品前面加上product_
				params: {dictCode: (this.formData.substanceType=="2"?"product_":"")+({out:"out_type",in:"in_type"})[this.mode]}
			})
			if(res.statu){
				this.inOutType.list=[res.data]
			}else{
				uni.showModal({
					title:"提示",
					content:res.msg||"获取出库类型选项失败!",
					showCancel:false,
				})
			}

			//规格
			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,
				})
			}
		},
		submit(){
			this.$refs.form.validate().then(async valid=>{
				if(valid){
					var res=await request("/api/Inventory/inventoryOperate",{
						method:"post",
						body:{
							...this.formData,
							inventoryId: this.detail.inventoryId,
							equivalentAmount: this.detail.equivalentInventory,
							supplier: this.detail.supplierName,
							pic:this.pic,
							type: ({in:2, out:1})[this.mode],
							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.formData.buyerType=e.buyerType[1]
					this.buyerType.value=e.buyerType
				}}
			})
		},
	},
}
</script>