<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" :style="mainHeight">
			<view class="middle view planout-block-item">
				<view class="text-block">
					<text class="middle-date">全部订单</text>
					<view class="middle-date-des uni-list picker-year">
						<view class="uni-list-cell">
							<view class="uni-list-cell-db">
								<picker class="picker-block" @change="bindPickerTypesChange($event,'type')" range-key='name'
									mode="selector" :value="condition.PROCESS_TYPE_VIEW" :range="salePlanArr">
									<text class="uni-input-text">{{condition.PROCESS_TYPE_VIEW}}</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 class="middle-date">全部状态</text>
					<view class="middle-date-des uni-list picker-year">
						<view class="uni-list-cell">
							<view class="uni-list-cell-db">
								<picker class="picker-block" @change="bindPickerTypesChange($event,'status')" range-key='name'
									mode="selector" :value="condition.ESTAT_VIEW" :range="saleStatusArr">
									<text class="uni-input-text">{{condition.ESTAT_VIEW}}</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 class="middle-date">创建日期</text>
					<view class="middle-date-des uni-list picker-year">
						<view class="uni-list-cell">
							<view class="uni-list-cell-db">
								<picker class="picker-block" @change="bindPickerYearStartChange" mode="date"
									:value="condition.POSTING_DATE" fields='day'>
									<text class="uni-input-text">{{condition.POSTING_DATE}}</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>
					<input class="middle-date-des" type="text" v-model="condition.OBJECT_ID" 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" scroll-y="true" lower-threshold='150'
				@scrolltolower='scrolltolower'> -->
				<list ref="list" class="scroll-area view" loadmoreoffset='100' @loadmore="scrolltolower"
					:show-scrollbar='false'>
					<refresh @refresh="refresh" :display="refreshing ? 'show' : 'hide'">
						<!-- <loading-indicator></loading-indicator> -->
					</refresh>
					<cell class="" v-for="(item,index) in lists" :key='index'>
						<view class="scroll-item block-detal">
							<view class="block-detal-item">
								<text class="detal-item-label">下单日期</text>
								<text class="detal-item-value">{{item.postingDate}}</text>
							</view>
							<view class="block-detal-item">
								<text class="detal-item-label">{{item.objectId}}</text>
								<text class="middle-pro-des">{{item.description}}</text>
							</view>
							<view class="block-detal-item">
								<text class="detal-item-label">制票通知单(瓶/件)</text>
								<text class="detal-item-value">{{item.zzfld000007}}/{{item.zzfld000009}}</text>
							</view>
							<view class="block-detal-item">
								<text class="detal-item-label">本单可发货量(件)</text>
								<text class="detal-item-value">{{item.zzfld000010}}</text>
							</view>
							<view class="block-detal-item block-detal-item-last">
								<text class="detal-item-label">已发货(件)</text>
								<text class="detal-item-value">{{item.zdeliver}}</text>
							</view>
						</view>
						<view class="empty-block"></view>
					</cell>
					<cell>
						<view class="loading-more" v-if='lists.length>2'>
							<text class="loading-more-text">{{loadingText}}</text>
						</view>
					</cell>
					<!-- <view class="planout-block-item__last"> </view> -->
				</list>
			<!-- </scroll-view> -->
			<no-data v-if="lists.length === 0"></no-data>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import {
		apiSalesPlanSelect,
		apiSalesPlanTypesSelect
	} from '@/servers/common.js'
	import {
		querySysDictionaryDetailsByCode,
	} from '@/servers/purchaseList.js'
	import {
		querySaleOrder,
		findAppWantGoods
	} from '@/servers/breakwords.js'
	
	import noData from '@/components/nodata.nvue';
	export default {
		data() {
			return {
				refreshing: false,
				isOpened: 'none',
				loadingText: '加载中...',
				condition: {
					POSTING_DATE: '',
					OBJECT_ID: '',
					PROCESS_TYPE: '',
					PROCESS_TYPE_VIEW: '',
					ESTAT_VIEW: '',
					ESTAT: '',
				},
				page: {
					"PAGE": "1",
					"PAGESIZE": "10",
				},
				loadParams: {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				},
				lists: [{
					checked: false,
					"PLAN_NUMBER": "3800000826",
					"PLAN_TYPE": "运营商配额酒年度计划",
					"WLYBRAND": "五粮液.1618",
					"GJAHR": "2022",
					"REMAIN_QTY": 9.000
				}, ],
				selectedSalePlan: {},
				selectedSalePlanTypes: [],
				typesIndex: -1,
				toPageParams: {
					page: '',
					index: 0,
					product: {},
					head: {}
				},
				salePlanArr: [],
				saleStatusArr: [],
			}
		},
		components: {
			noData,
		},
		computed: {
			...mapState(['sysinfo', 'userInfo']),
			navHeight() {
				return {
					'height': `${this.sysinfo.safeArea.top + 44}px`
				}
			},
			mainHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 44 }px`,
				}
			},
			scrollHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 44 - 156 }px`
				}
			},

		},
		// onPullDownRefresh() {
		// 	console.log('refresh');
		// 	this.reset()
		// 	this.getSalesPlan(true)
		// 	setTimeout(function() {
		// 		uni.stopPullDownRefresh();
		// 	}, 600);
		// },
		created() {
			this.reset()
			this.getSalesPlan(true)
			this.getSelectDictDataOrigin('A0043')
			this.getSelectDictDataOrigin('A0041')
		},
		methods: {
			search() {
				this.reset()
				this.getSalesPlan(true)
			},
			bindPickerYearStartChange(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.condition.POSTING_DATE = e.detail.value
			},
			bindPickerTypesChange(e, type) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				const index = e.detail.value || 0
				switch (type){
					case 'type':
						this.condition.PROCESS_TYPE = this.salePlanArr[index].value
						this.condition.PROCESS_TYPE_VIEW = this.salePlanArr[index].name
						break;
					case 'status':
						this.condition.ESTAT = this.saleStatusArr[index].value
						this.condition.ESTAT_VIEW = this.saleStatusArr[index].name
						break;
					default:
						break;
				}
			},
			getSelectDictDataOrigin(dictcode) {
			  querySysDictionaryDetailsByCode({'code': dictcode})
				.then(res => {
					if(res.data.Rows.length>0){
						switch (dictcode){
							case 'A0043':
								const arr1 = {
									name: "全部订单",
									value: "",
								}
								this.salePlanArr = [arr1, ...res.data.Rows]
								break;
							case 'A0041':
								const arr2 = {
									name: "全部状态",
									value: "",
								}
								this.saleStatusArr = [arr2, ...res.data.Rows]
								break;
							default:
								break;
						}
					}
				})
				.catch(() => {})
			},
			async getSalesPlan(isRefresh) {
				if (isRefresh) {
					this.page.PAGE = 1
				}
				if (this.loadParams.isDone) {
					return
				}
				const {
					PROCESS_TYPE,
					ESTAT,
					OBJECT_ID,
					POSTING_DATE
				} = this.condition
				const params = {
					pagesize: this.page.PAGESIZE,
					page:  this.page.PAGE,
					partner: this.userInfo.code,
					processType: PROCESS_TYPE,
					estat: ESTAT,
					objectId: OBJECT_ID,
					postingDate: POSTING_DATE.replace(/-/g,'')
				}
				uni.showLoading({
					title: '加载中'
				});
				setTimeout(function() {
					uni.hideLoading();
				}, 2000);
				const res = await findAppWantGoods(params)
				uni.hideLoading();
				
				const {
					Rows,
					page,
					Total
				} = res.data
				this.page.PAGE = page * 1
				this.loadParams.total = Total * 1
				this.loadParams.totalPage = Math.ceil(Total * 1 / this.page.PAGESIZE * 1)
				if (isRefresh) {
					this.lists = Rows
					this.loadParams.isDone = false
				} else {
					this.lists = this.lists.concat(Rows)
				}
				
				setTimeout(() => {
					this.refreshing = false;
				}, 300)
			},
			refresh(e) {
				this.refreshing = true;
				this.reset()
				this.getSalesPlan(true)
				// #ifdef APP-NVUE
				try {
					// console.log('-------', this.$refs.list)
					this.$refs.list.resetLoadmore();
				} catch (e) {
					console.log('onrefresh', e)
				}
				// #endif

			},
			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 = '加载中'
			},
			
		}
	}
