<template>
	<view class="moreShelfPart flex flex_column">
		<view class="moreShelfPart-search">
			<u-search
			placeholder="请输入货架名称搜索" 
			v-model="shelfName" 
			:show-action="false" 
			@input="$u.debounce(getShelfList, 800)"
			@custom="$u.debounce(getShelfList, 500)" 
			@search="$u.debounce(getShelfList, 500)" 
			@clear="clearSearch" 
			:action-style="{'color': '#fff', 'font-size': '24upx', 'background-color': '#57a3f3', 'border-radius': '6upx', 'padding': '12upx 0'}">
			</u-search>
		</view>
		<view class="moreShelfPart-body">
			<scroll-view
			class="nav-right" 
			scroll-y 
			@scrolltolower="onreachBottom">
				<u-checkbox-group wrap class="nav-right-item" @change="checkboxGroupChange">
					<u-checkbox 
						v-model="item.checked" 
						v-for="(item, index) in shelfList" 
						:key="item.shelfSn"
						:name="item.shelfSn"
					>{{item.shelfName}}</u-checkbox>
				</u-checkbox-group>
				<view v-if="shelfList&&shelfList.length==0">
					<u-empty v-if="status!='loading'" :src="`/static/${$config('themePath')}/def_no_data@3x.png`" icon-size="180" :text="noDataText" mode="list" :margin-top="50"></u-empty>
				</view>
				<view style="padding: 20upx;" v-else>
					<u-loadmore :status="status" />
				</view>
			</scroll-view>
		</view>
		<view class="batchPrint-manualPrint-footer">
			<view style="color: #999;">
				已选 {{checkList.length}} 个货架
			</view>
			<view>
				<u-button class="handle-btn" :loading="loading" shape="circle" size="medium" hover-class="none" @click="resetForm">重置</u-button>
				<u-button class="handle-btn" :loading="loading" shape="circle" size="medium" hover-class="none" :custom-style="customBtnStyle" @click="submit">确定</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import { getShelfList } from '@/api/shelf'
	export default {
		props: {
			checkEdList: {  //  默认查询条件
				type: Array,
				default: () => {
					return []
				}
			},
		},
		data() {
			return {
				shelfName: '',
				isGobleSearch: false,
				shelfList: [],
				pageNo:1,
				pageSize: 1000,
				total: 0, // 列表总数
				noDataText: '暂无货架',
				status: 'loading',
				tempData: null,
				allChecked: false,
				customBtnStyle: {  //  自定义按钮样式
					borderColor: this.$config('primaryColor'),
					backgroundColor: this.$config('primaryColor'),
					color: '#fff'
				},
				checkList: [],
				loading: false
			}
		},
		mounted() {
			this.clearSearch()
			this.checkList = this.checkEdList
		},
		methods: {
			checkboxGroupChange(e) {
				console.log(e);
				this.getChecked()
			},
			clearSearch(){
				this.shelfName = ''
				this.pageNo = 1
				this.total = 0
				this.shelfList = []
				this.getShelfList()
			},
			resetForm(){
				this.checkList = []
				this.$emit("ok", [])
			},
			getChecked(){
				const ret = this.shelfList.filter(item => item.checked)
				const list = []
				ret.map(item => {
					list.push(item.shelfSn)
				})
				this.checkList = list
				return list
			},
			submit(){
				const ret = this.getChecked()
				let shelfName = ''
				if(ret.length == 1){
					shelfName = this.shelfList.find(item => item.shelfSn == ret[0]).shelfName
				}
				this.$emit("ok", ret, shelfName)
			},
			setChecked(){
				this.shelfList.map(item => {
					item.checked = this.checkEdList.find(k => k == item.shelfSn) ? true : false
				})
			},
			// 获取数字货架列表
			getShelfList(){
				const _this = this
				if(this.shelfName == ''){
					this.pageNo = 1
					this.total = 0
				}
				let params = {
				    pageNo: this.pageNo,
				    pageSize: this.pageSize,
					shelfName: this.shelfName
				}
				_this.status = 'loading'
				getShelfList(params).then(res => {
					uni.hideLoading()
					if(res.status == 200){
						let list = res.data.list
						if (list && list.length){
							// 分页 拼接数据
							if(_this.pageNo>1){
								_this.shelfList = _this.shelfList.concat(res.data.list || [])
							}else{
								_this.shelfList = res.data.list
							}
							_this.total = res.data.count
							console.log(res.data.count)
							if (_this.shelfList.length == res.data.count) {
								_this.status = 'nomore'
							} else {
								_this.status = 'loadmore'
							}
						} else {
							_this.shelfList = list || []
							_this.total = 0
							_this.status = 'nomore'
							_this.noDataText = '没有查询到相关货架'
						}
						_this.noDataText = '暂无货架'
						_this.setChecked()
					}else{
						_this.status = 'loadmore'
						_this.shelfList = []
						_this.total = 0
						_this.noDataText = res.message ? res.message : '网络似乎出错了，请稍后再试'
					}
				})
			},
			// 加载更多
			onreachBottom () {
				if(this.shelfList.length < this.total ){
					if(this.isGobleSearch&&this.shelfName==''){
						return
					}
					this.pageNo++
					this.getShelfList()
				}else{
					this.status = "nomore"
				}
			},
		}
	}
</script>

<style lang="less">
	.moreShelfPart{
		width: 100%;
		height: calc(100vh - 120rpx) ;
		.moreShelfPart-search{
			padding: 0 20rpx;
			background-color: #FFFFFF;
		}
		.moreShelfPart-body{
			flex-grow: 1;
			background-color: #fff;
		}
		.nav-right{
			padding: 0 20upx;
			height: calc(100vh - 250rpx);
			box-sizing: border-box;
			 
			.nav-right-item{
				 /deep/ .u-checkbox{
					 border-bottom: 1rpx solid #f8f8f8;
					 padding: 20upx 10upx;
				 }
				 /deep/ .u-checkbox__label{
					 flex-grow: 1;
					 margin-right: 0;
				 }
			}
		}
		.batchPrint-manualPrint-footer{
			padding: 10upx;
			background-color: #fff;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-top: 1rpx solid #f8f8f8;
			> view{
				&:first-child{
					flex-grow: 1;
					width: 40%;
					font-size: 24rpx;
				}
				&:last-child{
					width: 60%;
					display: flex;
					justify-content: space-between;
					align-items: center;
					.handle-btn{
						width: 50%;
						margin-right: 15rpx;
					}
				}
			}
		}
	}
</style>
