<template>
  <div class="wrap">
    <div class="navBar">
      <van-nav-bar
          title="路桥费报销"
          left-arrow
          left-arrow-color="#FFFFFF"
          :border="false"
          :fixed="true"
          :safe-area-inset-top="true"
          @click-left="goBack"
      />
    </div>
    <div :style="{ 'height': activeIndex == 0 ? '83px' : '42px' }">
      <div class="tab_wrap">
        <div v-for="(item, index) in tabArr" :key="index" :class="{'active' : activeIndex == index}"
             @click="changeTab(index)">
          {{ item.name }}
        </div>
      </div>

    </div>
    <div class="monthSearch" v-show="activeIndex == 0">
      <span v-for="(item,index) in monthList" :class="monthIndex === index ? 'active' : ''" :key="index" @click="clickHandle(index)">{{item}}</span>
    </div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh" v-show="!show" :class="activeIndex==0 ? 'firstpullRefresh' : 'secondpullRefresh'">
      <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
      >
        <div class="listWrap" :style="{ 'padding-top': activeIndex == 0 ? '0px' : '10px' }">
          <div class="itemWrap" v-for="(item,index) in pageList" :key="index" :style="{ 'padding-left': activeIndex == 0 ? '34px' : '11px' }">
            <div class="contentWrap" @click="noMultipleClicks(goDetail,item.taskOrderId)">
              <div class="line1">
                <div><span>订单编号:</span><span>{{item.userOrderCode}}</span></div>
                <div class="right">
                  <img class="commonImg" :src="getStatus(item.state)">
                  <div :class="getStatusColor(item.state)">{{item.stateStr}}</div>
                  <img class="rightArrImg"  src="@/assets/list_rightArrow.png">
                </div>
              </div>
              <div><span>报销金额:</span><span>{{item.reimburseAmount}}元</span></div>
              <div v-show="activeIndex == 2 || item.state == 3"><span>审核备注:</span><span style="color: #FF5D2E">{{item.auditRemark}}</span></div>
              <div v-show="(activeIndex != 0 && item.state != 1) || (activeIndex == 0 && item.state == 3)"><span>审核人员:</span><span>{{item.auditUser}}</span></div>
              <div v-show="(activeIndex != 0 && item.state != 1) || (activeIndex == 0 && item.state == 3)"><span>审核时间:</span><span>{{item.auditTime}}</span></div>
            </div>
            <img v-show="activeIndex == 0" class="checkImg" @click="toggleSelection(item)" :src="item.isSelected ? require('@/assets/baoxiao_check.png') : require('@/assets/baoxiao_uncheck.png')">
          </div>
        </div>

      </van-list>
    </van-pull-refresh>
    <div v-show="activeIndex == 0" class="submitBtnWrap">
      <div   class="submitBtn" @click="noMultipleClicks(submitHandle)">提交</div>
    </div>

    <div class="bgEmptyImg" v-show="show" >
      <img  src="@/assets/empty.png" />
    </div>
  </div>

</template>