</script>

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

	.header-bg {
		height: 284rpx;
		opacity: 0.72;
		margin-top: -88rpx;
	}
	.header-bg-img {
		width: 750rpx;
	}

	.lists {
		position: relative;
		flex-direction: column;
		background: #f0f4f5;
		flex: 1;
	}

	.main {
		position: relative;
		flex-direction: column;
		padding: 0 16rpx;
		// padding-top: 40rpx;
		// background-color: #fff;
	}

	.scroll-area {
		overflow-y: scroll;
		background: #f0f4f5;
	}

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

	.text-block {
		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;
		overflow: hidden;
		text-overflow: ellipsis;
		color: #888;
	}

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

	.uni-list-cell {
		flex: 1;
		height: 96rpx;
		line-height: 96rpx;
	}

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

	.picker-block {
		flex: 1;
	}

	.uni-input-text {
		height: 96rpx;
		line-height: 96rpx;
		font-size: 14px;
		color: #888;
		text-overflow: ellipsis;
		white-space: nowrap;
		overflow: hidden;
	}

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

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

	.planout-block-item {
		// margin: 32rpx 0;
	}

	.scroll-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		// margin-bottom: 32rpx;
		background-color: #fff;
		border-radius: 16rpx;
		// padding: 32rpx 64rpx 40rpx 48rpx;
	}

	.planout-block-item__last {
		height: 40px;
	}

	.item-block {
		overflow: hidden;
	}

	.item-block-label {
		font-size: 24rpx;
		color: #333;
	}

	.item-block__label {
		flex: 1;
		text-align: left;
		font-size: 32rpx;
		color: #333;
		font-weight: 500;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.item-block-code {
		padding-bottom: 16rpx;
	}

	.item-block-name {
		padding-top: 24rpx;
		padding-bottom: 32rpx;
	}

	.item-block-label-des {
		flex: 1;
		text-align: left;
		font-size: 28rpx;
		color: #333;
	}
	.item-block-right-des {
		flex: 1;
		text-align: right;
		font-size: 28rpx;
		color: #333;
	}

	.block-detal {
		display: flex;
		flex-direction: column;
		background: #f8f8f8;
		border-radius: 20rpx;
		padding:  0 20rpx;
		margin-top: 32rpx;
	}

	.block-detal-item {
		display: flex;
		overflow: hidden;
		height: 118rpx;
		// margin-bottom: 14rpx;
		border-bottom: 1px solid #f4f5f6;
	}

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

	.detal-item-label {
		flex: 2;
		font-size: 28rpx;
		line-height: 118rpx;
		color: #333;
		// margin-bottom: 8rpx;
	}

	.detal-item-value {
		flex: 2;
		font-size: 28rpx;
		line-height: 118rpx;
		height: 118rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		color: #888;
	}
	.middle-pro-des {
		display: flex;
		flex: 2;
		font-size: 28rpx;
		// height: 96rpx;
		color: #888;
		margin-top: 10rpx;
		// align-items: center;
	}
	.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;
	}
	.navs-block {
		flex-direction: column;
		background-color: #fff;
		padding-top: 40rpx;
	}
	
	.navs-tab {
		display: flex;
		flex: 1;
		align-items: center;
		height: 60rpx;
		border-bottom: 1px solid #F4F5F6;
		padding-bottom: 8rpx;
	}
	
	.navs-tab-item {
		display: flex;
		justify-content: center;
		flex: 1;
		align-items: center;
		height: 50rpx;
		overflow: hidden;
	}
	
	.search-type-text {
		color: #333;
		font-size: 28rpx;
	}
	
	.icon-arrow-down {
		width: 6px;
		height: 4px;
		margin-left: 16rpx;
	}
	.planout-block {
		display: flex;
		justify-content: space-between;
		margin-top: 30rpx;
		margin-bottom: 30rpx;
		padding: 0 24rpx;
	}
	
	.btn {
		flex: 1;
		height: 92rpx;
		background: $wly-primary-color;
		border-radius: 46rpx;
	}
	
	.btn-text {
		color: #fff;
		font-size: 34rpx;
	}
</style>
