<template>
	<view class="address-page">
		<navBar :title="type==1?'收货地址':'联系邮箱'"  />
		<!-- <uni-nav-bar left-icon="left" title="收货地址" fixed color="#8B2316" height="180rpx" :border="false" backgroundColor="#ffffff" @clickLeft="goBack" /> -->

		<scroll-view scroll-y class="content">
			<view class="form-item">
				<view class="label">收件人</view>
				<input class="input" v-model="receiver" placeholder="请输入收货人名字" />
			</view>
			<view class="divider-line"></view>

			<view class="form-item">
				<view class="label">手机号</view>
				<input class="input" v-model="mobile" type="number" placeholder="收货人的电话，方便联系" />
			</view>
			<view class="divider-line"></view>

			<view class="form-item" v-if="type==2">
				<view class="label">邮箱</view>
				<input class="input" v-model="email" type="text" placeholder="请输入您的邮箱" />
			</view>
			<view class="divider-line" v-if="type==2"></view>

			<view class="form-item select-item" @click="openAreaPicker" v-if="type==1">
				<view class="label">地址</view>
				<view class="input placeholder" v-if="!regionText">请选择地址</view>
				<view class="input" v-else>{{ regionText }}</view>
				<text class="arrow">›</text>
			</view>
			<view class="divider-line" v-if="type==1"></view>

			<view class="form-item" v-if="type==1">
				<view class="label">详细地址</view>
				<input class="input" v-model="detail" placeholder="请输入街道、门牌等详细地址信息" />
			</view>
			<view class="divider-line" v-if="type==1"></view>
		</scroll-view>

		<view class="footer-bar" @click="submit">
			<view class="confirm-btn" >确定兑换</view>
		</view>

		<!-- 省市区选择器 -->
		<view v-if="showAreaPicker" class="picker-mask" @click="closeAreaPicker"></view>
		<view v-if="showAreaPicker" class="picker-panel">
			<view class="picker-header">
				<text class="picker-btn" @click="closeAreaPicker">取消</text>
				<text class="picker-title">选择地区</text>
				<text class="picker-btn ok" @click="confirmArea">确定</text>
			</view>
			<picker-view v-if="provinces.length && cities.length && areas.length" class="picker-view" :indicator-style="indicatorStyle" :value="pickerIndex" @change="onAreaChange">
				<picker-view-column>
					<view v-for="(p,pi) in provinces" :key="pi" class="picker-item">{{ p.name }}</view>
				</picker-view-column>
				<picker-view-column>
					<view v-for="(c,ci) in cities" :key="ci" class="picker-item">{{ c.name }}</view>
				</picker-view-column>
				<picker-view-column>
					<view v-for="(a,ai) in areas" :key="ai" class="picker-item">{{ a.name }}</view>
				</picker-view-column>
			</picker-view>
			<view v-else class="picker-empty">地区数据加载中...</view>
		</view>
	</view>
	<unidialog
	:title="'确认兑换信息'"
    :visible="freeVisible"
    :content="freeContent"
    :titleColor="'#000'"
    @close="freeClose"
    @confirm="freeConfirm"
  >
  <template #content>
	<view>
		<view class="row">
			<view class="left">收件人：</view>
			<view class="right">{{ receiver }}</view>
		</view>
		<view class="row">
			<view class="left">手机号：</view>
			<view class="right">{{ mobile }}</view>
		</view>
		<view class="row" v-if="type==1">
			<view class="left">地址：</view>
			<view class="right">{{ regionText+detail }}</view>
		</view>
		<view class="row" v-else>
			<view class="left">邮箱：</view>
			<view class="right">{{ email?email:'无' }}</view>
		</view>
		<view class="row">
			<view class="left">兑换积分：</view>
			<view class="right"><text class="price">{{ points }}积分</text>(包邮)</view>
		</view>
		<view class="row">请确认此次兑换，物品不退不换</view>
	</view>
  </template>
  </unidialog>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import areaList from '@/utils/areaList.js'
