<template>
  <z-paging
    ref="pagingRef"
    v-model="records"
    :paging-style="{'top': '0','background-color': '#f2f3f7'}"
    empty-view-text="您还没有提现记录"
    loading-more-no-more-text="没有更多了~"
    class="Mall4j page-income-details"
    @query="onLoadPageData"
  >
    <view class="Mall4j page-withdrawal-record">
      <view class="total">
        <text>总提现：</text>
        <text class="number red">
          ￥{{ totalWithdrawCash }}
        </text>
      </view>
      <view
        v-if="records"
        class="record-box"
      >
        <view
          v-for="(item,index) in records"
          :key="index"
          class="item-box"
        >
          <view
            v-if="!(index>=1 && records[0].date === item.date)"
            class="date"
          >
            {{ (item.date.split("-")[0] === nowTime[0] && item.date.split("-")[1] === nowTime[1])?'本月':item.date.split("-")[0]+'-'+item.date.split("-")[1] }}
          </view>
          <block v-if="paySettlementType !== 1 && item && item.distributionWithdrawCashList">
            <view
              v-for="(withdraw,wid) in item.distributionWithdrawCashList"
              :key="wid"
              class="item"
            >
              <view class="text-box">
                <view class="text">
                  {{ withdraw.moneyFlow ===1 ? '线上打款' : '线下打款' }}
                </view>
                <view class="time">
                  {{ withdraw.createTime }}
                </view>
              </view>
              <view class="right">
                <view :class="['number',withdraw.state===1?'':'red']">
                  + {{ wxs.parsePrice(withdraw.amount)[0] }}.{{ wxs.parsePrice(withdraw.amount)[1] }}
                </view>
                <view
                  v-if="withdraw.state===-1"
                  class="state"
                >
                  {{ $t('distribution.theFallback') }}
                </view>
                <view
                  v-if="withdraw.state===0"
                  class="state"
                >
                  {{ $t('distribution.withdrawing') }}
                </view>
                <view
                  v-if="withdraw.state===1"
                  class="state"
                >
                  {{ $t('distribution.hasArrived') }}
                </view>
                <view
                  v-if="withdraw.state===2"
                  class="state"
                >
                  {{ $t('distribution.rejection') }}
                </view>
              </view>
            </view>
          </block>
          <block v-if="paySettlementType === 1 && item && item.withdrawCashVOs">
            <view
              v-for="(withdraw,wid) in item.withdrawCashVOs"
              :key="wid"
              class="item"
            >
              <view class="text-box">
                <view class="time">
                  {{ withdraw.createTime }}
                </view>
              </view>
              <view class="right">
                <view :class="['number',withdraw.status===2?'':'red']">
                  + {{ wxs.parsePrice(withdraw.amount)[0] }}.{{ wxs.parsePrice(withdraw.amount)[1] }}
                </view>
                <view
                  v-if="withdraw.status===0"
                  class="state"
                >
                  {{ $t('distribution.unpaid') }}
                </view>
                <view
                  v-if="withdraw.status===1"
                  class="state"
                >
                  {{ $t('distribution.applicationApproved') }}
                </view>
                <view
                  v-if="withdraw.status===2"
                  class="state"
                >
                  {{ $t('distribution.successfulWithdrawal') }}
                </view>
                <view
                  v-if="withdraw.status===3"
                  class="state"
                >
                  {{ $t('distribution.withdrawalFailed') }}
                </view>
              </view>
            </view>
          </block>
        </view>
      </view>
    </view>
  </z-paging>
</template>

<script setup>
import Big from 'big.js'
const wxs = number()

const paySettlementType = uni.getStorageSync('cloudPaySettlementType') // 1:通联支付

const nowTime = ref([])
onLoad(() => {
  const date = new Date()
  const year = date.getFullYear()
  const month = date.getMonth() + 1 > 10 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1)
  nowTime.value = [year + '', month]
  onGetTotalWithdrawCash()
})

/**
 * 获取提现总金额
 */
const totalWithdrawCash = ref(0)
const onGetTotalWithdrawCash = () => {
  const url = paySettlementType === 1 ? '/tmerclub_user/user_withdraw/get_total_withdraw_amount' : '/tmerclub_marketing/distribution_withdraw_cash/total_withdraw_cash'
  http.request({
    url,
    method: 'GET'
  }).then(res => {
    Big.DP = 2
    totalWithdrawCash.value = new Big(res).div(100).valueOf()
  })
}

/**
 * 分页获取提现记录
 */
const pagingRef = ref(null)
const records = ref([])
const onLoadPageData = (pageNum, pageSize) => {
  const url = paySettlementType === 1 ? '/tmerclub_user/user_withdraw/page_date_user_withdraw_cash' : '/tmerclub_marketing/distribution_withdraw_cash/page'
  http.request({
    url,
    method: 'GET',
    data: {
      pageNum,
      pageSize
    }
  }).then(res => {
    uni.hideLoading()
    // 将请求结果通过complete传给z-paging处理，同时也代表请求结束，这一行必须调用
    pagingRef.value.complete(res.list)
  })
}

</script>

<style lang="scss" scoped>
@use "./withdrawal-record.scss";
</style>
