// packageE/groupCode/groupCodeShow/groupCodeShow.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    group_code: {},
    showOne: false,
    showTwo: false,
    shareBox: false,
    openAuthData: null,
    showAuthDialog: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id,
      });
    }
    if (options.label) {
      this.setData({
        label: options.label,
      });
    }
    if(options.mid){
      app._setMid(options.mid);
    }
    this.getData();
  },
  shareBox(e) {
    wx.showLoading({
      title: "加载中",
    });
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.group-code.api.group-code.get-share-poster"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        id: this.data.id,
        ingress: "weChatApplet",
      },
      success: (resdata) => {
        var res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          that.setData({
            minImga: res.data.image_url,
            base: res.data.base64Image,
            shareBox: true,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
      },
    });
  },
  taplong(e) {
    wx.previewImage({
      current: this.data.info.code_url_img, // 当前显示图片的http链接
      urls: [this.data.info.code_url_img], // 需要预览的图片http链接列表
    });
  },
  explain() {
    this.setData({
      showTwo: true,
    });
  },
  service() {
    this.setData({
      showOne: true,
    });
  },
  CloseBtn() {
    this.setData({
      showOne: false,
      showTwo: false,
      shareBox: false,
    });
  },
  tapSave(){
    var imgSrc = this.data.minImga;
    wx.previewImage({
      current: imgSrc, // 当前显示图片的http链接
      urls: [imgSrc] // 需要预览的图片http链接列表
    });
  },
  saveImg() {
    wx.getSetting({
      success(res) {
        if (!res.authSetting["scope.writePhotosAlbum"]) {
          wx.authorize({
            scope: "scope.writePhotosAlbum",
            success() {
              console.log("授权成功");
            },
          });
        }
      },
    });
    var imgSrc = this.data.minImga;
    wx.downloadFile({
      url: imgSrc,
      success: function (res) {
        console.log(res);
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.showToast({
              title: "保存成功",
              icon: "none",
              duration: 2000,
            });
          },
          fail: function (err) {
            console.log(err);
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              console.log("当初用户拒绝，再次发起授权");
              wx.openSetting({
                success(settingdata) {
                  if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                    wx.showToast({
                      icon: "none",
                      title: "获取权限成功,再次长按保存图片",
                      duration: 1500,
                    });
                  } else {
                    wx.showToast({
                      icon: "none",
                      title: "获取权限失败,保存图片需要用户授权",
                      duration: 1500,
                    });
                  }
                },
              });
            }
          },
          complete(res) {
            console.log(res);
          },
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

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

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let pages = getCurrentPages();
    if(pages[pages.length - 3] && pages[pages.length - 3].route == "packageA/detail_v2/detail_v2"){
      // 前第二个页面时商品详情的话，返回的时候直接返回详情页
      wx.navigateBack({
        delta: 1,
      });
      return;
    }
    // if (!this.data.label) {
    //   wx.navigateTo({
    //     url: '/packageE/groupCode/groupCode',
    //   });
    //   return
    // }
  },

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.info.group_code.code_name,
      path:
        "/packageE/groupCode/groupCodeLabel/groupCodeLabel?id=" + this.data.id,
    };
  },
  getData() {
    let urlStr = app.getNetAddresss(
      "plugin.group-code.api.group-code.get-group-code-data"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        id: this.data.id,
        label: this.data.label,
      },
      success: (resdata) => {
        var res = resdata.data;
        // 没有进群权限
        if (res.data && res.data.status) {
          let openAuthData = res.data.auth_data;
          this.setData({
            openAuthData:  openAuthData,
            showAuthDialog: true
          });
          return;
        }

        if (res.result == 1) {
          if(res.data.data.group_code && res.data.data.group_code.explain_content) {
            res.data.data.group_code.explain_content = res.data.data.group_code.explain_content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block" ');
          }
          this.setData({
            info: res.data.data,
            group_code: res.data.data.group_code || {}
          });
          wx.setNavigationBarTitle({
            title: res.data.data.group_code.code_name,
          });
        } else {
          wx.showModal({
            title: "提示",
            content: res.msg,
            showCancel: false,
            success (res) {
              if (res.confirm) {
                let pages = getCurrentPages();
                if (pages.length <= 1) {
                  wx.reLaunch({
                    url: '/packageG/index/index',
                  });
                } else {
                  wx.navigateBack({ //返回
                    delta: 1
                  });
                }
              }
            },
          });
        }
      },
    });
  },

  CloseAuthDialog () {
    this.setData({
      showAuthDialog: false
    });
    wx.navigateBack({
      delta: 1,
    });
  },

  toHome () {
    wx.reLaunch({
      url: '/packageG/index/index',
    });
  },

  toMemberGradeList () {
    wx.redirectTo({
      url: '/packageB/member/MemberGradeList/MemberGradeList',
    });
  },

  toGroupCodeGoodsList () {
    wx.navigateTo({
      url: '/packageE/groupCode/groupCodeGoodsList/groupCodeGoodsList?id=' + this.data.id,
    });
  },
});
