<template>
  <business-page-layout title="提现审核" :show-back="true">
    <view class="page-wrap">
      <view v-for="item in list" :key="item.id" class="card">
        <view class="row">
          <view class="title-wrap">
            <text class="type-tag">{{ item.userTypeText }}</text>
            <text class="name">{{ item.entityName }}</text>
          </view>
          <text class="amount">{{ item.amountText }}</text>
        </view>
        <view class="sub">订单号：{{ item.orderNo }}</view>
        <view class="sub">申请时间：{{ item.applyTime }}</view>
        <view class="status-row">
          <text class="status" :class="'s' + item.check_status">{{ item.statusText }}</text>
        </view>
        <view v-if="item.check_status === 1" class="actions">
          <button size="mini" class="ok" :disabled="submitting" @click="auditPass(item)">通过</button>
          <button size="mini" class="no" :disabled="submitting" @click="auditRefuse(item)">拒绝</button>
        </view>
      </view>
      <view v-if="!list.length" class="empty">暂无待审核记录</view>
    </view>
  </business-page-layout>
</template>

<script>
import BusinessPageLayout from '@/subPackages/sub_platform_admin/components/business-page-layout.vue'
import { getPlatformAdminContext } from '@/subPackages/sub_platform_admin/common/context.js'
import platformAdminApi from '@/api/platformAdmin.js'
import { formatMoney } from '@/subPackages/sub_business_shared/common/format.js'
import { formatDateTime, withdrawalCheckStatusText } from '@/subPackages/sub_business_shared/common/display.js'

export default {
  components: { BusinessPageLayout },
  provide() { return { businessContext: getPlatformAdminContext() } },
  data() {
    return { list: [], submitting: false }
  },
  onShow() { this.load() },
  methods: {
    mapItem(item) {
      const store = item.store || {}
      const supplier = item.supplier || {}
      return {
        ...item,
        userTypeText: item.user_type_txt || '平台',
        entityName: store.name || supplier.name || '萧康云医',
        amountText: formatMoney(item.apply_cash),
        orderNo: item.order_no || '-',
        applyTime: formatDateTime(item.apply_time),
        statusText: item.check_status_txt || withdrawalCheckStatusText(item.check_status),
      }
    },
    load() {
      platformAdminApi.getWithdrawalAuditList({ page: 1, pageSize: 50, check_status: 1 }).then(w => {
        const items = (w.data && w.data.items) || []
        this.list = items.map(item => this.mapItem(item))
      })
    },
    showError(w) {
      const msg = (w.res && (w.res.message || w.res.msg)) || '操作失败'
      uni.showToast({ title: msg, icon: 'none' })
    },
    submitAudit(item, api, payload) {
      if (this.submitting) return
      this.submitting = true
      api(payload).then(w => {
        if (w.ok) {
          uni.showToast({ title: '操作成功' })
          this.load()
        } else {
          this.showError(w)
        }
      }).finally(() => {
        this.submitting = false
      })
    },
    auditPass(item) {
      uni.showModal({
        title: '审核通过',
        content: `确认通过 ${item.entityName} 的提现申请 ${item.amountText}？`,
        success: (res) => {
          if (!res.confirm) return
          this.submitAudit(item, platformAdminApi.passWithdrawal, {
            id: item.id,
            reason: '审核通过',
          })
        },
      })
    },
    auditRefuse(item) {
      uni.showModal({
        title: '拒绝审核',
        content: '请输入拒绝原因',
        editable: true,
        placeholderText: '拒绝原因',
        success: (res) => {
          if (!res.confirm) return
          const reason = (res.content || '').trim()
          if (!reason) {
            uni.showToast({ title: '请输入拒绝原因', icon: 'none' })
            return
          }
          this.submitAudit(item, platformAdminApi.refuseWithdrawal, {
            id: item.id,
            reason,
          })
        },
      })
    },
  },
}
</script>

<style scoped>
.page-wrap { padding: 24rpx; }
.card { background: #fff; padding: 28rpx; border-radius: 16rpx; margin-bottom: 16rpx; }
.row { display: flex; justify-content: space-between; align-items: flex-start; }
.title-wrap { flex: 1; min-width: 0; }
.type-tag {
  display: inline-block;
  margin-right: 12rpx;
  padding: 4rpx 12rpx;
  background: #E8FFEA;
  color: #00B42A;
  font-size: 22rpx;
  border-radius: 6rpx;
}
.name { font-size: 30rpx; font-weight: 600; }
.amount { font-size: 32rpx; font-weight: 600; color: #1D2129; flex-shrink: 0; margin-left: 16rpx; }
.sub { margin-top: 12rpx; color: #86909C; font-size: 24rpx; }
.status-row { margin-top: 12rpx; }
.status { font-size: 24rpx; }
.s1 { color: #FF7D00; }
.s2 { color: #00B42A; }
.s3 { color: #F53F3F; }
.s4 { color: #F53F3F; }
.actions { display: flex; gap: 16rpx; margin-top: 20rpx; }
.ok { background: #6acdbb; color: #fff; }
.no { background: #F53F3F; color: #fff; }
.empty { text-align: center; color: #86909C; padding: 80rpx 0; }
</style>
