// packageE/groupCode/groupCodeRecord/groupCodeRecord.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    shareBox: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },
  del(e) {
    let id = e.currentTarget.dataset.id;
    console.log(id);
    this.dels(id);
  },
  edit(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageE/groupCode/groupCodeEdit/groupCodeEdit?id=" + id,
    });
  },
  codeData(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageE/groupCode/groupCodeData/groupCodeData?id=" + id,
    });
  },
  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);
          },
        });
      },
    });
  },
  CloseBtn() {
    this.setData({
      shareBox: false,
    });
  },
  shareBox(e) {
    let sid = e.currentTarget.dataset.id;
    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: sid,
        ingress: "weChatApplet",
      },
      success: (resdata) => {
        var res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          console.log(res);
          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);
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      wx.showToast({
        title: "没有更多了",
        icon: "none",
        duration: 1000,
      });
      return;
    } else {
      let pages = this.data.page + 1;
      this.setData({
        page: pages,
      });
      this.getMoreData();
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    let ind = e.target.dataset.id;
    console.log(ind);
    if (ind) {
      console.log(this.data.list[ind].code_name);
      return {
        title: this.data.list[ind].code_name,
        path:
          "/packageE/groupCode/groupCodeLabel/groupCodeLabel?id=" +
          this.data.list[ind].id,
      };
    }
  },
  dels(kid) {
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.group-code.api.group-code.deleted-group-code"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        id: kid,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          wx.showToast({
            title: res.msg,
            icon: "none",
            success: function () {
              that.getData();
            },
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
      },
    });
  },
  getData() {
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.group-code.api.group-code.my-group-code"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        page: this.data.page,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            list: res.data.list.data,
            current_page: res.data.list.current_page,
            last_page: res.data.list.last_page,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
      },
    });
  },
  getMoreData() {
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.group-code.api.group-code.my-group-code"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        page: this.data.page,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            list: res.data.list.data,
            current_page: res.data.list.current_page,
            last_page: res.data.list.last_page,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
      },
    });
  },
});
