// packageE/others/skin/skin.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    showExample: false,
    showAgreement: false,
    showTips: false,
    showUpload: false,
    progress: "98%",
    tranImg: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/example.png",
    kFootshow: true,
    //
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },
  tabrshowbtn(e) {
    let bol = e.detail;
    if (bol) {
      this.setData({
        kFootshow: false,
      });
    }
  },
  checkboxinp(e) {
    console.log(e);
    let sta = e.detail;
    this.setData({
      checked: sta,
    });
  },
  goband() {
    wx.navigateTo({
      url: "/packageE/others/rank/rank",
    });
  },
  goLog() {
    wx.navigateTo({
      url: "/packageE/others/log/log",
    });
  },
  tap_showExample() {
    this.setData({
      showExample: !this.data.showExample,
    });
  },
  posterClose() {
    this.setData({
      showExample: false,
    });
  },
  posterClose_b() {
    console.log("执行");
    this.setData({
      showAgreement: false,
    });
  },
  tap_showAgreement() {
    this.setData({
      showAgreement: !this.data.showExample,
    });
  },
  tap_showTips() {
    this.setData({
      showTips: false,
    });
  },
  tapUpload() {
    if (this.data.set.is_open == 1) {
      if (!this.data.checked) {
        wx.showToast({
          title: "请勾选测肤协议",
          icon: "none",
          duration: 1000,
        });
        return;
      }
    }

    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        console.log(res);
        let is_size = Number(res.tempFiles[0].size);
        if (is_size < 1024) {
          is_size = Number(is_size).toFixed(2) + "b";
        } else if (is_size >= 1024) {
          is_size = Number(is_size / 1024).toFixed(2) + "kb";
        }
        that.setData({
          size: is_size,
          showUpload: true,
          tranImg: res.tempFilePaths[0],
        });
        // that.uploadImg(tempFilePaths[0])
        let urlStr = app.getNetAddresss("upload.uploadPic");
        const uploadTask = wx.uploadFile({
          url: urlStr, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: "file",
          success(res) {
            let data = res.data;
            data = JSON.parse(data);
            console.log(data);
            that.uploadImg(data.data.img);
            // wx.navigateTo({
            //   url: '/packageE/others/skin_result/skin_result',
            // })
          },
        });
        // 监听进度
        uploadTask.onProgressUpdate((res) => {
          that.setData({
            progress: res.progress,
          });
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getData() {
    wx.showLoading({
      title: "加载中",
    });
    let that = this;
    let urlStr = app.getNetAddresss("plugin.skin-check.api.skin-check.index");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        wx.hideLoading({
          complete: (res) => {},
        });
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            set: res.data.set.treaty_data,
            customizeName: res.data.customizeName,
            rank_list: res.data.rank_list,
          });
          try {
            wx.setNavigationBarTitle({
              title: res.data.customizeName,
            });
          } catch (error) {
            console.log(error);
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.hideLoading({
          complete: (res) => {},
        });
      },
    });
  },
  uploadImg(img) {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.skin-check.api.skin-check.skin-check"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        img,
        is_check: that.data.checked ? 1 : 0,
      },
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          if (app._isTextEmpty(res.data.checkAuth)) {
            wx.navigateTo({
              url:
                "/packageE/others/skin_result/skin_result?id=" +
                res.data.reportId,
            });
            that.setData({
              showUpload: false,
            });
          } else {
            wx.showToast({
              title: res.data.checkAuth,
              icon: "none",
              duration: 1000,
            });
          }
        } else {
          if (app._isTextEmpty(res.data)) {
            if (!app._isTextEmpty(res.data.checkAuth)) {
              that.setData({
                showUpload: false,
                tranImg: false,
                showTips: true,
                checkAuth: res.data.checkAuth,
              });
            } else {
              that.setData({
                showUpload: false,
                tranImg: false,
                showTips: true,
                checkAuth: false,
              });
            }
          } else {
            wx.showToast({
              title: res.msg,
              icon: "none",
              duration: 1000,
              success: function () {
                setTimeout(() => {
                  that.setData({
                    showUpload: false,
                    tranImg: false,
                  });
                }, 1000);
              },
            });
          }
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
});
