const { baseUrl } = require('../../../request');
import { globalImgUrl } from '../../../request';
import { decodeBase64  } from '../../../utils/decodebase64'

Page({
  data: {
    projectDetail: {},
    notificationList: [],
    settlementDetailList: [],
    promoCodeList: [],
    activeTab: 0,
    id: null,
    showPromoPop: false,
    currentQrcode: '',
    currentPromoLink: '',
    globalImgUrl,
    settlementDesc: '',
    projectDesc: '',
    projectFlow: '',
    projectDescription: '',
  },

  onLoad(options) {
    // console.log('---->',options);
    const id = options.id;
    const isPromo = options.isPromo;
    console.log(isPromo)
    this.setData({ id ,activeTab: Number(isPromo) });
  },

   // 每次页面展示都刷新（含navigateBack返回时）
   onShow() {
    // 防止id为null
    if (this.data.id) {
      this.fetchProjectDetail(this.data.id);
    }
  },

  fetchProjectDetail(id) {
    const token = wx.getStorageSync('token');
    wx.request({
      url: baseUrl + '/project/query/id', // 替换为你的接口地址
      method: 'POST',
      header: {
        Authorization: token
      },
      data: {
          id
      },
      success: (res) => {
        console.log('项目详细---->',res.data.data);
        if (res.data.code === 1) {
          const detail = res.data.data || {};
          this.setData({
            projectDetail: detail,
            notificationList: detail.projectNotificationVOList || [],
            settlementDetailList: detail.projectAllDetailVOList || [],
            promoCodeList: detail.promoCodeApplyVOList || [],
            settlementDesc: decodeBase64(detail.settlementDesc),
            projectDesc: decodeBase64(detail.projectDesc),
            projectFlow: decodeBase64(detail.projectFlow),
            projectDescription: decodeBase64(detail.projectDescription)
          });
        } else {
          wx.showToast({
            title: res.data.message || '数据获取失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  switchTab(e) {
    const idx = +e.currentTarget.dataset.index;
    this.setData({ activeTab: idx });
  },

  // “开码记录”跳tab
  goToPromoTab() {
    this.setData({ activeTab: 1 });
  },

  // “新增推广码”跳转
  addPromoCode() {
    const id = this.data.id;
    const promoCodeDesc = this.data.projectDetail.applyPromoCodeDesc || '';
    // encodeURIComponent 防止有特殊字符
    wx.navigateTo({
      url: `/pages/projectModule/applyCode/applyCode?id=${id}&desc=${encodeURIComponent(promoCodeDesc)}&mode=add`
    });
  },

  // 跳转结算明细
  gotoSettlement(e) {
    const projectId = e.currentTarget.dataset.id;
    const url = e.currentTarget.dataset.url;
    const name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: `/pages/projectModule/settlement/settlement?id=${projectId}&picurl=${url}&name=${name}`,
    })
  },

  gotoSubSettlement(e) {
    const promoId = e.currentTarget.dataset.id;
    const image = e.currentTarget.dataset.image
    const name = e.currentTarget.dataset.name
    wx.navigateTo({
      url: `/pages/projectModule/subSettlement/subSettlement?id=${promoId}&image=${image}&name=${name}`,
    })
  },


  // 推广码-查看资料
  goToPromoMaterial(e) {
    const id = e.currentTarget.dataset.id;    // 项目id
    const name = e.currentTarget.dataset.name; // 业务员姓名
    const phone = e.currentTarget.dataset.phone; // 业务员手机号
    const promoCodeDesc = this.data.projectDetail.applyPromoCodeDesc || '';
    wx.navigateTo({
     url: `/pages/projectModule/applyCode/applyCode?id=${id}&desc=${encodeURIComponent(promoCodeDesc)}&mode=view&name=${encodeURIComponent(name)}&phone=${encodeURIComponent(phone)}`
    });
  },

// 弹出作业码弹窗
onShowPromoPop(e) {
  const qrcode = e.currentTarget.dataset.qrcode;
  const link = e.currentTarget.dataset.link;
  this.setData({
    showPromoPop: true,
    currentQrcode: qrcode,
    currentPromoLink: link
  });
},

// 关闭弹窗
onClosePromoPop() {
  this.setData({ showPromoPop: false });
},


  // 复制推广码
  copyPromoCode(e) {
    const code = e.currentTarget.dataset.code;
    wx.setClipboardData({
      data: code,
      success: function () {
        wx.showToast({
          title: '复制成功',
          icon: 'success'
        });
      }
    });
  },

  goToNotificationDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/projectModule/projectNotice/projectNotice?id=${id}` // 路径请根据你的项目实际调整
    });
  }
  
});
