<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">
			<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">开票方编号</text>
								<text class="middle-date-des">{{item.billto_bp}}</text>
							</view>
							<view class="text-block">
								<text class="middle-date">客户名称</text>
								<text class="middle-date-des">{{item.billto_name}}</text>
							</view>
							<view class="text-block">
								<text class="middle-date">发生日期</text>
								<text class="middle-date-des">{{item.posting_date}}</text>
							</view>
							<view class="text-block">
								<text class="middle-date">货款金额</text>
								<text class="middle-date-des">{{item.goods_amount}}</text>
							</view>
							<view class="text-block">
								<text class="middle-date">随货保证金</text>
								<text class="middle-date-des">{{item.forward_amount}}</text>
							</view>
							<view class="text-block text-block-last">
								<text class="middle-date">经销商保证金</text>
								<text class="middle-date-des">{{item.dealing_amount}}</text>
							</view>
						</view>
						<view class="empty-block"></view>
					</cell>
					<cell>
						<view class="loading-more" v-if='lists.length>0'>
							<text class="loading-more-text">-- 到底了 --</text>
						</view>
						<view class="loading-more" v-else>
							<text class="loading-more-text">暂无数据</text>
						</view>
					</cell>
				</list>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'

	import {
		apiAccountInquiry
	} from '@/servers/billingInvoice.js'

	export default {
		data() {
			return {
				itemHead: {},
				lists: [],
				condition: {
					customer: []
				},
				page: {
					page: 1,
					pagesize: 50,
					totalCount: 0
				}
			}
		},
		computed: {
			navHeight() {
				return {
					'height': `${this.sysinfo.safeArea.top + 44}px`
				}
			},
			mainHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height }px`,
				}
			},
			scrollHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 180 - 44}px`
				}
			},
			...mapState(['sysinfo', 'userInfo', 'userBpData'])
		},
		created() {
			this.accountInquiry(1);
		},
		methods: {
			scrolltolower() {
				if (this.lists.length >= this.page.totalCount && this.page.totalCount > 0) {
					return;
				}
				this.page.page++;
				this.accountInquiry(this.page.page);
			},
			async accountInquiry(pageNo = 1) {
				// 处理客户条件，转换为SAP格式（属性名小写，与PC端保持一致）
				const customer = (this.condition.customer || []).map(v => ({
					sign: v.sign || v.SIGN || 'I',
					option: v.option || v.OPTION || 'EQ',
					low: v.low || v.LOW || '',
					high: v.high || v.HIGH || ''
				}))
				
				// 构建查询条件对象，与PC端格式保持一致
				const allConditions = {}
				if (customer.length > 0) {
					allConditions.customer = customer
				}
				
				// 获取用户编号和角色信息，参考 ticket-order.nvue 的方式
				const userCode = this.userInfo?.code || ''
				// 从 userBpData 获取第一个角色的 ROLE_ID
				const roleId = this.userBpData?.ROLES && this.userBpData.ROLES.length > 0 
					? this.userBpData.ROLES[0].ROLE_ID 
					: ''
				let params = {
					user_bp: userCode,
					role: roleId,
					...allConditions,
					page: pageNo,
					pagesize: this.page.pagesize,
					totalCount: this.page.totalCount,
					is_result: 1,
					sort: []
				}
				uni.showLoading({
					title: '加载中'
				});
				const res = await apiAccountInquiry(params);
				uni.hideLoading();
				if (res) {
					if (pageNo === 1) {
						this.lists = res.data || [];
					} else {
						this.lists = this.lists.concat(res.data || []);
					}
					this.page.totalCount = res?.TOTALCOUNT || res?.totalCount || res?.Total || this.page.totalCount;
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifndef APP-PLUS */
	page {
		width: 100%;
		min-height: 100%;
		display: flex;
		background: #F2F3F5;
	}

	.status_bar {
		height: var(--status-bar-height);
	}

	/* #endif */

	.lists {
		position: relative;
		flex-direction: column;
		overflow-y: scroll;
		background: #f0f4f5;
		overflow-x: hidden;
	}

	.header-bg {
		height: 284rpx;
		margin-top: -88rpx;
	}

	.header-bg-img {
		width: 750rpx;
	}

	.main {
		position: relative;
		flex-direction: column;
		margin-top: -164rpx;
		padding: 0 16rpx;
	}

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

	.view {
		flex-direction: column;
	}

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

	.middle-date-des {
		flex: 3;
		font-size: 28rpx;
		line-height: 96rpx;
		height: 96rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		color: #888;
	}

	.planout-block-item {
		flex: 1;
		width: 750rpx;
		margin-bottom: 26rpx;
	}

	.loading-more {
		align-items: center;
		justify-content: center;
		padding-top: 20rpx;
		padding-bottom: 20rpx;
	}

	.loading-more-text {
		font-size: 28rpx;
		color: #999;
	}
	
	.empty-block {
		height: 24rpx;
	}
</style>

