const navigationBarHeight = (getApp().statusBarHeight + 50) + 'px'
let config = require("../../config/config.js");
let Http = require("../../utils/HttpBasics");
let app = getApp();
const imgurl = require("../../utils/imgurl");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navigationBarHeight,
    addUrl: imgurl.add.url,
    carList: []
  },
  initUsrCarList: function() {
    var that = this;
    // 绑定车获取
    Http.get({
      url: config.api.getUserCarList,
      data: {}
    }).then(res => {
      that.setData({
        carList: res.data
      });
    })
    .catch(err => {
      wx.showToast({
        title: err.errMsg,
        icon: 'none',
        duration: 2000,
        mask: false
      });
    })
  },
  unbindCarBtn: function(e) {
    var that = this;
    var carNum = e.currentTarget.dataset.car;
    that.unbindCar(carNum);
  },

  unbindCar: function(carNum) {
    var that = this;
    var postData =
      app.globalData.parkVendor == 1 ? {
        etcpToken: app.globalData.etcpToken,
        carNumber: carNum
      } : {
        carNumber: carNum
      };
    Http.post({
        url: config.api.unbindCar,
        data: postData
      })
      .then(res => {
        that.initUsrCarList();
        wx.showModal({
          title: "提示",
          showCancel: false,
          content: "解绑车牌成功！",
          success: function() {}
        });
      })
      .catch(error => {
        wx.showModal({
          title: "提示",
          showCancel: false,
          content: "解绑车牌失败！",
        });
      });
  },
  bindCar: function(carNum) {
    var that = this;
    // ETCP
    var etcpData = {
      etcpToken: app.globalData.etcpToken,
      carNumber: carNum
    };
    var tjdData = {
      carNumber: carNum
    };
    var postData = app.globalData.parkVendor == 1 ? etcpData : tjdData;
    Http.post({
        url: config.api.bindCar,
        data: postData
      })
      .then(res => {
        that.setData({
          addCar: null
        });
        that.initUsrCarList();
        wx.showModal({
          title: "提示",
          showCancel: false,
          content: "绑车牌成功！",
          success: function() {}
        });
      })
      .catch(error => {
        wx.showModal({
          title: "提示",
          showCancel: false,
          content: error.data.message,
          success: function() {}
        });
      });
  },
  jumpToAdd: function() {
    wx.navigateTo({
      url: `/pages/addPark/addPark?flags=managepalte`
    });
  },
  onShow: function(options) {
    var that = this;
    that.initUsrCarList();

    if (that.data.addCar) {
      // 绑车牌
      if (app.globalData.carLogin) {
        that.bindCar(that.data.addCar);
      } else {
        that.bindCar(that.data.addCar);
      }
      that.setData({
        addCar: null
      });
    }
  },
})