<script setup>
	// 
	import {
		ref,
		reactive,
		getCurrentInstance,
		computed,
		defineEmits,
		onMounted,
		defineProps,
	} from 'vue'
	// 
	import util from '@/common/js/util.js'
	// 
	import api from '@/api/index.js'

	const {
		proxy
	} = getCurrentInstance()
	// 
	const props = defineProps({
		// 商品信息
		detail: {
			type: Object,
			default: () => ({})
		},
	})
	// 地址
	const address = reactive({})
	// 已选择的规格下标
	const spaceIndex = ref(0)
	// 数量
	const payNum = ref(1)
	// 当前选择的规格
	const currentSpec = computed(() => {
		let spec = props.detail.specs || []
		return spec[spaceIndex.value] || {}
	})
	// 应付总价
	const total = computed(() => {
		let price = parseFloat(props.detail.price) * 100
		let result = parseInt(price * payNum.value) / 100
		return result
	})

	onMounted(() => {
		// 获取默认收货地址
		getDefaultAddress()
	})

	// 获取默认收货地址
	function getDefaultAddress() {
		api.shop.getDefaultAddress({}).then(res => {
			if (res.code === 200) {
				if (res.data) Object.assign(address, {}, res.data)
				return
			}
			util.alert({
				content: res.msg,
				showCancel: false,
			})
		})
	}

	/**
	 * 跳转
	 * @param {Object} url 跳转路径
	 */
	function link(url) {
		uni.navigateTo({
			url,
		})
	}

	/**
	 * 选择规格
	 * @param {Object} index 选择规格下标
	 */
	function handleSpec(index) {
		if (spaceIndex.value !== index) spaceIndex.value = index
	}
</script>

<template>
	<!-- 规格 -->
	<uni-popup type="bottom" ref="payment">
		<view class="buy popBot plr20 bfff">
			<view class="address mtb40">
				<template v-if="address.id">
					<JyCommodityAddress :address="address" />
				</template>
				<template v-else>
					<view class="fmid c999 f28" @click="link('/pages/mine/address/index?select=1')">
						<view>暂无默认地址</view>
						<uni-icons type="right" color="#999" size="30rpx" />
					</view>
				</template>
			</view>
	
			<!-- 商品图 价格 明细 数量 -->
			<view class="jy-card-commodity-content df mtb40">
				<!-- 商品图 -->
				<image class="wh200 br10" :src="currentSpec.image" mode="aspectFill" />
				<!-- 价格 明细 数量 -->
				<view class="info f1 df fdc jcsb ml30">
					<!-- 价格 -->
					<view class="content-info-price">
						<text class="cFF9B27 f28">单价</text>
						<text class="cFF9B27 f24">￥</text>
						<text class="cFF9B27 f50">{{detail.price}}</text>
					</view>
					<!-- 已选 -->
					<view class="content-info-num">
						<text class="f26 c333">已选： {{currentSpec.sku}}</text>
					</view>
					<!-- 计数器 -->
					<view class="w200">
						<uni-number-box v-model="payNum" :step="1" />
					</view>
				</view>
			</view>
	
			<!-- 规格 -->
			<view class="spec">
				<view class="selection df">
					<!-- disabled 销量为零不能选 -->
					<view class="option mtb20 mr20" :class="{'active': spaceIndex === index}"
						v-for="(item,index) in detail.specs" :key="index" @click="handleSpec(index)">
						<text class="txt">{{item.sku}}</text>
					</view>
				</view>
			</view>
	
			<view class="btn lg primary mtb30" @click="handlePay">
				<text class="cfff">立即下单 ￥{{total}}</text>
			</view>
		</view>
	</uni-popup>
</template>

<style>
</style>