// pages/vehicleMaintenance/maintainSend/maintainSend.ts
Page({
  /**
   * 页面的初始数据
   */
  data: {
    message: {} as any, // 展示的信息
    userInfo: {} as any, // 用户信息
    doNotUnload: false, // 是否触发页面卸载的navigateBack方法
    routerHasList: false // 路由中是否含有列表或者待办列表
  },
  // 前往下一状态的维修单
  goRepair() {
    let data = this.data.message;
    // 如果步骤2 质保维修选择是 则直接跳转到步骤6 维修中
    if (data.flowStatus == 2 && data.isGuarantee === '1') {
      data.flowStatus = 6;
    }
    // 如果步骤4 5 10审核不通过，返回上一步
    // 报价3 <- 审核4 <- 复审10 <- 审批5
    else if (
      data.checkFlag == '2' &&
      (data.flowStatus == 4 || data.flowStatus == 5 || data.flowStatus == 10)
    ) {
      switch (data.flowStatus) {
        case 4:
          data.flowStatus = 3;
          break;
        case 5:
          data.flowStatus = 10;
          break;
        case 10:
          data.flowStatus = 4;
          break;
      }
    }
    // 审核后进入复核
    else if (data.flowStatus == 4) {
      data.flowStatus = 10;
    }
    // 步骤10复核后 如果金额超过2000 需要先进入步骤5 领导审批，如何不超过2000 直接跳到步骤6 维修中
    else if (data.flowStatus == 10 && data.price <= 2000) {
      data.flowStatus = 6;
    }
    // 没有超过2000 进入审批
    else if (data.flowStatus == 10 && data.price > 2000) {
      data.flowStatus = 5;
    }
    //  如果步骤7确认维修结果不通过，返回步骤6
    else if (data.checkFlag == '2' && data.flowStatus == 7) {
      data.flowStatus = 6;
    }
    // 如果是数据录入，跳转道步骤8 已归档数据
    else if (data.flowStatus == 100) {
      data.flowStatus = 8;
    }
    // 如果是作废，跳转到已作废
    else if (data.flowStatus == 9) {
      data.flowStatus = 9;
    } else data.flowStatus += 1;
    wx.setStorageSync('selectMaintain', this.data.message);
    let url = '';
    switch (data.flowStatus) {
      case 2:
        url = '../../maintainSend/maintainSend';
        break;
      case 3:
        url = '../../maintainSend/maintainSend';
        break;
      case 4:
        url = '../../maintainCheck/maintainCheck';
        break;
      case 5:
        url = '../../maintainCheck/maintainCheck';
        break;
      case 6:
        url = '../../maintainProcedure/maintainProcedure';
        break;
      case 7:
        url = '../../maintainProcedure/maintainProcedure';
        break;
      case 8:
        url = '../../maintainResult/maintainResult';
        break;
      case 9:
        url = '../../maintainResult/maintainResult';
        break;
      case 10:
        url = '../../maintainCheck/maintainCheck';
        break;
    }
    this.setData({
      doNotUnload: true
    });
    wx.redirectTo({
      url: url,
      fail: e => {
        console.log(e);
      }
    });
  },
  backHome() {
    // 返回首页 申请和数据录入
    if (
      this.data.message.flowStatus == 1 ||
      this.data.message.flowStatus == 100
    ) {
      if (this.data.userInfo.type == '1') {
        wx.reLaunch({
          url: '/pages/index/index'
        });
      } else if (this.data.userInfo.type == '2') {
        wx.reLaunch({
          url: '/pages/manufacturerIndex/manufacturerIndex'
        });
      } else if (this.data.userInfo.type == '3') {
        wx.reLaunch({
          url: '/pages/projectIndex/projectIndex'
        });
      } else if (this.data.userInfo.type == '4') {
        wx.reLaunch({
          url: '/pages/maintenanceIndex/maintenanceIndex'
        });
      } else {
        wx.reLaunch({
          url: '/pages/login/login'
        });
      }
    }
    // 返回列表
    else {
      this.setData({
        doNotUnload: true
      });
      if (this.data.message.flowStatus == 2) this.goBack(1);
      else this.goBack(2);
    }
  },
  goBack(delta: number) {
    wx.navigateBack({
      delta: delta,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        if (getApp().EventChannel) getApp().EventChannel.emit('refresh');
      },
      fail: e => {
        console.log(e);
      }
    });
  },
  getInf() {
    // 获取信息
    let info = wx.getStorageSync('message');
    wx.removeStorage({ key: 'message' });
    this.setData({
      message: info
    });
    this.changeTitle(info);
  },
  // 获取用户信息
  getUserInfo() {
    let userInfo = wx.getStorageSync('userInfo');
    this.setData({
      userInfo: userInfo
    });
  },
  // 修改页眉
  changeTitle(info: any) {
    let title = '操作成功';
    switch (info.flowStatus) {
      case 1:
        title = '已提交';
        break;
      case 2:
        title = '派单成功';
        break;
      case 3:
        title = '报价成功';
        break;
      case 4:
        title = '已提交';
        break;
      case 5:
        title = '已提交';
        break;
      case 6:
        title = '已提交';
        break;
      case 7:
        title = '已提交';
        break;
    }
    wx.setNavigationBarTitle({
      title: title
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 不放onReady 是因为有可能切出去然后状态改了得跳转到别的页面
    // 判断router中是否有列表
    let pages = getCurrentPages();
    let routerHasList = pages.some((item: any) => {
      return (
        item.route ==
          'pages/vehicleMaintenance/maintainRecord/maintainRecord' ||
        item.route == 'pages/backlog/backlogList/backlogList'
      );
    });
    this.setData({
      routerHasList: routerHasList
    });
  },

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

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (!this.data.doNotUnload) {
      // 非待派单
      if (this.data.message.flowStatus !== 2) {
        this.goBack(1);
      } else if (getApp().EventChannel) getApp().EventChannel.emit('refresh');
    } else {
      this.setData({
        doNotUnload: false
      });
    }
  },

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

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

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