<template>
	<view class="main">
		<view class="toptitle">
			<view class="toptitle_left" @click="ht">
				<u-icon name="arrow-left" color="#fff" size="20"></u-icon>
			</view>
			<view class="toptitle_title">
				信息推送
			</view>
		</view>
		<view class="" style="margin-bottom: 10px;">
			<u-notice-bar :text="text1"></u-notice-bar>
		</view>
		<view class="box" v-for="(item, index) in xxtsList" :key="index" @click="openDetail(item)">
			<view class="l1">
				<!-- '../../../static/img/head.jpg' -->
				<image style="width: 80%;border-radius: 1000px" src="/static/head.png" mode="widthFix"></image>
			</view>
			<view class="center">
				<view class="f1">
					{{ item.bt }}
				</view>
				<view class="f2">
					{{ item.fl }} · {{ formatTime(item.tssj) }}
				</view>
			</view>
			<view class="r1">
				<view v-if="item.sfyd !== '是'" style="border: 1px solid red;border-radius: 10000px;width: 12px;height: 12px;background-color: red;"></view>
			</view>
		</view>
		<u-empty
		        v-if="!loading && xxtsList.length === 0"
		        mode="message"
		        icon="http://cdn.uviewui.com/uview/empty/message.png"
		>
		</u-empty>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				text1: '请大家多多关心消息内容',
				xxtsList: [],
				loading: false,
				form:{
					currentPage: 1,
					pageSize: 20,
					sort: 'desc',
					sidx: '',
					jsr:""
				},
				nv_length: 0,
			}
		},
		onShow(){
			this.form.currentPage = 1
			this.nv_length = 0
			this.xxtsList = []
			this.fetchXxtsList();
		},
		onReachBottom() {
			var page = Math.ceil(this.nv_length / this.form.pageSize);
			if (page > parseInt(this.form.currentPage)) {
				this.form.currentPage = parseInt(this.form.currentPage) + 1
				this.fetchXxtsList(true);
			} else {
				uni.showToast({
					icon: "error",
					title: "没有更多内容"
				})
			}
		},
		methods:{
			ht(){
				uni.navigateBack({
				    delta: 1
				});
			},
			formatTime(ts){
				if(!ts) return '';
				const d = new Date(ts);
				const Y = d.getFullYear();
				const M = String(d.getMonth()+1).padStart(2,'0');
				const D = String(d.getDate()).padStart(2,'0');
				const h = String(d.getHours()).padStart(2,'0');
				const m = String(d.getMinutes()).padStart(2,'0');
				return `${Y}-${M}-${D} ${h}:${m}`;
			},
			fetchXxtsList(append = false){
				if (this.loading) return;
				this.loading = true;
				const userInfo = uni.getStorageSync('userinfo') || {};
				this.form.jsr = userInfo.userId || userInfo.id;
				// 确保不传递 sfyd 参数，返回所有消息（包括已读和未读）
				// 显式构建 params 对象，只包含需要的字段，避免意外包含 sfyd
				const params = {
					currentPage: this.form.currentPage,
					pageSize: this.form.pageSize,
					sort: this.form.sort,
					sidx: this.form.sidx || '',
					jsr: this.form.jsr
					// 明确不包含 sfyd 字段
				};
				// 双重保险：确保 params 对象中没有 sfyd 字段
				if ('sfyd' in params) {
					delete params.sfyd;
				}
				// 检查 form 对象是否被意外修改
				if (this.form.hasOwnProperty('sfyd')) {
					console.warn('警告: form 对象包含 sfyd 字段:', this.form.sfyd);
				}
				console.log('获取消息列表参数:', JSON.stringify(params));
				console.log('form 对象:', JSON.stringify(this.form));
				this.API.xxtsjl(params).then(res=>{
					console.log('消息列表响应:', res);
					if (res && res.code === 200 && res.data) {
						this.nv_length = res.data.pagination ? res.data.pagination.total : 0;
						const rows = Array.isArray(res.data.list) ? res.data.list : [];
						console.log(`获取到 ${rows.length} 条消息，总数: ${this.nv_length}`);
						if (!append) {
							this.xxtsList = [];
						}
						for(let i=0;i<rows.length;i++){
							this.xxtsList.push(rows[i]);
						}
						console.log(`当前列表共有 ${this.xxtsList.length} 条消息`);
						if (this.xxtsList.length === 0 && !append) {
							uni.showToast({
								icon: 'none',
								title: '暂无消息'
							});
						}
					} else {
						console.warn('消息列表响应异常:', res);
						if (!append) {
							this.xxtsList = [];
						}
						uni.showToast({
							icon: 'none',
							title: res?.msg || '加载失败'
						});
					}
				}).catch(err => {
					console.error('获取消息列表失败:', err);
					if (!append) {
						this.xxtsList = [];
					}
					uni.showToast({
						icon: 'none',
						title: '加载失败，请稍后重试'
					});
				}).finally(()=>{
					this.loading = false;
				});
			},
			confirm(e){
				console.log("11",e);
				this.title = e.value[0]
				this.show = false
				this.keyword = ''
			},
			openDetail(item){
				console.log(item);
				try{
					uni.setStorageSync('xxts_item', JSON.stringify(item));
				}catch(e){}
				const id = item.ff || item.ff || item.ff;
				if(id){
					uni.navigateTo({
						url: `/pages/new/xxts/detail?id=${id}`
					})
				}else{
					uni.navigateTo({
						url: `/pages/new/xxts/detail`
					})
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	@import 'index.scss';
</style>
			