// pages/evaluate/evaluate.js
import { get, postwithoutahth , upload } from "../../common/request.js"
import pathmap from "../../common/pathmap"
import stylemap from "../../common/stylemap"
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    getbrand:pathmap.getbrands,
    serieslist:pathmap.carseries,
    windowh: app.globalData.windowh,
    area:{},
    areaselect:{},
    series:{},
    selectseries:{},
    date:"2018-01-01",
    datename:["2018","1"],
    detail:{},
    describe:[{
      dis:"无瑕疵\n无色差",
      int:"无磨损\n无异味",
      mil:"4万公里以内",
      maintain:"无事故维修"
    },{
      dis:"无色差\n个别瑕疵",
      int:"轻微磨损\n无异味",
      mil:"15万公里以内",
      maintain:"无事故\n少量维修"
    },{
      dis:"轻微色差\n少量瑕疵",
      int:"个别严重磨损",
      mil:"20万公里以内",
      maintain:"有碰撞事故\n少量维修"
    }],
    showeva:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData(stylemap);
    this.getCararea(0,0,0);
    //this.getCarSeries(0,0,0);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  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);
  //   }
  // },
  getMileage:function(e){
    var mileage = e.detail.value;
    if(mileage>10){
      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) {
    var value = e.detail.value;
    var values = value.split("-")
    this.setData({
      date: e.detail.value,
      datename:values
    })
  },
  getMobile:function(e){
    var mobile = e.detail.value;
    this.setData({mobile:mobile});
  },
  subEvaluate:function(){
    var _self=this;
    var title = "";
    // if(!this.data.mobile&&/^[1][3|4|5|6|7|8|9][0-9]{9}$/.test(this.data.mobile)){
    //   title="请填写有效手机号"
    // }
    if(!this.data.model){
      wx.showToast({
        title:"请选择车型！",
        icon:"none"
      });
      return;
    }
    postwithoutahth(pathmap.estimatedprice,{
      mobile:this.data.mobile,
      model_types:this.data.model.car_model_id,
      car_name:this.data.model.car_model_name,
      license_time:this.data.date,
      mileage:this.data.mileage,
      area:this.data.area['1'][this.data.areaselect['1']].area_id,
      city_name:`${this.data.area['0'][this.data.areaselect['0']].area_name} ${this.data.area['1'][this.data.areaselect['1']].area_name}`,
    },function(json){  

      if(json.data.status==1){        
        wx.showToast({
          title:"估价成功!"
        }); 
        var obj = {
          "excellent":0,
          "good":1,
          "normal":2
        }      
        _self.setData({
          detail:json.data,
          showeva:true,
          currenteva:obj[json.data.default_car_condition]
        })
      }else{
        wx.showToast({
          title:"估价失败！",
          icon:"none"
        });
      }
    })
  },
  switchTab: function (e) {
    this.setData({
      currenteva:e.detail.current
    })
  },
  swatchEva:function(e){
    this.setData({
      currenteva:e.currentTarget.dataset.index
    })
  },
  evaluateBack:function(e){
    this.setData({
      showeva:false
    })
  },
  showSeriesSelect:function(){
    this.setData({brandlistshow:true});
  },
  closeBrandList:function(){
    this.setData({brandlistshow:false});
  },
  selectModel:function(e){
    var model = e.detail.model;
    this.setData({
      brandlistshow:false,
      model:model
    })
  }
})