// pages/embedPayTest/embedPayTest.js

// import { PayAd } from '../../utils/gtAdUtils/wxSdk.es.js';
// import { PayAd } from '../../utils/gtAdUtils/wxSdk.umd.js';
const GTPayAd = require("../../utils/gtAdUtils/wxSdk.umd.js");
const payAd = new GTPayAd.PayAd("prod");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    trafficId: "", //流量主ID
    orderNumber: "", //订单号
    otherStr: "", //其他参数json字符串
    tipStr: ` { "trafficId": "16sdf7a4b", "orderNumber": "1212121www" }`, ////其他参数json字符串 提示文字
    requestTraceId: '',//请求流水号
    adName: '',//弹窗中的广告名称
  },
  onLoad(options) {
    // 获取本地缓存数据，方便测试
    this.getStorageData();
    this.getAdInfo()      

  },
  // 获取广告配置
  getAdInfo: function () {
    const otherData = this.data.otherStr ? JSON.parse(this.data.otherStr) : "";
    const parms = {
      ...otherData,
      trafficId: this.data.trafficId,
      orderNumber: this.data.orderNumber,
    }
    payAd.getAdsInfo(parms,
      {
        onSuccess: (res) => {
          console.log("获取广告配置成功", res);
          this.setData({
            requestTraceId: res.requestTraceId,
            adName: res.adName
          })
          // 执行后续业务逻辑
        },
        onError: (err) => {
          console.error("获取广告配置失败", err);
          // 执行错误处理逻辑
        },
      }
    );
  },
  // 跳转
  openAdTap: function () {
    // 写入本地缓存数据，方便测试
    this.setStorageData();
    if (!this.data.adName) {
      console.log('暂无广告,停止展示广告流程');
      wx.showToast({
        title: "暂无广告,停止展示广告流程",
        icon: "none",
      });
      return
    }
    if (this.data.orderNumber === "" || this.data.trafficId === "") {
      wx.showToast({
        title: "流量主id和订单号为必填",
        icon: "none",
      });
      return;
    }
    // 根据自己UI需求，进行弹窗自定义
    wx.showModal({
      title: '支付成功',
      content: '您即将进入'+this.data.adName+'页面查看广告',
      complete: (res) => {
        if (res.confirm) {
          this.openAd();
        }
      }
    })
  },
  // 打开广告
  openAd:function(){
    wx.showLoading({
      title: "正在跳转...",
      mask: true,
    });
    setTimeout(() => {
      wx.hideLoading();
    }, 2000);
    const otherData = this.data.otherStr ? JSON.parse(this.data.otherStr) : "";
    const parms =  {
      ...otherData,
      trafficId: this.data.trafficId,
      orderNumber: this.data.orderNumber,
      requestTraceId: this.data.requestTraceId
    }
    console.log('openAd请求参数',parms);
    payAd.showAd(
     parms,
      {
        onSuccess: () => {
          console.log("广告展示成功");
          // 执行后续业务逻辑
        },
        onError: (err) => {
          console.error("广告展示失败", err);
          wx.showToast({
            title: err.msg,
            icon: "none",
            duration: 2000,
          });
          // 执行错误处理逻辑
        },complete:() => {
          wx.hideLoading();
        }
      }
    );
  },
  getStorageData() {
    this.setData({
      trafficId: wx.getStorageSync("trafficId") || "",
      orderNumber: wx.getStorageSync("orderNumber") || "",
      otherStr: wx.getStorageSync("otherStr") || "",
    });
  },
  setStorageData() {
    wx.setStorageSync("trafficId", this.data.trafficId || "");
    wx.setStorageSync("orderNumber", this.data.orderNumber || "");
    wx.setStorageSync("otherStr", this.data.otherStr || "");
  },
});
