import { get,post } from "../../common/request.js"
import stylemap from "../../common/stylemap"
import pathmap from "../../common/pathmap"
import { retCartData } from "../../data/car"
import bindSearch from "../../module/search/search.js"

const app = getApp();
// pages/buy/buy.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowh: app.globalData.windowh,
    list:false,
    limit:10,
    pageindex:0,
    lock:false,
    searchfouce:false,
    selectbar:null,
    hasloadall:false,
    filters:[
      {
        title:"排序",
        name:"sortby"
      },
      {
        title:"品牌",
        name:"brand"
      },
      {
        title:"价格",
        name:"price"
      },
      {
        title:"更多",
        name:"more"
      }
    ],
    sortbymap:[
      {
        title:"默认排序",
        value:""
      },
      {
        title:"价格最低",
        value:"price&asc"
      },
      {
        title:"价格最高",
        value:"price&desc"
      },
      {
        title:"车龄最短",
        value:"car_age&asc"
      },
      {
        title:"里程最少",
        value:"mileage&asc"
      },
      {
        title:"最新发布",
        value:"new&asc"
      }
    ],
    searchdata:{
      sortby:"",
      price:"",
      car_age:"",
      mileage:"",
      transmission:"",
      displacement:"",
      emission:"",
      colour:"",
      filterlock:true
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    bindSearch(this);
    var initialheight = (app.globalData.windowh-300)/26;
    var searchdata = Object.assign(this.data.searchdata,options);
    this.setData(Object.assign({
      initialheight:initialheight
    },stylemap));
    this.getWheres();
    this.getList(this.data.searchdata);
    this.loadBrandRecommend()
  },
  transformSearchData:function(searchdata){
    var para = Object.assign({},searchdata);
    var sortby = para.sortby;
    if(sortby){
      var sortinfo = sortby.split("&");
      para.order_key = sortinfo[0];
      para.order_val = sortinfo[1];
    }
    delete para.sortby;
    return para;
  },
  //获取筛选信息
  getWheres:function(){
    var _self = this;
    get(pathmap.wheres, {
      no_brand_series:1
    }, function (json) {
      _self.setData({
        wheres: json.data
      })
    })
  },
  loadBrandRecommend:function(){
    var _self = this;
    get(pathmap.brandrecommend, {
    }, function (json) {

    })
  },
  getList:function(searchdata){
    var _self = this;
    var para = this.transformSearchData(searchdata);
    para.offset = 0;
    para.limit = this.data.limit;
    if(!this.data.lock){
      this.setData({
        lock:true
      })
      get(pathmap.searchcar, para, function (json) {
        var list = retCartData(json.data&&json.data.list);
        var obj = {
          lock:false,
          list:list,
          searchdata:searchdata,
          offset:_self.data.limit          
        }
        if(json.data.total<=_self.data.limit){
          obj.hasloadall=true;
        }else{
          obj.hasloadall=false;
        }
        _self.setData(obj);
        if(list.length==0){
          _self.loadLikeCar()
        }
      })
    }
  },
  loadListMore:function(){
    var _self = this;
    var para = this.transformSearchData(this.data.searchdata);
    para.offset = this.data.offset;
    para.limit = this.data.limit;
    if(!this.data.lock&&!this.data.hasloadall){
      this.setData({
        lock:true
      })
      get(pathmap.searchcar, para, function (json) {
        var list = retCartData(json.data&&json.data.list);
        var nextoffset = _self.data.offset+_self.data.limit
        var obj = {
          lock:false,
          list:_self.data.list.concat(list),
          offset:nextoffset          
        }
        if(json.data.total<=nextoffset){
          obj.hasloadall=true;
        }else{
          obj.hasloadall=false;
        }
        _self.setData(obj);
      })
    }
  },
  showFilter:function(e){
    var el = e.currentTarget;
    var name = el.dataset.name;
    if(name==this.data.selectbar){
      name=null;
    }
    if(name=="brand"&&!this.data.brandlist){
      this.getBrands();
    }
    this.setData({
      selectbar:name
    })
  },
  getBrands:function(){
    var _self=this;
    get(pathmap.getbrand, {}, function (json) {

      var initiallist = {};
      var brandlist = []
      for(var i = 65; i < 91; i++){
        initiallist[String.fromCharCode(i)] = [];
      }
      json.data.brand.forEach(function(item){
        if(Object.prototype.toString.call(initiallist[item.initial])=="[object Array]"){
          initiallist[item.initial].push(item);
        }
      })
      var inith = 562;
      var scale = app.globalData.scale;
      for(var name in initiallist){
        var l = initiallist[name].length;
        if(l>0){
          brandlist.push({
            initial:name,
            list:initiallist[name],
            top:inith/scale
          });          
          inith = inith+45+l*79;
        }
      }


      _self.setData({wheres:Object.assign(_self.data.wheres,{brandlist:brandlist})})
    })
  },
  changeSort:function(e){
    var el = e.currentTarget;
    var name = el.dataset.sort;
    var searchdata = Object.assign(this.data.searchdata,{sortby:name});
    this.setData({
      selectbar:""
    })
    this.getList(searchdata);
  },
  changePrice:function(e){
    var el = e.currentTarget;
    var id = el.dataset.id;
    var searchdata = Object.assign(this.data.searchdata,{price:id});
    this.setData({
      selectbar:""                      //关闭筛选卡
    })
    this.getList(searchdata);
  },
  loadLikeCar:function(){
    var _self = this;
    if(!this.data.like){
      get(pathmap.like, {
        offset: 0,
        limit: 10
      }, function (json) {
        var list = retCartData(json.data.list)
        _self.setData({
          like: list
        })
      })
    }
  },
  scrollInit:function(e){
    var init = e.currentTarget.dataset.init;
    this.setData({
      scrolltoinit:init
    })
  },
  getBrandDetail:function(e){
    var _self = this;
    var brand = e.currentTarget.dataset.brand;
    var series = [];
    get(pathmap.serieslist, {brand_id:brand.brand_id}, function (json) {
      series = json.data.car_series;      
      _self.setData({
        serieslist:{
          belong:brand,
          list:series
        },
        searchdata:Object.assign(_self.data.searchdata,{brand:brand.brand_id,car_series:""})
      })
    })
    // this.data.wheres.car_series.map(function(item){
    //   if(item.brand_id == brand.brand_id){
    //     series.push(item);
    //   }
    // })
    // this.setData({
    //   serieslist:{
    //     belong:brand,
    //     list:series
    //   },
    //   searchdata:Object.assign(this.data.searchdata,{brand:brand.brand_id,car_series:""})
    // })
    // get(pathmap.series, {
    //   brand_id:brand.brand_id
    // }, function (json) {
    //   var list = retCartData(json.data.list)
    //   _self.setData({
    //     serieslist: json.data
    //   })
    // })
  },
  waitScroll:null,
  brandScroll:function(e) {
    var _self = this;
    //this.setData({ brandScrollTop: e.detail.scrollTop })
    if(this.waitScroll){
      try{
        clearTimeout(this.waitScroll);
      }catch(err){}
    }
    this.waitScroll = setTimeout(function(){      
      var scrollinit = "";
      var list = _self.data.wheres.brandlist;
      for(var i=0,l=list.length;i<l;i++){
        if(e.detail.scrollTop<list[i].top){
          scrollinit = list[i?i-1:i].initial;
          break;
        }
      }
      _self.setData({scrollinit:scrollinit})
    },50)
  },
  selectSeries:function(e){
    var _self = this;
    var series = e.currentTarget.dataset.series;
    this.setData({
      serieslist:"",
      selectbar:""                      //关闭品牌筛选卡
    })
    this.getList(Object.assign(this.data.searchdata,{car_series:series.car_series_id}));
  },
  filterBack:function(){
    this.setData({selectbar:""})
  },
  getMoreFilter:function(e){
    var info = e.currentTarget.dataset;
    var obj = {};
    obj[info.type]=info.value;
    this.setData({searchdata:Object.assign(this.data.searchdata,obj)})
  },
  resetFilter:function(){
    this.setData({searchdata:Object.assign(this.data.searchdata,{      
      car_age:"",
      mileage:"",
      transmission:"",
      displacement:"",
      emission:"",
      colour:""
    })})
  },
  moreSearch:function(){    
    this.setData({selectbar:""})
    this.getList(this.data.searchdata);
  },
  submitsearchword:function(value){    
    this.getList(Object.assign(this.data.searchdata,{keys:value}));
  },
  lockFilter:function(e){
    if(e.detail&&!this.data.filterlock){
      this.setData({
        filterlock:true
      })
    }else if(!e.detail&&this.data.filterlock){
      this.setData({
        filterlock:false
      })
    }
  }
})