// pages/home/index.js
const app = getApp();
Component({

  /**
   * 页面的初始数据
   */
  data: {
    adList: [{id:1, title: "垃圾分类，保护地球"},{id:2, title: '垃圾分类，人人有责'}],
    result: "",
    keyvalue: "",
    isSearch: false,
    answer_result: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.getNews();
      this.get_answer_result();
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  methods: {
    cancleW: function(){
      this.setData({
        result: !this.data.result
      })
    },
    answer_action: function(){
      var userInfo = wx.getStorageSync('userInfo');
      if(!userInfo){
        wx.showModal({
          'title': '登录提示',
          content: '请先登录哦',
          cancelColor: 'cancelColor',
          success: function(){
           
          }
        })
        return ;
      }
      wx.navigateTo({
        url: '/pages/problem/index',
      })
    },
    answer_result: function(){
      var userInfo = wx.getStorageSync('userInfo');
      if(!userInfo){
        wx.showModal({
          'title': '登录提示',
          content: '请先登录哦',
          cancelColor: 'cancelColor',
          success: function(){
           
          }
        })
        return ;
      
      }
      wx.navigateTo({
        url: '/pages/problem_log/index',
      })
    },
    get_answer_result: function(){
      var that = this;
      var userId = wx.getStorageSync('userInfo')['id'];
      app.data.utils.request({
        url: app.data.api.answers_result + userId,
          data: {},
          method: 'get',
          success: (data) => {
            console.log(data)
            that.setData({
              answer_result: (data.length > 0? true: false)
            })
          }
        })
    },
    setKeyword: function(e){
      this.setData({
        keyvalue: e.detail.value
      })
    },
    search: function(){
      var userInfo = wx.getStorageSync('userInfo');
      if(!userInfo){
        wx.showModal({
          'title': '登录提示',
          content: '请先登录哦',
          cancelColor: 'cancelColor',
          success: function(){
           
          }
        })
        return ;
      }
     this.setData({
       isSearch: true
     })
      this.postGarbage(this.data.keyvalue);
    },
    getNews() {
      console.log("NEWS")
      wx.request({
        url: app.data.api.news,
        dataType: 'json',
        method: 'get',
        data: {},
        success: (res) => {
            if(res.statusCode == 200){
              this.setData({
                adList: res.data.data
              })
            }
        }
      })
    },
    postGarbage(keyword) {
      var userInfo = wx.getStorageSync('userInfo');
       wx.request({
         url: app.data.api.garbage1,
         dataType: 'json',
         method: 'post',
         data: {
           keyword: keyword,
           user_id: userInfo.id
           
         },
         success: (response) => {
             if(response.statusCode == 200){
               console.log(response.data.data);
               if(response.data.data.length <= 0){
                 this.setData({
                   result: "",
                 })
                 return;
               }
               // name data code
               var name = "";
               console.log(response.data.data[0].data);
               if(response.data.data[0].data.search("干垃圾") != -1){
                name = "ava";
             }
               if(response.data.data[0].data.search("其他") != -1){
                  name = "other";
               }
               if(response.data.data[0].data.search("可回收") != -1){
                name = "ava";
              }
             if(response.data.data[0].data.search("有害") != -1){
              name = "harm";
              }
              if(response.data.data[0].data.search("厨余") != -1){
                name = "dinner";
             }
               this.setData({
                 result:name
               });
             }
         }
       });
   
     }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})