<template>
	<view class="entry-list-wrap">
		<safe-nav-bar title="我的入驻申请" />
		<view class="entry-list-page">
		<view v-if="loading" class="entry-list-page__loading">加载中…</view>
		<view v-else-if="list.length === 0" class="entry-empty">
			<text>暂无入驻申请记录</text>
			<button class="mine-btn-primary entry-empty__btn" @click="goApply">我要入驻</button>
		</view>
		<view v-else>
			<view
				v-for="item in list"
				:key="item.applyId"
				class="entry-card"
				@click="goDetail(item)"
			>
				<view class="entry-card__row">
					<text class="entry-card__no">{{ item.applyNo }}</text>
					<text class="entry-card__status" :style="{ color: statusStyle(item).color }">
						{{ statusStyle(item).label }}
					</text>
				</view>
				<text class="entry-card__time">申请时间：{{ item.applyTime || '—' }}</text>
				<text
					v-if="item.applyStatus === '2' && item.rejectReason"
					class="entry-card__reject"
				>{{ item.rejectReason }}</text>
			</view>
		</view>
		<view v-if="canApply" class="entry-list-footer">
			<button class="form-footer__btn" @click="goApply">发起新申请</button>
		</view>
		</view>
	</view>
</template>

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

const list = ref([])
const loading = ref(true)
const entryOpen = ref(true)
const canApply = ref(false)

onShow(() => {
  if (!ensureApiToken(false)) return
  loadData()
})

function statusStyle(item) {
  return formatApplyStatus(item.applyStatus)
}

function loadData() {
  loading.value = true
  Promise.all([getMyEntryApplies(), getEntryStatus()])
    .then(([myRes, stRes]) => {
      list.value = myRes.data || []
      entryOpen.value = stRes.data?.entryOpen !== false
      canApply.value = canSubmitNewApply(list.value, entryOpen.value)
    })
    .catch(() => {
      list.value = []
    })
    .finally(() => {
      loading.value = false
    })
}

function goDetail(item) {
  uni.navigateTo({
    url: `${PAGE_ENTRY_DETAIL}?applyId=${item.applyId}`
  })
}

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

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

.entry-list-page {
  min-height: 100vh;
  padding: 24rpx;
  padding-bottom: 160rpx;
  background: #f0ebe5;
}
.entry-empty {
  padding: 100rpx 40rpx;
  text-align: center;
  background: #fff;
  border-radius: 16rpx;
  color: #999;
  font-size: 28rpx;
}
.entry-empty__btn {
  margin-top: 40rpx;
  width: 100%;
}
.entry-card {
  margin-bottom: 20rpx;
  padding: 28rpx;
  background: #fff;
  border-radius: 16rpx;
}
.entry-card__row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.entry-card__no {
  font-size: 28rpx;
  font-weight: 600;
  color: #333;
}
.entry-card__status {
  font-size: 26rpx;
}
.entry-card__time {
  display: block;
  margin-top: 12rpx;
  font-size: 24rpx;
  color: #999;
}
.entry-card__reject {
  display: block;
  margin-top: 12rpx;
  font-size: 24rpx;
  color: #d32f2f;
}
.entry-list-footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 24rpx 32rpx;
  padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
  background: #fff;
}
.entry-list-page__loading {
  padding: 80rpx;
  text-align: center;
  color: #999;
}
</style>
