<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.createTime }}</view>
          <view class="status">{{ item.flag | statusFilter }}</view>
        </view>
        <view class="row">
          <view class="label">申请单号</view>
          <view class="value">{{ item.salesId }}</view>
          <view class="tag">{{ item.goodsType == 'M' ? '辅材' : '配件' }}</view>
        </view>
        <view class="row">
          <view class="label">购买网点</view>
          <view class="value">{{ item.websitName }}</view>
        </view>
        <view class="row">
          <view class="label">申领人员</view>
          <view class="value">{{ item.workerName }}</view>
        </view>
        <view class="row">
          <view class="label">订单金额</view>
          <view class="value price">{{ item.totalAmount | priceFilter }}元</view>
        </view>
        <view class="btns" @tap.stop v-if="item.flag == 'SUBMIT'">
          <u-button text="取消订单" @click="cancelOrder(item)"></u-button>
          <!-- <u-button type="primary" text="立即支付" @click="toPay(item)"></u-button> -->
        </view>
      </view>
    </view>
    <Loading :loadStatus="loadStatus" :dataList="dataList" />
  </zj-page-layout>
  <!-- #endif -->

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

<script>
// #ifdef H5
export default {
  filters: {
    statusFilter(val) {
      const MAP = {
        SAVE: '保存',
        SUBMIT: '待支付',
        PAY_NOT_TAKE: '未提货',
        PAY_TAKE: '已完成',
        CANCEL: '已取消',
      }
      return MAP[val];
    }
  },

  data() {
    return {
		dataList: [],
		pageNum: 1,
		loadStatus: 0,
      refresherTriggered: false,
      tabList: [
        {name: '全部', value: '', badge: {value: 0}},
        {name: '已提交', value: 'SAVE', badge: {value: 0}},
        {name: '待支付', value: 'SUBMIT', badge: {value: 0}},
        {name: '未提货', value: 'PAY_NOT_TAKE', badge: {value: 0}},
        {name: '完成/退货', value: 'PAY_TAKE', badge: {value: 0}},
      ],
      tabCurrent: 0,
      status: '',
    }
  },

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

  onShow(){
    this.getCount();
  },

  methods: {
    getCount() {
    	this.$api.post('/material/salses/count')
      .then(res => {
        if(!res.data) return false;
    		this.tabList[1].badge.value = res.data.saveCount || 0;
    		this.tabList[2].badge.value = res.data.waitPayCount || 0;
    		this.tabList[3].badge.value = res.data.endPayCount || 0;
    		this.tabList[4].badge.value = res.data.endCount || 0;
    	})
    },

    getList() {
    	this.loadStatus = 1;
    	this.$api.post('/material/salses/buy/list', {
        pageNum: this.pageNum,
        pageSize: 10,
        flag: 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);
        this.getCount();
    	}).catch(() => {
    		this.loadStatus = 2;
    	}).finally(res => {
    		this.refresherTriggered = false;
    	})
    },

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

    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/stock/buyDetail?id=${item.salesId}`
      })
    },

    // 取消订单
    cancelOrder(item) {
    	this.$modal({
    	  content: '确认取消订单吗？'
    	}).then(() => {
    	  this.$api.post('/material/salses/cancel', {
    	    salesId: item.salesId
    	  }).then(res => {
    	    this.$successToast();
    	    this.getCount();
    	    this.refreshList();
    	  })
    	}).catch(() => {})
    },

    toPay(item) {
      this.$navToPage({
        url: `/packageMaterial/pages/apply/pay?orderId=${item.salesId}`
      })
    }
  },
}

// #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;
        }
      }
    }
    .btns {
      display: flex;
      justify-content: flex-end;
      margin-top: 30rpx;
      ::v-deep .u-button {
        width: 200rpx;
        margin: 0;
        margin-left: 20rpx;
      }
    }
  }
}
</style>
