<template>
	<view class="container">
		<!-- 顶部进度条 -->
		<view class="step-bar">
			<view class="step-pill active">填写信息</view>
			<view class="step-pill">提交审核</view>
			<view class="step-pill">入驻成功</view>
		</view>

		<view class="confirm-head">请确认以下信息</view>

		<!-- 基本信息 -->
		<view class="cf-group">
			<view class="cf-group-title">基本信息</view>
			<view class="cf-row"><text class="cf-k">门店名称</text><text class="cf-v">{{ d.merchant_name || '—' }}</text></view>
			<view class="cf-row"><text class="cf-k">所在地区</text><text class="cf-v">{{ d._region_label || '—' }}</text></view>
			<view class="cf-row"><text class="cf-k">详细地址</text><text class="cf-v">{{ d.address || '—' }}</text></view>
			<view class="cf-row"><text class="cf-k">商家类目</text><text class="cf-v">{{ d._cate_name || '—' }}</text></view>
			<view class="cf-row" v-if="d._biz_status_label"><text class="cf-k">营业状态</text><text class="cf-v">{{ d._biz_status_label }}</text></view>
			<view class="cf-row"><text class="cf-k">负责人姓名</text><text class="cf-v">{{ d.contact_name || '—' }}</text></view>
			<view class="cf-row"><text class="cf-k">负责人手机</text><text class="cf-v">{{ d.phone || '—' }}</text></view>
		</view>

		<!-- 资质信息 -->
		<view class="cf-group">
			<view class="cf-group-title">资质信息</view>
			<view class="cf-row"><text class="cf-k">执照名称</text><text class="cf-v">{{ d.license_name || '—' }}</text></view>
			<view class="cf-row"><text class="cf-k">信用代码</text><text class="cf-v">{{ d.credit_code || '—' }}</text></view>
			<view class="cf-row"><text class="cf-k">营业执照</text><text class="cf-v">{{ d.license_is_forever === 1 ? '永久有效' : (d.license_valid_until || '—') }}</text></view>
			<view class="cf-row" v-if="d.industry_name"><text class="cf-k">证书名称</text><text class="cf-v">{{ d.industry_name }}</text></view>
			<view class="cf-row" v-if="d.industry_cert_no"><text class="cf-k">证书编号</text><text class="cf-v">{{ d.industry_cert_no }}</text></view>
			<view class="cf-row" v-if="d.industry_name"><text class="cf-k">资质有效期</text><text class="cf-v">{{ d.industry_is_forever === 1 ? '永久有效' : (d.industry_valid_until || '—') }}</text></view>
			<view class="cf-row"><text class="cf-k">法人姓名</text><text class="cf-v">{{ d.legal_person || '—' }}</text></view>
			<view class="cf-row"><text class="cf-k">身份证号</text><text class="cf-v">{{ d.id_card_no || '—' }}</text></view>
			<view class="cf-row"><text class="cf-k">证件有效期</text><text class="cf-v">{{ d.id_is_forever === 1 ? '永久有效' : (d.id_valid_until || '—') }}</text></view>
		</view>

		<!-- 结算信息 -->
		<view class="cf-group">
			<view class="cf-group-title">结算信息</view>
			<view class="cf-row"><text class="cf-k">银行开户名</text><text class="cf-v">{{ d.account_name || '—' }}</text></view>
			<view class="cf-row"><text class="cf-k">{{ d.account_type === 2 ? '对公账号' : '银行卡号' }}</text><text class="cf-v">{{ d.bank_card || '—' }}</text></view>
			<view class="cf-row"><text class="cf-k">开户银行</text><text class="cf-v">{{ d.bank_name || '—' }}</text></view>
			<view class="cf-row"><text class="cf-k">开户支行</text><text class="cf-v">{{ d.bank_branch || '—' }}</text></view>
			<view class="cf-row" v-if="d._bank_region_label"><text class="cf-k">银行所在地</text><text class="cf-v">{{ d._bank_region_label }}</text></view>
			<view class="cf-row" v-if="d.bank_union_no"><text class="cf-k">联行号</text><text class="cf-v">{{ d.bank_union_no }}</text></view>
		</view>

		<!-- 文件资料 -->
		<view class="cf-group">
			<view class="cf-group-title">文件资料</view>
			<view class="cf-file-row">
				<text class="cf-k">营业执照</text>
				<view class="cf-thumbs">
					<image v-for="(u, i) in licenseUrls" :key="i" :src="u" class="cf-thumb" mode="aspectFill" @click="preview(licenseUrls, i)" />
					<text v-if="!licenseUrls.length" class="cf-empty">未上传</text>
				</view>
			</view>
			<view class="cf-file-row">
				<text class="cf-k">行业资质</text>
				<view class="cf-thumbs">
					<image v-for="(u, i) in industryUrls" :key="i" :src="u" class="cf-thumb" mode="aspectFill" @click="preview(industryUrls, i)" />
					<text v-if="!industryUrls.length" class="cf-empty">未上传</text>
				</view>
			</view>
			<view class="cf-file-row">
				<text class="cf-k">法人证件</text>
				<view class="cf-thumbs">
					<image v-for="(u, i) in idUrls" :key="i" :src="u" class="cf-thumb" mode="aspectFill" @click="preview(idUrls, i)" />
					<text v-if="!idUrls.length" class="cf-empty">未上传</text>
				</view>
			</view>
			<view class="cf-file-row">
				<text class="cf-k">门头环境</text>
				<view class="cf-thumbs">
					<image v-for="(u, i) in facadeUrls" :key="i" :src="u" class="cf-thumb" mode="aspectFill" @click="preview(facadeUrls, i)" />
					<text v-if="!facadeUrls.length" class="cf-empty">未上传</text>
				</view>
			</view>
			<view class="cf-file-row">
				<text class="cf-k">店内环境</text>
				<view class="cf-thumbs">
					<image v-for="(u, i) in interiorUrls" :key="i" :src="u" class="cf-thumb" mode="aspectFill" @click="preview(interiorUrls, i)" />
					<text v-if="!interiorUrls.length" class="cf-empty">未上传</text>
				</view>
			</view>
		</view>

		<!-- 协议勾选 -->
		<view class="agreement">
			<view class="agreement-check" @click="agreeProtocol = !agreeProtocol">
				<u-icon :name="agreeProtocol ? 'checkmark-circle-fill' : 'checkmark-circle'" :color="agreeProtocol ? '#FF370B' : '#ccc'" size="36"></u-icon>
			</view>
			<view class="agreement-text">
				我已阅读并同意<text class="agreement-link" @click.stop="viewProtocol">《商家入驻协议》</text>
			</view>
		</view>

		<view class="bottom-bar">
			<view class="btn-secondary" @click="goBack">返回修改</view>
			<view class="btn-primary" @click="submit">提交审核</view>
		</view>
	</view>
