<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">
							<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">
									<view class="uni-input">
										{{condition.PROCESS_TYPE_VIEW}}
									</view>
								</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">
									<view class="uni-input">
										{{condition.ESTAT_VIEW}}
									</view>
								</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">{{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 :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">
							<view class="text-block">
								<text class="middle-date">{{item.OBJECT_ID}}</text>
								<text class="middle-date-des">{{item.DESCRIPTION}}</text>
							</view>
							<view class="text-block">
								<text class="middle-date">制票通知单</text>
								<text class="middle-date-des">{{item.ZZFLD000007}}/{{item.ZZFLD000009}}</text>
							</view>
							<view class="text-block">
								<text class="middle-date">发货</text>
								<text class="middle-date-des">{{item.ZDELIVER}}/{{item.ZZFLD000007}}</text>
							</view>
						</view>
						<view class="empty-block"></view>
						<!-- </scroll-view> -->
					</cell>
				</list>
			</scroll-view>

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

<script>
	import {
		mapState
	} from 'vuex'
	import {
		apiSalesPlanSelect,
		apiSalesPlanTypesSelect
	} from '@/servers/common.js'
	import {
		querySysDictionaryDetailsByCode,
	} from '@/servers/purchaseList.js'
	import {
		querySaleOrder
	} from '@/servers/breakwords.js'
	
	import timeFormat from '@/uview-ui/libs/function/timeFormat.js'
	
	export default {
		data() {
			return {
				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: [],
			}
		},
		computed: {
			...mapState(['sysinfo', 'userInfo']),
			navHeight() {
				return {
					'height': `${this.sysinfo.safeArea.top + 44}px`
				}
			},
			scrollHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 297- 44 - 20 -40}px`
				}
			},
			mainHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 44 }px`,
				}
			},
		},
		onNavigationBarButtonTap(e) {
			// console.log('onNavigationBarButtonTap：', e, this.isOpened, this.selectedSalePlan);
			// if (this.isOpened === 'left' && this.selectedSalePlan && this.selectedSalePlan.PLAN_NUMBER) {
			// 	uni.$emit('selectedSalePlan', {
			// 		selectedSalePlan: this.selectedSalePlan,
			// 		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(option) {
			console.log('select-saleplan', option)
			if (option && option.from) {
				const {
					from,
					product,
					head
				} = option
				this.toPageParams.product = JSON.parse(product)
				this.toPageParams.page = from
				this.toPageParams.head = JSON.parse(head)
			}
		},
		onPullDownRefresh() {
			console.log('refresh');
			this.reset()
			this.getSalesPlan(true)
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 600);
		},
		created() {
			// this.condition.GJAHR = timeFormat(new Date(), 'yyyy')
			// this.condition.MONAT = timeFormat(new Date(), 'mm')
			this.reset()
			this.getSalesPlan(true);
			this.getSelectDictDataOrigin('A0043')
			this.getSelectDictDataOrigin('A0041')
		},
		methods: {
			getSelectDictDataOrigin(dictcode) {
			  querySysDictionaryDetailsByCode({'code': dictcode})
				.then(res => {
					if(res.data.Rows.length>0){
						switch (dictcode){
							case 'A0043':
								this.salePlanArr = res.data.Rows
								break;
							case 'A0041':
								this.saleStatusArr = res.data.Rows
								break;
							default:
								break;
						}
					}
				})
				.catch(() => {})
			},
			bindPickerYearStartChange(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.condition.POSTING_DATE = e.detail.value
			},
			async getSalesPlan(isRefresh) {
				if (this.loadParams.isDone) {
					return
				}
				const {
					PROCESS_TYPE,
					ESTAT,
					OBJECT_ID,
					POSTING_DATE
				} = this.condition
				const params = {
					'$filter':"PARTNER eq '"+this.userInfo.code+"'"+
					" and PROCESS_TYPE eq '" + PROCESS_TYPE+"'"+
					" and ESTAT eq '" + ESTAT+"'"+
					" and OBJECT_ID eq '" + OBJECT_ID+"'"+
					"and POSTING_DATE eq '"  + POSTING_DATE+"'",
					'$skip':0,
					'$top':50,
				}
	
				uni.showLoading({
					title: '加载中'
				});
				setTimeout(function() {
					uni.hideLoading();
				}, 2000);
				try{
					const res = await querySaleOrder(params)
					uni.hideLoading();
					this.loadParams.isDone = false
					this.lists = res.d.results
				}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);
			},
			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;
				}
			},
			bindPickerYearChange(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.condition.GJAHR = e.detail.value
			},
			handleChecked(e, item, index) {
				console.log('当前索引：', e, index);
	
				this.lists.forEach((cur) => {
					if (cur.PLAN_NUMBER === item.PLAN_NUMBER) {
						cur.checked = !cur.checked
					} else {
						cur.checked = false
					}
				})
	
				if (item.checked === true) {
					this.selectedSalePlan = item
	
					if (this.selectedSalePlan && this.selectedSalePlan.PLAN_NUMBER) {
						uni.$emit('selectedSalePlan', {
							selectedSalePlan: this.selectedSalePlan,
							index: this.toPageParams.index
						})
						uni.navigateBack()
					}
				} else {
					// const selectIndex = this.selectedSalePlan.findIndex((saleplan)=>{
					// 	return saleplan.id === item.id
					// })
					// this.selectedSalePlan.splice(selectIndex,1)
					this.selectedSalePlan = {}
				}
			},
		}
	}
</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-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;
	}

	.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;
	}

	/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;
	}
</style>
