<template>
	<view class="">
		<list ref="list" class="page-news" loadmoreoffset='100' @loadmore="scrolltolower"
		:show-scrollbar='false'>
			<!-- <uni-refresh class="refresh" @refresh="onrefresh" @pullingdown="onpullingdown"
				:display="refreshing ? 'show' : 'hide'">
				<div class="refresh-view">
					<image class="refresh-icon" :src="refreshIcon" :style="{width: (refreshing || pulling) ? 0: '32px'}"
						:class="{'refresh-icon-active': refreshFlag}"></image>
					<uni-load-more v-if="refreshing" class="loading-icon" status="loading"
						:contentText="loadingMoreText"></uni-load-more>
					<text class="loading-text">{{refreshText}}</text>
				</div>
			</uni-refresh> -->
			<refresh @refresh="refresh" :display="refreshing ? 'show' : 'hide'">
				<!-- <loading-indicator></loading-indicator> -->
			</refresh>
			<cell v-for="(item, index) in dataList" :key="item.id">
				<order-item :newsItem="item" @click.native="goDetail(item)"></order-item>
			</cell>
			<cell v-if="isLoading || dataList.length > 4">
				<view class="loading-more">
					<text class="loading-more-text">{{loadingText}}</text>
				</view>
			</cell>
		</list>
		<no-data class="no-data" v-if="isNoData" @retry="loadMore"></no-data>
	</view>
</template>

