// components/carslider.js
import { get,post } from "../../common/request.js"
import stylemap from "../../common/stylemap"
import pathmap from "../../common/pathmap"
var app = getApp()

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    "show":{
      value:false,
      type:Boolean
    },
    "brandrecommend":{
      value:[],
      type:Array
    },
    "hasmodel":{
      value:false,
      type:Boolean
    },
    "getbrand":{
      value:"",
      type:String
    },
    // "serieslist":{
    //   value:"",
    //   type:String
    // }
  },

  /**
   * 组件的初始数据
   */
  data: {
    windowh:app.globalData.windowh
  },
  ready:function(){
    this.getBrands();
    var initialheight = Math.round((app.globalData.windowh-300)/26/app.globalData.scale);
    this.setData(Object.assign({
      initialheight:initialheight
    },stylemap));
    if(!this.data.brandrecommend.length){
      this.loadBrandRecommend();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    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 = 392;
        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({
          brandlist:brandlist
        })
      })
    },
    getBrandDetail:function(e){
      var _self = this;
      var brandinfo = e.currentTarget.dataset;
      var series = [];
      get(pathmap.serieslist, {brand_id:brandinfo.id}, function (json) {
        series = json.data.car_series;      
        _self.setData({
          serieslist:{
            belong:brandinfo,
            list:series
          }
          // ,
          // searchdata:Object.assign(_self.data.searchdata,{brand:brandinfo.id,car_series:""})
        })
      })
    },
    getModel:function(series){
      var _self = this;
      get(pathmap.carmodel,{
        car_series_id:series.car_series_id
      },function(json){
        _self.setData({
          modellist:json.data
        })
      })
    },
    selectSeries:function(e){
      var _self = this;
      var series = e.currentTarget.dataset.series;
      var data = {}
      if(this.data.hasmodel){
        data.showmodel = true;
        this.getModel(series);
      }else{
        data.serieslist = "";
        this.triggerEvent("select",{series:series});
      }      
      this.setData(data);
    },
    selectModel:function(e){
      var _self = this;
      var model = e.currentTarget.dataset.model;
      var data = {serieslist:"",showmodel:false}
      this.triggerEvent("select",{model:model}); 
      this.setData(data);
    },
    filterBack:function(){
      this.triggerEvent("close");
      this.setData({serieslist:""})
    },
    modelBack:function(){      
      this.setData({showmodel:false});
    },
    scrollInit:function(e){
      var init = e.currentTarget.dataset.init;
      this.setData({
        scrolltoinit:init
      })
    },
    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.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;
            scrollinit=(i?i-1:i)
            break;
          }
        }
        _self.setData({scrollinit:scrollinit})
      },50)
    },
    loadBrandRecommend:function(){
      var _self = this;
      get(pathmap.brandrecommend, {
      }, function (json) {
        _self.setData({
          brandrecommend:json.data
        })
      })
    }
  }
})
