<template>
	<view v-if="detail" class="entry-detail-wrap">
		<safe-nav-bar title="申请详情" />
		<view class="form-page">
		<view class="form-card detail-card">
			<view class="detail-row">
				<text class="detail-row__label">申请单号</text>
				<text class="detail-row__val">{{ detail.applyNo }}</text>
			</view>
			<view class="detail-row">
				<text class="detail-row__label">状态</text>
				<text class="detail-row__val" :style="{ color: statusInfo.color }">{{ statusInfo.label }}</text>
			</view>
			<view class="detail-row">
				<text class="detail-row__label">申请时间</text>
				<text class="detail-row__val">{{ detail.applyTime || '—' }}</text>
			</view>
			<view v-if="detail.auditTime" class="detail-row">
				<text class="detail-row__label">审核时间</text>
				<text class="detail-row__val">{{ detail.auditTime }}</text>
			</view>
			<view
				v-if="detail.applyStatus === APPLY_STATUS_PUBLICITY"
				class="detail-publicity"
			>
				<text class="detail-publicity__title">公示期</text>
				<text>{{ detail.publicityStartTime || '—' }} 至 {{ detail.publicityEndTime || '—' }}</text>
				<text class="detail-publicity__tip">公示结束后将完成开店建档</text>
			</view>
			<view
				v-if="detail.applyStatus === APPLY_STATUS_REJECT && detail.rejectReason"
				class="detail-reject"
			>
				<text class="detail-reject__title">驳回原因</text>
				<text>{{ detail.rejectReason }}</text>
			</view>
			<view v-if="detail.applyStatus === APPLY_STATUS_DONE" class="detail-done">
				<text>入驻已完成，请使用经营账号登录商家后台进行发品与店铺经营。</text>
				<text v-if="detail.merchantId" class="detail-done__sub">商户 ID：{{ detail.merchantId }}</text>
			</view>
		</view>
		<view v-if="detail.applyStatus === APPLY_STATUS_REJECT && canReapply" class="form-footer">
			<button class="form-footer__btn" @click="goReapply">重新申请</button>
		</view>
		</view>
	</view>
</template>

<script setup>
import { ref, computed } from 'vue'
import SafeNavBar from '@/components/common/SafeNavBar.vue'
import { onLoad } from '@dcloudio/uni-app'
import { getMyEntryApplies, getEntryStatus } from '@/api/merchantEntry'
import { ensureApiToken } from '@/utils/apiAuth'
import {
  formatApplyStatus,
  APPLY_STATUS_REJECT,
  APPLY_STATUS_PUBLICITY,
  APPLY_STATUS_DONE,
  canSubmitNewApply
} from '@/utils/entryConstants'
import { PAGE_ENTRY_APPLY } from '@/utils/pageRoute'

const applyId = ref(null)
const detail = ref(null)
const entryOpen = ref(true)
const listCache = ref([])

const statusInfo = computed(() => formatApplyStatus(detail.value?.applyStatus))

const canReapply = computed(() => canSubmitNewApply(listCache.value, entryOpen.value))

onLoad((options) => {
  if (!ensureApiToken()) return
  applyId.value = Number(options.applyId)
  loadDetail()
})

function loadDetail() {
  Promise.all([getMyEntryApplies(), getEntryStatus()])
    .then(([myRes, stRes]) => {
      listCache.value = myRes.data || []
      entryOpen.value = stRes.data?.entryOpen !== false
      detail.value = listCache.value.find((x) => x.applyId === applyId.value) || null
      if (!detail.value) {
        uni.showToast({ title: '申请不存在', icon: 'none' })
      }
    })
    .catch(() => {})
}

function goReapply() {
  uni.navigateTo({ url: PAGE_ENTRY_APPLY })
}
</script>

<style lang="scss" scoped>
@import '@/styles/mine.scss';

.detail-card {
  padding: 16rpx 24rpx;
}
.detail-row {
  display: flex;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f5f5f5;
}
.detail-row__label {
  width: 180rpx;
  font-size: 28rpx;
  color: #9a938c;
}
.detail-row__val {
  flex: 1;
  font-size: 28rpx;
  color: #4a4542;
}
.detail-reject {
  margin-top: 24rpx;
  padding: 20rpx;
  background: #ffebee;
  border-radius: 12rpx;
  color: #c62828;
  font-size: 26rpx;
  line-height: 1.5;
}
.detail-reject__title {
  display: block;
  font-weight: 600;
  margin-bottom: 8rpx;
}
.detail-publicity {
  margin-top: 24rpx;
  padding: 20rpx;
  background: #e3f2fd;
  border-radius: 12rpx;
  font-size: 26rpx;
  color: #1565c0;
  line-height: 1.5;
}
.detail-publicity__title {
  display: block;
  font-weight: 600;
  margin-bottom: 8rpx;
}
.detail-publicity__tip {
  display: block;
  margin-top: 8rpx;
  font-size: 24rpx;
}
.detail-done {
  margin-top: 24rpx;
  padding: 20rpx;
  background: #e8f5e9;
  border-radius: 12rpx;
  font-size: 26rpx;
  color: #2e7d32;
  line-height: 1.5;
}
.detail-done__sub {
  display: block;
  margin-top: 8rpx;
  font-size: 24rpx;
}
</style>
