const api = require('../../api/index.js');
Page({

  data: {
    tabsindex:"",
    driverlist:[],
    selected:1,
    userdata:[],
    pageNum: 1, // 当前页码
    pageSize: 10, // 每页条数
    loading: false, // 加载中状态（防止重复请求）
    noMoreData: false, // 是否无更多数据
  },
  onLoad() {
    // this.getdriver()
    this.setData({
      userdata:wx.getStorageSync('user')
    })
wx.$on('wsMessage', this.toolWsMessage);

  },
  onUnload() {
    // 页面卸载时，取消订阅！
    wx.$off('wsMessage', this.toolWsMessage);
  },
   /**
   * 处理接收到的 WebSocket 消息
   * @param {Object} message 从 app.js 广播过来的消息对象
   */
  
  toolWsMessage(message) {
    console.log('Other Page 收到 WebSocket 消息:', message);
 
    // 根据消息类型进行不同的处
     switch (message.api) {
      case '/sysworkorder/selectworkorderlist':
        // 在这里可以更新地图、刷新UI等
        if (message.type==1) {
           this.getdriver()
        }

        break;
      
      case 'trajectoryInit':
        // 处理初始轨迹数据
        break;

      case 'heartbeatReply':
        // 心跳回复，通常不需要UI处理
        break;
    
      default:
        // 处理其他类型的消息
        break;
    }
  },
  onselected(e){
    console.log(e.currentTarget.dataset.index);
this.setData({
  selected:e.currentTarget.dataset.index
})
this.getdriver()
  },
  getdriver(){
      // 设置加载中状态
      this.setData({ loading: true });
      let data ={
     
      }
    if (this.data.userdata.operationRole==4) {
      data ={
        workorderId:'',
        workorderName:'',
        addressId:'',
        driverId:'',
        carId:'',
        customerId:'',
        settlementType:'',
        workorderType:this.data.tabsindex,
        chargedischargeType:this.data.selected,
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize,
      }
    }else if(this.data.userdata.operationRole==5){
    data ={
      workorderId:'',
      workorderName:'',
      addressId:'',
      driverId:'',
      carId:'',
      customerId:'',
      settlementType:'',
      workorderType:this.data.tabsindex,
      chargedischargeType:1,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
    }
    }else if(this.data.userdata.operationRole==6){
      data ={
        workorderId:'',
        workorderName:'',
        addressId:'',
        driverId:'',
        carId:'',
        customerId:'',
        settlementType:'',
        workorderType:this.data.tabsindex,
        chargedischargeType:2,
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize,
      }
    }
    if((this.data.userdata.operationRole==6||this.data.userdata.operationRole==5)&&this.data.selected==2){
      data ={
        workorderId:'',
        workorderName:'',
        addressId:'',
        driverId:'',
        carId:'',
        customerId:'',
        settlementType:this.data.tabsindex,
        workorderType:3,
        chargedischargeType:this.data.userdata.operationRole==5?1:2,
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize,
      }
    
    }
   

    api.request(`/sysworkorder/selectworkorderlist`, 'post',data,{ isPublic: false })
    .then((res) => {
  console.log(res.data);
  if (res.code==200) {
    const data = res.data.list; //data（当前页数据）
    const total = res.data.total; // total（总条数）
        console.log(data);
    // 拼接新数据
    const newList = this.data.pageNum === 1 ? data : [...this.data.driverlist, ...data];
    console.log(newList);
    // 判断是否有更多数据
    const noMoreData = newList.length >= total;
    // const noMoreData = false;

    this.setData({
      driverlist: newList,
      noMoreData:noMoreData,
    });
        // 无论成功失败，结束加载状态
        this.setData({ loading: false });
    // this.setData({
    //   driverlist:data.data
    // })
  }
  //  wx.navigateTo({
  //   url: '/pages/map/index',
  // })
    })
    .catch((err) => {
      wx.showToast({
        title: '加载失败',
        icon: 'none',
      });
      // 失败时页码回退（避免下次触底页码错误）
      this.setData({ pageNum: this.data.pageNum - 1 });
          // 无论成功失败，结束加载状态
          this.setData({ loading: false });
      console.error('请求失败：', err);
    });
  },
  // 选项卡
  onTabsChange(e){
  console.log(e.detail.value);
  this.setData({
    tabsindex:e.detail.value,
  })
  this.getdriver()

  // this.gitreview()
  },
  details(e){
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
    url: `/package-map/pages/map/index?id=${e.currentTarget.dataset.id}`,
  })
  },

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

    console.log('监听用户下拉动作');
  },
    /**
   * 监听页面触底事件
   */
  onReachBottom() {
 
    console.log(this.data.loading);
    console.log(this.data.noMoreData);
    // 防止重复加载（加载中/无更多数据时不触发）
    if (this.data.loading || this.data.noMoreData) return;
    // 页码+1
    this.setData({ pageNum: this.data.pageNum + 1 });
    // 加载下一页数据
this.getdriver()

  }
});
// 