<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="tabCurrent"
      		@click="changeTab"
      		lineColor="#3D8FFD"
      		:activeStyle="{
      			color: '#3D8FFD'
      		}"
      		:inactiveStyle="{
      			color: '#666666'
      		}"
      		itemStyle="padding-left: 0; padding-right: 0; 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 top">
          <view class="label">申请时间</view>
          <view class="value">{{item.createTime}}</view>
          <view class="status">{{item.status | statusFilter}}</view>
        </view>
        <view class="row">
          <view class="label">申请单号</view>
          <view class="value">{{item.id}}</view>
          <view class="tag" @tap.stop="$copy(item.id)">复制</view>
        </view>
        <view class="row">
          <view class="label">关联工单</view>
          <view class="value">{{item.orderBaseId}}</view>
          <view class="tag" @tap.stop="$copy(item.orderBaseId)">复制</view>
        </view>
        <view class="row">
          <view class="label">客户电话</view>
          <view class="value">{{item.userMobile}}</view>
        </view>
        <view class="row">
          <view class="label">配件数量</view>
          <view class="value">{{item.totalQty}}</view>
        </view>
        <view class="row" v-if="item.status == 'END'">
          <view class="label">到货时间</view>
          <view class="value">{{item.confirmTime}}</view>
        </view>
        <view class="row" v-if="item.status == 'CANCEL'">
          <view class="label">取消时间</view>
          <view class="value">{{item.cancelTime}}</view>
        </view>
        <view class="btns" @tap.stop v-if="item.status == 'ING'">
          <u-button text="取消申请" @click="cancelOrder(item)"></u-button>
        </view>
      </view>
    </view>
    <Loading :loadStatus="loadStatus" :dataList="dataList" />

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

<script>
  export default {
    filters: {
      statusFilter(val) {
        const MAP = {
          ING: '申请中',
          END: '已到货',
          CANCEL: '已取消',
        }
        return MAP[val];
      }
    },

    data() {
      return {
				dataList: [],
				pageNum: 1,
				loadStatus: 0,
        refresherTriggered: false,
        tabList: [
          {name: '申请中', value: 'ING', badge: {value: 0}},
          {name: '已到货', value: 'END', badge: {value: 0}},
          {name: '已取消', value: 'CANCEL', badge: {value: 0}},
        ],
        tabCurrent: 0,
        status: '',
      }
    },

    onLoad({tab = 'ING'}) {
      this.status = tab;
      this.tabCurrent = this.tabList.map(o => o.value).indexOf(tab);

      this.getCount();
      this.getList();
    },

    methods: {
      getCount() {
      	this.$api.post('/material/parts/apply/count')
        .then(res => {
          if(!res.data) return false;
      		this.tabList[0].badge.value = res.data.partsIngCount || 0;
      		this.tabList[1].badge.value = res.data.partsEndCount || 0;
      		this.tabList[2].badge.value = res.data.partsCancelCount || 0;
      	})
      },

      getList() {
      	this.loadStatus = 1;
      	this.$api.post('/material/parts/apply/list', {
          pageNum: this.pageNum,
          pageSize: 10,
          status: this.status,
      	}).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;
      	})
      },

      changeTab(e) {
      	this.tabCurrent = e.index;
      	this.status = e.value;
        this.refreshList();
        this.getCount();
      },

      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();
      },

      toDetail(item) {
        this.$navToPage({
          url: `/packageMaterial/pages/partsApply/detail?id=${item.id}`
        })
      },

      // 取消申请
      cancelOrder(item) {
      	this.$modal({
      	  content: '确认取消申请吗？'
      	}).then(() => {
      	  this.$api.post('/material/parts/apply/cancel', {
      	    id: item.id
      	  }).then(res => {
      	    this.$successToast();
      	    this.getCount();
      	    this.refreshList();
      	  })
      	}).catch(() => {})
      },
    },
  }
</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;
      align-items: center;
      margin-top: 20rpx;
      &:first-child {
        margin-top: 0;
      }
      .label {
        font-size: 28rpx;
        color: $sec-font;
        margin-right: 20rpx;
      }
      .value {
        flex: 1;
        font-size: 28rpx;
        text {
          color: $minor-color;
        }
        &.price {
          color: $theme-color;
        }
      }
      .tag {
        font-size: 28rpx;
        color: $theme-color;
      }
      &.top {
        .label {
          color: $main-font;
          font-weight: 500;
        }
        .value {
          font-weight: 500;
        }
        .status {
          font-size: 28rpx;
          color: $warning-color;
        }
      }
    }
    .btns {
      display: flex;
      justify-content: flex-end;
      margin-top: 30rpx;
      ::v-deep .u-button {
        width: 200rpx;
        margin: 0;
        margin-left: 20rpx;
      }
    }
  }
}
</style>
