// packageE/healthy/healthyEv/healthyEv.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    posterFlag: false,
    explainFlag: false,
    agreement: false,
    evaluationData: {},
    poster_url: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let health_id = options.health_id;
    this.getData(health_id);
    this.getPoste(health_id);
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  getData(health_id) {
    let urlStr = app.getNetAddresss(
      "plugin.health-assessment.api.index.getData"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: true,
      data: {
        health_id,
        art: "getEvaluation",
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
          return;
        }
        console.log(res.data.getEvaluation.top);
        this.setData({
          evaluationData: res.data.getEvaluation.top,
        });
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },

  onChange(evt) {
    this.setData({
      agreement: evt.detail,
    });
  },
  gotoRecord() {
    wx.navigateTo({
      url: "/packageE/healthy/healthyRecord/healthyRecord",
    });
  },
  setPosterFlag() {
    this.setData({
      posterFlag: !this.data.posterFlag,
    });
  },
  setExplainFlag() {
    this.setData({
      explainFlag: !this.data.explainFlag,
    });
  },
  gotoPaper() {
    let evaluationData = this.data.evaluationData;
    if (evaluationData.is_evaluation == 1) {
      wx.showToast({
        icon: "none",
        title: "你好，本周你已测评过了",
        duration: 1500,
      });
      return false;
    }
    if (evaluationData.is_evaluation_agreement == 1) {
      if (!this.data.agreement) {
        wx.showToast({
          icon: "none",
          title: "请先阅读同意测评协议",
          duration: 1500,
        });
        return false;
      }
    }

    console.log(evaluationData);
    wx.navigateTo({
      url:
        "/packageE/healthy/healthyPaper/healthyPaper?health_id=" +
        evaluationData.health_id,
    });
  },
  getPoste(health_id) {
    let urlStr = app.getNetAddresss(
      "plugin.health-assessment.api.index.getData"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        art: "getPoster",
        health_type: "1",
        health_id,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) {
          return;
        }

        this.setData({ poster_url: res.data.getPoster.url_path });
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  async checkWritePotosAlbum() {
    var _this = this;
    // var times = _this.data.timeend - _this.data.timestart;
    let writePhotosAlbum = await this.getAuthSetting("scope.writePhotosAlbum");
    if (!writePhotosAlbum) {
      wx.showModal({
        title: "请开启授权",
        content: "是否跳转设置页开启授权",
        success(res) {
          if (res.confirm) {
            wx.openSetting({
              success(settingdata) {
                if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                  _this.saveImg();
                } else {
                  console.log("还是没授权");
                }
              },
            });
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
      return;
    }
    this.saveImg();
  },
  saveImg() {
    console.log("执行");
    var imgSrc = this.data.poster_url;
    wx.downloadFile({
      url: imgSrc,
      success: (res) => {
        console.log(res);
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: (data) => {
            wx.showToast({
              title: "保存成功",
              icon: "none",
              duration: 2000,
            });
            this.setPosterFlag();
          },
          fail: function (err) {
            console.log(err);
          },
        });
      },
    });
  },
  getAuthSetting(authName) {
    return new Promise((resolve, reject) => {
      wx.getSetting({
        success(res) {
          if (!res.authSetting[authName]) {
            wx.authorize({
              scope: authName,
              success() {
                resolve(true);
              },
              fail() {
                resolve(false);
              },
            });
          } else resolve(true);
        },
        fail() {
          resolve(false);
        },
      });
    });
  },
});
