<template>
	<view class="content">
		<!-- 自定义导航 -->
		<view class="navbar-box">
			<u-navbar title="库存查询" :safeAreaInsetTop="true" @leftClick="leftClick">
			</u-navbar>
		</view>
		<view class="screen-box">
			<view class="screen-class">
				<u-search
				placeholder="搜索" 
				height="70" 
				bg-color="#f7f7f7" 
				v-model="mateName" 
				@search="search" 
				@custom="search"></u-search>
			</view>
		</view>
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view v-for="item in dataList" :key="item.id">
				<view class="item u-border-bottom">
					<view class="name">{{item.mateName}}</view>
					<view class="u-tips-color u-m-b-10">规格:{{item.specName}}/{{item.unitName}}</view>
					<view class="u-tips-color u-m-b-10">存放仓库:{{filterStoreName(item.storeId)}}</view>
					<view class="number">{{item.mateNum}}{{item.unitName}}</view>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		components: {
			
		},
		data() {
			return {
				// 下拉刷新的配置(可选, 绝大部分情况无需配置)
				downOption: {
				},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				mateName:'',
				// 列表数据
				dataList: [],
				storeList:[],
			}
		},
		onLoad() {
			this.getStoreList();
		},
		computed:{
			filterStoreName(){
				return function(value){
					let v = '';
					for (let i = 0; i < this.storeList.length; i++){
						// console.log('value',value);
						// console.log('value',this.storeList[i]);
						let item = this.storeList[i];
						if (value == item.value) {
						  v = item.label;
						  break;
						}
					}
					return v
				}
			}
		},
		methods: {
			/*上拉加载的回调*/
			upCallback(page) {
				let params ={
					pageNum:page.num,
					pageSize:page.size,
					mateName:this.mateName
				} 
				this.$u.api.stockList(params).then(curPageData=>{
					console.log('curPageData',curPageData)
					//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
					// this.mescroll.endSuccess(curPageData.total);
					this.mescroll.endBySize(curPageData.rows.length, curPageData.total);
					//设置列表数据
					if(page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
					this.dataList=this.dataList.concat(curPageData.rows); //追加新数据
				}).catch((err)=>{
					uni.showToast({
						title:'链接失败'
					});
					console.log('err',err)
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
				
			},
			/*若希望重新加载列表,只需调用此方法即可(内部会自动page.num=1,再主动触发up.callback)*/
			reloadList(){
				this.mescroll.resetUpScroll();
			},
			leftClick() {
			   let canNavBack = getCurrentPages();
			     if(canNavBack && canNavBack.length>1) {  
			       uni.navigateBack({  
			         delta: 1  
			       });  
			     } else {  
			       history.back();  
			     }
			},
			goCheck(item){
				this.$u.route({
					url: 'pages/check/checkItem/checkItem',
					params: {
						id: item.id
					}
				})
				console.log('goPutOut',item)
			},
			search(){
				this.mescroll.resetUpScroll();
			},
			async getStoreList() {
				let { code, data, msg} = await this.$u.api.storeList();
				if(code === 200) {
					this.storeList = data.map(item => {
						return {
							label: item.text,
							value: item.value
						}
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
.item{
	margin: 24rpx;
	padding-bottom: 24rpx;
	.name{
		font-size: 34rpx;
		margin-bottom: 10rpx;
	}
	.number{
		color: var(--main-color);
		font-size: 32rpx;
	}
}
</style>