<template>
	<view class="lists">
		<view class="status_bar" :style="navHeight"></view>
		<view class="header-bg">
			<image class="header-bg-img" src="@/static/image/nav_bg@3x.png" mode="scaleToFill"></image>
		</view>
		<view class="main">
			<view class="middle view">
				<view class="text-block ">
					<text class="middle-date">产品名称</text>
					<input class="middle-date-des" type="text" v-model="condition.product" placeholder="模糊查询" />
				</view>
				<view class="text-block text-block-last">
					<text class="middle-date">产品编码</text>
					<input class="middle-date-des" type="text" v-model="condition.code" placeholder="请输入" />
				</view>
			</view>

			<view class="planout-block">
				<button class="btn" type="default" @tap="search">
					<text class="btn-text">查询</text>
				</button>
			</view>

			<scroll-view class="scroll-area" :style='scrollHeight' scroll-y="true" lower-threshold='150'
				@scrolltolower='scrolltolower'>
				<view class="middle view planout-block-item" v-for="(item,index) in lists" :key='item.PRODUCT_ID'
					@click="handleChecked($event,item, index)">
					<view class="text-block">
						<text class="middle-date middle-date-select">产品编码</text>
						<text class="middle-date-des middle-date-des-select">{{item.PRODUCT_ID}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date middle-date-select">产品描述</text>
						<text class="middle-date-pro middle-date-des-select">{{item.SHORT_TEXT}}</text>
					</view>
					<view class="text-block">
						<text class="middle-date middle-date-select">存货分类</text>
						<text class="middle-date-des middle-date-des-select">{{item.CHFL_D}}</text>
					</view>
					<view class="text-block text-block-last">
						<text class="middle-date middle-date-select">规格型号</text>
						<text class="middle-date-des middle-date-des-select">{{item.NORMS}}</text>
					</view>
				</view>
				<view class="loading-more" v-if='lists.length>2'>
					<text class="loading-more-text">{{loadingText}}</text>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	import {
		apiProductSelect
	} from '@/servers/common.js'

	const modeMap = {
		'quotaOrder': 'QUOTA',
		'noQuotaOrder': 'NO_QUOTA',
		'groupOrder': 'GGTG',
	}

	export default {
		data() {
			return {
				isOpened: 'none',
				loadingText: '加载中...',
				condition: {
					product: '',
					code: '',
				},
				page: {
					"PAGE": "1",
					"PAGESIZE": "10",
				},
				loadParams: {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				},
				lists: [{
					checked: false,
					"PRODUCT_ID": "",
					"SHORT_TEXT": "",
					"NORMS": "",
					"MSEHT": "",
					"PECYCLE": ""
				}],
				selectedProduct: {},
				toPageParams: {
					page: '',
					mode: '',
					index: 0,
					head: {}
				}
			}
		},
		computed: {
			...mapState(['sysinfo']),
			navHeight() {
				return {
					'height': `${this.sysinfo.safeArea.top + 44}px`
				}
			},
			scrollHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 237- 44 - 20}px`
				}
			}
		},
		// 右上角按钮事件
		onNavigationBarButtonTap(e) {
			// console.log('onNavigationBarButtonTap：', e, this.isOpened, this.selectedProduct);
			// if (this.isOpened === 'left' && this.selectedProduct && this.selectedProduct.PRODUCT_ID) {
			// 	uni.$emit('selectedProduct', {
			// 		selectedProduct: this.selectedProduct,
			// 		mode: this.toPageParams.mode,
			// 		index: this.toPageParams.index
			// 	})
			// 	uni.navigateBack({})
			// 	return
			// }
			// this.isOpened = this.isOpened === 'none' ? 'left' : 'none';
			// #ifdef APP-PLUS  
			// let pages = getCurrentPages()
			// let page = pages[pages.length - 1]
			// let webView = page.$getAppWebview();
			// console.log('webView', webView)
			// webView.setTitleNViewButtonStyle(0, {
			// 	text: this.isOpened === 'none' ? '选择' : '完成',
			// });

			// let titleObj = webView.getStyle().titleNView;
			//  if(!titleObj.buttons){
			// 	 returnl
			//  }
			//  titleObj.buttons[0].text = 'dd';
			//  webView.setStyle({
			// 	 titleNView:titleObj
			//  })
			// #endif
		},
		// onLoad(options) {
		// 	setTimeout(function() {
		// 		console.log('start pulldown');
		// 	}, 1000);
		// 	uni.startPullDownRefresh();
		// },
		onPullDownRefresh() {
			console.log('refresh');
			this.reset()
			this.getProduct(true)
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 600);
		},
		// onReachBottom() {
		// 	console.log('onReachBottom');
		// },
		onLoad(option) {
			console.log('select-product', option)
			const {
				from,
				mode,
				index,
				head
			} = option
			this.toPageParams.index = index
			this.toPageParams.mode = mode
			this.toPageParams.page = from
			this.toPageParams.head = JSON.parse(head)
		},
		created() {
			this.reset()
			this.getProduct(true);
		},
		methods: {
			async getProduct(isRefresh) {
				if (this.loadParams.isDone) {
					return
				}

				const params = {
					...this.page,
					"MODE": modeMap[this.toPageParams.page],
					"INFO": this.toPageParams.head,
					"input": {
						"SHORT_TEXT": this.condition.product,
						"PRODUCT_ID": this.condition.code,
					},
				}

				uni.showLoading({
					title: '加载中'
				});
				setTimeout(function() {
					uni.hideLoading();
				}, 2000);
				const res = await apiProductSelect(params)
				uni.hideLoading();

				const {
					DATA,
					TOTAL
				} = res
				this.loadParams.total = TOTAL
				this.loadParams.totalPage = Math.ceil(TOTAL * 1 / this.page.PAGESIZE * 1)

				const dataMap = DATA && DATA.map((item) => {
					item.checked = false
					return item
				})

				if (isRefresh) {
					this.lists = dataMap
					this.loadParams.isDone = false
				} else {
					this.lists = this.lists.concat(dataMap)
				}
			},
			scrolltolower() {
				console.log('onReachBottom');
				if (this.page.PAGE < this.loadParams.totalPage) {
					this.page.PAGE++
					this.getProduct()
				} else {
					this.loadParams.isDone = true
					this.loadingText = '-- 到底了 --'
				}
			},
			reset() {
				this.loadParams = {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				}
				this.page.PAGE = 1
				this.lists = []
				this.loadingText = '加载中'
			},
			search() {
				this.reset()
				this.getProduct(true)
			},
			handleChecked(e, item, index) {
				// console.log('当前索引：', e, index);
				this.lists.forEach((cur) => {
					if (cur.PRODUCT_ID === item.PRODUCT_ID) {
						cur.checked = !cur.checked
					} else {
						cur.checked = false
					}
				})

				if (item.checked === true) {
					this.selectedProduct = item

					if (this.selectedProduct && this.selectedProduct.PRODUCT_ID) {
						uni.$emit('selectedProduct', {
							selectedProduct: this.selectedProduct,
							mode: this.toPageParams.mode,
							index: this.toPageParams.index
						})
						uni.navigateBack()
					}
				} else {
					// const selectIndex = this.selectedProduct.findIndex((saleplan)=>{
					// 	return saleplan.id === item.id
					// })
					// this.selectedProduct.splice(selectIndex,1)
					this.selectedProduct = {}
				}
			},
			handleClick(e, content, index) {
				console.log('click当前索引：', e, content, index);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.view {
		flex-direction: column;
	}

	.header-bg {
		height: 284rpx;
		margin-top: -88rpx;
	}

	.header-bg-img {
		width: 750rpx;
	}

	.lists {
		position: relative;
		flex-direction: column;
		overflow-y: scroll;
		background: #f0f4f5;
		overflow-x: hidden;
		height: 100vh;
	}

	.main {
		position: relative;
		flex-direction: column;
		margin-top: -164rpx;
		padding: 0 16rpx;
	}

	.middle {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 32rpx 64rpx 32rpx 48rpx;
	}

	.text-block {
		display: flex;
		overflow: hidden;
		height: 96rpx;
		margin-bottom: 14rpx;
		border-bottom: 1px solid #f4f5f6;
	}

	.text-block-last {
		margin-bottom: 0;
		border-bottom: 0;
	}

	.middle-date {
		flex: 2;
		font-size: 28rpx;
		line-height: 96rpx;
		color: #333;
		margin-bottom: 8rpx;
	}

	.middle-date-des {
		flex: 3;
		font-size: 28rpx;
		line-height: 96rpx;
		height: 96rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		color: #888;
	}
	.middle-date-pro {
		flex: 3;
		font-size: 28rpx;
		height: 96rpx;
		color: #888;
	}

	.middle-date-select {
		color: #888;
	}

	.middle-date-des-select {
		color: #333;
	}

	.picker-year {
		position: relative;
		border-radius: 4px;
		align-items: center;
	}

	.uni-list-cell {
		flex: 1;
	}

	.uni-list-cell-db {
		flex: 1;
	}

	.picker-block {
		flex: 1;
	}

	.icon-arrow {
		position: absolute;
		right: 0;
		top: 16px;
		width: 12rpx;
		height: 20rpx;
		z-index: 10;
	}

	.scroll-area {
		overflow-y: scroll;
	}

	.planout-block-lists {
		display: flex;
		flex-direction: column;
	}

	.uni-swipe {
		flex: 1;
		margin-bottom: 26rpx;
	}

	::v-deep  .uni-swipe_box {
		flex: 1;
	}

	.swipe-right-block {
		display: flex;
		width: 100rpx;
		justify-content: center;
		align-items: center;
	}

	.swipe-right-block__text {
		display: inline-block;
		width: 52rpx;
		height: 52rpx;
		border-radius: 50%;
		background-color: #fff;
	}

	.swipe-right-block__active {
		width: 52rpx;
		height: 52rpx;
		border-radius: 50%;
		background-color: $wly-primary-color;
	}

	.planout-block {
		display: flex;
		justify-content: space-between;
		height: 84rpx;
		margin-top: 64rpx;
		margin-bottom: 48rpx;
		padding: 0 24rpx;
	}

	.planout-block-item {
		flex: 1;
		width: 750rpx;
		margin-bottom: 26rpx;
	}

	.btn {
		flex: 1;
		height: 92rpx;
		background: $wly-primary-color;
		border-radius: 46rpx;
	}

	.btn-text {
		color: #fff;
		font-size: 34rpx;
	}

	.loading-more {
		flex: 1;
		align-items: center;
		justify-content: center;
		padding-top: 14px;
		padding-bottom: 14px;
		text-align: center;
	}

	.loading-more-text {
		font-size: 12px;
		color: #999;
	}
</style>
