// index.js
const app = getApp()
const db = wx.cloud.database({});
const cont = db.collection('Vote');


//const { envList } = require('../../envList.js');

Page({
  onLoad: function (options) {
    console.log(options)
     },
        
  data: {
    inputShowed: false, //初始文本框不显示内容
    vote: [],
    lookserch: false,
    search: null,
    listsearch: [],
  },
  // 获取用户输入值
  onChange(e) {
    let that = this
    if (e.detail == "") {
      that.setData({
        search: null,
        lookserch: false,
        listsearch: []
      })
    } else {
      that.setData({
        search: e.detail,
      })
    }
  },
  // 搜索投票
  onSearch() {
    let that = this
    if (that.data.search == null) {
      wx.showToast({
        title: '请输入关键词',
        icon: 'none'
      })
    }
    // 毛搜索书籍
    else {
      wx.showLoading({
        title: '搜索中',
      })
      // 根据名称搜索
      db.collection("Vote").where({ //毛搜索
          votename: db.RegExp({ //name 搜索的字段
            regexp: that.data.search, //regexp搜索值
            options: 'i',
          })
        }).get()
        .then(res => {
          that.setData({
            listsearch: that.data.listsearch.concat(res.data)
          })
          //  根据详情搜索
          db.collection("Vote").where({ //毛搜索
              votedetail: db.RegExp({
                regexp: that.data.search,
                options: 'i',
              })
            }).get()
            .then(res => {
              if (that.data.listsearch.length != 0) {
                for (var i = 0; i < res.data.length; i++) {
                  let jude = false
                  for (var a = 0; a < that.data.listsearch.length; a++) {
                    if (that.data.listsearch[i]._id == res.data[a]._id) {
                      jude = true
                    }
                  }
                  if (!jude) {
                    that.setData({
                      listsearch: that.data.listsearch.concat(res.data[i])
                    })
                  }
                }
              } else {
                that.setData({
                  listsearch: that.data.listsearch.concat(res.data)
                })
              }
              // 判断是否有所搜结果
              if (that.data.listsearch.length == 0) {
                wx.showToast({
                  title: '无匹配结果',
                  icon: 'none'
                })
              } else {
                that.setData({
                  lookserch: true
                })
              }
              wx.hideLoading()



            })
        })
    }


  },
  onLoad: function (options) {
    var _this = this;
    //2、开始查询数据了 news对应的是集合的名称
    db.collection('Vote').orderBy("time", "desc").get({
      //如果查询成功的话
      success: res => {
        this.setData({
          vote: res.data
        })
      }
    })
  },

  tovotecontent(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/voteContent/index?id=' + id,
    })
  },

  //刷新
  onRefresh() {
    //在当前页⾯显⽰导航条加载动画
    wx.showNavigationBarLoading();
    //显⽰ loading 提⽰框。需主动调⽤ wx.hideLoading 才能关闭提⽰框
    wx.showLoading({
      title: '刷新中...',
    })
    this.getData();
  },
  //⽹络请求，获取数据
  getData() {
    wx.request({
      url: '/pages/index/index',
      //⽹络请求执⾏完后将执⾏的动作
      complete(res) {
        //隐藏loading 提⽰框
        wx.hideLoading();
        //隐藏导航条加载动画
        wx.hideNavigationBarLoading();
        //停⽌下拉刷新
        wx.stopPullDownRefresh();
      }
    })
  },
  /**
   * 页⾯相关事件处理函数--监听⽤户下拉动作
   */
  onPullDownRefresh: function () {
    //调⽤刷新时将执⾏的⽅法
    this.onRefresh();
  },



  search: function () {
    wx.navigateTo({
      url: '../searchPage/index'
    })
  },
  his: function () {
    wx.navigateTo({
      url: '../voteContent/index'
    })

  },
  cs: function () {
    wx.navigateTo({
      url: '../cs/index'
    })

  },




})