// pages/cardetail/cardetail.js


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

const app = getApp();

Page(Object.assign({

  /**
   * 页面的初始数据
   */
  data: {
    windowh: app.globalData.windowh,
    collocatetype:[{
      label:"基本参数",
      name:"jiben"
    },{
      label:"发动机参数",
      name:"fadongji"
    },{
      label:"底盘及制动",
      name:"dipan"
    },{
      label:"安全配置",
      name:"anquan"
    },{
      label:"外部配置",
      name:"waibu"
    },{
      label:"内部配置",
      name:"neibu"
    },{
      label:"操控/辅助配置",
      name:"caokong"
    }],
    imagemap:{
      "事故排查":"accident",
      "轻微碰撞":"checkcollide",
      "易损耗部件":"wastage",
      "常用功能":"comonuse",
      "启动检测":"startcheck"
    },
    openscheme:false,
    loanscale:[0.1,0.2,0.3,0.4,0.5,0.6],
    loantime:[12,24,36],
    scaleindex:0,
    timeindex:0,
    interestrate:0.003,
    timetorate:{
      "12":"rates_a",
      "24":"rates_b",
      "36":"rates_c"
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData(Object.assign({id:options.id},stylemap));
    this.getDetail();
    bindLog(this);
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  loginback:function(){
    this.getDetail();
  },
  getDetail:function(){
    var _self=this;
    get(pathmap.cardetail,{id:this.data.id},function(json){
      if(json.statusCode==200){
        var detail = json.data;
        var time = detail.first_plate_at.split('-');
        /*数据结构处理*/
        detail.imgs = detail.imgs&&detail.imgs.split(",");
        detail.imgslength = detail.imgs.length;
        detail.first_plate_at = time[0]+"/"+time[1];
        detail.check_info.forEach(function(item){
          item.passn = 0;
          item.unpassn = 0;
          item._child.forEach(function(item2){
            item2.passn = 0;
            item2.unpassn = 0;
            item2._child.forEach(function(item3){
              if(item3.state){
                item2.passn++;
                item.passn++;
              }else{
                item2.unpassn++;
                item.unpassn++;                
              }
            })
          })
        })
        _self.setData({
          detail:detail
        })
      }else{        
        if(json.data.message){
          wx.showToast({
            title:json.data.message,
            icon:"none",
            duration:2000
          })
        }
      }
    })
  },
  switchTab:function(e){
    this.setData({
      currentimg: e.detail.current+1
    });
  },
  swCollocate:function(e){
    var name = e.currentTarget.dataset.name;
    var collocatetype = this.data.collocatetype.map(function(item){
      if(item.name==name){
        if(item.opened){
          item.opened=false;
        }else{
          item.opened=true;
        }
      }else{
        item.opened=false;
      }
      return item;
    })
    this.setData({collocatetype:collocatetype});
  },
  collectCar:function(){
    var _self=this;
    if(this.data.detail.is_collect=="0"){
      post(pathmap.collect,{
        id:this.data.id
      },function(json){
        if(json.statusCode==200){
          wx.showToast({
            title:json.data.message
          });
          _self.setData({detail:Object.assign(_self.data.detail,{is_collect:'1'})});        
        }else{
          wx.showToast({
            title:json.data.message,
            icon:"none"
          });
        }
      },null,function(){
        _self.openLog();
      })
    }else{
      post(pathmap.collectdel,{
        id:this.data.id
      },function(json){
        if(json.statusCode==200){
          wx.showToast({
            title:json.data.message
          });
          _self.setData({detail:Object.assign(_self.data.detail,{is_collect:'0'})});        
        }else{
          wx.showToast({
            title:json.data.message,
            icon:"none"
          });
        }
      },null,function(){
        _self.openLog();
      })
    }
  },
  appointmentBuy:function(){
    var _self=this;
    var mobile = wx.getStorageSync("mobile");
    post(pathmap.reserve,{
      "car_source_id":this.data.id,
      mobile:mobile
    },function(json){
      var obj = {
        title:json.data.message
      }
      if(json.statusCode!=200){
        obj.icon="none"
      }
      wx.showToast(obj);
    },null,function(){
      _self.openLog();
    })
  },
  openScheme:function(){
    var _self=this;
    var mobile = wx.getStorageSync("mobile");
    if(mobile){
      this.setData({openscheme:true});
      if(!this.data.rates_a){
        get(pathmap.rates,{
          mobile:mobile
        },function(json){
          _self.setData(json.data);
          _self.getPay(0,0);
        })
      }
    }else{
      this.openLog();
    }
  },
  closeScheme:function(){
    this.setData({openscheme:false});
  },
  getPay:function(scaleindex,timeindex){
    var price = this.data.detail.price*10000;
    var scale = this.data.loanscale[scaleindex];
    var time = this.data.loantime[timeindex];
    var interestrate = this.data[this.data.timetorate[time]]/100;
    var firstpay = price*scale;
    var premonthpay = (price-firstpay)*interestrate*(Math.pow((1+interestrate),time))/(Math.pow((1+interestrate),time)-1);
    this.setData({
      firstpay:firstpay,
      premonthpay:Math.round(premonthpay),
      scaleindex:scaleindex,
      timeindex:timeindex
    })
  },
  selectScale:function(e){
    var index = e.currentTarget.dataset.n;
    this.getPay(index,this.data.timeindex)
    //this.setData({scaleindex:index});
  },
  selectTime:function(e){
    var index = e.currentTarget.dataset.n;
    this.setData({timeindex:index});
    this.getPay(this.data.scaleindex,index)
  },
  callPhone:function(){
    if(this.data.detail.custom_tel){
      wx.makePhoneCall({
        phoneNumber:this.data.detail.custom_tel
      })
    }else{
      wx.showToast({
        title:"缺少手机号码",
        icon:"none",
        duration:2000
      })
    }
  }
},logs))