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

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInf: {},
    infObj: {
      totalCount: 0,
      speedAlarmCount: 0,
      fenceAlarmCount: 0
    },

    tabList: [] as any,
    active: 0,
    showStatus: 0,

    flowStatus: 3,
    current: 1,
    isLast: false,
    loading: false,
    list: [],
    repairOrUpkeep: 1 // 1为维修列表 2为保养列表
  },

  // 登出
  logOut() {
    // 清除缓存
    wx.setStorageSync('token', '');
    wx.setStorageSync('userInfo', '');
    wx.reLaunch({
      url: '/pages/login/login'
    });
  },
  getUserInf() {
    this.setData({
      userInf: wx.getStorageSync('userInfo')
    });
  },
  // 变更tab
  tabOnChange(e: any) {
    console.log(e);
    this.setData({
      active: e.detail.index,
      flowStatus: this.data.tabList[e.detail.index].flowStatus,
      repairOrUpkeep: this.data.tabList[e.detail.index].repairOrUpkeep,
      list: [], // 初始化参数
      current: 1,
      isLast: false,
      loading: false
    });
    this.getTabList();
    this.getList();
  },
  toMaintainRecord() {
    wx.navigateTo({
      url: '../vehicleMaintenance/maintainRecord/maintainRecord',
      fail: e => {
        console.log(e);
      }
    });
  },
  toUpkeepRecord() {
    wx.navigateTo({
      url: '../UpkeepBill/UpkeepBillRecord/UpkeepBillRecord',
      fail: e => {
        console.log(e);
      }
    });
  },
  toDetailPage(e: any) {
    console.log(e.currentTarget.dataset.inf);
    wx.setStorageSync('selectMaintain', e.currentTarget.dataset.inf);
    let url = '' as any;
    if (this.data.flowStatus == 3 && this.data.repairOrUpkeep == 1)
      url = '../vehicleMaintenance/maintainSend/maintainSend';
    else if (this.data.flowStatus == 6 && this.data.repairOrUpkeep == 1)
      url = '../vehicleMaintenance/maintainProcedure/maintainProcedure';
    else if (this.data.flowStatus == 3 && this.data.repairOrUpkeep == 2)
      url = '../UpkeepBill/UpkeepBillSend/UpkeepBillSend';
    else if (this.data.flowStatus == 6 && this.data.repairOrUpkeep == 2)
      url = '../UpkeepBill/UpkeepBillProcedure/UpkeepBillProcedure';
    // 跳转到待报价详情
    wx.navigateTo({
      url: url,
      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);
        }
      });
    }
  },
  getTabList() {
    getAction('api/vehicles/repair/getRepairShopPageStatic').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 params = {
      flowStatus: this.data.flowStatus,
      pageNo: this.data.current,
      pageSize: 10
    };
    let url;
    if (this.data.repairOrUpkeep == 1) url = 'api/vehicles/repair/list';
    else url = 'api/vehicles/upkeepBill/list';
    wx.showLoading({
      title: '数据加载中'
    });
    getAction(url, params).then((res: any) => {
      wx.hideLoading();
      if (res.code == 200) {
        let data: any = { ...this.data };
        data.list = data.list.concat(res.result.records);
        console.log(data);
        this.setData(data);
        if (data.list.length == res.result.total) {
          this.setData({
            isLast: true
          });
        }
        // this.setData({
        //   list: res.result.records
        // });
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    });
  },
  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({
        current: (this.data.current += 1),
        loading: true
      });
      this.getList();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      list: [], // 初始化参数
      current: 1,
      isLast: false,
      loading: false
    });
    this.getUserInf();
    this.getTabList();
    this.getList();
  },

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

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      list: [], // 初始化参数
      current: 1,
      isLast: false,
      loading: false
    });
    this.getUserInf();
    this.getTabList();
    this.getList();
    //停止下拉刷新
    wx.stopPullDownRefresh();
  },

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

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