<template>
  <div class="main" :style="{ paddingBottom: `${safeHeight}px` }">
    <div v-if="showType === 1">
      <div class="pageTitle">{{ detailObj.NOTICEINFO_TITLE || "" }}</div>
      <div class="time">{{ detailObj.START_DATE || "-" }}</div>
      <div class="navList" v-if="detailObj.ImageList && detailObj.ImageList.length > 0">
        <swiper class="swiper" circular
          :indicator-dots="detailObj.ImageList && detailObj.ImageList.length > 2 ? true : false" :autoplay="true"
          indicator-color="rgba(255,255,255,0.6)" indicator-active-color="#ffffff"
          v-if="detailObj.ImageList && detailObj.ImageList.length > 0">
          <swiper-item v-for="(item, index) in detailObj.ImageList" :key="index"
            @click="handleShowPreview(detailObj.ImageList[0].ImageUrl)">
            <view class="swiper-item" :style="{ backgroundImage: `url(${item.ImageUrl})` }"></view>
          </swiper-item>
        </swiper>
      </div>
      <text class="content">{{ detailObj.NOTICEINFO_CONTENT || "" }}</text>
    </div>

    <div class="newContent" v-if="detailObj.content && showType === 2" v-html="detailObj.content"></div>

    <!-- 报名按钮 -->
    <div class="action-section">
      <div class="action-btn" :class="hasApplied ? 'applied-btn' : 'primary-btn'" @click="handleApply">
        <span class="btn-text">{{ hasApplied ? '已报名' : '我要报名' }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      safeHeight: "",
      detailObj: {},
      showType: '',
      id: "",
      hasApplied: false, // 是否已经报名
    };
  },
  onLoad(query) {
    let systemInfo = uni.getSystemInfoSync();
    let height = systemInfo.safeAreaInsets.bottom;
    this.safeHeight = Number(height);

    if (query.id) {
      this.id = query.id
      if (query.type === 'true') {
        this.hanldeGetDetail(query.id);
        this.showType = 1
      } else {
        this.handleGetNewDetail(query.id);
        this.showType = 2
      }
    }
    this.$utils.addUserBehaviorNew();
  },
  onShareAppMessage() {
    return {
      title: `${this.detailObj.NOTICEINFO_TITLE}`,
      path: `/pages/highwayHeadlines/attractInvestmentDetail?id=${this.id}&type=${this.showType}`,
    };
  },
  onShareTimeline() {
    return {
      title: `${this.detailObj.NOTICEINFO_TITLE}`,
      path: `/pages/highwayHeadlines/attractInvestmentDetail?id=${this.id}&type=${this.showType}`,
    };
  },
  methods: {
    // 找到id一样的
    async handleGetNewDetail(id) {
      let start = `${this.$moment
        .now()
        .set("month", -1)
        .format("YYYY-MM-DD")} 00:00:00`;
      let end = `${this.$moment.now().format("YYYY-MM-DD")} 23:59:59`;
      let params = {
        startQueryTime: start, // 开始时间
        endQueryTime: end, // 结束时间
      };
      let time = Math.ceil(new Date().getTime() / 1000);

      let req = {
        app_id: "530000301",
        biz_content: JSON.stringify(params),
        charset: "UTF-8",
        pid: "530000301",
        service: "trawe.eats.etc",
        utc_timestamp: time,
        version: "1.0",
        sign_type: "RSA2",
      };
      let signText = `app_id=${req.app_id}&biz_content=${req.biz_content}&charset=UTF-8&pid=${req.app_id}&service=trawe.eats.etc&utc_timestamp=${req.utc_timestamp}&version=1.0`;

      // 签名
      const sign = await new Promise((resolve, reject) => {
        uni.request({
          url: "https://admin.es.eshangtech.com/prod-api/RSA/sign",
          method: "POST",
          data: {
            originalText: signText,
            privateKey:
              "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCtIkIJLbZYI9NvTRkRbqtjJ73gkAUJZBJw88Z+O8prX2HXMTIedmZZxTwbfM5kGpl9VmS0CY8QL0DnDsgHO8QnSDpN1jiqTD9lk1evycuF7ujmlOk/cJ5x2cLPn9rPIrQn1lxp9mBiO1m9vsrNOze/kx7wRyif/+hvn5Q9KtNEMy0K8oOE5vktnjkk1AhhXrTii9eIjOrO3pRgh9cFA9kqQpvD14CImD7tdCgyntKcYAB7rlin6a2N0yH9JJINjOyOiyHufP8sR7pb9VYXVYXukOCItdC9QQluQhSdyC/3BfZD5zV1A+X7pUle7dDLGG6aSZyCibg16siFIEX94KYfAgMBAAECggEAcP5uwrbGzvdywq06sxnbWTdSwQC9iMd7EaTH4xL6pOD3Zg6Be0givGvxMq9dtA94JUkBKE8kw5dTcHlBKxdiiZCmHK63BM4GgjC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FK/4KmAjH/QN24KXm395Qu8C0UFfgA66yysakJFkvXVBiPg==",
          },
          header: {
            "content-type": "application/json",
          },
          success(res) {
            resolve(res.data.data);
          },
        });
      });

      req.sign = sign;

      // 推文数据
      const tweetsData = await new Promise((resolve, reject) => {
        uni.request({
          url: `https://sxgzh.etclife.cn/etc/api/v1/gzh/point/tweets`,
          method: "POST",
          data: req,
          header: {
            "content-type": "application/x-www-form-urlencoded",
          },
          success(res) {
            resolve(res.data.response.data.data.tweets);
          },
        });
      });

      if (tweetsData && tweetsData.length > 0) {
        tweetsData.forEach((item) => {
          if (Number(item.id) === Number(id)) {
            this.detailObj = item;
          }
        });
      }
    },
    // 预览查看
    handleShowPreview(url) {
      uni.previewImage({
        current: url, // 当前显示图片的http链接
        urls: [url], // 需要预览的图片http链接列表
      });
    },
    async hanldeGetDetail(id) {
      const req = {
        NOTICEINFOId: id,
      };
      const data = await this.$api.$get(
        "/EShangApiMain/Notice/GetNOTICEINFODetail",
        req
      );
      console.log("detail", data);
      const formattedText =
        data.Result_Data.NOTICEINFO_CONTENT.split("\n").join("\n\n");
      this.detailObj = data.Result_Data;
      this.detailObj.NOTICEINFO_CONTENT = formattedText;

      // 格式化时间，去掉时分秒
      if (this.detailObj.START_DATE) {
        this.detailObj.START_DATE = this.detailObj.START_DATE.split(' ')[0];
      }

      console.log("this.detailObj", this.detailObj);

      // 检查是否已经报名
      await this.checkApplyStatus();
    },
    // 立即报名
    handleApply() {
      if (this.hasApplied) {
        uni.showToast({
          title: '您已经报名过该项目',
          icon: 'none',
          duration: 2000
        });
        return;
      }

      uni.navigateTo({
        url: '/pages/highwayHeadlines/investmentApplication'
      });
    },
  },
};
</script>

