// pages/ucenter/car/chooseCar.js

var selectPopup;
var idx;
var plateNumber;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    textArray : ["设为默认","使用此车牌号","修改车牌号","删除车牌号"],
    textArrayColor : ["#576B95","#576B95","#576B95","#576B95"],
    userCarList: []
  },
  showMore(e){
    idx = e.currentTarget.dataset.idx;
    plateNumber = e.currentTarget.dataset.plateNumber;
    if(idx.defaultType==0){
      this.setData({
        textArray : ["设为默认","使用此车牌号","修改车牌号","删除车牌号"],
      })
    }else{
      this.setData({
        textArray : ["使用此车牌号","修改车牌号","删除车牌号"],
      })
    }
    selectPopup.show(this.data.textArray,this.data.textArrayColor);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    selectPopup = this.selectComponent("#selectpopup");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    let userInfo = wx.getStorageSync('userInfo');
		wx.request({
		  url: getApp().globalData.postHeadAgreement + '/restapi/wechat/carList?loginName=' + userInfo.loginName,
		  // data: {
			// loginName: userInfo.loginName
		  // },
		  method: 'POST',
		  success: function (res) {
        console.info(res)
        that.setData({
          userCarList: res.data
        })
		  },
		  fail(e) {
			console.log("查询车辆失败");
			log.info('[查询车辆]','[fail]', e);
		  }
		});
  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  bindCar(){
    let url = '/pages/ucenter/car/chooseAndBindCar'
    wx.navigateTo({
      url
    });
  },
  selectPopupItemClick:function(e){
    let that = this;
    if((!idx) && idx!=0){
      console.info("idx is null!!!")
      return;
    }
    let index = e.detail[0]
    let textArrVal = this.data.textArray[index]
    // ["设为默认","修改车牌号","删除车牌号"],
    let userInfo = wx.getStorageSync('userInfo');
    if(textArrVal=="设为默认"){
      console.info("设为默认")
      wx.request({
        url: getApp().globalData.postHeadAgreement + '/restapi/wechat/carAlter?loginName=' + userInfo.loginName,
        data: {
          defaultType: 1,
          carId: idx.carId,
          plateNumber: idx.plateNumber
        },
        method: 'POST',
        success: function (res) {
          console.info(res)
          that.onShow()
        },
        fail(e) {
        console.log("查询车辆失败");
        log.info('[查询车辆]','[fail]', e);
        }
      });
    }else if(textArrVal=="修改车牌号"){
      console.info("修改车牌号")
      console.info(idx)
      let url = '/pages/ucenter/car/bindCar?carId=' + idx.carId + '&plateNumber=' + idx.plateNumber + '&defaultType=' + idx.defaultType
      wx.navigateTo({
        url
      });
    }else if(textArrVal=="删除车牌号"){
      console.info("删除车牌号")
      wx.showModal({
        title: '提示',
        content: '确定要删除当前车辆信息？',
        confirmText: '确定',
        showCancel:true,
        confirmColor:'#00AADD',
        cancelColor:'#00AADD',
        success: function (res1) {
          if(!res1.confirm){
            return;
          }
          wx.request({
            url: getApp().globalData.postHeadAgreement + '/restapi/wechat/carRemove?loginName=' + userInfo.loginName + '&carId=' + idx.carId,
            // data: {
            // loginName: userInfo.loginName
            // },
            method: 'POST',
            success: function (res) {
                console.info(res)
                if(res.data.code==200){
                    console.log("删除车辆成功");
                    that.onShow()
                }else{
                    wx.showModal({
                      title: '提示',
                      content: res.data.msg,
                      showCancel: false,
                      complete: (res) => {
                        if (res.cancel) {
                          
                        }
                        if (res.confirm) {
                          
                        }
                      }
                    })
                }

              // that.setData({
              //   userCarList: res.data
              // })
            },
            fail(e) {
            console.log("删除车辆失败");
            log.info('[删除车辆]','[fail]', e);
            }
          });
        }
      });
    }else if(textArrVal=="使用此车牌号"){
        /*
        
        {
            carId: 4168
            createBy: "yawuga"
            createTime: "2023-08-11 20:41:36"
            defaultType: 0
            params: {}
            plateNumber: "232321111"
            remark: null
            searchValue: null
            status: 0
            sysUser: {searchValue: null, createBy: null, createTime: "2019-05-30 13:34:38", updateBy: null, updateTime: null, …}
            updateBy: "yawuga"
            updateTime: "2023-08-11 20:45:12"
        }
        
        */
       console.info(idx)
        wx.setStorageSync("chooseCar_plateNumber", idx);
        wx.navigateBack();
    }
    
    // console.log("选择的item下标："+e.detail[0])
    // console.log("组件标识："+e.detail[1])
  },

  selectPopupExit:function(e){
    console.log(e)
    // console.log("点击了取消或空白区域取消")
    // console.log("组件标识："+e.detail[1])
  }
})