const app = getApp();
Page({
  data: {
    list: [],
    keyword: "",
    page: 1,
    pageSize:10,
    pageTotal: 9999,
    tabDetail:[{name:'222',type:'1',isSelete:false}],
    seleteTabType:'',
    transportStatusInit:'',
    transportInit:'',
    packageTypeInit:'',
    packageName:'',
    seleteName:'',
    transportName:'',
    orderTime:'',
    packageType:[
      {name:'支',type:'10',isSelete:false},
      {name:'包',type:'20',isSelete:false},
      {name:'箱',type:'30',isSelete:false},
      {name:'托盘',type:'40',isSelete:false},
      {name:'其他',type:'50',isSelete:false},
    ], 
    //运输类型id  10 汽车 20船 30火车 40飞机 50其他
    transportType:[
      {name:'汽车',type:'10',isSelete:false},
      {name:'船',type:'20',isSelete:false},
      {name:'火车',type:'30',isSelete:false},
      {name:'飞机',type:'40',isSelete:false},
      {name:'其他',type:'50',isSelete:false},
    ],
    // 运输状态
    seleteType:[{type:'00',name:'已入库',isSelete:false},{type:'10',name:'已出库',isSelete:false},{type:'20',name:'已结束',isSelete:false}],
    isTab:false
  },
  onShow: function (options) {
    //获取token,若无则判定用户是游客访问，不进行任何请求
    var token = wx.getStorageSync('token')
    if (!token) {
      return
    }
    //获取列表
    var that = this;
    that.data.list = []
    that.data.page = 1
    that.getList();
  },
  searchList: function (e) {
    //获取token,若无则判定用户是游客访问，不进行任何请求
    var token = wx.getStorageSync('token')
    if (!token) {
      app.showToptip(this, "worning", "请先登录后，再查看相关数据")
      return
    }
    var that = this;
    that.data.list = []
    that.data.page = 1
    that.getList()
  },
  setKeyword(e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  setorderTime(e){
    this.setData({
      orderTime: e.detail.value
    },()=>{
      this.getList()
    })
  },
  orderTimeCancel(){
    this.setData({
      orderTime: ''
    },()=>{
      this.getList()
    })
  },
  getList() {
    var that = this;
    wx.showLoading({
      title: '正在加载...',
      mask: true
    })
    app.requestP({
      url: "/transport/pageTransOrder",
      method: "get",
      data: {
        pageNum: that.data.page,
        pageSize: that.data.pageSize,
        // orderTime:that.data.orderTime,
        keyword: that.data.keyword || '',
        dateTime:this.data.orderTime || '',
        transportStatus:this.data.transportStatus || '',
      }
    }).then(res => {
      const rows = res.data.rows;
      var data = rows || [];
      that.setData({
        list: data,
        pageTotal: Math.ceil(res.data.total / 5),
        page: that.data.page,
      })
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
      wx.hideLoading()
    }).catch(err => {
      
      console.log(err)
      // app.showToptip(that, "error", "数据获取失败，请稍后再试")
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
      app.showToptip(that,'error',err.data.msg);
      that.setData({
        list:[]
      })
    })
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var that = this;
    that.data.list = []
    that.data.page = 1
    that.getList()
  },
  //上拉加载更多
  onReachBottom: function () {
    var that = this;
    var page = that.data.page;
    var pageSize  = that.data.pageSize;
    if (that.data.pageTotal <= page) {

    } else {
      // that.data.page = page + 1;
      that.data.pageSize = pageSize + 10;
      that.getList()
    }
  },
  getUser() {
    const that = this
    //去拉取用户数据
    app.requestP({
      url: "/user/getUserInfo",
    }).then(function (res) {
      wx.setStorageSync('userInfo', res.data.data)
      that.setData({
        user: res.data.data
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg)
    })
  },
  goChainInfo(e){
    const id = e.currentTarget.dataset.id;
    const cid = e.currentTarget.dataset.cid;
    let userInfo = wx.getStorageSync('userInfo')
    const {loginRole,ifOverFlow,status} = userInfo;
    if(ifOverFlow == 'Y'){
      if(status == 20){
        if(loginRole == 'doctor'){
          wx.navigateTo({
            url: '/pages/workbench/scanningType/reportPdf/reportPdf?id='+id,
          })
        }else if(loginRole == 'admin' || loginRole == 'personnel'){
          wx.navigateTo({
            url: '/pages/query/orderDetails?id='+id+'&cid='+cid,
          })
        }
      }else if(status == 10){
        app.showToptip(this,'worning','身份审核中，审核通过可以使用该功能');
      }else if(status == 0){
        app.showToptip(this,'worning','您还没有选择身份，请选择您的身份并提交认证');
        wx.navigateTo({
          url: '/pages/userAuth/chooseRole',
        })
      }
    }else if(ifOverFlow == 'N'){
      if(loginRole == 'personnel'){
        this.showModel('/pages/userAuth/workerSelect');
      }else if(loginRole == 'doctor'){
        this.showModel('/pages/userAuth/doctorInfo');
      }else if(loginRole == 'admin'){
        this.showModel('/pages/userAuth/adminInfo');
      }else{
        app.showToptip(this,'worning','您当前身份暂无权限');
      }
    }
    // /pages/query/details?id={{item.id}}
  },
  setTab(e){
    var that = this;
    const type = e.currentTarget.dataset.type;
    if(this.data.seleteTabType == type && this.data.isTab == true){
      this.setData({
        isTab:false
      })
      return ;
    }
    if(this.data.seleteTabType == 'transportStatus'){
      this.setData({
        packageTypeInit:'',
        transportInit:'',
        packageType:that.data.packageType.map(item=>{return{...item,isSelete:false}}),
        transportType:that.data.transportType.map(item=>{return{...item,isSelete:false}}),
      })
    }
    if(type == 'package'){
      this.setData({
        tabDetail:this.data.packageType,
        seleteTabType:type,
        isTab:true
      })
    }else if(type == 'transport'){
      this.setData({
        tabDetail:this.data.transportType,
        isTab:true,
        seleteTabType:type,
      })
    }else if(type == 'transportStatus'){
      this.setData({
        tabDetail:this.data.seleteType,
        isTab:true,
        seleteTabType:type,
      })
    }
  },
  seleteTab(e){
    const item = e.currentTarget.dataset.item;
    const type = e.currentTarget.dataset.type;
    var seleteTabType = this.data.seleteTabType;
    var tabDetail = this.data.tabDetail;
    var seleteArr = tabDetail.map(itemTabType=>{
      if(itemTabType.type == type){
        return {...itemTabType,isSelete:true}
      }else{
        return {...itemTabType,isSelete:false}
      }
    })
    if(seleteTabType == 'package'){
      this.setData({
        packageType:seleteArr,
      })
    }else if(seleteTabType == 'transport'){
      this.setData({
        transportType:seleteArr,
      })
    }else if(seleteTabType == 'transportStatus'){
      this.setData({
        seleteType:seleteArr,
      })
    }
    this.setData({
      tabDetail:seleteArr,
    })
  },
  closeChaWin(){
   this.setData({
      tabDetail:[],
      isTab:false
    })
  },
  searchType(){
    var tabDetail = this.data.tabDetail;
    var type = this.data.seleteTabType;
    var seleteType = '';
    var name = '';
    tabDetail.map(item=>{
      if(item.isSelete){
        seleteType = item.type
        name = item.name
      }
    })
    if(type == 'package'){
      this.setData({
        packageTypeInit:seleteType,
        transportInit:'',
        transportStatusInit:'',
        packageName:name,
        isTab:false
      },()=>{
        this.getList()
      })
    }else if(type == 'transport'){
      this.setData({
        transportInit:seleteType,
        transportStatusInit:'',
        packageTypeInit:'',
        transportName:name,
        isTab:false
      },()=>{
        this.getList()
      })
    }else if(type == 'transportStatus'){
      this.setData({
        transportStatusInit:seleteType,
        packageTypeInit:'',
        transportInit:'',
        seleteName:name,
        isTab:false
      },()=>{
        this.getList()
      })
    }
  },
  cancel(){
    this.setData({
      packageType:this.data.packageType.map(item=>{
        if(item.isSelete){
          return {...item,isSelete:false}
        }else{
          return item
        }
      }),
      transportType:this.data.transportType.map(item=>{
        if(item.isSelete){
          return {...item,isSelete:false}
        }else{
          return item
        }
      }),
      seleteType:this.data.seleteType.map(item=>{
        if(item.isSelete){
          return {...item,isSelete:false}
        }else{
          return item
        }
      }),
      isTab:false,
      packageTypeInit:'',
      transportInit:'',
      transportStatusInit:'',
      pageNum:10
    },()=>{
      this.getList();
    })
  }
})