// packageE/questionnaire/index/index.js
var app = getApp();
var int = app.globalData.statusBarHeight * 2 + 20;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: int,
    posterShow: false,
    navShow: true,
    page: 1,

    plugin_name:'问卷调查'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var amun = '';
    // wx.getSystemInfo({
    //   success(res){
    //     console.log(res.statusBarHeight)
    //     amun = (res.statusBarHeight * 2)+20
    //   }
    // })
    // this.setData({
    //   statusBarHeight:amun
    // })
    this.getData();
  },
  goSearch(e) {
    wx.navigateTo({
      url: "/packageE/questionnaire/questionsSearch/questionsSearch",
    });
  },
  tapshow(e) {
    this.setData({
      posterShow: true,
    });
  },
  goBanner(e) {
    console.log(e.currentTarget.dataset.link);
    let src = e.currentTarget.dataset.link;
    try {
      wx.navigateTo({
        url: src,
      });
    } catch (err) {
      console.log(err);
    }
  },
  back(e) {
    wx.navigateBack({
      delta: 1,
    });
  },
  posterClose(e) {
    this.setData({
      posterShow: false,
    });
  },
  detail(e) {
    let item = e.currentTarget.dataset.item;
    let timestamp = Date.parse(new Date());
    let resSteam = wx.getSystemInfoSync();
    if(resSteam.platform == 'ios'){
      item.start_time = item.start_time.replace(/\./g, "/");
      item.end_time = item.end_time.replace(/\./g, "/");
    }
    if (item.start_time && item.end_time) {
      if (timestamp < Date.parse(new Date(item.start_time))) {
        wx.showToast({
          title: `${this.data.plugin_name}未开始`,
          icon:"none"
        });
        return;
      }
      if (timestamp > Date.parse(new Date(item.end_time))) {
        wx.showToast({
          title: `${this.data.plugin_name}已结束`,
          icon:"none"
        });
        return;
      }
    }
    wx.navigateTo({
      url:
        "/packageE/questionnaire/questions/questions?id=" + item.id + "&tag=index",
    });
  },
  gohistory(e) {
    wx.navigateTo({
      url: "/packageE/questionnaire/history/history",
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _pages = getCurrentPages();
    console.log(_pages[_pages.length - 1].route);
    try {
      if (
        _pages[_pages.length - 1].route ===
        "packageE/questionnaire/surveyResult/surveyResult"
      ) {
        this.setData({
          navShow: false,
        });
      }
    } catch (err) {
      console.log(err);
    }
  },

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoading) {
      let pages = this.data.page + 1;
      this.setData({
        page: pages,
      });
      this.getMoreData();
    } else {
      return;
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.questionnaire.api.index.indexInfo");
    app._getNetWork({
      url: urlStr,
      data: {
        index: "index",
        page: that.data.page,
      },
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            info: res.data.index,
            list: res.data.index.list.data,
            current_page: res.data.index.list.current_page,
            last_page: res.data.index.list.last_page,
            link: res.data.index.link,
            plugin_name:res.data.plugin_name
          });
          if (that.data.current_page >= that.data.last_page) {
            that.setData({
              isLoading: false,
            });
          } else {
            that.setData({
              isLoading: true,
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getMoreData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.questionnaire.api.index.indexInfo");
    app._getNetWork({
      url: urlStr,
      data: {
        index: "index",
        page: that.data.page,
      },
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            info: res.data.index,
            list: that.data.list.concat(res.data.index.list.data),
            current_page: res.data.index.list.current_page,
            last_page: res.data.index.list.last_page,
            link: res.data.index.link,
          });
          if (that.data.current_page >= that.data.last_page) {
            that.setData({
              isLoading: false,
            });
          } else {
            that.setData({
              isLoading: true,
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
});
