//index.js
//获取应用实例
import {get,post} from "../../common/request.js"
import stylemap from "../../common/stylemap"
import share from "../../common/share"
import pathmap from "../../common/pathmap"
import bindTopSource from "../../common/topsource"
import { retCartData} from "../../data/car"
import bindSearch from "../../module/search/search.js"
const app = getApp();

Page(Object.assign({
  data: {    
    banner:[],
    goodcars:[],
    recommended:[],
    like:[],
    searchfouce:false
  },
  onLoad: function () {
    var _self = this;
    //this.testPost();
    this.setData(stylemap);
    this.loadBanner();
    this.loadBrandRecommend();
    this.loadGoodCar();
    this.loadRecommendedCar();
    this.loadLikeCar();
    bindSearch(this);
    //bindTopSource(this);
  },
  loadBanner: function () {
    var _self = this;
    get(pathmap.banner, {
      terminal: 0
    }, function (json) {
      _self.setData({
        banner: json.data
      })
    })
  },

  loadBrandRecommend:function(){
    var _self = this;
    get(pathmap.brandrecommend, {
    }, function (json) {
      _self.setData({
        brandrecommend:json.data
      })
    })
  },
  loadGoodCar:function(){
    var _self = this;
    get(pathmap.goodcar, {
      offset: 0,
      limit:3
    }, function (json) {
      var list = json.data.list;
      list.forEach(function(item){
        item.times = new Date(item.time.replace("-","/")).getTime();
      })
      list.sort(function(a,b){
        return a.times<b.times
      })
      list.forEach(function(item,index){
        if(index%2==1){
          item.labels=["精选"];
        }else{
          item.labels=["热议"];          
        }
      })
      //console.log(JSON.stringify(list));
      if(list.length==1){
        list = list.concat(list,list);
      }
      _self.setData({
        goodcars: list
      })
    })
  },
  loadRecommendedCar:function(e){
    var _self = this;
    get(pathmap.recommended, {
      offset: 0,
      limit: 9
    }, function (json) {
      var list = retCartData(json.data.list)
      _self.setData({
        recommended: list
      })
    })
  },
  loadLikeCar:function(){
    var _self = this;
    get(pathmap.like, {
      offset: 0,
      limit: 10
    }, function (json) {
      var list = retCartData(json.data.list)
      _self.setData({
        like: list
      })
    })
  },
   /*
    切换头部banner自定义指示点
    */
  switchTab: function (e) {
    var banner = this.data.banner;
    banner.forEach(function(item,index){
      item.selected = e.detail.current == index;
    });
    this.setData({
      banner: banner
    });
  },
  /*
    切换今日推荐自定义指示点
    */
  switchReTab:function(e){
    var recommended = this.data.recommended;
    // var i, item;
    // for (i = 0; item = recommended[i]; ++i) {
    //   item.selected = e.detail.current == i;
    // }
    recommended.forEach(function(item,index){
      item.selected = e.detail.current == index;
    })
    this.setData({
      recommended: recommended
    });
  },
  submitsearchinfo:function(obj){
    if(obj){
      var info = "";
      for(var name in obj){
        info+=`${name}=${obj[name]}`;
      }
      wx.navigateTo({ url: `../buy/buy?${info}`});
    }
  },
  testPost:function(){
    post(pathmap.login,{
      mobile:17343011102,
      code:1234
    },{
      "Content-Type":"application/x-www-form-urlencoded"
    },function(){

    })
  },
  catchTouchMove:function(){
    return false
  }
//   /*
//     搜索失去聚焦事件
//    */
//   inputBlur:function(e){
//     inputBlur.call(this,e);
//   },
//   /*
//    触发搜索聚焦事件
//   */
//   searchFocus: function (e) {
//     searchFocus.call(this, e);
//   }
//  /*
//     搜索确定时事件
//    */
  // inputConfirm:function(e){
  //   inputConfirm.call(this,e)
  // },
  // scroll:function(e){
  //   this.setData({
  //     scrolltop: e.detail.scrollTop == 0
  //   })
  // },
  // checkshowout:function(e){

  // },
  // checkshowoutend:function(e){

  // }
},share))
