<template>
	<view class="container">
		<!-- 审核中 -->
		<view class="audit-card" v-if="status === 1">
			<view class="audit-icon audit-icon--ing">
				<u-icon name="clock" color="#FF9F0A" size="56"></u-icon>
			</view>
			<view class="audit-title">正在审核中</view>
			<view class="audit-desc">您的店铺资料已提交，工作人员将在 1-3 个工作日内完成审核，请耐心等待。</view>
			<view class="audit-times">
				<view class="audit-time-row"><text>申请时间：</text><text>{{ itemObj.create_time || '—' }}</text></view>
			</view>
		</view>

		<!-- 审核通过 -->
		<view class="audit-card" v-else-if="status === 2">
			<view class="audit-icon audit-icon--ok">
				<u-icon name="checkmark" color="#34C759" size="56"></u-icon>
			</view>
			<view class="audit-title">审核通过</view>
			<view class="audit-desc">恭喜您，您的店铺资料已同意审核</view>
			<view class="audit-times">
				<view class="audit-time-row"><text>申请时间：</text><text>{{ itemObj.create_time || '—' }}</text></view>
				<view class="audit-time-row"><text>审核时间：</text><text>{{ itemObj.handle_time || '—' }}</text></view>
			</view>
		</view>

		<!-- 审核失败 -->
		<view class="audit-card" v-else-if="status === 3">
			<view class="audit-icon audit-icon--fail">
				<u-icon name="close" color="#FF3B30" size="56"></u-icon>
			</view>
			<view class="audit-title">审核失败</view>
			<view class="audit-desc">很抱歉，您的资料未通过审核，请修改后重新提交。</view>

			<view class="reject-box" v-if="itemObj.remark || rejectImages.length">
				<view class="reject-title">
					<u-icon name="error-circle" color="#FF370B" size="28"></u-icon>
					<text>驳回原因</text>
				</view>
				<view class="reject-text">{{ itemObj.remark || '—' }}</view>
				<view class="reject-imgs" v-if="rejectImages.length">
					<view class="reject-imgs-label">问题截图</view>
					<view class="reject-thumbs">
						<image v-for="(u, i) in rejectImages" :key="i" :src="u" class="reject-thumb"
							mode="aspectFill" @click="preview(rejectImages, i)" />
					</view>
				</view>
			</view>

			<view class="audit-times">
				<view class="audit-time-row"><text>申请时间：</text><text>{{ itemObj.create_time || '—' }}</text></view>
				<view class="audit-time-row"><text>审核时间：</text><text>{{ itemObj.handle_time || '—' }}</text></view>
			</view>
		</view>

		<view class="audit-footer" v-if="status === 3">
			<view class="btn-primary" @click="resubmit">修改资料并重新提交</view>
		</view>
	</view>
</template>

<script>
import { NavgateTo } from '../../../utils/index';
import { signPrivateView } from '../../../utils/uploadOSS.js';
export default {
	data() {
		return {
			itemObj: {},
			rejectImages: [],
		}
	},
	computed: {
		status() {
			return Number(this.itemObj.status) || 0
		}
	},
	onLoad(options) {
		if (options && options.itemObj) {
			try {
				this.itemObj = JSON.parse(decodeURIComponent(options.itemObj))
			} catch (e) {
				console.error('解析审核状态参数失败:', e)
				this.itemObj = {}
			}
		} else if (options && options.status) {
			// 兼容老版本：仅带 status，没有完整对象
			this.itemObj = { status: Number(options.status) }
		}
		// 驳回问题截图：后端 reject_images 字段（逗号分隔的私密 key），有则签名展示
		if (this.itemObj.reject_images) {
			this.signRejectImages(this.itemObj.reject_images)
		}
	},
	methods: {
		async signRejectImages(paths) {
			const list = (paths || '').split(',').filter(Boolean)
			const result = []
			for (const p of list) {
				try {
					const r = await signPrivateView(p)
					result.push(r.url)
				} catch (e) {
					console.error('签发驳回截图 URL 失败:', p, e)
				}
			}
			this.rejectImages = result
		},
		preview(urls, current) {
			if (!urls || !urls.length) return
			uni.previewImage({ urls, current: urls[current] })
		},
		resubmit() {
			// 编辑数据较大（含多组图片路径），用本地存储中转，避免 URL 过长被截断
			try {
				uni.setStorageSync('shopEnterEditData', this.itemObj)
			} catch (e) {
				console.error('暂存编辑数据失败:', e)
			}
			NavgateTo('/packages/shopEnter/index/index?edit=1');
		},
		// 返回首页（审核态为终态，避免回退到表单/确认页造成二次提交）
		goHome() {
			uni.reLaunch({ url: '/pages/index/index' })
		}
	},
	// 拦截物理返回 / 左上角返回：统一回首页，而非弹出上一页
	onBackPress(e) {
		// 编辑页发起的二次提交流程（status 3 驳回后点重新提交跳走）不拦截
		if (e && e.from === 'backbutton') {
			uni.reLaunch({ url: '/pages/index/index' })
			return true
		}
		return false
	}
}
</script>

<style>
@import url("./index.css");
</style>
