<template>
  <zj-page-layout
    :isScroll="true"
    :refresherTriggered="refresherTriggered"
    @refresherrefresh="refresherrefresh"
    @scrolltolower="scrolltolower">
    <template slot="header">
      <view class="tab-container">
      	<u-tabs
      		:scrollable="false"
      		:list="tabList"
      		:current="curTab"
      		@click="changeTab"
      		lineColor="#3D8FFD"
      		:activeStyle="{
      			color: '#3D8FFD'
      		}"
      		:inactiveStyle="{
      			color: '#666666'
      		}"
      		itemStyle="height: 88rpx;">
      	</u-tabs>
      </view>
    </template>

    <view class="list-container">
      <view class="item" v-for="(item, index) in dataList" :key="index" @tap="toDetail(item)">
        <view class="row">
          <view class="label">订单编号</view>
          <view class="value">{{item.orderId}}</view>
          <view class="status">{{item.status | statusFilter}}</view>
        </view>
        <view class="row">
          <view class="label">订单类型</view>
          <view class="value">{{item.orderType | orderTypeFilter}}</view>
        </view>
        <view class="row">
          <view class="label">发票类型</view>
          <view class="value">{{item.taxType | taxTypeFilter}}</view>
        </view>
        <view class="row">
          <view class="label">发票内容</view>
          <view class="value">{{item.content}}</view>
        </view>
        <view class="row">
          <view class="label">抬头类型</view>
          <view class="value">{{item.type | typeFilter}}</view>
        </view>
        <view class="row">
          <view class="label">开票金额</view>
          <view class="value">{{item.amount | priceFilter}}</view>
        </view>
      </view>
    </view>
    <Loading :loadStatus="loadStatus" :dataList="dataList" />

  </zj-page-layout>
</template>

<script>
  export default {
    filters: {
      statusFilter(val) {
        const MAP = {
          true: '已开票',
          false: '申请中',
        }
        return MAP[val];
      },
      orderTypeFilter(val) {
        const MAP = {
          INCRE: '增值服务',
          ORDER: '商城订单',
        }
        return MAP[val]
      },
      taxTypeFilter(val) {
        const MAP = {
          true: '专用发票',
          false: '电子普通发票',
        }
        return MAP[val]
      },
      typeFilter(val) {
        const MAP = {
          true: '公司',
          false: '个人',
        }
        return MAP[val]
      },
    },

    data() {
      return {
        dataList: [],
        pageNum: 1,
        loadStatus: 0,
        refresherTriggered: false,
        tabList: [
        	{key: '', name: '全部', badge: { value: 0 }},
        	{key: false, name: '申请中', badge: { value: 0 }},
        	{key: true, name: '已完成', badge: { value: 0 }},
        ],
        tabCurrent: '',
      }
    },

    computed:{
    	curTab() {
    		return this.tabList.map(item => item.key).indexOf(this.tabCurrent) || 0;
    	}
    },

    onLoad() {
      this.getCount();
      this.getList();

      this.crossPage.$on('refreshInvoiceList', () => {
        this.getCount();
        this.refreshList();
      })
    },

    // onUnload() {
    //   this.crossPage.$off('refreshInvoiceList');
    // },

    methods: {
      getCount() {
      	this.$api.get('/user/order/tax/count', {
          userId: this.$store.state.user.userId
        }).then(res => {
          if(!res.data) return false;
      		this.tabList[0].badge.value = res.data.qb || 0;
      		this.tabList[1].badge.value = res.data.sqz || 0;
      		this.tabList[2].badge.value = res.data.ywc || 0;
      	})
      },

      getList() {
      	this.loadStatus = 1;
      	this.$api.get('/user/order/tax/list', {
          userId: this.$store.state.user.userId,
          pageNum: this.pageNum,
          pageSize: 10,
          status: this.tabCurrent
      	}).then(res => {
      		this.loadStatus = 0;
      		let list = res.data.records;
      		if (list.length < 10) {
      		  this.loadStatus = 2;
      		}
      		this.dataList = this.dataList.concat(list);
      	}).catch(() => {
      		this.loadStatus = 2;
      	}).finally(res => {
      		this.refresherTriggered = false;
      	})
      },

      refreshList() {
      	this.dataList = [];
      	this.pageNum = 1;
      	this.getList();
      },

      // 滚动到底部
      scrolltolower(e) {
      	if (this.loadStatus === 0) {
      		this.pageNum++;
      		this.getList();
      	}
      },

      // 触发下拉刷新
      refresherrefresh(e) {
      	this.refresherTriggered = true;
        this.getCount();
      	this.refreshList();
      },

      // 切换类型
      changeTab(item) {
      	this.tabCurrent = item.key;
        this.getCount();
      	this.refreshList();
      },

      toDetail(item) {
        this.$navToPage({
          url: `/packageMine/pages/invoice/detail?orderTaxId=${item.orderTaxId}`
        })
      }
    },
  }
</script>

<style lang="scss" scoped>
.tab-container {
  background: #ffffff;
}
.list-container {
  padding: 0 20rpx;
  .item {
    @include zj-card;
    margin-top: 20rpx;
    padding: 30rpx;
    .row {
      display: flex;
      margin-top: 20rpx;
      &:first-child {
        margin-top: 0;
      }
      .label {
        font-size: 28rpx;
        color: $sec-font;
        margin-right: 20rpx;
      }
      .value {
        flex: 1;
        font-size: 28rpx;
      }
      .status {
        font-size: 28rpx;
        color: $theme-color;
      }
    }
  }
}
</style>
