<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-block-last">
					<text class="middle-date">终端名称</text>
					<input class="middle-date-des" type="text" v-model="serchStoreName" 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" @click="handleChecked(item)">
							<view class="text-block">
								<text class="middle-date">{{item.storeName}}</text>
							</view>
							<view class="text-block">
								<text class="middle-date">联系人</text>
								<text class="middle-date-des">{{(item && item.owner)?item.owner.userName:''}}</text>
							</view>
							<view class="text-block">
								<text class="middle-date">电话</text>
								<text class="middle-date-des">{{(item && item.owner)?item.owner.userMobile:''}}</text>
							</view>
							<view class="text-block text-block-last">
								<text class="middle-date">地址</text>
								<text class="middle-date-des">{{item.address}}</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 timeFormat from '@/uview-ui/libs/function/timeFormat.js'
	import {
		byDealerConditon
	} from '@/servers/address.js'
	import noData from '@/components/nodata.nvue';
	export default {
		components: {
			noData,
		},
		data() {
			return {
				isOpened: 'none',
				loadingText: '加载中...',
				page: {
					"size": 50,
					"current": 1
				},
				loadParams: {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				},
				lists: [],
				selectedSalePlan: {},
				selectedSalePlanTypes: [],
				typesIndex: -1,
				toPageParams: {
					page: '',
					index: 0,
					product: {},
					head: {}
				},
				salePlanArr: [],
				saleStatusArr: [],
				serchStoreName: ''
			}
		},
		computed: {
			...mapState(['sysinfo', 'userInfo']),
			navHeight() {
				return {
					'height': `${this.sysinfo.safeArea.top + 44}px`
				}
			},
			scrollHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 180- 44}px`
				}
			},
			mainHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 44 }px`,
				}
			},
		},
		onLoad(option) {
		},
		onPullDownRefresh() {
			console.log('refresh');
			this.reset()
			this.getSalesPlan(true)
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 600);
		},
		created() {
			this.reset()
			this.getSalesPlan(true);
		},
		methods: {
			async getSalesPlan(isRefresh) {
				if (this.loadParams.isDone) {
					return
				}
				const params = {
					"dealerCode": this.userInfo.code,
					"storeName": this.serchStoreName,
					...this.page
				}
	
				uni.showLoading({
					title: '加载中'
				});
				setTimeout(function() {
					uni.hideLoading();
				}, 2000);
				try{
					const res = await byDealerConditon(params)
					uni.hideLoading();
					this.loadParams.isDone = false
					this.lists = res.data.records
					this.loadParams.total = res.data.total
					this.loadParams.totalPage = Math.ceil(res.data.total * 1 / this.page.size * 1)
					// if (isRefresh) {
					// 	this.loadParams.isDone = false
					// 	this.lists = res.data.records
					// } else {
					// 	this.lists = this.lists.concat(res.data.records)
					// }
				}catch(e){
					//TODO handle the exception
				}
			},
			scrolltolower() {
				console.log('onReachBottom');
				// if (this.page.current < this.loadParams.totalPage) {
				// 	this.page.current++
				// 	this.getSalesPlan()
				// } else {
				// 	this.loadParams.isDone = true
				// 	this.loadingText = '-- 到底了 --'
				// }
			},
			reset() {
				this.loadParams = {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				}
				this.page.current = 1
				this.lists = []
				this.loadingText = '加载中'
			},
			search() {
				this.reset()
				this.getSalesPlan(true);
			},
			handleChecked(item) {
				console.log('当前索引：', item.id, item.storeName);
				uni.navigateTo({
					url: `/pages/terminal-detail/terminal-detail?id=${item.id}&storeName=${item.storeName}`
				})
			},
		}
	}
</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;
	}
	.no-data {
		flex: 1;
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: 10;
	}
</style>
