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

		&.agr{
			padding:30rpx;
			background:#fff;
		}
		&>.agrInfo{
			margin-top:0;
			.data{
				display:flex;
				align-items: center;
				margin-bottom:16rpx;
				text{
					&:first-child{
						flex-shrink: 0;
						color:#fff;
						background:#10C176;
						border-radius: 6rpx;
						min-width:80rpx;
						height:40rpx;
						line-height: 40rpx;
						text-align: center;
						font-size:20rpx;
						margin-right:16rpx;
					}
					&:nth-child(3){
						background:#10C176;
						border-radius: 50%;
						width:36rpx;
						height:36rpx;
						text-align: center;
						line-height: 36rpx;
						color:#fff;
						margin-left:auto;
					}
				}
			}
		}
		.u-form{
			&.card{
				margin:30rpx 30rpx 0;
			}
			&>.addAgr{
				color:#10C176;
				background:rgba(16, 193, 119, 0.1);
				border-radius: 24rpx;
				height:100rpx;
				display:flex;
				align-items: center;
				justify-content: space-between;
				padding:30rpx;
				margin-bottom:28rpx;
			}
		}
	}
</style>
<template>
	<view class="container" :class="{agr:!isAgr}">
		<d-navbar text="农资入库" isBack style="--bg:#10C176;--c:#fff;">
			<button slot="right" class="cu-btn round" @click="isAgr && submit()">提交</button>
		</d-navbar>

		<view class="agrInfo card" v-if="isAgr">
			<view class="data"> <text>{{agrDetail.detailedParentName}}</text><text class="over">{{agrDetail.goodsName}}</text><text class="plant-jiantou_zuoyouqiehuan" @click="toAddAgr"/> </view>
			<view style="color:#707070;font-size:20rpx;">{{agrDetail.supplier}}</view>
		</view>

		<u-form class="inline" :class="{card:isAgr}" errorType="toast" :model="formData" ref="form" :rules="rules" labelWidth="auto">
			<view v-if="!isAgr" class="addAgr" @click="toAddAgr">添加农资<text class="plant-batch-add"/></view>
			<template v-for="(v,k) in fields">
				<d-form
					v-if="!v.agr ||v.agr&&isAgr"
					:key="k"
					v-bind="v"
					:field="k"
					:required="!!rules[k]"
					:value="/^select$/.test(v.type)?fields[k].value:formData[k]"
					@input="formData[k]=$event"
					@select="formData[k]=$event.value[0].value;fields[k].value=[formData[k]]"
					@imgs="fields[k].value=$event"/>
			</template>
		</u-form>
	</view>
</template>
<script>
import request from '@/common/request'
	export default{
		data(){
			return{
				fields:{
					specName:{label:"规格", disabled:true, agr:true},
					number:{label:"入库数量", type:"digit",suffix:"袋", tip:"",agr:true},
					inOutBatchNo:{label:"入库批次号",agr:true, btnText:"生成", btnFun:()=>this.formData.inOutBatchNo=`RK${new Date().format('yyyyMMdd')}${new Date().getTime()}`},
					wareHouseId:{label:"选择仓库", type:"select",columns:[],value:[],agr:true},
					inOutType:{label:"入库类型",type:"select",columns:[],value:[]},
					operateTime:{label:"入库时间",type:"calendar"},
					pic:{label:"单据、凭证图片",type:"upImg",value:''},
					remark:{label:"备注",type:"textarea"},
				},
				formData:{
					enterpriseId: this.$store.getters['userInfo'].companyId,
					specName:"",
					specId:"",
					number:"",
					wareHouseId:"",
					inOutBatchNo:'',
					inOutType:"",
					operateTime:"",
					pic:[],
					remark:"",
				},
				rules:{
					number:{required:true,type:"number", message:"入库数量必填!"},
					inOutBatchNo:{required:true, message:"入库批次号必填!"},
					wareHouseId:{required:true, message:"仓库必选!"},
					inOutType:{required:true, message:"入库类型必选!"},
					operateTime:{required:true, message:"入库时间必选!"},
				},

				agrDetail:{},//农资详情
				isAgr: false,
				eventChannel:null,
			}
		},
		computed:{
			specs(){
				return{
					specs:(this.formData.specName.match(/(\d+)(.+?)\/(.+?)/)||[]).slice(1,4),
					num:this.formData.number
				}
			},
		},
		watch:{
			specs(n){
				this.fields.number.suffix=n.specs[2]
				if(n.num>0 && n.specs[0]){
					this.fields.number.tip=`共计:${parseFloat(n.num)*parseFloat(n.specs[0])}${n.specs[1]}`
				}else{
					this.fields.number.tip=''
				}
			}
		},
		onLoad(){
			this.eventChannel=this.getOpenerEventChannel()
			this.init()
		},
		methods:{
			async init(){
				//入库类型
				var res=await request("/common/api/dict/queryDictItemByDictCode",{
					method:"post",
					params:{dictCode:"in_type"}
				})
				if(res.statu){
					this.fields.inOutType.columns=[res.data]
				}else{
					uni.showModal({
						title:"提示",
						content:res.msg||"获取企业类型选项失败!",
						showCancel:false,
					})
				}
				//仓库下拉选项
				var res=await request("/api/WarehouseManageApi/queryWarehouseByBaseId",{
					params:{baseId: this.$store.state.plantBaseInfoId}
				})
				if(res.statu){
					this.fields.wareHouseId.columns=[res.data]
				}else{
					uni.showModal({
						title:"提示",
						content:res.msg||"获取仓库选项失败!",
						showCancel:false,
					})
				}
			},
			toAddAgr(){
				uni.navigateTo({
					url:"/pages/purchase/agrProdList?mode=stock",
					events:{submit: e=>{
						this.agrDetail=e
						this.formData.specId=e.specificationId
						this.formData.specName=e.goodsSpecification
						this.isAgr=true
					}}
				})
			},
			submit(){
				this.$refs.form.validate().then(async valid=>{
					if(valid){
						var res=await request("/api/InventoryManageApi/addInventory",{
							method:"post",
							body: {
								...this.formData,
								pic: this.fields.pic.value
							}
						})
						if(res.statu){
							uni.navigateBack()
							this.eventChannel.emit("update")
						}else{
							uni.showModal({
								title:"提示",
								content:res.msg||"保存失败!",
								showCancel:false,
							})
						}
					}
				})
			},
		},
	}
</script>