<template>
  <view class="policy-detail">

    
    <!-- 详情内容区 -->
    <view class="content-wrap">
      <view class="policy-title">{{ policyDetail.name }}</view>
      <view class="policy-time">生效时间:{{ policyDetail.effectiveDate }}</view>
      
      <!-- 富文本内容 -->
      <view class="policy-content">
        <rich-text :nodes="policyDetail.context"></rich-text>
      </view>
    </view>
  </view>
</template>

<script>
import { PolicyApi } from '@/api/policy/index.js'

export default {
  data() {
    return {
      id: '', // 政策ID
      policyDetail: {
        name: '',
        createTime: '',
        context: ''
      }
    }
  },
  onLoad(options) {
    if (options.id) {
      this.id = options.id
      this.getPolicyDetail()
    }
  },
  methods: {
    // 获取详情数据
    async getPolicyDetail() {
      try {
        const res = await PolicyApi.getPolicy(this.id)
        if (res.code === 0) {
          this.policyDetail = {
            ...res.data,
            effectiveDate: this.formatTime(res.data.effectiveDate),
            // 处理富文本内容,确保样式正确显示
            context: this.formatRichText(res.data.context)
          }
        }
      } catch (error) {
        uni.showToast({
          title: '获取详情失败',
          icon: 'none'
        })
      }
    },
    // 格式化时间
    formatTime(time) {
      if (!time) return ''
      const date = new Date(time)
      return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
    },
    // 处理富文本内容
    formatRichText(html) {
      if (!html) return ''
      // 添加样式以确保图片自适应
      return html.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;"')
    },
    // 返回上一页
    handleBack() {
      uni.navigateBack()
    }
  }
}
</script>

<style lang="scss">
.policy-detail {
  height: 100vh;
  background: #fff;


  .nav-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 88rpx;
    background: #fff;
    display: flex;
    align-items: center;
    padding: 0 30rpx;
    border-bottom: 1rpx solid #eee;
    z-index: 100;
    
    .back-btn {
      font-size: 28rpx;
      color: #333;
    }
    
    .title {
      flex: 1;
      text-align: center;
      font-size: 32rpx;
      font-weight: bold;
      padding-right: 60rpx;
    }
  }
  
  .content-wrap {
    padding: 30rpx 30rpx 30rpx;
    
    .policy-title {
      font-size: 36rpx;
      font-weight: bold;
      color: #333;
      margin-bottom: 20rpx;
    }
    
    .policy-time {
      font-size: 24rpx;
      color: #999;
      margin-bottom: 40rpx;
    }
    
    .policy-content {
      font-size: 28rpx;
      line-height: 1.8;
      color: #333;
      
      :deep(img) {
        max-width: 100%;
        height: auto;
      }
      
      :deep(p) {
        margin-bottom: 20rpx;
      }
    }
  }
}
</style>