<template>
	<view class="content flex flex_column">
		<view class="searchBar">
			<view class="p-title flex align_center">
				<text></text>
				<view class="shelfName">
					<view>{{nowData.shelfName}}</view>
				</view>
				<view class="screeningBox" @click="chooseShow = true">
					<text>滞销天数</text>
					<u-icon color="#0485F6" size="30" name="shaixuan" custom-prefix="iscm-icon"></u-icon>
				</view>
			</view>
			<!-- @input="$u.debounce(changeSearch, 800)" -->
			<view class="search flex align_center">
				<view class="input">
					<u-search 
					v-model="keyword" 
					@change="$u.debounce(getSearchCon, 800)"
					@search="getSearchCon"
					@custom="getSearchCon"
					@clear="clearSearch"
					bg-color="#fff" 
					:show-action="false"
					placeholder="请输入产品编码或名称搜索"></u-search>
				</view>
				<view class="icon" @click="handleScanCode">
					<u-icon name="scan"></u-icon>
				</view>
			</view>
		</view>
		<view class="check-list">
			<productList ref="productList" noDataText="暂无产品" @updateNums="updateNums" @allChecked="allCheckedCallback"></productList>
		</view>
		<!-- 底部栏 -->
		<view class="footer-bar flex align_center" v-if="partList.length">
			<view class="f-left">
				<u-checkbox size="40" @change="allCheckeChange" v-model="allChecked" shape="circle">{{allChecked?'取消全选':'全选'}}</u-checkbox>
			</view>
			<view class="f-mid">
				<view v-if="totalCategory">
					已选<text>{{totalCategory}}</text>款产品
				</view>
			</view>
			<view class="f-btns">
				<u-button size="medium" @click="submitOrder" :custom-style="customBtnStyle" shape="circle" type="primary" hover-class="none" >确定选择</u-button>
			</view>
		</view>
		<!-- 滞销天数弹窗 -->
		<u-popup v-model="chooseShow" mode="right" width="60%">
			<view class="choosePopup">
				<view class="chooseTit">滞销天数</view>
				<view class="chooseDay u-flex">
					<u-input v-model="queryParam.unsalableDaysBegin" input-align="center" placeholder="起始天数" type="number" />
					<text>至</text>
					<u-input v-model="queryParam.unsalableDaysEnd" input-align="center" placeholder="截止天数" type="number" />
				</view>
				<view class="butBox u-flex">
					<u-button hover-class="none" shape="circle" size="medium" @click="reset">重置</u-button>
					<u-button hover-class="none" :custom-style="customBtnStyle" size="medium" shape="circle" @click="handelFilter">查询</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	
	import { reportPage } from '@/api/vinLog';
	import productList from './productList.vue'
	import { controlQueryList,insert } from '@/api/shelf';
	export default {
		components: {
			productList
		},
		data() {
			return {
				nowData:null,
				keyword:'',
				noDataText: '暂无产品',
				partList: [],
				allChecked: false,
				customBtnStyle: {  //  自定义按钮样式
					borderColor: this.$config('primaryColor'),
					backgroundColor: this.$config('primaryColor'),
					color: '#fff'
				},
				chooseList: [],
				chooseShow:false,
				queryParam: { //  查询条件
				  shelfSn:undefined,
				  productCode: undefined,
				  productName: undefined,
				  unsalableDaysBegin: undefined,
				  unsalableDaysEnd: undefined,
				  queryWord:undefined
				},
				againChooseList:[]
			}
		},
		onBackPress(e) {
		  if(this.chooseShow) {  
			this.chooseShow = false;
			return true;  
		  } 
		},
		onLoad(opts) {
			this.nowData = JSON.parse(decodeURIComponent(opts.data));
			let ajaxData = {
				shelfSn: this.nowData.shelfSn
			};
			this.loadData(ajaxData);
		},
		computed: {
			totalCategory () {
			  return this.chooseList.length
			},
		},
		methods: {
			// 全选
			allCheckeChange(e){
				this.$refs.productList.allSelect(e.value)
				this.chooseList = this.$refs.productList.getAllChecked()
			},
			allCheckedCallback(val){
				this.allChecked = val
				this.chooseList = this.$refs.productList.getAllChecked()
			},
			// 清空已选数据
			clearChoose(){
				this.chooseList = []
				this.allChecked = false
				if(this.$refs.productList){
					this.$refs.productList.allSelect(false)
				}
			},
			updateNums(){
				this.chooseList = this.$refs.productList.getAllChecked()
			},
			clearSearch(){
				// 重置
				this.keyword = ''
				this.changeSearch()
			},
			changeSearch(){
				let ajaxData = {
					shelfSn:this.nowData.shelfSn,
					productCode: undefined,
					productName: undefined,
					unsalableDaysBegin: undefined,
					unsalableDaysEnd: undefined,
					queryWord:undefined
				};
				this.queryParam = ajaxData;
				this.partList = []
				this.loadData(ajaxData)
			},
			// 重置
			reset() {
				let ajaxData = {
					shelfSn:this.nowData.shelfSn,
					productCode: undefined,
					productName: undefined,
					unsalableDaysBegin: undefined,
					unsalableDaysEnd: undefined,
					queryWord:undefined
				};
				this.queryParam = ajaxData;
				this.loadData(ajaxData);
			},
			handelFilter(){
				if(this.checkValueRange()){
					this.queryParam.shelfSn = this.nowData.shelfSn;
					this.chooseShow =false;
					this.loadData(this.queryParam);
				}	
			},
			// 获取数字货架列表
			loadData(params) {
				const _this = this;
				_this.clearChoose()
				_this.partList = []
				_this.$refs.productList && _this.$refs.productList.setData(_this.partList)
				uni.showLoading({
					mask:true,
					title:"正在加载..."
				})
				controlQueryList(params).then(res => {
					if (res.status == 200) {
						res.data.forEach(item=>{
							item.isChecked= false
							item.confirmQty = item.qty - item.recallBillQty>0 ?item.qty - item.recallBillQty>0: 0;
						})
						_this.partList=res.data;
					}else{
						_this.partList =[]
					}
					// 赋值
					// 赋值
					if(_this.nowData.chooseKey.length){
						_this.partList = _this.partList.filter(item => {
							const row = _this.nowData.chooseKey.findIndex(k => k == item.id)
							return !(row>=0)
						})
					}
					// 已选
					_this.partList.map(item => {
						const row = _this.chooseList.find(k => k.id == item.id)
						item.checked = !!row
					})
					this.$refs.productList.setData(_this.partList)
					setTimeout(()=>{
						uni.hideLoading()
					},_this.partList.length*40)
				});
			},
			// 提交
			submitOrder(){
				if(this.totalCategory){
					uni.$emit("chooseBackProduct",this.chooseList)
					uni.navigateBack()
				}else{
					uni.showToast({
						icon: 'none',
						title: '请选择产品'
					})
				}
			},
			// 扫描
			handleScanCode(){
				this.mpaasScanModule = uni.requireNativePlugin("wss-scan")
				this.mpaasScanModule.scan(
				{
					"scanMode":"Customized",
					"scanStyle":{
						"scanFrameSizePlus":{"width":250,"height":250},
						"scanFrameSize":200,
						"scanLight":"visible",
						"scanText":"对准条形码/二维码进行识别",
						"scanTitle":"扫码搜索货位产品",
					}
				},
				(result) => {
					if(result.scanStatus == 1){
						this.scanResult(result)
					}
				})
			},
			// 扫描结果
			scanResult(data){
				// 二维码
				if(data.scanType == 'QRCODE'){
					const ret = data.scanValue.split("&")
					this.queryParam.queryWord = ret[1] // 产品编码
					this.keyword= ret[1]
				}else{
					this.queryParam.queryWord = data.scanValue;
					this.keyword= data.scanValue
				}
				this.queryParam.shelfSn = this.nowData.shelfSn
				this.loadData(this.queryParam);
			},
			// 搜索
			getSearchCon() {
				var reg = new RegExp('[\\u4E00-\\u9FFF]+', 'g');
				if(!this.queryParam.queryWord){
					if (reg.test(this.keyword)) {
						//包含汉字
						this.queryParam.productName = this.keyword.trim()
					} else {
						this.queryParam.productCode=this.keyword.trim()
					}
				}
				this.queryParam.shelfSn = this.nowData.shelfSn
				this.loadData(this.queryParam);
			},
			// 校验滞销天数数值范围
			checkValueRange () {
			  const isONull = this.queryParam.unsalableDaysBegin === null || this.queryParam.unsalableDaysBegin === undefined
			  const isOEmpty = this.queryParam.unsalableDaysBegin === ''
			  const isOZero = this.queryParam.unsalableDaysBegin === 0
			  const isTNull = this.queryParam.unsalableDaysEnd === null || this.queryParam.unsalableDaysEnd === undefined
			  const isTEmpty = this.queryParam.unsalableDaysEnd === ''
			  const isTZero = this.queryParam.unsalableDaysEnd === 0
			  //  第一个为空（可为null可为空字符）第二个不为空
			  //  第一个不为空第二个为空（可为null可为空字符）
			  //  第一个大于第二个
			  if ((isONull || isOEmpty) && (this.queryParam.unsalableDaysEnd || isTZero)) {
			    uni.showToast({
			    	title:'请输入正确的滞销天数查询范围！',
					icon:'none'
			    })
			    return false
			  }
			  if ((this.queryParam.unsalableDaysBegin || isOZero) && (isTNull || isTEmpty)) {
				uni.showToast({
					title:'请输入正确的滞销天数查询范围！',
					icon:'none'
				})
			    return false
			  }
			  if (this.queryParam.unsalableDaysBegin*1 > this.queryParam.unsalableDaysEnd *1) {
			    uni.showToast({
			    	title:'请输入正确的滞销天数查询范围！',
			    	icon:'none'
			    })
			    return false
			  }
			  this.queryParam.unsalableDaysBegin = this.queryParam.unsalableDaysBegin > 999999999 ? 999999999 : this.queryParam.unsalableDaysBegin
			  this.queryParam.unsalableDaysEnd = this.queryParam.unsalableDaysEnd > 999999999 ? 999999999 : this.queryParam.unsalableDaysEnd
			  return true
			}
		}
	}