<script>
import {myMixins} from "@/utils/myMixins";
import { waitReimburseList ,reimbursedList,waitReimburseMonth,submitReimburseBatch} from '@/api/reimbursementBatch'
//
export default {
  name: "driverCaseList",
  mixins: [myMixins],

  data(){
    return{
      tabArr: [{name: '待报销', status: 1}, {name: '已报销', status: 2},{name: '报销失败', status: 3},],
      activeIndex: 0,
      pageList:[],
      pageNum:1,
      pageSize:10,
      total: 0,
      isCheck:false,
      driverId: '',//
      submitData:[],
      noClick:true,
      loading:false,
      isLoading: false,
      finished: false,
      monthList:[],
      monthIndex:0,
    }
  },
  computed:{
    show() {
      return (this.pageList.length < 0 || this.pageList.length == 0); // 判断数组长度是否大于 0
    },
  },
  async mounted() {
    const urlParams = new URLSearchParams(window.location.search);
    this.driverId = urlParams.get('driverId');
    // this.driverId = 78948;
    await this.getMonth();
    await this.getList()
  },
  methods:{
    async onLoad(){
      console.log('onLoadonLoadonLoadonLoadonLoad')
      if (this.pageList.length >= this.total) {
        this.finished = true;
        return
      }
      this.pageNum++;
      await this.getList()
      this.loading = false;

    },
    async clickHandle(index){
      // console.log("index",index)
      this.monthIndex=index
      this.pageNum=1
      this.total=0
      this.pageList=[]
      this.monthIndex=index
      // console.log("this.monthIndex",this.monthIndex)
      await this.getList()
      this.submitData=[]
    },
    onRefresh() {
      this.pageNum=1;
      setTimeout(() => {
        this.$toast('刷新成功');
        this.isLoading = false;
      }, 1000);
    },
    toggleSelection(item) {
      item.isSelected = !item.isSelected; // 切换 isSelected 属性的值
    },
    async submitHandle(){
      this.pageList.forEach((item)=>{
        if(item.isSelected){
          this.submitData.push({...item})
        }
      })
      // console.log("this.submitData",this.submitData)
      if(this.submitData && this.submitData.length>0){
        const uniqueArr = this.submitData.filter(
            (item, index, self) =>
                index === self.findIndex((t) => t.userOrderCode === item.userOrderCode)
        );
        // console.log("uniqueArr",uniqueArr)
        let res = await submitReimburseBatch({
          driverId: this.driverId,
          orderList: [...uniqueArr]
        })
        if(res.code === 200){
          this.$toast(res.msg)
        }
      }else{
        this.$toast("请勾选要报销的订单")
      }
      await this.getList()
    },
    async changeTab(index) {
      this.activeIndex = index
      this.pageNum = 1
      this.pageList = [];
      this.total=0
      this.loading=false
      this.finished=false
      await this.getList()
    },
    goDetail(id){
      window.android.sendMessage("taskId=" + id);
    },
    async getList(){
      let res=[]
      if(this.activeIndex==0){
        res=await waitReimburseList({
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          driverId:this.driverId,
          type: this.activeIndex+1,
          periodMonth:this.monthList[this.monthIndex]
        })
      }else{
        res=await reimbursedList({
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          driverId:this.driverId,
          type: this.activeIndex+1
        })
      }
      // this.pageList=res.data
      this.total=res.total
      if(this.pageNum == 1){// 第一页直接赋值
        this.pageList=res.data
      }else{// 第二页数据拼接
        let preList = this.pageList;
        let arr = res.data;
        this.pageList = preList.concat(arr)
      }
      if(this.activeIndex==0){
        this.pageList.forEach(item => {
          this.$set(item,'isSelected',false)
        });
      }
    },
   async getMonth(){
      let res = await waitReimburseMonth({
        driverId:this.driverId,
      })
      this.monthList=res.data
    },
    getStatus(i){
      switch (i){
        case 0://待提交
        case 1://待审核
          return require('@/assets/waitSubmit.png')
        case 2://审核通过
        case 7://已打款
          return require('@/assets/dakuan.png')
        case 3://审核不通过
          return require('@/assets/unpass.png')
        case 4://报销失败
          return require('@/assets/unSubmit.png')
        case 6://代打款
          return require('@/assets/waitDakuan.png')
        default:
          return require('@/assets/waitSubmit.png')
      }
    },
    getStatusColor(i){
      switch (i){
        case 0://待提交
        case 1://待审核
          return 'yelColor'
        case 2://审核通过
        case 6://代打款
        case 7://已打款
          return 'greColor'
        case 3://审核不通过
          return 'oraColor'
        case 4://报销失败
          return 'garyColor'
        default:
          return 'yelColor'
      }
    },
  }
}
</script>

