// pages/charginfo/eleCarMap.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    longitude:116.4965075,
    latitude: 40.006103,
    speed:0,
    scale:20,
    accuracy:0,
    chargid:0,
    ele_chargList:[],
    ele_park:[1,2,3],
    markers:[],
    markTemp:{
        id: 1,
        latitude: 23.098994,
        longitude: 113.322520,
        iconPath: '/images/triangle.png',
        height: 5,
        width: 5,
        customCallout: {
            anchorX: 0,
            anchorY: 0,
            display: "ALWAYS"
        }

        // anchor:{x: 0.5, y:1 },
        // callout: {
        //   content: '文本内容',
        //   color: '#000000',
        //   fontSize: 14,
        //   borderWidth: 2,
        //   borderRadius: 5,
        //   borderColor: '#FFFFFF',
        //   bgColor: '#ffffff',
        //   padding: 5,
        //   display: 'ALWAYS',
        //   textAlign: 'center'   
        // }
    },
    eleActiveIndex: -1,
    parkActiveIndex: -1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    let json = wx.getStorageSync("eleCarMapPage")
    let {chargid} = json;
    that.setData({chargid})
    let userInfo = wx.getStorageSync('userInfo');
    that.setData({
        userInfo
    })
    this.getEleData()
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  addHandler(json){
    let {
        chargid
    } = json
    this.setData({
        chargid
    })
    this.getEleData()
  },
  callouttap(e){

    // eleActiveIndex: -1,
    // parkActiveIndex: -1,
    let that = this;
    let index = e.markerId;
    console.info(e.markerId)
    let ele_chargList = this.data.ele_chargList;
    let mm = ele_chargList[index]
    if(mm.lock){
        return
    }

    if(mm.active){
        
        mm.active = false;
        if(mm.objType == 'ele'){
            
            that.setData({
                eleActiveIndex:-1
            })
        }else{
            
            that.setData({
                parkActiveIndex:-1
            })
        }
        this.setData({
            ele_chargList
        })
    
        return;
    }
    mm.active = true;
    if(mm.objType == 'ele'){
        if( that.data.eleActiveIndex >=0){
            ele_chargList[ that.data.eleActiveIndex].active = false
        }
       
        that.setData({
            eleActiveIndex:index
        })
    }else{
        if( that.data.parkActiveIndex >=0){
            ele_chargList[ that.data.parkActiveIndex].active = false
        }
       
       
        that.setData({
            parkActiveIndex:index
        })
    }


    // ele_chargList.forEach(key=>{

    // })
    console.info(this.data.ele_chargList)

    let car_active = false
    if(this.data.parkActiveIndex >=0 && this.data.eleActiveIndex >=0 ){
        car_active = true
    }

    this.setData({
        ele_chargList,
        car_active
    })

  },
  getEleData(){
      console.info("查询数据")
    // 新源恒远210调试站
    let that = this;
    // 充电桩
    wx.request({
      url: getApp().globalData.postHeadAgreement +'/restapi/wechat/chargInfo',
      data: {
        id: that.data.chargid,
        pagenum: 1,
        ivType: 1,
        pagesize: 10000,
      },
      method: 'POST',
      success(res) {
        console.log(res);
        let chargList = res.data.chargList.rows;
        let ele_chargList = []
        chargList.forEach(key => {
          let chargstatusname = '';
          let chargstatusclass = '';
          if(key.chargstatus == 2){
            chargstatusname='空闲中'
            chargstatusclass = 'chargstatus_kxz'
          }else if(key.chargstatus == 11){
            chargstatusname='已插枪'
            chargstatusclass = 'chargstatus_ycq'
          }else if(key.chargstatus == 3 || key.chargstatus == 10){
            chargstatusname='充电中'
            chargstatusclass = 'chargstatus_cdz'
          }else if(key.chargstatus == 0){
            chargstatusname='离线'
            chargstatusclass = 'chargstatus_lx'
          }else{
            chargstatusname='故障'
            chargstatusclass = 'chargstatus_gz'
          }
          key.chargstatusname = chargstatusname;
          key.chargstatusclass = chargstatusclass;
          let mark = JSON.parse(JSON.stringify(that.data.markTemp));
          
          if(key?.currentParkPoint?.latitude && key?.currentParkPoint?.longitude ){
            mark.id = that.data.ele_chargList.length + ele_chargList.length;
            mark.latitude = key?.currentParkPoint?.latitude
            mark.longitude = key?.currentParkPoint?.longitude
            mark['ele_lock'] = key?.lockStatus == 1
            mark['ele_unlock'] = key?.lockStatus == 0
            mark['park_lock'] = false
            mark['park_unlock'] = false
            mark['lock'] = mark['ele_lock'] || mark['park_lock']
            mark['active'] = false
            mark['title'] = key?.currentSoc? key?.currentSoc + '%' : '-'
            mark['type'] = mark['ele_lock'] ? '锁定' : '未锁定'
            mark['objType'] = 'ele'
            // mark['chargPileId'] = that.data.chargid
            // mark['chargPileId'] = key?.chargPileId
            mark['chargPileId'] = key?.id


            // mark.callout.content = key?.currentParkPoint?.pointShowName
            ele_chargList.push(mark)
            that.setData({
                longitude:mark.longitude,
                latitude:mark.latitude,
            })
          }

      })

        console.log(ele_chargList);
        // that.rechargeTimeOrder(chargemoneylogs);
        wx.hideLoading()
        that.setData({
          ele_chargList: that.data.ele_chargList.concat(ele_chargList),
          markers: that.data.markers.concat(ele_chargList),
        //   ele_total: res.data.chargList.total,
        //   ele_dataLoading: false,
        //   ele_fastFree : res.data.fastFree,
        //   ele_slowFree : res.data.slowFree,
        //   ele_fastSum : res.data.fastSum,
        //   ele_slowSum : res.data.slowSum
        });
        console.log(ele_chargList);
      }
    });

    // 停靠点
    wx.request({
        url: getApp().globalData.postHeadAgreement +'/restapi/wechat/ivparkByStation',
        data: {
          id: that.data.chargid,
          pagenum: 1,
          ivType: 1,
          pagesize: 10000,
        },
        method: 'POST',
        success(res) {
          console.log(res);
          let chargList = res.data;
          let ele_chargList = []
          chargList.forEach(key => {
            let chargstatusname = '';
            let chargstatusclass = '';
            if(key.chargstatus == 2){
              chargstatusname='空闲中'
              chargstatusclass = 'chargstatus_kxz'
            }else if(key.chargstatus == 11){
              chargstatusname='已插枪'
              chargstatusclass = 'chargstatus_ycq'
            }else if(key.chargstatus == 3 || key.chargstatus == 10){
              chargstatusname='充电中'
              chargstatusclass = 'chargstatus_cdz'
            }else if(key.chargstatus == 0){
              chargstatusname='离线'
              chargstatusclass = 'chargstatus_lx'
            }else{
              chargstatusname='故障'
              chargstatusclass = 'chargstatus_gz'
            }
            key.chargstatusname = chargstatusname;
            key.chargstatusclass = chargstatusclass;

            let mark = JSON.parse(JSON.stringify(that.data.markTemp));
          

            mark.id = that.data.ele_chargList.length + ele_chargList.length;
            mark.latitude = key?.latitude
            mark.longitude = key?.longitude
            mark['ele_lock'] = false
            mark['ele_unlock'] = false
            mark['park_lock'] = key?.parkStatus == 1
            mark['park_unlock'] = key?.parkStatus == 0
            mark['lock'] = mark['ele_lock'] || mark['park_lock']
            mark['active'] = false
            mark['title'] = key?.pointShowName? key?.pointShowName : '-'
            mark['type'] = mark['ele_lock'] ? '占用' : '空闲'
            mark['objType'] = 'park'
            mark['ivParkPointId'] = key?.ivParkPointId;
            // mark.callout.content = key?.currentParkPoint?.pointShowName
            ele_chargList.push(mark)
            that.setData({
                longitude:mark.longitude,
                latitude:mark.latitude,
            })


        })
  
          console.log(ele_chargList);
          // that.rechargeTimeOrder(chargemoneylogs);

        

          wx.hideLoading()
          that.setData({
            ele_chargList: that.data.ele_chargList.concat(ele_chargList),
            markers: that.data.markers.concat(ele_chargList),
            // ele_park: that.data.ele_park.concat(chargList),
          //   ele_total: res.data.chargList.total,
          //   ele_dataLoading: false,
          //   ele_fastFree : res.data.fastFree,
          //   ele_slowFree : res.data.slowFree,
          //   ele_fastSum : res.data.fastSum,
          //   ele_slowSum : res.data.slowSum
          });
        }
      });



  },
  callCar(){
    let that = this
    let car_active = that.data.car_active;
    if(!car_active){
        return;
    }

    let ivParkPointId = that.data.ele_chargList[that.data.parkActiveIndex].ivParkPointId
    let chargePileId = that.data.ele_chargList[that.data.eleActiveIndex].chargPileId
    let userId = that.data.userInfo.userId

    wx.request({
        url: getApp().globalData.postHeadAgreement + '/restapi/wechat/callCarRequirement?'
        + 'ivParkPointId=' + ivParkPointId
        + '&chargePileId=' + chargePileId
        + '&userId=' + userId
        + '&requirementType=0',
        data: {
        },
        method: 'POST',
        success: function (res) {
            if( res.data.code ==200){
                // 走你
                let url = `/pages/batteryPack/batteryPackWait`;
                wx.redirectTo({
                  url
                });
            }else{
                // 给出提示
                wx.showModal({
                    showCancel: false,
                    content: "调度失败" + res.data?.msg,
                    confirmColor:'#00AADD',
                  });
            }
        },
        fail(e) {
          console.log("呼叫充电车失败");
        }
    })
    }
})