import api from '@/api/api.js'
import navTo from '@/utils/navTo';
import goods_api from '@/api/goods_api';
import unidialog from '@/components/dialog/dialog.vue';
const freeVisible = ref(false)
const freeContent = ref('')
const goods_order_id = ref('')
const points = ref(0)
const freeClose = () => {
	freeVisible.value = false
}
const freeConfirm = () => {
	freeVisible.value = false;
	goods_api.payGoodsOrder({
		goods_order_uuid: goods_order_id.value,
	}).then(res => {
		if(res.code == 1 || res.code == 200){
			uni.showToast({
				title: '兑换成功',
				icon: 'none',
				duration: 1000
			});
			setTimeout(() => {
				navTo({
					url: '/pages_goods/pointMall/pointMall?from=exchange'
				})
			}, 1000)
		}
	})
}
const receiver = ref('')
const mobile = ref('')
const regionText = ref('')
const detail = ref('')
const email = ref('')
const editingId = ref(null)
const goodsUuid = ref('')
const type = ref(1)
const goodsNum = ref(1);
const goodsName = ref('');
const goBack = () => uni.navigateBack()

// 省市区数据（适配树形数组：[{code,label,value,children:[...] }...]）
const provinces = ref([])
const cities = ref([])
const areas = ref([])
const pickerIndex = ref([0,0,0])
const showAreaPicker = ref(false)
const indicatorStyle = `height: 80rpx;`

const normalizeNode = (node) => ({
	code: node?.code || '',
	name: node?.label || node?.value || node?.name || '',
	children: Array.isArray(node?.children) ? node.children : []
})

const getAreaTree = () => {
	const raw = areaList && (areaList.default || areaList)
	return Array.isArray(raw) ? raw.map(normalizeNode) : []
}

const buildData = () => {
	const tree = getAreaTree()
	provinces.value = tree.length ? tree : [{ code: '', name: '', children: [] }]
	const pIdx = Math.min(pickerIndex.value[0], Math.max(provinces.value.length - 1, 0))
	const pNode = provinces.value[pIdx]
	cities.value = (pNode?.children || []).map(normalizeNode)
	if (!cities.value.length) cities.value = [{ code: '', name: '', children: [] }]
	const cIdx = Math.min(pickerIndex.value[1], Math.max(cities.value.length - 1, 0))
	const cNode = cities.value[cIdx]
	areas.value = (cNode?.children || []).map(normalizeNode)
	if (!areas.value.length) areas.value = [{ code: '', name: '' }]
}

const openAreaPicker = () => {
	showAreaPicker.value = true
	pickerIndex.value = [0,0,0]
	buildData()
}
const closeAreaPicker = () => { showAreaPicker.value = false }

const onAreaChange = (e) => {
	const val = (e && e.detail && e.detail.value) ? e.detail.value : [0,0,0]
	const [pi, ci, ai] = val
	if (pi !== pickerIndex.value[0]) {
		pickerIndex.value = [pi, 0, 0]
		buildData()
		return
	}
	if (ci !== pickerIndex.value[1]) {
		pickerIndex.value = [pi, ci, 0]
		buildData()
		return
	}
	pickerIndex.value = [pi, ci, ai]
}

const confirmArea = () => {
	const p = provinces.value[pickerIndex.value[0]]
	const c = cities.value[pickerIndex.value[1]]
	const a = areas.value[pickerIndex.value[2]]
	regionText.value = [p?.name, c?.name, a?.name].filter(Boolean).join(' ')
	closeAreaPicker()
}

