<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 class="search_wrap">
      <div class="search_left">
        <el-select v-model="form.statusValue" size="mini" clearable placeholder="请选择审核状态">
          <el-option
            v-for="item in statusValueOptions"
            :key="item.value"
            :label="item.name"
            :value="item.value">
          </el-option>
        </el-select>
      </div>
      <div class="search_right">
        <van-search
          v-model="form.financeBatchCode"
          show-action
          placeholder="请输入搜索关键词"
          @search="onSearch"
        >
          <template #action>
            <div @click="onSearch">搜索</div>
          </template>
        </van-search>
      </div>
    </div>
    <div class="contentWrap">
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="getMore"
        >
          <div class="itemWrap" v-for="(item,index) in invoiceList" :key="index">
            <div class="item line1">
              <div class="itemTime">
                <span class="title">开票编号:</span>
                <span class="content">{{ item.financeBatchCode }}</span>
              </div>
              <div class="checkBtn" @click="getInvoiceInfo(item)">查看</div>
            </div>
            <div class="item">
              <span class="title">工单数量:</span>
              <span class="content">{{ item.orderNum }}</span>
            </div>
            <div class="item">
              <span class="title">开票金额:</span>
              <span class="content">{{ item.invoiceMoney || 0 }}元</span>
            </div>
            <div class="item">
              <span class="title">创建时间:</span>
              <span class="content">{{ item.createTime }}</span>
            </div>
            <div class="item">
              <span class="title">对账状态:</span>
              <span class="content">{{ statusStr(item.status, item.createTime) }}</span>
            </div>
          </div>
        </van-list>
      </van-pull-refresh>

    </div>
  </div>
</template>

<script>
  import { myMixins } from "@/utils/myMixins"
  import { getFinanceBatchList } from  "@/api/mine"
  export default {
    name: "invoiceListInfo",
    mixins: [ myMixins ],
    data() {
      return {
        form: {
          statusValue: '',
          financeBatchCode: '',
          statusList: [],
        },
        invoiceList: [],
        isLoading: false,
        loading: false,
        finished: false,
        page: {
          current: 1,
          pageSize: 10,
          total: 0,
          pageNum: 1,
        },
        supplierId: '',
        statusValueOptions:[{name:'待审核',value:1},{name:'审核通过待打款',value: 2},{name:'审核失败',value: 3},{name:'打款中',value: 4},{name:'已打款',value: 5}],
      }
    },
    watch:{
      async 'form.statusValue' (){
        if( this.form.statusValue == 1 ) {
          this.form.statusList = [1]
        } else if( this.form.statusValue == 2 ) {
          this.form.statusList = [2, 3]
        } else if( this.form.statusValue == 3 ) {
          this.form.statusList = [7]
        } else if( this.form.statusValue == 4 ) {
          this.form.statusList = [8]
        } else if( this.form.statusValue == 5 ) {
          this.form.statusList = [4]
        } else {
          this.form.statusList = []
        }
      }
    },
    async mounted() {
      this.supplierId = this.$route.query.supplierId;
    },
    methods: {
      async onSearch() {
        this.page.pageNum=1;
        await this.getList()
      },
      onRefresh() {
        this.page.pageNum=1;
        this.getList();
        setTimeout(() => {
          this.$toast('刷新成功');
          this.isLoading = false;
        }, 1000);
      },
      async getMore(){
        await this.getList()
        this.page.pageNum++;
        // 加载状态结束
        this.loading = false;
        // 数据全部加载完成
        if (this.invoiceList.length >= this.page.total) {
          this.finished = true;
        }
      },
      async getList() {
        let res = await getFinanceBatchList({
          ...this.page,
          ...this.form,
          supplierId: this.supplierId
        });
        if(res.code == 200){
          this.page.total = res.total;
          if(this.page.pageNum == 1){
            this.invoiceList = res.data;
          }else{
            let preList = this.invoiceList;
            let arr = res.data;
            this.invoiceList = preList.concat(arr)
          }
          if (this.invoiceList.length === 0) {
            this.show=true
          }else{
            this.show=false
          }
        }
      },
      getInvoiceInfo(item) {
        localStorage.setItem('showItemInfo', JSON.stringify(item));
        this.goPage('invoiceInfo', {
          id: item.id
        })
      },
      statusStr(status,date) {
        if(new Date('2024-01-01 00:00:00') > new Date(date)){
          return ''
        }
        if( status == 1 ) {
          return '待审核'
        } else if( [2 ,3].includes(status)) {
          return '审核通过'
        } else if( status == 7 ) {
          return '审核失败'
        } else if( status == 4 ) {
          return '已打款'
        } else if( status == 8 ) {
          return '打款中'
        } else {
          return ''
        }
      },
    }
  }
</script>

<style scoped lang="scss">
  @import "@/styles/mixin.scss";
  @import "@/styles/common.scss";
  .wrap{
    @include wh(100%,100%);
    background-color: #F4F5F7;
    overflow-y: auto;
    box-sizing: border-box;
  }
  .navBar{
    margin-bottom: 46px;
  }
  .contentWrap {
    background-color: #F4F5F7;
    width: 100%;
    box-sizing: border-box;
    padding: 10px;
  }
  .search_wrap {
    display: flex;
    align-items: center;
    padding-left: 10px;
    background-color: #fff;
    .search_left {
      width: 100px;
      font-size: 12px;
    }
    .search_right {
      flex: 1;
    }
  }

  .itemWrap{
    background: #FFFFFF;
    position: relative;
    width: 100%;
    box-sizing: border-box;
    padding: 15px;
    border-radius: 6px;
    line-height: 26px;
    @include flexBetween;
    margin-bottom: 10px;
    .item{
      @include fontWeightSize(400,12px);
      span{
        display: inline-block;
      }
      .title{
        opacity: .5;
        margin-right: 5px;
      }
    }
    .line1{
      width: 100%;
      @include flexColBet;
      .checkBtn{
        @include bgFontColor(#FFFFFF,#354D93);
        @include wh(48px,18px);
        border-radius: 3px;
        line-height: 18px;
        text-align: center;
      }
    }
  }
</style>
