import { getAction } from '../../api/base';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInf: {},
    infObj: {
      totalCount: 0,
      speedAlarmCount: 0,
      fenceAlarmCount: 0
    }
  },
  // 登出
  logOut() {
    // 清除缓存
    wx.setStorageSync('token', '');
    wx.setStorageSync('userInfo', '');
    wx.reLaunch({
      url: '/pages/login/login'
    });
  },
  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) {
    console.log(e.currentTarget.dataset.type);
    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);
        }
      });
    }
  },
  // 数据录入
  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);
        }
      });
    }
  },
  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'
        });
      });
  },
  messageSubscription() {
    wx.getSetting({
      withSubscriptions: true,
      success: res => {
        if (res.subscriptionsSetting.itemSettings) {
          wx.showModal({
            title: '提示',
            content: '您已完成订阅，请到设置中心设置',
            confirmText: '去设置',
            success(res) {
              if (res.confirm) {
                wx.openSetting();
              }
            }
          });
        } else {
          wx.requestSubscribeMessage({
            tmplIds: ['1gdC7WQuVq3OLbx8fzlo4o2XtX0hQU7KmjkNvRUOrKw']
          });
        }
      }
    });
  },

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

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      });
    }
  },

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

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

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

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

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