onLoad((opts) => {
	console.log(22);
	console.log(opts);
	goodsUuid.value = opts.goodsUuid
	goodsNum.value = opts.goodsNum;
	type.value = opts.type;
	goodsName.value = opts.goodsName;
	getAddress()
})
const saveAddress = () => {
	uni.setStorageSync('goods_address', {
		receiver: receiver.value,
		mobile: mobile.value,
		regionText: regionText.value,
		detail: detail.value,
		email: email.value
	})
}
const getAddress = () => {
	const address = uni.getStorageSync('goods_address')
	if(address){
		receiver.value = address.receiver
		mobile.value = address.mobile
		regionText.value = address.regionText
			detail.value = address.detail
			email.value = address.email
		}
	
}
const submit = () => {
	
	if (!receiver.value) return uni.showToast({ title: '请输入收件人', icon: 'none' })
	if (!/^1\d{10}$/.test(mobile.value)) return uni.showToast({ title: '请输入正确手机号', icon: 'none' })

	if(type.value == 1){
		if (!regionText.value) return uni.showToast({ title: '请选择地址', icon: 'none' })
		if (!detail.value) return uni.showToast({ title: '请输入详细地址', icon: 'none' })
	}else{
		if (email.value && !/^([a-zA-Z0-9_\.-]+)@([a-zA-Z0-9\.-]+)\.([a-zA-Z]{2,})$/.test(email.value)) return uni.showToast({ title: '邮箱格式不正确', icon: 'none' })
	}
	saveAddress();
    goods_api.createGoodsOrder({
		goodsUuid: goodsUuid.value,
		goodsNum: goodsNum.value,
		user_name: receiver.value,
		mobile: mobile.value,
		email:email.value,
		address: regionText.value+detail.value
	}).then(res => {
       if(res.code == 1 || res.code == 200){
		uni.sendNativeEvent("portraitReport",{
      msg:{
        event_nickname: "boint_jifengood",
        page_type: "积分商城",
		resource_name:goodsName.value,
		event_val:points.value,
		event_unit:'积分',
		number:goodsNum.value,
		belong:type.value,
      },
    },
    (ret) => {
      console.log(ret);
    });
		points.value = res.data.pionts;
		freeVisible.value = true
		goods_order_id.value = res.data.goods_order_id
		
	   }
	})
	
}

</script>

<style scoped lang="scss">
.address-page :deep(.dialog-title){
   color:#000!important;
  }
.address-page { min-height: 100vh; background: #fff; }
.content { position: absolute; top: calc(var(--status-bar-height) + 44px); bottom: 120rpx; left: 0; right: 0; background: #fff; }
.form-item { display: flex; align-items: center; padding: 30rpx 24rpx; }
.label { width: 160rpx; color: #333; font-size: 30rpx; }
.input { flex: 1; color: #333; font-size: 30rpx; }
.placeholder { color: #808080; }
.divider-line { height: 2rpx; background: #eee; margin: 0 24rpx; }
.select-item { position: relative; }
.arrow { position: absolute; right: 24rpx; color: #999; font-size: 48rpx; }
.footer-bar { position: fixed; left: 0; right: 0; bottom: 0; height: 120rpx; background: #27c5b8; display: flex; align-items: center; justify-content: center; }
.confirm-btn { color: #fff; font-size: 34rpx; font-weight: 700; }

/* 地区选择器 */
.picker-mask { position: fixed; left: 0; right: 0; top: 0; bottom: 0; background: rgba(0,0,0,0.4); }
.picker-panel { position: fixed; left: 0; right: 0; bottom: 0; background: #fff; }
.picker-header { display: flex; align-items: center; justify-content: space-between; padding: 20rpx 24rpx; border-bottom: 1rpx solid #eee; }
.picker-title { font-size: 30rpx; color: #333; }
.picker-btn { color: #666; font-size: 28rpx; }
.picker-btn.ok { color: #38c1b1; }
.picker-view { height: 480rpx; }
.picker-item { height: 80rpx; line-height: 80rpx; text-align: center; color: #333; }
.picker-empty { padding: 40rpx; text-align: center; color: #999; }
.row{
	display: flex;
	align-items: flex-start;
	font-size: 28rpx;
	padding: 12rpx 0;

	max-width: 800rpx;
	
	.left{
		text-align: left;
		max-width: 142rpx;
		white-space: nowrap;
		width:auto!important;
	}
	.price{
		color: #FF4D4F;
	}
}
.left{
	width: 160rpx;
}
</style>

