// pages/sell/sell.js
import { get,post,postwithoutahth,upload } from "../../common/request.js"
import pathmap from "../../common/pathmap"
import stylemap from "../../common/stylemap"
import share from "../../common/share"
// import bindLog from "../../module/logs/logs"
const app = getApp();

Page(Object.assign({

  /**
   * 页面的初始数据
   */
  data: {  
    getbrand:pathmap.getbrands,
    getseries:pathmap.carseries,
    
    area:{},
    areaselect:{},
    series:{},
    brandlistshow:false,
    //selectseries:{},
    imgssp:["","","",""],
    imgs:[],
    date:"2018-01-01"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    //bindLog(this);
    this.setData(stylemap);
    this.getCararea(0,0,0);
    //this.getCarSeries(0,0,0);
    try{
      var authorization = wx.getStorageSync("authorization");
    }catch(e){

    }
    // if(!authorization){      
    //   this.openLog();
    // }
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getCararea:function(id,index,sel){
    var _self=this;
    sel = sel||0;
    get(pathmap.cararea,{pid:id},function(json){
      var obj = {},select={};
      obj[index] = json.data;
      select[index] = sel;
      _self.setData({
        area:Object.assign({},_self.data.area,obj),
        areaselect:Object.assign({},_self.data.areaselect,select)
      });
      if(index<1){
        _self.getCararea(obj[index][sel]['area_id'],1+parseInt(index),0);
      }
    })
  },
  changeArea:function(e){
    var index = e.currentTarget.dataset.index;
    var select = e.detail.value;
    var obj = {};
    obj[index]=select;
    this.setData({areaselect:Object.assign(this.data.areaselect,obj)});
    if(index<1){
      this.getCararea(this.data.area[index][select]['area_id'],1+parseInt(index),0);
    }
  },
  // getCarModel:function(){
  //   var _self=this;
  //   get(pathmap.carmodel,{pid:id},function(json){
  //     var obj = {},select={};
  //     obj[index] = json.data;
  //     select[index] = sel;
  //     _self.setData({
  //       area:Object.assign({},_self.data.area,obj),
  //       select:Object.assign({},_self.data.areaselect,select)
  //     });
  //     if(index<1){
  //       _self.getCararea(obj[index][sel]['area_id'],1+parseInt(index),0);
  //     }
  //   })
  // },
  // getCarSeries:function(id,index,sel){
  //   var _self=this;
  //   sel = sel||0;
  //   //resule 为借口返回数组对应的属性名
  //   var path="",para={},key = "",resule="";
  //   if(index==0){
  //     path = pathmap.brandlist;
  //     key = "brand_id";
  //     resule = "brand";         
  //   }else if(index==1){
  //     path = pathmap.serieslist;
  //     para = {brand_id:id}; //this.data.series['0'][this.data.selectseries['0']].brand_id}
  //     key = "car_series_id";
  //     resule = "car_series";
  //   }else if(index==2){
  //     path = pathmap.carmodel;
  //     para = {car_series_id:id};  //this.data.series['1'][this.data.selectseries['1']].car_series_id}
  //   }
  //   get(path,para,function(json){
  //     var obj = {},select={};
  //     obj[index] = json.data;
  //     if(resule){
  //       obj[index] = obj[index][resule];
  //     }
  //     select[index] = sel;
  //     _self.setData({
  //       series:Object.assign({},_self.data.series,obj),
  //       selectseries:Object.assign({},_self.data.selectseries,select)
  //     });
  //     if(index<2){
  //       _self.getCarSeries(obj[index][sel][key],1+parseInt(index),0);
  //     }
  //   })
  // },
  // bindSeriesChange:function(e){
  //   var index = e.currentTarget.dataset.index;
  //   var select = e.detail.value;
  //   var obj = {};
  //   var key=""
  //   if(index==0){
  //     key = "brand_id";       
  //   }else if(index==1){
  //     key = "car_series_id";
  //   }
  //   obj[index]=select;
  //   this.setData({selectseries:Object.assign(this.data.selectseries,obj)});
  //   if(index<2){
  //     this.getCarSeries(this.data.series[index][select][key],1+parseInt(index),0);
  //   }
  // },
  uploadImg:function(e){
    var _self=this;
    var index = e.currentTarget.dataset.index;
    wx.chooseImage({
      success: function(res) {
        var tempFilePaths = res.tempFilePaths[0];
        upload(pathmap.uploadimg,tempFilePaths,function(json){
          var imgs = _self.data.imgs;
          imgs.push(JSON.parse(json.data).url);
          //imgs.splice(index,1,JSON.parse(json.data).url);
          _self.setData({imgs:imgs});
        })
      }
    })
  },
  getMileage:function(e){
    var mileage = e.detail.value;
    var sn = mileage.split(".")[1];
    if(mileage>=100||sn&&sn.length>1){
      wx.showToast({
        title:"行驶里程最多为二位整数和一位小数",
        icon:"none"
      })
      this.setData({mileage:this.data.mileage});
    }else{
      this.setData({mileage:mileage});
    }
  },
  initMileage:function(e){
    var mileage = e.detail.value;
    mileage = new Number(mileage).toFixed(1)
    this.setData({mileage:mileage});
  },
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindMobile:function(e){
    var mobile = e.detail.value;
    this.setData({mobile:mobile});
  },
  subSell:function(){
    var _self=this;
    var imgs = this.data.imgs.filter(function(elem){
      return elem!='';
    })
    //var mobile = wx.getStorageSync("mobile")
    postwithoutahth(pathmap.sellingcar,{
      mobile:this.data.mobile,
      car_name:this.data.series.car_series_name,
      plate_at:this.data.date,
      mileage:this.data.mileage,
      city_name:`${this.data.area['0'][this.data.areaselect['0']].area_name} ${this.data.area['1'][this.data.areaselect['1']].area_name}`,
      imgs:imgs.join(',')
    },function(json){
      var obj = {          
        title:json.data.message
      }
      if(json.statusCode!=200){
        obj.icon="none"
      }
      wx.showToast(obj);
    })
  },
  showSeriesSelect:function(){
    this.setData({brandlistshow:true});
  },
  closeBrandList:function(){
    this.setData({brandlistshow:false});
  },
  selectSeries:function(e){
    var series = e.detail.series;
    this.setData({
      brandlistshow:false,
      series:series
    })
  },
  deletePic:function(e){
    var index = e.currentTarget.dataset.index;
    var imgs = this.data.imgs;
    imgs.splice(index,1);
    this.setData({imgs:imgs});
  }
},share))