<style lang="scss">
	.container{
		height:100%;
		background:#f6f6f6;

		scroll-view{
			flex-grow: 1;
			height:1rpx;
			.card{
				margin-top:0;
				.head{
					display:flex;
					align-items: center;
					margin-bottom:20rpx;
					text{
						&.type{
							flex-shrink: 0;
							color:#fff;
							font-size:20rpx;
							border-radius: 0px 20rpx 0px 20rpx;
							background:#10C176;
							width:80rpx;
							height:40rpx;
							text-align: center;
							line-height: 40rpx;
							margin-right:10rpx;
						}
						&.name{
							font-weight: bold;
							margin-right:20rpx;
						}
						&.spec{
							color:#999;
							font-size:22rpx;
						}
						&.XP-youbian{
							margin-left:auto;
						}
					}
				}
				.item{
					display: flex;
					align-items: center;
					justify-content: space-between;
					text{
						font-size:24rpx;
						&:first-child{
							color:#999;
							display: flex;
							align-items: center;
							&:before{
								color:#10C176;
								font-size:35rpx;
								margin-right:6rpx;
							}
						}
						&.amount{
							font-size:40rpx;
							font-weight: bold;
							&:after{
								content:attr(data-unit);
								font-size: 28rpx;
								font-weight: normal;
							}
						}
						&.surplus{
							color:#F8A41A;
						}
					}
				}
			}
		}
		&>.u-popup{
			flex: 0;
			&/deep/ .u-transition{
				z-index: 10069!important;
			}
			&/deep/ .u-modal__content{
				padding:25rpx 20rpx!important;
				input{
					width:100%;
					border: 1rpx solid rgba(153, 153, 153, 0.3);
					border-radius: 10rpx;
					font-size:24rpx;
					height:60rpx;
					padding:0 20rpx;
				}
			}
		}
	}
</style>
<template>
	<view class="container flex">
		<d-search v-model="searchVal.search" @search="search()"/>

		<scroll-view scroll-y>
			<block v-if="!loading">
				<view class="card" v-for="(v,k) in comList" :key="k" @click="toSpecs(v)">
					<view class="head">
						<text class="type">{{v.goodsType}}</text>
						<text class="name over">{{v.substanceName}}</text>
					</view>
					<view class="item">
						<text class="over PA-hebing">入库编号：{{v.inOutBatchNo}}</text> <text class="amount" :data-unit="v.unit">{{v.equivalentAmount}}</text>
					</view>
					<view class="item">
						<text class="PA-path1">入库日期：{{v.operateTime}}</text> <text class="surplus">剩余：{{v.surplus}}{{v.unit||v.specs[1]}}</text>
					</view>
				</view>
			</block>
			<u-empty v-else-if="comList.length==0||loading" text="未查询到相关信息" width="70%" icon="/static/noData.png"/>
			<view style="border:0.5rpx solid transparent"/>
		</scroll-view>
		
		<u-toast ref="toast"/>
		<u-modal :show="!!detail" @close="detail=null" @cancel="detail=null" showCancelButton closeOnClickOverlay @confirm="confirm">
			<input v-if="!!detail" placeholder="请输入数量" type="digit" v-model="detail.inputTotal"/>
		</u-modal>
	</view>
</template>
<script>
import request from '@/common/request'
export default {
	data(){
		return{
			searchVal:{
				baseId: this.$store.state.baseInfo.id,
				pageNo:1,
				flagGroupBy:'y',
				pageSize:50,
				search:"",
			},
			loading:true,
			list:[],
			saveList:[],
			detail:null,
			total:0,
			eventChannel:null,
		}
	},
	computed:{
		comList(){
			return this.saveList.concat(this.list.filter(v=>!this.saveList.some(i=>i.id==v.id)))
		},
	},
	onLoad(){
		this.eventChannel=this.getOpenerEventChannel()
		this.eventChannel.on("list", e=>this.saveList=e)
		this.search()
	},
	methods:{
		async search(){
			this.loading=true
			var res=await request("/api/Inventory/queryCollectingList",{
				method:"post",
				body: this.searchVal
			})
			this.loading=false
			if(res.statu){
				this.list=res.data.records.map(v=>({
					...v,
					unitNum:parseFloat(v.specDescribe.match(/^[\d\.]+/)?.[0]||0),
				}))
			}else{
				uni.showModal({
					title:"提示",
					content:res.msg||"获取领用农资失败！",
					showCancel:false,
				})
			}
		},
		toSpecs(e){
			this.detail={
				goodsType: e.goodsType,
				inputTotal:'',
				inOutId: e.id,
				surplus:e.surplus,
				specificationId: e.specificationId||e.specId,
				specs: e.specs||(e.specDescribe.match(/(\d+)(.+?)\/(.+?)/)||[]).slice(1,4),
				substanceName: e.substanceName,
			}
		},
		confirm(){
			if(!(parseFloat(this.detail.inputTotal))){
				this.$refs.toast.show({
					type:"error",
					icon:false,
					message:"请输入数量！",
				})
			}else if(parseFloat(this.detail.inputTotal)>this.detail.surplus){
				this.$refs.toast.show({
					type:"error",
					icon:false,
					message:"输入数量不能大于剩余数量！",
				})
			}else{
				this.eventChannel.emit("addSpecs", {...this.detail, inputTotal:parseFloat(this.detail.inputTotal)})
				uni.navigateBack()
			}
		},
	}
}
</script>
