<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="box" style="margin: 20rpx;padding-top: 10px;">
      <view class="box_item" style="margin-bottom: -10px;">
        <view class="f1">{{ info.bt || '-' }}</view>
      </view>
      <view class="box_item" style="display: flex;flex-direction: column;align-items: flex-start;">
        <view class="f2" style="color:#969696;margin-bottom: 10px;">{{ info.fl || '-' }} · {{ formatTime(info.tssj) }}</view>
        <view class="f2" v-html="info.nr"></view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data(){
    return {
      info: {},
      base_url: 'https://hhjj.antissoft.com'
    }
  },
  onLoad(options){
    const cached = uni.getStorageSync('xxts_item');
    let id = options.id;
    if(!id && cached){
      try{ id = JSON.parse(cached).ff || JSON.parse(cached).ID || JSON.parse(cached).F_Id }catch(e){}
    }
    this.fetchDetail(id);
	this.xgxxwyd(cached);
  },
  methods:{
	// 已读消息
	xgxxwyd(data){
		if (!data) {
			console.warn('xgxxwyd: data is empty');
			return;
		}
		try {
			var info = JSON.parse(data);
			console.log("更新前 info:", info);
			// 只更新必要字段，避免覆盖其他数据
			const updateData = {
				ff: info.ff || info.ID || info.F_Id,
				sfyd: '是',
				ydsj: new Date().toISOString() // 设置阅读时间
			};
			console.log("更新数据:", updateData);
			this.API.xgxxwyd(updateData).then(res=>{
				console.log("已读更新成功", res);
			}).catch(err => {
				console.error("已读更新失败:", err);
			});
		} catch(e) {
			console.error("解析消息数据失败:", e, data);
		}
	},
    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');
      const s = String(d.getSeconds()).padStart(2,'0');
      return `${Y}-${M}-${D} ${h}:${m}:${s}`;
    },
    fetchDetail(id){
      if(!id){
        uni.showToast({ icon:'none', title:'缺少消息ID' });
        return;
      }
      const user = uni.getStorageSync('userinfo') || {};
      const n = user.id || 1763367257;
      this.API.xxtsjlxq({ id, n }).then(res=>{
        if(res && res.code === 200){
          this.info = res.data || {};
        }else{
          this.info = {};
        }
      }).catch(()=>{
        this.info = {};
      })
    }
  }
}
</script>

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