// packageH/newRetail/newRetail_agencyPackage/newRetail_agencyPackage.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    isShowPage: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.salesId && options.NRLevelId) {
      // wx.showToast({
      //   title: `${options.salesId},,,${options.NRLevelId}`,
      //   duration: 1000,
      //   icon: 'none'
      // });
      this.gotoOrder();
    } else {
      this.setData({
        isShowPage:true
      });
      this.getData();
    }
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.new-retail.frontend.index.getSalesRecord');
    app._postNetWork({
      url: urlStr,
      data: {
        id: this.options.id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          if (res.data.remaining_time) {
            let time = app.getTimeDifference(res.data.remaining_time);
            that.setData({
              remaining_time: time
            });
          }
          that.setData({
            info: res.data
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.msg);
      }
    });
  },
  gotoOrder() {
    let url = "/packageD/buy/myOrder_v2/myOrder_v2?tag=newRetail";
    wx.navigateTo({
      url: url + "&goodsId=0&optionsId=0&total=0&retailState=5&NRLevelId=" + this.options.NRLevelId + "&sales_id=" + this.options.salesId
    });
  },
  ImgPreview() {
    let currentUrl = this.data.info.url;
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: [currentUrl] // 需要预览的图片http链接列表
    });
  },
  saveImg() {
    // var _this = this;
    // var times = _this.data.timeend - _this.data.timestart;
    wx.getSetting({
      success(res) {
        if (!res.authSetting["scope.writePhotosAlbum"]) {
          wx.authorize({
            scope: "scope.writePhotosAlbum",
            success() {
              console.log("授权成功");
            },
          });
        }
      },
    });
    var imgSrc = this.data.info.small_url;
    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 () {

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    return {
      title: this.data.info.goods[0].goods_title,
      imageUrl: this.data.info.goods[0].goods_thumb,
      path: "/packageD/buy/myOrder_v2/myOrder_v2?tag=newRetail&goodsId=0&optionsId=0&total=0&retailState=5&NRLevelId=" + this.data.info.level_id + "&sales_id=" + this.options.id + "&mid=" + mid,
    };
  },
});