</template>

<script>
import { apiArr } from '../../../api/v2local';
import { request, picUrl, NavgateTo } from '../../../utils';
import { signPrivateView } from '../../../utils/uploadOSS.js';
export default {
	data() {
		return {
			picUrl,
			d: {},
			agreeProtocol: false,
			licenseUrls: [],
			industryUrls: [],
			idUrls: [],
			facadeUrls: [],
			interiorUrls: [],
			submitting: false,
		}
	},
	onLoad(options) {
		if (options && options.itemObj) {
			try {
				this.d = JSON.parse(decodeURIComponent(options.itemObj))
			} catch (e) {
				console.error('解析确认页参数失败:', e)
				this.d = {}
			}
		}
		// 门头环境 / 店内环境：公开 bucket，直接拼 URL
		this.facadeUrls = (this.d.facade_photo || '').split(',').filter(Boolean).map(p => this.picUrl + p)
		this.interiorUrls = (this.d.interior_photo || '').split(',').filter(Boolean).map(p => this.picUrl + p)
		// 营业执照 / 行业资质 / 身份证：私密 bucket，需要签名
		this.signList(this.d.license_photo, 'licenseUrls')
		this.signList(this.d.industry_photo, 'industryUrls')
		this.signList([this.d.id_card_front, this.d.id_card_back].filter(Boolean).join(','), 'idUrls')
	},
	methods: {
		async signList(paths, key) {
			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[key] = result
		},
		preview(urls, current) {
			if (!urls || !urls.length) return
			uni.previewImage({ urls, current: urls[current] })
		},
		viewProtocol() {
			uni.showModal({
				title: '商家入驻协议',
				content: '本协议是您与平台之间关于商家入驻服务的法律协议。入驻后您需遵守平台规则，按时提供商品或服务，保证信息真实有效。平台有权对违规商家进行处罚。',
				showCancel: false,
				confirmText: '我知道了'
			})
		},
		goBack() {
			uni.navigateBack()
		},
		submit() {
			if (!this.agreeProtocol) {
				uni.showToast({ title: '请先阅读并同意商家入驻协议', icon: 'none' })
				return
			}
			if (this.submitting) return
			this.submitting = true
			// 剥离仅用于展示的下划线字段，避免传给后端
			const params = {}
			Object.keys(this.d).forEach(k => {
				if (!k.startsWith('_')) params[k] = this.d[k]
			})
			request(apiArr.createStore, "POST", params).then(res => {
				// 审核页仅需少量字段；用 reLaunch 清空页面栈，避免返回到表单/确认页造成二次提交
				const auditItem = {
					status: 1,
					create_time: new Date().toLocaleString('sv-SE').replace('T', ' '),
					handle_time: '',
					remark: ''
				}
				uni.reLaunch({
					url: "/packages/shopEnter/auditStatus/index?itemObj=" + encodeURIComponent(JSON.stringify(auditItem))
				})
			}).catch(res => {
				this.submitting = false
				const msg = res && res.message ? res.message : '提交失败，请稍后重试'
				if (msg.includes("agent_nil")) {
					uni.showModal({ title: '提交失败', content: '未找到对应的代理商信息，请联系平台', showCancel: false })
					return
				}
				uni.showModal({ title: '提交失败', content: msg, showCancel: false })
			})
		}
	}
}
</script>

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