<style scoped lang="scss">
@import "@/styles/mixin.scss";
@import "@/styles/common.scss";
.wrap{
  @include wh(100%,100%);
  box-sizing: border-box;
  overflow-y: auto;
}
.navBar{
  margin-bottom: 46px;
}
::v-deep .van-nav-bar__content{
  background-color: #000000 !important;
}
.tab_wrap {
  @include fontWeightSize(500, 15px);
  @include flexColAround();
  padding: 2px 0 10px 0;
  @include bgFontColor(#FFFFFF,#101011);
  position: fixed;
  width: 100%;
  z-index: 111;
  div {
    padding-top: 8px;
    @include colorOpa(#FFFFFF,0.5);
  }
  .active {
    position: relative;
    opacity: 1;
  }
  .active:after {
    content: '';
    display: block;
    @include wh(18px,2px);
    background: #FFFFFF;
    position: absolute;
    border-radius: 2px;
    margin-top: 3px;
    left: 50%;
    transform: translateX(-50%);
  }
}
.monthSearch{
  @include wh(100%,45px);
  background: #F4F5F7 ;
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  width: 100%;
  z-index: 111;
  top: 84px;
  span{
    display: inline-block;
    @include wh(102px,25px);
    box-shadow: 0px 1px 2px 0px rgba(236,236,236,0.5);
    border-radius: 14px;
    margin-right: 8px;
    line-height: 25px;
    text-align: center;
    @include fontWeightSize(600,14px);
    background: #FFFFFF;
  }
  .active{
    background: #3687FF;
    color: #FFFFFF;
  }
}
::v-deep .van-pull-refresh{
  //height: calc(100% - 140px);
  overflow-y: auto;
  background-color: #F4F5F7;
}
.firstpullRefresh{
  height: calc(100% - 140px) ;
}
.secondpullRefresh{
  height: calc(100% - 91px) ;
}
::v-deep .van-list__finished-text{
  padding-bottom: 70px;
  background-color: #F4F5F7;
}
.listWrap{
  //@include wh(100%,100%);
  width: 100%;
  height: calc(100% - 50px);
  box-sizing: border-box;
  padding: 0px 7px 10px 8px;
  overflow-y: auto;
  position: relative;
  background-color:#F4F5F7 ;
  .itemWrap{
    position: relative;
    width: 100%;
    @include flexColBet();
    box-sizing: border-box;
    padding: 14px 12px 11px 0;
    box-shadow: 0px 0px 8px 0px rgba(98,107,128,0.07);
    border-radius: 10px;
    border: 1px solid #4C81F5;
    margin-bottom: 10px;
    background-color: #FFFFFF;
    .contentWrap{
      width: 100%;
      @include fontWeightSize(400,12px);
      line-height: 27px;
      .line1{
        @include flexColBet();
      }
      span:first-child{
          display:inline-block;
          width: 60px;
          opacity: .5;
        }
      .right{
        @include flexCenter();
      }
    }
    .checkImg{
      position: absolute;
      top: -1px;
      left: -1px;
      @include wh(25px,27px)
    }
  }
}
.submitBtnWrap{
  background-color: #F4F5F7;
  height: 50px;
  width: 100%;
  position: fixed;
  bottom: 10px;
  @include flexTwoCenter;
}
.submitBtn{
  //position: absolute;
  //position:;
  //bottom: 15px;
  //margin:20px 0 10px 6%;
  border-radius: 4px;
  text-align: center;
  @include bgFontColor(#FFFFFF,#333333);
  @include fontWeightSize(bold,15px);
  @include whLin(88%,48px);
}
.commonImg{
  @include widHeiMar(13px,13px,4px);
}
.rightArrImg{
  @include wh(6px,9px);
  margin-left: 6px;
}
.yelColor{
  color: #E69B0B;
}
.greColor{
  color: #56B628;
}
.oraColor{
  color: #FF5D2E;
}
.garyColor{
  color: #9C9C9C;
}
.bgEmptyImg{
  @include flexTwoCenter;
  height: 90% ;
  background-color: #FAFAFA;
  img{
    width: 100%;
  }
}
//::v-deep .van-pull-refresh__track{
//  position: static !important;
//}
</style>
