//logs.js
let util = require('../../../utils/util.js');
let wechat = require("../../../utils/wechat");
let amap = require("../../../utils/amap");
Page({
  data: {
    userInfo: {},
    isLogin: false,
    searchString: null,
    chargpiles: [],
    pageNum: 1,
    total: 0,
    showAll: false,
    scrollTop: 0
  },
  onLoad(e) {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    let { keywords } = e ;
    this.setData({
      searchString: keywords
    });
    let userInfo = wx.getStorageSync('userInfo');
    let isLogin = wx.getStorageSync('isLogin');
    let that = this;
    // 页面显示
    if (userInfo && isLogin) {
      this.setData({
        userInfo: userInfo,
        isLogin: isLogin
      });
      wx.request({
        url: getApp().globalData.postHeadAgreement + '/restapi/wechat/chargpileList',
        data: {
          pams: userInfo.userId,
          chargStationName:that.data.searchString,
          params: {
            pageSize: 10,
            pageNum: 1
            //orderByColumn:
            //isAsc: asc
          }
        },
        method: 'POST',
        success(res) {
          //console.log(res);
          if (res.data && res.data.code == 0) {
            let {
              rows: chargpiles
            } = res.data;
            var show_chargpiles = [];
            chargpiles.forEach((item, index) => {
              show_chargpiles.push({
                chargStationId: item.chargStationId,
                chargPileName: item.chargPileName,
                chargPileId: item.chargPileId,
                chargStationName: item.chargStationName,
                chargmode: item.chargmode,//name = "充电模式", readConverterExp = "1=自动,2=手动"
                chargway: item.chargway,//name = "充电方式", readConverterExp = "1=直流,2=交流"
                chargstatus: item.chargstatus//充电状态0：离线、1：故障、2：空闲中、3：充电中、4：欠压故障、5：过压故障、6：过电流故障、8：预约、9：在线升级、10：操作中、11:已插枪等
              });
            });

            //充电状态0：离线、1：故障、2：空闲中、3：充电中、4：欠压故障、5：过压故障、6：过电流故障、8：预约、9：在线升级、10：操作中、11:已插枪等
            // if (!res.data.chargstatus || res.data.chargstatus == 0 || res.data.chargstatus == 1 || res.data.chargstatus == 4 || res.data.chargstatus == 5 || res.data.chargstatus == 6 || res.data.chargstatus == 9) {
            //   //充电桩故障
            //   wx.showModal({
            //     showCancel: false,
            //     content: '终端故障，维修中'
            //   });
            // } else if (res.data.chargstatus == 3 || res.data.chargstatus == 8 || res.data.chargstatus == 10) {
            //   wx.showModal({
            //     showCancel: false,
            //     content: '正在充电中'
            //   });
            // } else if (res.data.chargstatus == 2 || res.data.chargstatus == 11) {
            //   wx.setStorage({
            //     key: "scan_chargpile",
            //     data: res.data
            //   });
            //   log.info('[首页]', '[扫一扫充电桩空闲跳转去充电界面]');
            //   let url = `/pages/scan_result/scan_result`;
            //   wx.navigateTo({
            //     url
            //   });
            // }

            if (res.data.total <= 10) {
              that.setData({
                chargpiles: show_chargpiles,
                total: res.data.total,
                showAll: true,
                pageNum: 1,
                scrollTop: 24
              });
            } else {
              that.setData({
                chargpiles: show_chargpiles,
                total: res.data.total,
                showAll: false,
                pageNum: 1,
                scrollTop: 24
              });
            }
            wx.hideLoading();
          }
        }
      });
    }
  },
  loadInitData(e) {
    wx.showLoading({
      title: '刷新中',
      mask: true
    });
    let that = this;
    // 页面显示
    if (that.data.userInfo && that.data.isLogin) {
      wx.request({
        url: getApp().globalData.postHeadAgreement + '/restapi/wechat/chargpileList',
        data: {
          pams: that.data.userInfo.userId,
          chargStationName: that.data.searchString,
          params: {
            pageSize: 10,
            pageNum: 1
            //orderByColumn:
            //isAsc: asc
          }
        },
        method: 'POST',
        success(res) {
          //console.log(res);
          if (res.data && res.data.code == 0) {
            let {
              rows: chargpiles
            } = res.data;
            var show_chargpiles = [];
            chargpiles.forEach((item, index) => {
              show_chargpiles.push({
                chargStationId: item.chargStationId,
                chargPileName: item.chargPileName,
                chargPileId: item.chargPileId,
                chargStationName: item.chargStationName,
                chargmode: item.chargmode,//name = "充电模式", readConverterExp = "1=自动,2=手动"
                chargway: item.chargway,//name = "充电方式", readConverterExp = "1=直流,2=交流"
                chargstatus: item.chargstatus//充电状态0：离线、1：故障、2：空闲中、3：充电中、4：欠压故障、5：过压故障、6：过电流故障、8：预约、9：在线升级、10：操作中、11:已插枪等
              });
            });
            if (res.data.total <= 10) {
              that.setData({
                chargpiles: show_chargpiles,
                total: res.data.total,
                showAll: true,
                pageNum: 1,
                scrollTop: 24
              });
            } else {
              that.setData({
                chargpiles: show_chargpiles,
                total: res.data.total,
                showAll: false,
                pageNum: 1,
                scrollTop: 24
              });
            }
            wx.hideLoading();
          }
        }
      });
    }
  },
  loadMoreData(e) {
    var totalPage = parseInt((this.data.total + 10 - 1) / 10);
    if ((this.data.pageNum + 1) > totalPage) {
      this.setData({
        showAll: true
      });
      return;
    }
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    let that = this;
    let pageNum = this.data.pageNum + 1;
    this.setData({
      pageNum
    });
    // 页面显示
    if (that.data.userInfo && that.data.isLogin) {
      wx.request({
        url: getApp().globalData.postHeadAgreement + '/restapi/wechat/chargpileList',
        data: {
          pams: that.data.userInfo.userId,
          chargStationName: that.data.searchString,
          params: {
            pageSize: 10,
            pageNum: pageNum
            //orderByColumn:
            //isAsc: asc
          }
        },
        method: 'POST',
        success(res) {
          //console.log(res);
          if (res.data && res.data.code == 0) {
            let {
              rows: chargpiles
            } = res.data;
            var show_chargpiles = that.data.chargpiles;
            chargpiles.forEach((item, index) => {
              show_chargpiles.push({
                chargStationId: item.chargStationId,
                chargPileName: item.chargPileName,
                chargPileId: item.chargPileId,
                chargStationName: item.chargStationName,
                chargmode: item.chargmode,//name = "充电模式", readConverterExp = "1=自动,2=手动"
                chargway: item.chargway,//name = "充电方式", readConverterExp = "1=直流,2=交流"
                chargstatus: item.chargstatus//充电状态0：离线、1：故障、2：空闲中、3：充电中、4：欠压故障、5：过压故障、6：过电流故障、8：预约、9：在线升级、10：操作中、11:已插枪等
              });
            });
            that.setData({
              chargpiles: show_chargpiles
            });
            if (pageNum * 10 >= that.data.total) {
              that.setData({
                showAll: true
              });
            }
            wx.hideLoading();
          }
        }
      });
    }
  }
});