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

		scroll-view{
			flex-grow: 1;
			height:1rpx;
			border-bottom:1rpx solid transparent;

			.card{
				margin-top:0;
				.name{
					display: flex;
					align-items: center;
					margin-bottom:20rpx;
					text{
						&:first-child{
							font-size:20rpx;
							color:#fff;
							background:#10C176;
							min-width:70rpx;
							height:40rpx;
							text-align: center;
							line-height: 40rpx;
							border-radius: 0 20rpx 0 20rpx;
							margin-right:10rpx;
							padding:0 8rpx;
						}
						&.plant-youbian{
							margin-left: auto;
						}
					}
				}
				.company{
					display:flex;
					align-items: center;
					text{
						&:first-child{
							flex-shrink: 0;
							color:#FBA83C;
							margin-right:14rpx;
						}
						&:nth-child(2){
							color:#999;
						}
					}
				}
			}
		}
	}
</style>
<template>
	<view class="container flex">
		<d-navbar text="农资列表" isBack style="--bg:#10C176;--c:#fff;">
			<text slot="right" class="plant-xinjian" @click="toAddPage"/>
		</d-navbar>

		<d-search v-model="searchVal.goodsName" @search="clean();search()"/>

		<scroll-view scroll-y @scrolltolower="search">
			<view class="card" v-for="(v,k) in list" :key="k" @click="toAddSepc(v.id)">
				<view class="name">
					<text>{{v.detailedParentName}}</text> {{v.goodsName}} <text class="plant-youbian"/>
				</view>
				<view class="company">
					<text>{{v.detailedType}}</text> <text class="over">{{v.supplier}}</text>
				</view>
			</view>
			<u-empty v-if="list.length==0" width="70%" icon="/static/noData.png"/>
		</scroll-view>
	</view>
</template>
<script>
import request from '@/common/request'
export default {
	data(){
		return{
			searchVal:{
				goodsName:"",
				pageNo:1,
				pageSize:5,
			},
			list:[],
			total:0,
			eventChannel:null,
		}
	},
	onLoad(){
		this.eventChannel=this.getOpenerEventChannel()
		this.search()
	},
	methods:{
		clean(){
			this.list=[]
			this.total=0
			this.searchVal.pageNo=1
		},
		async search(){
			if(this.total==0||this.list.length<this.total){
				var res=await request("/api/goods/goodsList",{
					params: this.searchVal
				})
				if(res.statu){
					this.list=this.list.concat(res.data.records)
					this.total=res.data.total
				}else{
					uni.showModal({
						title:"提示",
						content:res.msg||"获取农资列表失败！",
						showCancel:false,
					})
				}
			}
		},
		toAddPage(){
			uni.navigateTo({
				url:"/pages/purchase/newAgr",
				events:{update:()=>{ this.clean();this.search() }}
			})
		},
		toAddSepc(id){
			uni.navigateTo({
				url:`/pages/purchase/specs?id=${id}`,
				events:{submit: e=>{
					uni.navigateBack()
					this.eventChannel.emit("submit",e)
				}}
			})
		},
	}
}
</script>