<template>
  <!-- #ifdef H5 -->
  <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.partsRetId}}</view>
          <view class="status">{{item.refundState | statusFilter}}</view>
        </view>
        <view class="product">
          <view class="it" v-for="(it, idx) in item.items" :key="idx">
            <view class="name">{{it.goodsName}}<text>×{{item.type == 'NEW' ? it.newRefundQty : it.oldRefundQty}}{{it.goodsSalesUnit}}</text></view>
            <view class="price">¥{{it.refundAmount | priceFilter}}</view>
          </view>
            <view class="total">
              <view class="text">金额：</view>
              <view class="price">¥{{item.totalRefundAmount | priceFilter}}</view>
            </view>
        </view>
        <view class="row">
          <view class="label">网点</view>
          <view class="value">{{item.websitName}}({{item.websitId}})</view>
        </view>
      </view>
    </view>
		<Loading :loadStatus="loadStatus" :dataList="dataList" />
  </zj-page-layout>
  <!-- #endif -->

  <!-- #ifndef H5 -->
  <web-view :src="webViewHref(`/packageMaterial/pages/stock/returnList`, pam, crossPagePam)" @message="crossPage.$listener"></web-view>
  <!-- #endif -->
</template>

<script>
  // #ifdef H5
  export default {
    filters: {
      statusFilter(val) {
        const MAP = {
          OK: '已返还',
          WAIT: '待返还',
          FAIL: '返还失败',
        }
        return MAP[val];
      }
    },

    data() {
      return {
        type: 'NEW',
        tabList: [
          {name: '全部', value: 0, badge: {value: 0}},
          {name: '待返还', value: 1, badge: {value: 0}},
          {name: '已返还', value: 2, badge: {value: 0}},
        ],
        tabCurrent: 0, // 当前选择值
				count: {},
				dataList: [],
				pageNum: 1,
				loadStatus: 0,
				refresherTriggered: false, // 下拉刷新状态
      }
    },

    onLoad({type}) {
      this.type = type;
      uni.setNavigationBarTitle({
      　title: type == 'NEW' ? '新件返还' : '旧件返还'
      })
      this.getCount();
      this.getList();
    },

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

      getList() {
        const MAP = {
          0: '',
          1: 'SUBMIT',
          2: 'OK',
        }
      	this.loadStatus = 1;
      	this.$api.post('/material/parts/refund/list', {
          pageNum: this.pageNum,
          pageSize: 10,
          status: MAP[this.tabCurrent],
          type: this.type,
      	}).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;
      	})
      },

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

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

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

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

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

  // #endif
  // #ifndef H5
  export default {
    data() {
      return {
        pam: {},
      }
    },
    onLoad(pam) {
      this.pam = pam;
    }
  }
  // #endif
</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;
        }
      }
    }
    .product {
      background: #f5f5f5;
      border-radius: 12rpx;
      padding: 20rpx;
      margin-top: 20rpx;
      .it {
        display: flex;
        align-items: flex-end;
        .name {
          flex: 1;
          font-size: 28rpx;
          font-weight: 500;
          text {
            color: $sec-font;
            margin-left: 12rpx;
            font-weight: normal;
          }
        }
        .price {
          margin-left: 30rpx;
          font-size: 28rpx;
          font-weight: 500;
        }
      }
      .total {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        margin-top: 30rpx;
        .text {
          font-size: 28rpx;
          color: $sec-font;
        }
        .price {
          font-size: 28rpx;
          color: $minor-color;
          font-weight: 500;
        }
      }
    }
  }
}
</style>
