<template>
	<view>
		<!-- 固定导航栏 -->
		<view class="custom-navbar" :style="{backgroundColor: theme.bg}">
			<view class="navbar-content" :style="{paddingTop: statusBar + 'px', color: theme.text, height: `calc(${statusBar}px + 55px)`}">
				<text class="navbar-title">{{i18n.newWord.info}}</text>
			</view>
		</view>
		
		<!-- 占位空间 -->
		<view :style="{height: `calc(${statusBar}px + 55px)`}"></view>
		
		<z-paging :language="$i18n.locale" ref="paging" v-model="list" @query="queryList" :pageSize="10"
		:style="'background-color: '+theme.bg+';color: '+theme.text" :use-page-scroll="true"
		:loading-more-enabled="true" :empty-view-text="i18n.newWord.noData || '暂无数据'">
			<!-- 下拉刷新 -->
			<custom-refresher slot="refresher" slot-scope="{refresherStatus}" :status="refresherStatus">
			</custom-refresher>

			<!-- 文章列表 -->
			<view class="article-list">
				<view class="article-item" v-for="(item,index) in list" :key="index">
					<view class="article-time">{{formatTime(item.addtime)}}</view>
					<view class="article-content">{{item.title}}</view>
				</view>
			</view>
		</z-paging>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				theme: '',
				statusBar: uni.getSystemInfoSync().statusBarHeight,
			}
		},
		computed: {
			i18n() {
				return this.$t('message');
			},
		},
		onLoad() {
		},
		onShow() {
			if (uni.getStorageSync("theme") == "dark") {
				this.theme = this.$store.state.darkTheme
			} else {
				this.theme = this.$store.state.lightTheme
			}
		},
		methods: {
			formatTime(timeStr) {
				// 如果已经是格式化的字符串，直接返回
				if (typeof timeStr === 'string' && timeStr.includes('-')) {
					const date = new Date(timeStr.replace(/-/g, '/'));
					return this.formatDate(date);
				}
				// 如果是时间戳
				if (typeof timeStr === 'number') {
					const date = new Date(timeStr * 1000);
					return this.formatDate(date);
				}
				return timeStr;
			},
			formatDate(date) {
				const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
				const month = months[date.getMonth()];
				const day = date.getDate();
				const year = date.getFullYear();
				const hours = String(date.getHours()).padStart(2, '0');
				const minutes = String(date.getMinutes()).padStart(2, '0');
				const seconds = String(date.getSeconds()).padStart(2, '0');
				
				return `${month} ${day}, ${year} ${hours}:${minutes}:${seconds}`;
			},
			queryList(pageNo, pageSize) {
				this.post('/index/all_zixun', {
					page: pageNo,
					page_num: pageSize
				}).then(res => {
					if (res.code == 1) {
						console.log(res)
						this.$refs.paging.complete(res.data.data);
					} else {
						// 请求失败
						this.$refs.paging.complete(false);
					}
				}).catch(err => {
					console.error('请求失败', err);
					// 请求异常
					this.$refs.paging.complete(false);
				})
			},
		}
	}
</script>

<style lang="scss">
	.custom-navbar {
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 999;
	}
	
	.navbar-content {
		display: flex;
		align-items: center;
		padding-left: 32rpx;
		padding-right: 32rpx;
	}
	
	.navbar-title {
		font-size: 36rpx;
		font-weight: bold;
	}

	.article-list {
		padding: 20rpx 32rpx;
	}

	.article-item {
		margin-bottom: 40rpx;
		padding-bottom: 40rpx;
		border-bottom: 1rpx solid #F0F0F0;
		
		&:last-child {
			border-bottom: none;
		}
	}

	.article-time {
		font-size: 24rpx;
		color: #999999;
		margin-bottom: 20rpx;
		line-height: 1.5;
	}

	.article-content {
		font-size: 28rpx;
		color: #333333;
		line-height: 1.6;
		text-align: justify;
	}
</style>
