<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="keyword" 
				@search="search" 
				@custom="search"></u-search>
			</view>
		</view>
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<u-checkbox-group @change="checkboxGroupChange" active-color="#218AEC" style="display: block;">
			<view v-for="item in dataList" :key="item.id">
				<view class="item u-flex u-row-between u-border-bottom">
					<view class="left u-flex">
						<image :src="item.imgs|firstImg" mode="aspectFill"></image>
						<view class="text">
							<view class="name">{{item.mateName}}</view>
							<view class="name u-tips-color">规格：{{item.specCode}}{{filterUnitName(item.unitCode)}}</view>
						</view>
					</view>
					<view class="right">
						<u-checkbox v-model="item.checked" :name="item.id"></u-checkbox>
						<!-- <u-number-box v-model="item.value" @change="valChange(item)"></u-number-box> -->
					</view>
				</view>
			</view>
			</u-checkbox-group>
		</mescroll-body>
		<view class="fixed-button">
			<u-button type="success" @click="confirmEven">确定</u-button>
		</view>
	</view>
</template>

<script>
	import {otherApiUrl} from '@/common/apiurl.js';
	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: '暂无相关数据'
					}
				},
				keyword:'',
				// 列表数据
				dataList: [{id:1,value:1},{id:2,value:12}],
				checkboxList:[],
				choiceList:[],
				unitNameList:[],
			}
		},
		onLoad() {
			this.unitNameDict();
		},
		computed:{
			filterUnitName(){
				return function(value){
					let v = '';
					for (let i = 0; i < this.unitNameList.length; i++){
						// console.log('value',value);
						// console.log('value',this.unitNameList[i]);
						let item = this.unitNameList[i];
						if (value == item.value) {
						  v = item.label;
						  break;
						}
					}
					return v
				}
			}
		},
		methods: {
			/*上拉加载的回调*/
			upCallback(page) {
				let params ={
					pageNum:page.num,
					pageSize:page.size,
					keyword:this.keyword
				} 
				this.$u.api.epiStoreInList(params).then(curPageData=>{
					// console.log('curPageData',curPageData)
					//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
					// this.mescroll.endSuccess(curPageData.rows.total);
					this.mescroll.endBySize(curPageData.rows.length, curPageData.rows.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();
			},
			// valChange(item){
			// 	console.log('valChange',item);
			// },
			// 物资选择完成事件
			confirmEven(){
				this.choiceList = []
				this.dataList.forEach(item => {
					if(this.checkboxList.indexOf(item.id) > -1) {
						this.choiceList.push({
							mateId: item.id,
							classTypeName: item.classTypeName,
							mateName: item.mateName,
							unitCode: item.unitCode,
							mateNum:1,
						})
					}
				})
				console.log('this.choiceList',this.choiceList);
				this.$u.vuex('putinList', this.choiceList)
				this.$u.route({
					url:'pages/putin/putin',
				})
			},
			// 选中任一checkbox时，由checkbox-group触发
			checkboxGroupChange(e) {
				this.checkboxList = e;
				console.log('this.checkboxList',this.checkboxList);
			},
			async unitNameDict() {
				let { code, data, msg} = await this.$u.get(otherApiUrl.getDict + 'goods_unit_name')
				if(code === 200) {
					this.unitNameList = data.map(item => {
						return {
							label: item.dictLabel,
							value: item.dictValue
						}
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
.item{
	margin: 24rpx;
	padding-bottom: 24rpx;
	.left image {
		width: 120rpx;
		height: 120rpx;
		border-radius: 8rpx;
		margin-right: 12rpx;
	}
}
</style>