<script>
	import uniList from '@/components/uni-list.vue';
	import uniCell from '@/components/uni-cell.vue';
	import uniRefresh from '@/components/uni-refresh.vue';
	import uniLoadMore from '@/components/uni-load-more.vue';
	import noData from '@/components/nodata.nvue';
	import orderItem from './order-item.nvue';

	import {
		mapState
	} from 'vuex'

	import {
		apiGetSalesOrder
	} from '@/servers/salesOrder.js'

	export default {
		components: {
			uniList,
			uniCell,
			uniRefresh,
			uniLoadMore,
			noData,
			orderItem
		},
		props: {
			condition: {
				type: Object,
				default: () => {}
			},
			dateRange: {
				type: Object,
				default: () => {
					return {
						start: '',
						end: ''
					}
				}
			}
		},
		data() {
			return {
				dataList: [],
				navigateFlag: false,
				pulling: false,
				refreshing: false,
				refreshFlag: false,
				refreshText: "",
				isLoading: false,
				loadingText: '加载中...',
				isNoData: false,
				pulling: false,
				angle: 0,
				loadingMoreText: {
					contentdown: '',
					contentrefresh: '',
					contentnomore: ''
				},
				refreshIcon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAB5QTFRFcHBw3Nzct7e39vb2ycnJioqK7e3tpqam29vb////D8oK7wAAAAp0Uk5T////////////ALLMLM8AAABxSURBVHja7JVBDoAgDASrjqj//7CJBi90iyYeOHTPMwmFZrHjYyyFYYUy1bwUZqtJIYVxhf1a6u0R7iUvWsCcrEtwJHp8MwMdvh2amHduiZD3rpWId9+BgPd7Cc2LIkPyqvlQvKxKBJ//Qwq/CacAAwDUv0a0YuKhzgAAAABJRU5ErkJggg==",
				page: {
					"PAGE": "1",
					"PAGESIZE": "10",
				},
				loadParams: {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				},
			}
		},
		computed: {
			...mapState(['userInfo'])
		},
		created() {
			this.pullTimer = null;
			this._isWidescreen = false;
			// this.loadData(true)
		},
		methods: {
			loadData(refresh, dateRange) {
				if (this.isLoading) {
					return;
				}

				this.isLoading = true;
				this.isNoData = false;
				const params = {
					...this.condition,
					...this.page
				}
				!params.object_id.low && delete params.object_id
				!params.posting_date.low && delete params.posting_date
				!params.process_type.low && delete params.process_type
				!params.status.low && delete params.status
				
				apiGetSalesOrder(params).then((res) => {
					console.log('loadData', res)
					const {
						data,
						page,
						total
					} = res
					const data_list = data;
					this.page.PAGE = page
					this.loadParams.total = total
					this.loadParams.totalPage = Math.ceil(total * 1 / this.page.PAGESIZE * 1)

					this.isNoData = (data_list.length <= 0);

					if (refresh) {
						this.dataList = data_list;
					} else {
						this.dataList = this.dataList.concat(data_list);
					}

					if (this.dataList.length > 0 && this._isWidescreen && this.dataList.length <= 10) {
						// this.goDetail(this.dataList[0]);
					}
				}).catch(() => {
					if (this.dataList.length == 0) {
						this.isNoData = true;
					}
				}).finally(() => {
					this.isLoading = false;
					if (refresh) {
						this.refreshing = false;
						this.refreshFlag = false;
						this.refreshText = "已刷新";
						if (this.pullTimer) {
							clearTimeout(this.pullTimer);
						}
						this.pullTimer = setTimeout(() => {
							this.pulling = false;
						}, 1000);
					}
				})
			},
			loadMore(e) {
				console.log('loadMore')
				if (this.page.PAGE < this.loadParams.totalPage) {
					this.page.PAGE++
					this.loadData(false);
				} else {
					this.loadingText = '-- 到底了 --'
				}
			},
			clear() {
				this.dataList.length = 0;
				this.requestParams.minId = 0;
			},
			goDetail(detail) {
				let path = ''
				if (detail.process_type === 'ZS01') {
					path = 'order-detail-quota/order-detail-quota'
				} else if (detail.process_type === 'ZS02') {
					if (detail.zzfld00002j === 'V013') {
						path = 'order-detail-group/order-detail-group'
					} else {
						path = 'order-detail-noquota/order-detail-noquota'
					}
				} else {
					return
				}
				uni.navigateTo({
					url: `/pages/${path}?order=${JSON.stringify(detail)}`
				})
			},
			refreshData() {
				if (this.isLoading) {
					return;
				}
				this.pulling = true;
				this.refreshing = true;
				this.page.PAGE = 1
				this.refreshText = "正在刷新..."
				this.loadingText = '加载中...'
				this.loadParams = {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				}
				this.page.PAGE = 1
				this.loadData(true);
			},
			onrefresh(e) {
				this.refreshData();
				// #ifdef APP-NVUE
				this.$refs.list.resetLoadmore();
				// #endif
			},
			onpullingdown(e) {
				if (this.refreshing) {
					return;
				}

				// var angle = (e.pullingDistance) / e.viewHeight * 180;
				// if (angle > 180) {
				// 	angle = 180;
				// }
				// tab.angle = angle;

				this.pulling = false;
				if (Math.abs(e.pullingDistance) > Math.abs(e.viewHeight)) {
					this.refreshFlag = true;
					this.refreshText = "释放立即刷新";
				} else {
					this.refreshFlag = false;
					this.refreshText = "下拉可以刷新";
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.no-data {
		flex: 1;
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: 10;
	}

	.page-news {
		// flex: 1;
		// flex-direction: column;
		// position: absolute;
		// left: 0;
		// top: 0;
		// right: 0;
		// bottom: 0;
	}

	.listview {
		// position: absolute;
		// left: 0;
		// top: 0;
		// right: 0;
		// bottom: 0;
		// /* #ifndef APP-NVUE */
		// display: flex;
		// flex-direction: column;
		// /* #endif */
		// /* #ifndef MP-ALIPAY */
		// flex-direction: column;
		// /* #endif */
	}

	.refresh {
		justify-content: center;
	}

	.refresh-view {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		width: 750rpx;
		height: 64px;
		flex-direction: row;
		flex-wrap: nowrap;
		align-items: center;
		justify-content: center;
	}

	.refresh-icon {
		width: 32px;
		height: 32px;
		transition-duration: .5s;
		transition-property: transform;
		transform: rotate(0deg);
		transform-origin: 15px 15px;
	}

	.refresh-icon-active {
		transform: rotate(180deg);
	}

	.loading-icon {
		width: 28px;
		height: 28px;
		margin-right: 5px;
		color: gray;
	}

	.loading-text {
		margin-left: 2px;
		font-size: 16px;
		color: #999999;
	}

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