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

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabList: [] as any,
    active: 0,
    flowStatus: 2,
    current: 1,
    recordList: [],
    loading: false,
    isLast: false
  },

  getTabList() {
    getAction('api/vehicles/backlog/getBacklogCount').then((res: any) => {
      if (res.code == 200) {
        console.log(res.result);
        this.setData({
          tabList: res.result
        });
        console.log(this.data.tabList);
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    });
  },
  getList() {
    let parms = {
      type: this.data.flowStatus,
      pageNo: this.data.current,
      pageSize: 10
    };
    wx.showLoading({
      title: '加载中'
    });
    if (!this.data.loading) {
      this.setData({
        loading: true
      });
    }
    getAction('api/vehicles/backlog/getBacklogPage', parms)
      .then((res: any) => {
        wx.hideLoading();
        this.setData({
          loading: false
        });
        if (res.code == 200) {
          let tmpArr = this.data.recordList.concat(res.result.records);
          this.setData({
            listTotal: res.result.total,
            recordList: tmpArr
          });
          if (res.result.total == tmpArr.length) {
            // tmpArr 目前只是调试流程用
            this.setData({
              isLast: true
            });
          } else {
          }
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none'
          });
        }
      })
      .catch((err: any) => {
        wx.hideLoading();
        wx.showToast({
          title: err.message,
          icon: 'none'
        });
      });
  },
  // 变更tab
  tabOnChange(e: any) {
    console.log(e);
    this.setData({
      active: e.detail.index,
      flowStatus: this.data.tabList[e.detail.index].type,
      recordList: [],
      current: 1,
      isLast: false
    });
    this.getList();
  },
  // 跳转到对应的详情页
  toDetailPage(e: any) {
    let that = this;
    console.log('跳转', e.currentTarget.dataset.inf);
    wx.setStorageSync('backlogInf', e.currentTarget.dataset.inf);
    let url = '';
    switch (e.currentTarget.dataset.inf.type) {
      case '1': //出险
        url = '../insurance/insurance';
        break;
      case '2': //违章
        url = '../violation/violation';
        break;
      case '3': //事故
        url = '../accident/accident';
        break;
      case '4': //维修
        wx.removeStorageSync('backlogInf');
        wx.setStorageSync('selectMaintain', e.currentTarget.dataset.inf);
        switch (e.currentTarget.dataset.inf.flowStatus) {
          case 2:
            url = '../../vehicleMaintenance/maintainSend/maintainSend';
            break;
          case 3:
            url = '../../vehicleMaintenance/maintainSend/maintainSend';
            break;
          case 4:
            url = '../../vehicleMaintenance/maintainCheck/maintainCheck';
            break;
          case 5:
            url = '../../vehicleMaintenance/maintainCheck/maintainCheck';
            break;
          case 10:
            url = '../../vehicleMaintenance/maintainCheck/maintainCheck';
            break;
          case 6:
            url =
              '../../vehicleMaintenance/maintainProcedure/maintainProcedure';
            break;
          case 7:
            url =
              '../../vehicleMaintenance/maintainProcedure/maintainProcedure';
            break;
          case 8:
            url = '../../vehicleMaintenance/maintainResult/maintainResult';
            break;
          case 9:
            url = '../../vehicleMaintenance/maintainResult/maintainResult';
            break;
        }
        // url = '';
        break;
      case '5': //保养
        wx.removeStorageSync('backlogInf');
        url = '';
        if (e.currentTarget.dataset.inf.planOrBill == 'plan') {
          url = '../../UpkeepPlan/UpkeepPlanInfo/UpkeepPlanInfo';
          wx.setStorageSync('info', {
            info: e.currentTarget.dataset.inf
          });
        } else {
          wx.setStorageSync('selectMaintain', e.currentTarget.dataset.inf);
          switch (e.currentTarget.dataset.inf.flowStatus) {
            case 2:
              url = '../../UpkeepBill/UpkeepBillSend/UpkeepBillSend';
              break;
            case 3:
              url = '../../UpkeepBill/UpkeepBillSend/UpkeepBillSend';
              break;
            case 4:
              url = '../../UpkeepBill/UpkeepBillCheck/UpkeepBillCheck';
              break;
            case 5:
              url = '../../UpkeepBill/UpkeepBillCheck/UpkeepBillCheck';
              break;
            case 10:
              url = '../../UpkeepBill/UpkeepBillCheck/UpkeepBillCheck';
              break;
            case 6:
              url = '../../UpkeepBill/UpkeepBillProcedure/UpkeepBillProcedure';
              break;
            case 7:
              url = '../../UpkeepBill/UpkeepBillProcedure/UpkeepBillProcedure';
              break;
            case 8:
              url = '../../UpkeepBill/UpkeepBillResult/UpkeepBillResult';
              break;
            case 9:
              url = '../../UpkeepBill/UpkeepBillResult/UpkeepBillResult';
              break;
          }
        }
        break;
      case '6': //年检
        url = '../annualInspection/annualInspection';
        break;
      case '7': //回访
        wx.setStorageSync('customerFollowData', e.currentTarget.dataset.inf);
        url =
          '../../../packageA/pages/customerFollowDetail/customerFollowDetail';
        break;
    }
    if (url == '') {
      wx.showToast({
        title: '暂无详情',
        icon: 'none'
      });
      return;
    }
    wx.navigateTo({
      url: url,
      events: {
        // 编辑过后刷新页面
        refresh: function () {
          that.refresh();
        }
      },
      fail: e => {
        console.log(e);
      }
    });
  },
  lower(e: any) {
    console.log(e.detail.direction == 'bottom');
    if (this.data.isLast) {
      wx.showToast({
        title: '已经是最后一页',
        icon: 'none'
      });
      return;
    }
    if (e.detail.direction == 'bottom' && !this.data.loading) {
      // this.data.current += 1
      this.setData({
        loading: true,
        current: (this.data.current += 1)
      });
      this.getList();
    }
  },
  refresh() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      });
    }
    this.setData({
      recordList: [],
      current: 1,
      isLast: false
    });
    this.getTabList();
    this.getList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

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

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

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

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    getApp().EventChannel = null;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      recordList: [],
      current: 1,
      isLast: false
    });
    this.getTabList();
    this.getList();

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },

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

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