const app = getApp();
// pages/messageDetail/index.js
import { customRequest } from '../../utils/request.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    iosDialogTobe: false,
    serviceInfo: {},
    pageType: null,
    currentId: null,
    currentInfo: {},
    tagStyle: {
      p: "min-height:24px",
      body: " margin: 0; color: #333; font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-variant: tabular-nums; line-height: 1.8; -webkit-font-feature-settings: 'tnum'; font-feature-settings: 'tnum';",
      h1: " margin-top: 0; margin-bottom: 0.5em;margin-top:0.3em; color: rgba(0, 0, 0, 0.85); font-weight: 500;line-height: 1.1;font-size:24px; ",
      h2: " margin-top: 0; margin-bottom: 0.5em;margin-top:0.3em; color: rgba(0, 0, 0, 0.85); font-weight: 500;line-height: 1.1;font-size:21px; ",
      h3: " margin-top: 0; margin-bottom: 0.5em;margin-top:0.3em; color: rgba(0, 0, 0, 0.85); font-weight: 500;line-height: 1.1;font-size:16px; ",
      h4: " margin-top: 0; margin-bottom: 0.5em;margin-top:0.3em; color: rgba(0, 0, 0, 0.85); font-weight: 500;line-height: 1.1 ",
      h5: " margin-top: 0; margin-bottom: 0.5em;margin-top:0.3em; color: rgba(0, 0, 0, 0.85); font-weight: 500;line-height: 1.1 ",
      h6: " margin-top: 0; margin-bottom: 0.5em;margin-top:0.3em; color: rgba(0, 0, 0, 0.85); font-weight: 500;line-height: 1.1 ",
      strong: "font-weight: bolder;",
      b: "font-weight: bolder;",
      dl: "margin-top: 0; margin-bottom: 1em;",
      ol: "margin-top: 0; margin-bottom: 1em;",
      ul: "margin-top: 0; margin-bottom: 1em;",
      li: "line-height: 1.8",
      hr: "box-sizing: content-box;height:1px;overflow: visible;background-color: #ddd;border:0;",
      table: "border-collapse: collapse;border: 1px solid #d9d9d9;margin: 18px 0 16px 0;line-height: 1.8",
      td: "border: 1px solid #d9d9d9;vertical-align: top;padding: 4px 8px;line-height: 1.8",
    },
    ctx: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    console.log(options);
    if (options.type) {
      this.setData({
        pageType: options.type,
      });
    }
    if (options.id) {
      this.setData({
        currentId: options.id,
      });
      this.requestInfo();
    }
    var ctx = this.selectComponent("#article");
    this.setData({
      ctx,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {
    // var ctx = this.selectComponent("#article");
    // this.setData({
    // 	ctx,
    // });
    // ctx.navigateTo('anchor').then(() => {
    //   console.log('跳转成功')
    // }).catch(err => {
    //   console.log('跳转失败：', err)
    // })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    setTimeout(() => {
      this.setData({
        serviceInfo: app.globalData.serviceInfo,
      });
    }, 10);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () { },
  goTobe (e) {
    console.log(e);
    if (e.detail.href == "pages/tobeAgency/index") {
      if (app.globalData.loginUserInfo.agencyStatus == 1) {
        wx.showToast({
          title: "您已加入伯才",
          icon: "none",
        });
        return;
      }
    }

    wx.navigateTo({
      url: "/" + e.detail.href,
    });
  },
  /**
   * 用户点击右上角分享
   */
onShareAppMessage() {
  return app.sharePageImage()
},
  requestInfo () {
    let that = this;
    wx.showLoading({
      title: "正在获取内容",
    });
    customRequest("/bocai/cms/details/" + this.data.currentId, { header: 'headers', method: 'GET', data: {} }).then((res) => {
      if (res.data.status == 200) {
        // setTimeout(() => {
        // let reg = /<a[^>]*href=['"]([^"]*)['"][^>]*>(.*?)<\/a>/g.exec(res.data.data.content);
        // res.data.data.content = res.data.data.content.replace(reg[0], "<navigator bindtap='showTobe' class='' target='' url='/pages/tobeAgency/index' hover-class='navigator-hover' open-type='navigate'>申请成为代理</navigator>");
        // console.log(reg[0]);

        // res.data.data.content = res.data.data.content.replace('<span style="text-decoration: underline; color: #ffaf00;"><a style="color: #ffaf00; text-decoration: underline;" href="pages/tobeAgency/index" target="_blank" rel="noopener">申请成为代理</a></span>', "<navigator bindtap='showTobe' class='' target='' url='/pages/tobeAgency/index' hover-class='navigator-hover' open-type='navigate'>申请成为代理</navigator>");

        console.log(res.data.data.content);
        that.setData({
          currentInfo: res.data.data,
          html: res.data.data.content.replace(/<!doctype html>/g, ""),
        });
        wx.hideLoading();
        // }, 1000);
        customRequest("/bocai/cms/read/" + that.data.currentId, { header: 'headers', method: 'GET', data: {} }).then((res) => {
          if (res.data.status == 200) {
          }
        })
        // wx.request({
        //   url: app.globalData.ip + "/bocai/cms/read/" + that.data.currentId,
        //   header: app.globalData.headers,
        //   success: function (res) {
        //     console.log(res);
        //     if (res.data.status == 200) {
        //     }
        //   },
        // });
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none",
          duration: 2000,
        });
        wx.hideLoading();
      }
    })
    // wx.request({
    //   url: app.globalData.ip + "/bocai/cms/details/" + this.data.currentId,
    //   header: app.globalData.headers,
    //   success: function (res) {
    //     console.log(res);
    //     if (res.data.status == 200) {
    //       // setTimeout(() => {
    //       // let reg = /<a[^>]*href=['"]([^"]*)['"][^>]*>(.*?)<\/a>/g.exec(res.data.data.content);
    //       // res.data.data.content = res.data.data.content.replace(reg[0], "<navigator bindtap='showTobe' class='' target='' url='/pages/tobeAgency/index' hover-class='navigator-hover' open-type='navigate'>申请成为代理</navigator>");
    //       // console.log(reg[0]);

    //       // res.data.data.content = res.data.data.content.replace('<span style="text-decoration: underline; color: #ffaf00;"><a style="color: #ffaf00; text-decoration: underline;" href="pages/tobeAgency/index" target="_blank" rel="noopener">申请成为代理</a></span>', "<navigator bindtap='showTobe' class='' target='' url='/pages/tobeAgency/index' hover-class='navigator-hover' open-type='navigate'>申请成为代理</navigator>");

    //       console.log(res.data.data.content);
    //       that.setData({
    //         currentInfo: res.data.data,
    //         html: res.data.data.content.replace(/<!doctype html>/g, ""),
    //       });
    //       wx.hideLoading();
    //       // }, 1000);
    //       wx.request({
    //         url: app.globalData.ip + "/bocai/cms/read/" + that.data.currentId,
    //         header: app.globalData.headers,
    //         success: function (res) {
    //           console.log(res);
    //           if (res.data.status == 200) {
    //           }
    //         },
    //       });
    //     } else {
    //       wx.showToast({
    //         title: res.data.msg,
    //         icon: "none",
    //         duration: 2000,
    //       });
    //       wx.hideLoading();
    //     }
    //   },
    // });
  },
  showTobe () {
    this.setData({
      iosDialogTobe: true,
    });
  },
  closeDialog () {
    this.setData({
      iosDialogTobe: false,
    });
  },
});
