// pages/projectIndex/projectIndex.ts
import { getAction } from '../../api/base';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInf: {},
    infObj: {
      totalCount: 0,
      speedAlarmCount: 0,
      fenceAlarmCount: 0
    },
    current: 1,
    listTotal: 0,
    recordList: []
  },
  // 登出
  logOut() {
    // 清除缓存
    wx.setStorageSync('token', '');
    wx.setStorageSync('userInfo', '');
    wx.reLaunch({
      url: '/pages/login/login'
    });
  },
  getUserInf() {
    this.setData({
      userInf: wx.getStorageSync('userInfo')
    });
  },
  getInf() {
    getAction('api/index/vehicleCount')
      .then((res: any) => {
        if (res.code == 200) {
          this.setData({
            infObj: res.result
          });
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none'
          });
        }
      })
      .catch((err: any) => {
        wx.showToast({
          title: err.message,
          icon: 'none'
        });
      });
  },
  toVehicleMonitoring() {
    wx.navigateTo({
      url: '../vehicleMonitoring/global/global',
      fail: e => {
        console.log(e);
      }
    });
  },
  toFailureAler() {
    wx.navigateTo({
      url: '../vehicleMonitoring/failureAlert/failureAlert',
      fail: e => {
        console.log(e);
      }
    });
  },
  toPage(e: any) {
    if (e.currentTarget.dataset.type == 'eventReportRecord') {
      wx.navigateTo({
        url: '../eventReport/eventReportRecord/eventReportRecord',
        fail: e => {
          console.log(e);
        }
      });
    } else if (e.currentTarget.dataset.type == 'apply') {
      wx.navigateTo({
        url: '../vehicleMaintenance/operation/maintainApply/maintainApply',
        fail: e => {
          console.log(e);
        }
      });
    } else if (e.currentTarget.dataset.type == 'record') {
      wx.navigateTo({
        url: '../vehicleMaintenance/maintainRecord/maintainRecord',
        fail: e => {
          console.log(e);
        }
      });
    } else if (e.currentTarget.dataset.type == 'report') {
      wx.navigateTo({
        url: '../statement/reportStatement/reportStatement',
        fail: e => {
          console.log(e);
        }
      });
    } else if (e.currentTarget.dataset.type == 'upkeepApply') {
      wx.setStorageSync('type', {
        type: 'add'
      });
      wx.navigateTo({
        url: '../UpkeepPlan/UpkeepPlanApply/UpkeepPlanApply',
        fail: e => {
          console.log(e);
        }
      });
    } else if (e.currentTarget.dataset.type == 'upkeepPlanRecord') {
      wx.navigateTo({
        url: '../UpkeepPlan/UpkeepPlanRecord/UpkeepPlanRecord',
        fail: e => {
          console.log(e);
        }
      });
    } else if (e.currentTarget.dataset.type == 'upkeepBillRecord') {
      wx.navigateTo({
        url: '../UpkeepBill/UpkeepBillRecord/UpkeepBillRecord',
        fail: e => {
          console.log(e);
        }
      });
    }
  },
  getList() {
    let parms = {
      type: 2,
      pageNo: this.data.current,
      pageSize: -1
    };
    this.setData({
      loading: true
    });
    getAction('api/vehicles/backlog/getBacklogPage', parms)
      .then((res: any) => {
        this.setData({
          loading: false
        });
        if (res.code == 200) {
          if (1) {
            // tmpArr 目前只是调试流程用
            this.setData({
              isLast: true,
              listTotal: res.result.total,
              recordList: res.result.records
            });
          }
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none'
          });
        }
      })
      .catch((err: any) => {
        this.setData({
          loading: false
        });
        wx.showToast({
          title: err.message,
          icon: 'none'
        });
      });
  },
  toDetailPage(e: any) {
    console.log(e.currentTarget.dataset.inf);
    wx.setStorageSync('backlogInf', e.currentTarget.dataset.inf);
    // 跳转到违章代办详情
    wx.navigateTo({
      url: '../backlog/violation/violation',
      fail: e => {
        console.log(e);
      }
    });
  },
  // 数据录入
  toDataEntry(e: any) {
    if (e.currentTarget.dataset.type == 'upkeep') {
      wx.navigateTo({
        url: '../UpkeepBill/operation/dataEntryOne/dataEntryOne',
        fail: e => {
          console.log(e);
        }
      });
    } else {
      wx.navigateTo({
        url: '../vehicleMaintenance/operation/dataEntryOne/dataEntryOne',
        fail: e => {
          console.log(e);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getUserInf();
    this.getInf();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList();
  },

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

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

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

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

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