<template>
	<view class="page">
		<u-navbar title="公告协议" :border-bottom="false" :background="bgc" back-icon-color="#262B37" title-color='#262B37'
			title-size='36' height='36' id="navbar">
		</u-navbar>
		<view class="content">
			<!-- 加载态 -->
			<view v-if="loading" class="loading">加载中…</view>
			<!-- 列表 -->
			<scroll-view v-else scroll-y class="list">
				<!-- 公告卡片 -->
				<view class="card-wrapper">
					<view class="card-header">
						<view class="" style="display: flex;
		justify-content: space-between;
		align-items: center;">
							<text class="card-title">公告</text>
							<view v-if="hasNotice" class="card-status">
								<text class="status-text">已设置</text>
							</view>
						</view>
						<view class="" style="font-size: 26rpx;color: #999;margin-top: 10rpx;">
							用户扫码后会自动弹窗展示
						</view>
					</view>
					
					<view v-if="hasNotice" class="card-content" @click="btnid(noticeItem.id)">
						<view class="card-item">
							<view class="card-item__title">{{ noticeItem.title }}</view>
							<view class="card-item__meta">
								<text class="meta__area">{{ noticeItem.areaName || '-' }}</text>
								<text class="meta__time">至少观看{{ noticeItem.duration || '-' }}秒</text>
							</view>
						</view>
					</view>
					<view v-else class="card-empty">
						<text class="empty-text">暂未设置公告</text>
						<button class="add-btn" @click="goToAdd(0)">去添加</button>
					</view>
				</view>
				
				<!-- 协议卡片 -->
				<view class="card-wrapper">
					<view class="card-header">
						<view class="" style="display: flex;
		justify-content: space-between;
		align-items: center;">
							<text class="card-title">协议</text>
							<view v-if="hasAgreement" class="card-status">
								<text class="status-text">已设置</text>
							</view>
						</view>
						<view class="" style="font-size: 26rpx;color: #999;margin-top: 10rpx;">
							用户支付时需勾选的租赁协议
						</view>
					</view>
					
					<view v-if="hasAgreement" class="card-content" @click="btnid(agreementItem.id)">
						<view class="card-item">
							<view class="card-item__title">{{ agreementItem.title }}</view>
							<view class="card-item__meta">
								<text class="meta__area">{{ agreementItem.areaName || '-' }}</text>
								<text class="meta__time">至少观看{{ agreementItem.duration || '-' }}秒</text>
							</view>
						</view>
					</view>
					<view v-else class="card-empty">
						<text class="empty-text">暂未设置协议</text>
						<button class="add-btn" @click="goToAdd(1)">去添加</button>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	    export default {
	        data() {
	            return {
					bgc: {
						backgroundColor: "#FFFFFF",
					},
					areaId:'',
					list: [],
					loading: false,
					noticeItem: null, // 公告项
					agreementItem: null, // 协议项
					hasNotice: false, // 是否有公告
					hasAgreement: false, // 是否有协议
	            };
	        },
			onLoad(option) {
				this.areaId = option.areaId
			},
			onShow() {
				this.getxieyi()
			},
	        methods: {
				// 获取公告协议
				getxieyi(){
					this.loading = true
					this.$u.get(`/bst/agreement/list?areaId=${this.areaId}`).then((res) => {
						if (res && res.code == 200) {
							const rows = (res.data && (res.data.rows || res.data.list)) || res.rows || []
							this.list = Array.isArray(rows) ? rows : []
							
							// 判断是否有公告和协议
							this.noticeItem = this.list.find(item => item.agreementType == 0) || null
							this.agreementItem = this.list.find(item => item.agreementType == 1) || null
							this.hasNotice = !!this.noticeItem
							this.hasAgreement = !!this.agreementItem
						} else {
							this.list = []
							this.hasNotice = false
							this.hasAgreement = false
							this.noticeItem = null
							this.agreementItem = null
						}
					}).catch(() => {
						this.list = []
						this.hasNotice = false
						this.hasAgreement = false
						this.noticeItem = null
						this.agreementItem = null
					}).finally(() => {
						this.loading = false
					})
				},
				btnid(id){
					uni.navigateTo({
						url:'/page_shanghu/gonggao?id=' + id
					})
				},
				// 跳转到添加页面
				goToAdd(agreementType){
					uni.navigateTo({
						url:`/page_shanghu/gonggao?areaId=${this.areaId}&agreementType=${agreementType}`
					})
				}
	        },
	    }
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.content {
		padding: 24rpx;
		height: calc(100vh - 88rpx);
		box-sizing: border-box;
	}

	.loading {
		text-align: center;
		color: #999;
		padding: 48rpx 0;
	}

	.list {
		max-height: 100%;
	}

	.card-wrapper {
		background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
		border-radius: 20rpx;
		padding: 32rpx;
		margin-bottom: 24rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.06);
		border: 2rpx solid #f0f0f0;
	}

	.card-header {
		
		margin-bottom: 24rpx;
		padding-bottom: 20rpx;
		border-bottom: 2rpx solid #f0f0f0;
	}

	.card-title {
		font-size: 36rpx;
		font-weight: 700;
		color: #262B37;
		position: relative;
		padding-left: 16rpx;
	}

	.card-title::before {
		content: '';
		position: absolute;
		left: 0;
		top: 50%;
		transform: translateY(-50%);
		width: 6rpx;
		height: 28rpx;
		background: linear-gradient(180deg, #4297F3 0%, #5BA3F5 100%);
		border-radius: 3rpx;
	}

	.card-status {
		background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);
		padding: 8rpx 20rpx;
		border-radius: 20rpx;
	}

	.status-text {
		font-size: 24rpx;
		color: #4caf50;
		font-weight: 600;
	}


	.card-item {
		padding: 24rpx;
		background: #ffffff;
		border-radius: 16rpx;
		border: 2rpx solid #e8e8e8;
		transition: all 0.3s ease;
	}

	.card-item:active {
		transform: scale(0.98);
		box-shadow: 0 2rpx 12rpx rgba(66, 151, 243, 0.2);
		border-color: #4297F3;
	}

	.card-item__title {
		font-size: 32rpx;
		font-weight: 600;
		color: #262B37;
		margin-bottom: 16rpx;
		line-height: 1.5;
	}

	.card-item__meta {
		font-size: 24rpx;
		color: #7A7F8A;
		display: flex;
		align-items: center;
		gap: 16rpx;
	}

	.meta__area {
		padding: 4rpx 12rpx;
		background: #f5f5f5;
		border-radius: 8rpx;
	}

	.meta__time {
		margin-left: auto;
		color: #4297F3;
		font-weight: 500;
	}

	.card-empty {
		text-align: center;
		padding: 60rpx 0;
	}

	.empty-text {
		display: block;
		font-size: 28rpx;
		color: #999;
		margin-bottom: 32rpx;
	}

	.add-btn {
		width: 240rpx;
		height: 72rpx;
		line-height: 72rpx;
		background: linear-gradient(135deg, #4297F3 0%, #5BA3F5 100%);
		color: #ffffff;
		border: none;
		border-radius: 36rpx;
		font-size: 28rpx;
		font-weight: 600;
		box-shadow: 0 8rpx 20rpx rgba(66, 151, 243, 0.3);
		transition: all 0.3s ease;
	}

	.add-btn:active {
		transform: translateY(2rpx);
		box-shadow: 0 4rpx 12rpx rgba(66, 151, 243, 0.3);
	}
</style>