</script>

<style lang="less">
.content{
	height: 100vh;
	width: 100%;
	background: #fff;
	.searchBar{
		padding: 0 0.8rem 0.3rem;
		.p-title{
			padding: 0 15rpx;
			display: flex;
			align-items: center;
			color: #222;
			font-size: 30rpx;
			> text{
					display: block;
					height: 30rpx;
					width: 6rpx;
					background: #0485F6;
					margin-right: 10rpx;
					border-radius: 10rpx;
				}
			.shelfName{
				flex-grow: 1;
				font-weight: bold;
				width: 74%;
				> view{
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
				}
			}
			.screeningBox {
				font-size: 26rpx;
				width: 150rpx;
				text-align: right;
				color: #0485F6;
				text {
					vertical-align: top;
				}
			}
			.btns{
				width:100rpx;
				text-align: right;
				font-size: 28rpx;
				color: #0485F6;
			}
		}
		.search{
			margin-top: 15upx;
			padding: 0.1rem;
			border-radius: 50rpx;
			border:1rpx solid #eee;
			.input{
				flex-grow: 1;
				padding: 4rpx;
			}
			.icon{
				width: 13%;
				text-align: center;
				font-size: 46rpx;
				color: #999;
			}
		}
	}
	.check-list{
		padding: 0 0.8rem;
		flex-grow: 1;
		overflow: auto; 
	 }
	.footer-bar{
		background-color: #f8f8f8;
		padding: 20upx;
		.f-left{
			color: #fff;
		}
		.active{
			background-color: #0485F6;
		}
		.f-mid{
			flex-grow: 1;
			color: #666;
			> view{
				padding: 0 5upx;
				&:first-child{
					text{
						color: red;
					}
				}
				&:last-child{
					font-size: 0.8rem;
				}
			}
		}
		.f-btns{
			button{
				width: 200upx;
			}
		}
	}
	// 筛选
	.tabBox{
		height: 100vh;
		.tabs{
			overflow: auto;
			padding: 20rpx;
			> view{
				flex-wrap: wrap;
				justify-content: space-between;
				> view{
					width: 98%;
					border: 1rpx solid #eee;
					padding: 15rpx;
					border-radius: 50rpx;
					text-align: center;
					margin-bottom: 15rpx;
					/deep/ .u-checkbox__label{
						flex-grow: 1;
					}
				}
				&:first-child{
					> view{
						width: 100%;
					}
				}
			}
		}
		.btns{
			display: flex;
			padding: 40upx 20upx 20upx;
			justify-content: space-between;
			.handle-btn {
				font-size: 28upx;
			}
		}
	}
	// 弹窗样式
	.choosePopup {
		padding: 30rpx 20rpx;
		box-sizing: border-box;
		.chooseTit {
			text-align: center;
		}
		.chooseDay {
			margin: 60rpx 0;
			.u-input {
				border-bottom: 1rpx solid #e0e0e0;
			}
		}
		.butBox {
			margin-top: 100rpx;
			.u-size-medium {
				padding: 0 60rpx !important;
			}
		}
	}
}
</style>
