<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>
					<input class="middle-date-des" type="text" v-model="searchParams.rt_partner" 
						placeholder="请输入" />
				</view>
				<view class="text-block text-block-last">
					<text class="middle-date">经销商名称</text>
					<input class="middle-date-des" type="text" v-model="searchParams.rt_name_org1" 
						placeholder="请输入" />
				</view>
			</view>
			<view class="planout-block">
				<button class="btn" type="default" @tap="handleSearch">
					<text class="btn-text">查询</text>
				</button>
			</view>
			
			<list ref="list" class="scroll-area view" loadmoreoffset='100' @loadmore="scrolltolower"
				:show-scrollbar='false'>
				<refresh @refresh="refresh" :display="refreshing ? 'show' : 'hide'">
				</refresh>
				<cell v-for="(item,index) in lists" :key="item.partner" @click="handleChecked(item)">
					<view class="scroll-item">
						<view class="item-block item-block-code">
							<text class="item-block-label">经销商名称：{{item.name_org1}}</text>
						</view>
						<view class="item-block item-block-code">
							<text class="item-block-label">经销商编号：{{item.partner}}</text>
						</view>
					</view>
				</cell>
				<cell>
					<view class="loading-more" v-if='lists.length>0'>
						<text class="loading-more-text">{{loadingText}}</text>
					</view>
				</cell>
				<view class="planout-block-item__last"> </view>
			</list>

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

<script>
	import {
		mapState
	} from 'vuex'

	import {
		getCustomerList
	} from '@/servers/purchaseList.js'
	
	import timeFormat from '@/uview-ui/libs/function/timeFormat.js'

	export default {
		data() {
			return {
				scrollTop: 0,
				isOpened: 'none',
				loadingText: '加载中...',
				refreshing: false,
				page: {
					"page": "1",
					"pagesize": "50",
				},
				loadParams: {
					total: 0,
					totalPage: 1,
					isDone: false,
					isRefresh: false
				},
				lists: [],
				orderItem: {},
				searchParams: {
					rt_partner: '',
					rt_name_org1: ''
				}
			}
		},
		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`
				}
			},

		},
		onLoad(option) {
			console.log('select-shunt-all', option)
			this.orderItem = {
				...JSON.parse(option.order)
			}
		},
		created() {
			this.reset()
			this.getCustomerData(true);
		},
		methods: {
			handleChecked(item) {
				this.selectAllShuntCode = item
				uni.$emit('selectAllShuntCode', {
					selectAllShuntCode: item
				})
				uni.navigateBack()
			},
			handleSearch() {
				this.reset()
				this.getCustomerData(true)
			},
			async getCustomerData(isRefresh) {
				if (this.loadParams.isDone) {
					return
				}

				// 构建查询参数（参考PC端格式）
				const params = {
					partner: this.userInfo.code,
					role: 'WLY001',
					mode: 'BP_ALL',
					page: this.page.page,
					pagesize: this.page.pagesize
				}
				
				// 支持过滤条件
				const conditions = {}
				
				// 自动为编号和名称添加*号进行模糊查询
				if (this.searchParams.rt_partner) {
					const value = this.searchParams.rt_partner.trim()
					if (value) {
						conditions.rt_partner = [{
							sign: "I",
							option: "CP",
							low: value.includes('*') ? value : `*${value}*`,
							high: ""
						}]
					}
				}
				
				if (this.searchParams.rt_name_org1) {
					const value = this.searchParams.rt_name_org1.trim()
					if (value) {
						conditions.rt_name_org1 = [{
							sign: "I",
							option: "CP",
							low: value.includes('*') ? value : `*${value}*`,
							high: ""
						}]
					}
				}
				
				const requestParams = {
					...params,
					...conditions
				}

				uni.showLoading({
					title: '加载中'
				});
				setTimeout(function() {
					uni.hideLoading();
				}, 2000);
				
				try {
					const res = await getCustomerList(requestParams)
					uni.hideLoading();

					const total = res.total || 0
					const dataList = res.data && res.data.length > 0 ? res.data.map(v => v.info) : []

					if (isRefresh) {
						this.lists = dataList
						this.loadParams.isDone = false
						this.loadParams.total = total
						this.loadParams.totalPage = Math.ceil(total / parseInt(this.page.pagesize))
					} else {
						this.lists = this.lists.concat(dataList)
					}

					// 判断是否已加载完所有数据
					if (this.lists.length >= total) {
						this.loadParams.isDone = true
						this.loadingText = '-- 到底了 --'
					}
				} catch (error) {
					uni.hideLoading();
					console.error('查询失败', error)
					if (isRefresh) {
						this.lists = []
					}
				}

				setTimeout(() => {
					this.refreshing = false;
				}, 300)

			},
			refresh(e) {
				this.refreshing = true;
				this.reset()
				this.getCustomerData(true)
				// #ifdef APP-NVUE
				try {
					this.$refs.list.resetLoadmore();
				} catch (e) {
					console.log('onrefresh', e)
				}
				// #endif

			},
			scrolltolower() {
				console.log('onReachBottom');
				if (this.loadParams.isDone) {
					return
				}
				if (parseInt(this.page.page) < this.loadParams.totalPage) {
					this.page.page = (parseInt(this.page.page) + 1).toString()
					this.getCustomerData(false)
				}
			},
			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;
		margin-top: -164rpx;
		padding: 0 16rpx;
	}

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

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

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

	.scroll-area {
		overflow-y: scroll;
	}

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

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

