<template>
  <div class="my-afsorder">
    <!-- 中间内容 -->
    <div class="content">
      <!-- 商品 -->
      <div class="order-item"
        v-for="afsOrder in afsOrders"
        :key="afsOrder.afsOrderID"
        @click="toDetail(afsOrder)"
      >
        <AfsorderCell
          :afsOrder="afsOrder"
          @on-click="handleOp"
        ></AfsorderCell>
      </div>
      <!-- 没有查到商品 -->
      <div class="no-order" v-if="noOrderShow">
        <no-list msg="这里没有售后单" img="img-noOrders"></no-list>
      </div>
    </div>
    <zy-loading></zy-loading>
  </div>
</template>

<script>
import {errToast, alert, toast, modal,jumpPage} from '@/utils/uniapi.js'
import ZyLoading from '@/components/Loading/loading'
import NoList from '@/components/commonCmp/list-none.vue'
import AfsorderCell from '../afsOrderCmp/afsorder-cell.vue'
import orderPage from '@/apis/orderPage'
export default {
  components: {
    ZyLoading, NoList, AfsorderCell
  },
  data () {
    return {
      page: 0,
      totalCount: 0,
      afsOrders: [],
      offset: 0,
      pageSize: 50,
      noOrderShow: false
    }
  },
  async onLoad () {
    await this.getAfsOrders()
  },
  // 下拉刷新
  async onPullDownRefresh () {
    console.log('下拉刷新')
    await this.pageInit()
    uni.stopPullDownRefresh()
    // this.hideLoad()
  },
  async onReachBottom () {
    console.log('到底加载更多')
    try {
      this.showLoad()
      let {page, pageSize, totalCount} = this
      if (page * pageSize < totalCount) {
        await this.getAfsOrders('next')
      } else {
        toast('没有更多')
      }
    } catch (e) {
      errToast(e)
    } finally {
      this.hideLoad()
    }
  },
  methods: {
    async pageInit () {
      try {
        this.showLoad()
        await this.getAfsOrders()
      } catch (e) {
        errToast(e)
      } finally {
        this.hideLoad()
      }
    },
    async getAfsOrders(type) {
      try {
        const {pageSize} = this
        let offset = 0
        if (type === 'next') {
          this.page += 1
          offset = (this.page - 1) * pageSize
        } else {
          this.page = 1
        }
        // const {totalCount, data} = await getAfsOrders({offset, pageSize})
        const {totalCount, data} = await orderPage.getAfsOrders({offset, pageSize})
        this.totalCount = totalCount
        if (type === 'next') {
          this.afsOrders = [...this.afsOrders, ...data]
        } else {
          this.afsOrders = data
        }
        console.log('获取的售后单', totalCount, data)
        if (totalCount === 0 && this.page === 1) {
          this.noOrderShow = true
        }
      } catch (e) {
        throw e
      }
    },
    toDetail (afsOrder) {
      const {vendorOrderID, vendorID, afsOrderID} = afsOrder
      jumpPage('navigateTo',`/pagesOrders/afsorder-detail/index?vendorOrderID=${vendorOrderID}&afsOrderID=${afsOrderID}&vendorID=${vendorID}`)
    }
  }
}
</script>

<style lang="scss">
@import "my-afsorder";
</style>