<template>
<view class="view-page" :style="{backgroundImage : `url('${ThemeData?.app_bg || $config.PRIMARY_BGURL}')`}">
		<headerHeight />
		<navBar :navTitle="`${ leaderStatus ? '公会房间及流水' : '公会房间'}`">
			<template #rightView>
			</template>
		</navBar>
		<view class="content_view">
			<view>
				<uni-datetime-picker type="range" style="background-color: #f8f8f8;" start-placeholder="开始时间" :end="currentDate" end-placeholder="结束时间"
					v-model="dateSearch"  rangeSeparator="至" @change="changeDate" />
			</view>
			<view class="header-view" v-if="flowDetail && leaderStatus">
				<view class="flex-line flow-view w-fill flex-spaceB">
					<view class="flowTitle color-3">
						总流水
					</view>
				</view>
				<view class="flowNumber">
					{{flowDetail.total_transaction}}
				</view>
			</view>
			<view class="room-list flex-line" v-if="flowList && flowList.length">
				<view class="room-line flex-line flex-spaceB" v-for="data in flowList" :key="data.room_id" @click="jumpRoomPage(data)">
					<view class="flex-line">
						<view class="head-portrait">
							<img :src="data.room_cover || logo" alt="" />
						</view>
						<view class="ml-20">
							<view class="color-3 font-32 font-w500">
								{{data.room_name}}
							</view>
							<view class="color-6 font-24">
								ID:{{data.room_number}}
							</view>
						</view>
					</view>
					<view class="flex-line" v-if="leaderStatus">
						<view class="flowIcon">
							<img src="@/static/image/union/flowIcon.png" alt="" />
						</view>
						<view class="ml-20">
							{{data.total_price || 0}}
						</view>
					</view>
				</view>
			</view>
			<view class="mt-24">
				<uni-load-more :status="loading ? 'loading' : noMore ? 'noMore' : 'more'" />
			</view>
		</view>
	</view>
</template>

<script>
	import headerHeight from '@/component/headerHeight.vue';
	import navBar from '@/component/nav.vue';
	import http from '@/until/http.js';
	import logo from '@/static/image/logo.png'
	export default {
		components: {
			headerHeight,
			navBar
		},
		data() {
			return {
				dateSearch: [new Date(),new Date()],
				currentDate: +new Date(),
				logo,
				loading: false,
				noMore: false,
				detailData: null,
				pageConfig: {
					pageSize: 20,
					currentPage: 1,
					total: 0
				},
				searchParams: {
					guild_id: 0,
					start_time:new Date(),
					end_time: new Date(),
					token: uni.getStorageSync('token') || '',
					page: 1,
					page_size: 20
				},
				leaderStatus: null,
				flowDetail: null,
				flowList:[],
				ThemeData:null
			}
		},
		onLoad(options) {
			const {
				id,
				leader
			} = options

			this.leaderStatus = +leader
			this.searchParams.start_time = this.formatDate(new Date())
			this.searchParams.end_time = this.formatDate(new Date())
			this.searchParams.guild_id = id
			if (id) this.getFlow()
					if (uni.getStorageSync('Theme_Data')) {
			this.ThemeData = JSON.parse(uni.getStorageSync('Theme_Data'))
		}
		},
		onReachBottom() {
			if (!this.loading && !this.noMore) {
				this.getFlow()
			}
		},
		methods: {
			formatDate(timestamp) {
			  const date = new Date(timestamp);
			  const year = date.getFullYear();
			  const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始需+1
			  const day = String(date.getDate()).padStart(2, '0');
			  return `${year}-${month}-${day}`;
			},
			// 获取流水
			async getFlow() {
				const {
					code,
					data
				} = await http.get('/api/Guild/guild_flow', {
					...this.searchParams,
					page: this.pageConfig.currentPage,
					page_size: this.pageConfig.pageSize,
				})
				if(code) {
					this.flowDetail = data
					this.pageConfig.total = data.count
					this.loading = false
					const newData = data.list || []
					if (newData.length === 0) {
						this.noMore = true
						return
					}
					this.flowList = [...this.flowList, ...newData]
					this.pageConfig.currentPage++
					if (this.flowList.length === this.pageConfig.total) {
						this.noMore = true
						return
					}
					// console.log(this.flowList.length === this.pageConfig.total)
				}
			},
			jumpRoomPage(data){
				const platform = uni.getSystemInfoSync().platform;
				if (platform === 'ios') {
					window.webkit.messageHandlers.nativeHandler.postMessage({
						'action': 'jumpRoomPage',
						'data': {
							room_id: data.room_id
						}
					});
				} else if (platform === 'android') {
					window.Android.jumpRoomPage(data.room_id);
				}
			},
			// 日期范围
			changeDate(date) {
				this.searchParams.start_time = date.length ? date[0] : ''
				this.searchParams.end_time = date.length ? date[1] : ''
				this.getFlow()
			}
		}
	}
</script>

<style scoped lang="scss">
	.view-page {
		// padding: 24rpx 32rpx;
		min-height: 100vh;
		font-family: Source Han Sans CN, Source Han Sans CN;
		background-image: url('@/static/image/help/bg.png');
		background-repeat: no-repeat;
		background-size: 100% 100%;

		.content_view {
			padding: 0 24rpx;
		}

		.header-view {
			padding: 24rpx;
			margin-top: 24rpx;
			background-image: url('/static/image/union/flowbg.png');
			background-repeat: no-repeat;
			background-size: 100% 100%;
			height: 152rpx;

			.flow-view {
				font-weight: 400;
				font-size: 24rpx;
			}

			.flowNumber {

				font-weight: 500;
				font-size: 40rpx;
				color: #333;
				margin-top: 24rpx;
			}
		}

		.room-list {
			flex-wrap: wrap;
			width: 100%;
			.room-line {
				width: 100%;
				// background-color: #004D3C;
				margin-top: 24rpx;
				padding:24rpx;
				border-radius: 10rpx;
				background-color: #fff;

				.flowIcon {
					width: 48rpx;
					height: 48rpx;
				}

				.head-portrait {
					width: 100rpx;
					height: 100rpx;
					border-radius: 50%;

					img {
						border-radius: 50%;
					}
				}
			}
		}

	}
</style>