// packageE/questionnaire/questionsSearch/questionsSearch.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    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();
  },
  detail(e){
    let item = e.currentTarget.dataset.item;
    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) {
      let timestamp = Date.parse(new Date());
      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",
    });
  },
  tapkey(e){
    console.log(e);
    let key = e.detail.value;
    this.setData({
      _key:key
    });
    this.getData();
  },
  back(e){
    wx.navigateBack({
      delta: 1,
    });
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  getData(){
    let that = this;
    let urlStr = app.getNetAddresss("plugin.questionnaire.api.index.indexInfo");
    app._getNetWork({
      url: urlStr,
      data:{
        index:'index',
        keyword:{
          'title':that.data._key
        },
        page:that.data.page
      },
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            list:res.data.index.list.data,
            current_page:res.data.index.list.current_page,
            last_page:res.data.index.list.last_page,
            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',
        keyword:{
          'title':that.data._key
        },
        page:that.data.page
      },
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            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
          });
          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);
      }
    });
  }
});