import { get,post } from "../../common/request.js"
import stylemap from "../../common/stylemap"
import share from "../../common/share"
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(Object.assign({

  /**
   * 页面的初始数据
   */
  data: {
    
    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&desc"
      },
      {
        title:"里程最少",
        value:"mileage&asc"
      },
      {
        title:"最新发布",
        value:"new&desc"
      }
    ],
    searchdata:{
      sortby:"",
      price:"",
      keys:"",
      car_age:"",
      mileage:"",
      transmission:"",
      displacement:"",
      emission:"",
      colour:"",
      filterlock:true
    },
    keylist:[],    
    morebtheight:app.globalData.isIpx?120:98
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    bindSearch(this);
    //initialheight = initialheight-initialheight%2;
    var searchdata = Object.assign(this.data.searchdata,options);
    this.setData(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;
    var wheres = wx.getStorageSync("wheres");
    if(wheres){
      _self.setData({
        wheres: wheres
      })
    }else{
      get(pathmap.wheres, {
        no_brand_series:1
      }, function (json) {
        _self.setData({
          wheres: json.data
        })
        wx.setStorageSync("wheres",json.data);
      })
    }
  },
  loadBrandRecommend:function(){
    var _self = this;
    get(pathmap.brandrecommend, {
    }, function (json) {
      _self.setData({
        brandrecommend:json.data
      })
    })
  },
  getList:function(searchdata){
    var _self = this;
    var para = this.transformSearchData(searchdata);
    if (searchdata.keys){
      searchdata.keylist = searchdata.keys.split(/\W+/);
    }
    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 key = _self.data.searchdata.keys.replace(/(^\s*)|(\s*$)/g, ""); 
        var keylist = key?key.split(/\s+/):[];
        var obj = {
          lock:false,
          list:list,
          searchdata:searchdata,
          offset:_self.data.limit,
          keylist: keylist        
        }
        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;
    }
    this.setData({
      selectbar:name
    })
  },
  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
        })
      })
    }
  },
  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);
  },
  lockFilter:function(e){
    if(e.detail&&!this.data.filterlock){
      this.setData({
        filterlock:true
      })
    }else if(!e.detail&&this.data.filterlock){
      this.setData({
        filterlock:false
      })
    }
  },
  submitsearchinfo:function(obj){
    this.getList(Object.assign(this.data.searchdata,obj));
    this.setData({searchshow:false})
  },
  closeBrandList:function(){
    this.setData({selectbar:""});
  },
  filterBack:function(){
    this.setData({selectbar:""});
  },
  selectSeries:function(e){
    var series = e.detail.series;
    this.setData({
      selectbar:""
    })
    this.getList(Object.assign(this.data.searchdata,{car_series:series.car_series_id}));
  },
  checkAll:function(e){
    this.getList({
      sortby:"",
      price:"",
      car_age:"",
      mileage:"",
      transmission:"",
      displacement:"",
      emission:"",
      colour:"",
      filterlock:true
    });
  },
  deletekey:function(e){
    var index = e.currentTarget.dataset.index;
    this.data.keylist.splice(index,1)
    var keys = this.data.keylist.join(" ");
    this.submitsearchinfo({keys:keys});
  }
},share))