<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>
					<view class="middle-date-des uni-list picker-year">
						<view class="uni-list-cell" style="line-height: 100%;height: 100%;">
							<view class="uni-list-cell-db" style="height: 100%;">
								<picker class="picker-block" @change="bindPickerYearStartChange" mode="date"
									:end='endRange' :value="startDate" fields='day'>
									<text class="uni-input" style="color: #888;font-size: 28rpx;">{{startDate}}</text>
								</picker>
							</view>
						</view>
						<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode="aspectFit"></image>
					</view>
				</view>
				<view class="text-block text-block-last">
					<text class="middle-date">截至日期</text>
					<view class="middle-date-des uni-list picker-year">
						<view class="uni-list-cell" style="line-height: 100%;height: 100%;">
							<view class="uni-list-cell-db" style="height: 100%;">
								<picker class="picker-block" style="color: red;" @change="bindPickerYearEndChange" mode="date"
									:end='endRange' :value="endDate" fields='day'>
									<text class="uni-input" style="color: #888;font-size: 28rpx;">{{endDate}}</text>
								</picker>
							</view>
						</view>
						<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode="aspectFit"></image>
					</view>
				</view>
			</view>
			<view class="planout-block">
				<button class="btn" type="default" @tap="search">
					<text class="btn-text">查询</text>
				</button>
			</view>
			<view class="text-blocks">
				<text class="middle-num">终端进货量：{{inNum}}件</text>
				<text class="middle-num">消费者开瓶扫码数量：{{customerScan}}瓶</text>
			</view>
			<scroll-view :style='scrollHeight' class="scroll-area" scroll-y="true" lower-threshold='150'
				@scrolltolower='scrolltolower'>
				<list class="scroll-area view"  loadmoreoffset='100' show-scrollbar='false' @loadmore="scrolltolower">
					<cell class="list-cell" v-for="(item,index) in lists" :key='index'>
						<view class="middle view planout-block-item" @click="handleChecked(item)">
							<view class="text-block">
								<text class="middle-date">时间</text>
								<text class="middle-date-des">{{item.inboundsByProduct[0].date}}</text>
							</view>
							<view class="text-block">
								<text class="middle-date">产品</text>
								<text class="middle-date-des">{{item.inboundsByProduct[0].productName}}</text>
							</view>
							<view class="text-block text-block-last">
								<text class="middle-date">数量(件)</text>
								<text class="middle-date-des">{{item.inboundsByProduct[0].inbounds}}</text>
							</view>
						</view>
						<view class="empty-block"></view>
					</cell>
				</list>
				<no-data class="no-data" v-if="lists.length === 0"></no-data>
			</scroll-view>

		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import {
		inboundsById
	} from '@/servers/address.js'
	import timeFormat from '@/uview-ui/libs/function/timeFormat.js'
	import noData from '@/components/nodata.nvue';
	export default {
		components: {
			noData,
		},
		data() {
			return {
				isOpened: 'none',
				loadingText: '加载中...',
				page: {
					"PAGE": "1",
					"PAGESIZE": "1",
				},
				loadParams: {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				},
				lists: [],
				selectedSalePlan: {},
				selectedSalePlanTypes: [],
				typesIndex: -1,
				toPageParams: {
					page: '',
					index: 0,
					product: {},
					head: {}
				},
				salePlanArr: [],
				saleStatusArr: [],
				startDate: timeFormat(new Date(), 'yyyy-mm-dd'),
				endDate: timeFormat(new Date(), 'yyyy-mm-dd'),
				optionUrl: {},
				startRange: timeFormat(new Date(), 'yyyy-mm-dd'),
				endRange: timeFormat(new Date(), 'yyyy-mm-dd'),
				customerScan: 0,
				inNum: 0,
			}
		},
		computed: {
			...mapState(['sysinfo', 'userInfo']),
			navHeight() {
				return {
					'height': `${this.sysinfo.safeArea.top + 44}px`
				}
			},
			scrollHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 237- 44 - 30}px`
				}
			},
			mainHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 44 }px`,
				}
			},
		},
		onLoad(option) {
			console.log('option',option)
			this.optionUrl = { ...option }
			uni.setNavigationBarTitle({
				title: option.storeName
			});
		},
		onPullDownRefresh() {
			console.log('refresh');
			this.reset()
			this.getSalesPlan(true)
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 600);
		},
		created() {
			this.reset()
			this.getSalesPlan(true);
		},
		methods: {
			bindPickerYearStartChange(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.startDate = e.detail.value
			},
			bindPickerYearEndChange(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.endDate = e.detail.value
			},
			async getSalesPlan(isRefresh) {
				if (this.loadParams.isDone) {
					return
				}
				const params = {
					"terminalId": this.optionUrl.id,
					"from": this.startDate,
					"to": this.endDate
				}
	
				uni.showLoading({
					title: '加载中'
				});
				setTimeout(function() {
					uni.hideLoading();
				}, 2000);
				try{
					const res = await inboundsById(params)
					uni.hideLoading();
					this.loadParams.isDone = false
					this.customerScan = res.data.customerScan || 0
					this.inNum = res.data.in || 0
					this.lists = res.data.inboundsByDate
				}catch(e){
					//TODO handle the exception
				}
			},
			scrolltolower() {
				console.log('onReachBottom');
				if (this.page.PAGE < this.loadParams.totalPage) {
					this.page.PAGE++
					this.getSalesPlan()
				} 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.getSalesPlan(true);
			},
			handleChecked(item) {
				console.log('当前索引：', item);
			},
		}
	}
</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;
	}
	
	.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-blocks {
		display: flex;
		overflow: hidden;
		height: 40rpx;
		margin-bottom: 14rpx;
		border-bottom: 1px solid #f4f5f6;
	}
	.middle-num{
		flex: 2;
		font-size: 28rpx;
		line-height: 40rpx;
		color: #333;
		margin-bottom: 8rpx;
	}
	.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-select {
		color: #888;
	}

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

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

	.uni-list-cell {
		flex: 1;
		height: 48px;
	}

	.uni-list-cell-db {
		flex: 1;
		height: 48px;
		line-height: 48px;
	}

	.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;
		margin-top: 30rpx;
		margin-bottom: 30rpx;
		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;
	}
	.no-data {
		flex: 1;
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: 10;
	}
</style>