<style scoped lang="less">
.main {
  width: 100vw;
  height: 100vh;
  box-sizing: border-box;
  padding: 16rpx 32rpx;
  background: #fff;
  overflow-y: scroll;

  .pageTitle {
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 40rpx;
    color: #130f05;
    line-height: 56rpx;
    text-align: left;
    font-style: normal;
  }

  .time {
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 24rpx;
    color: #716f69;
    line-height: 36rpx;
    text-align: left;
    font-style: normal;
    margin-top: 8rpx;
  }

  .navList {
    width: 100%;
    height: 600rpx;
    border-radius: 6rpx;
    overflow: hidden;
    margin: 40rpx 0;

    .swiper {
      width: 100%;
      height: 100%;

      .swiper-item {
        width: 100%;
        height: 100%;
        background-size: cover;
        background-repeat: no-repeat;
        background-position: 50% 50%;
      }
    }
  }

  .content {
    display: block;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 30rpx;
    color: #130f05;
    line-height: 52rpx;
    text-align: justify;
    font-style: normal;
    overflow-y: auto;
  }

  .action-section {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background: white;
    border-top: 1rpx solid #f0f0f0;
    padding: 24rpx 32rpx calc(24rpx + env(safe-area-inset-bottom));
    z-index: 100;

    .action-btn {
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 28rpx;
      border-radius: 40rpx;
      font-size: 32rpx;
      font-weight: 600;
      transition: all 0.3s ease;
      border: 2rpx solid #e5e7eb;

      &.primary-btn {
        background: linear-gradient(135deg, #22c55e, #16a34a);
        color: white;
        border-color: #22c55e;

        &:active {
          opacity: 0.9;
        }

        .btn-text {
          margin-right: 12rpx;
        }

        .btn-icon {
          font-size: 28rpx;
        }
      }

      &.applied-btn {
        background: #f8fafc;
        color: #6b7280;
        border-color: #d1d5db;

        &:active {
          background: #f1f5f9;
        }

        .btn-text {
          margin-right: 12rpx;
        }

        .applied-icon {
          font-size: 24rpx;
          color: #10b981;
        }
      }
    }
  }
}

.main::-webkit-scrollbar {
  display: none;
}
</style>