<template>
	<div class="cart-container">
		<h1 class="cart-title">我的购物车</h1>
		<div v-if="state.getList.length > 0">
			<div
				class="cart-item"
				v-for="(item, index) in state.getList"
				:key="item.id"
			>
				<img :src="item.item.image" class="item-image" />
				<div class="item-details">
					<h3>{{ item.item.title }}</h3>
				</div>
				<div class="quantity-control">
					<button
						class="quantity-btn"
						@click="updateQuantity(item, -1)"
						:disabled="item.quantity === 1"
					>
						<span class="icon">−</span>
					</button>
					<input
						type="number"
						class="quantity-input"
						v-model.number="item.quantity"
						min="1"
					/>
					<button class="quantity-btn" @click="updateQuantity(item, 1)">
						<span class="icon">+</span>
					</button>
				</div>
				<div class="item-price">
					¥{{ (item.price * item.quantity).toFixed(2) }}
				</div>
				<button class="delete-btn" @click="removeItem(item.id)">
					<span class="trash-icon">🗑️</span>
					删除
				</button>
			</div>
			<div class="total-section">
				<div class="total-price">
					总计：¥{{ totalPrice.toFixed(2) }}
				</div>
				<!-- 收货地址选择 -->
				<div class="address-section">
					<label for="shipping-address">收货地址:</label>
					<select id="shipping-address" v-model="selectedAddress">
						<option value="">请选择收货地址</option>
						<option
							v-for="addr in state.addressList"
							:key="addr.id"
							:value="addr.id"
						>
						{{addr.recipientName}}-	{{ addr.city }}-{{addr.address}}
						</option>
					</select>
				</div>

				<button
					class="checkout-btn"
					:disabled="!selectedAddress"
					@click="checkout"
				>
					<span class="payment-icon">💳</span>
					立即结算
				</button>
			</div>
		</div>
		<div v-else class="empty-cart">
			<span class="cart-icon">🛒</span>
			购物车是空的，快去选购房源吧！
		</div>
	</div>
</template>

<script setup lang="ts">
import { reactive, ref, computed, onMounted } from "vue";
// 购物车数据
const state = reactive(<any>{
	getList: [],
	addressList:[]
});
/**
 * 计算总价
 */
const totalPrice = computed(() =>
	state.getList.reduce((sum:any, item:any) => sum + item.price * item.quantity, 0)
);
/**
 * 修改房源数量
 */
const updateQuantity = (data: any, sum: number) => {
	data.quantity = Number(data.quantity) + sum;
	frontRequest.put("/api/cart", data).then(() => {
		init();
	});
};

/**
 * 删除购物车房源
 */
const removeItem = (id: number) => {
	frontRequest.delete(`/api/cart/${id}`).then(() => {
		ElMessage.success("删除成功");
		init();
	});
};

/**
 * 初始化购物车数据
 */
async function init() {
	try {
		const res = await frontRequest.get("/api/cart/page");
		const address = await frontRequest.get("/api/address/page",{params:{limit:999}});
		state.getList = res.data.list;
		state.addressList = address.data.list;
	} catch (error) {
		console.error("请求出错：", error);
	}
}
onMounted(async () => {
	await init();
});
// 选中的收货地址，默认未选择
const selectedAddress = ref("");

// 结算处理方法
const checkout = () => {
	if (!selectedAddress.value) {
		ElMessage.error("请先选择收货地址！");
		return;
	}
	let resList = []
	for (let i = 0; i < state.getList.length; i++) {
		resList.push({id:state.getList[i].item.id,quantity:state.getList[i].quantity,cartId:state.getList[i].id})
	}
	let data = {
		dtoList:resList,
		addressId:selectedAddress.value
	}
	frontRequest.post("/api/order/cart",data).then(res =>{
		init()
		ElMessage.success('下单成功~')
	})


};
</script>

<style scoped>
/* 海鲜市场风格：清新海洋蓝绿色调 */
.cart-container {
	max-width: 1200px;
	margin: 2rem auto;
	background: linear-gradient(135deg, #e0f7fa, #b2ebf2);
	border-radius: 16px;
	box-shadow: 0 0 20px rgba(0, 150, 136, 0.2);
	padding: 2rem;
	border: 1px solid #4db6ac;
}

.cart-title {
	font-size: 2.8rem;
	color: #006064;
	margin-bottom: 2rem;
	font-family: 'Arial', sans-serif;
	letter-spacing: 2px;
	text-align: center;
}

.cart-item {
	display: grid;
	grid-template-columns: 100px 2fr 1fr 1fr 120px;
	align-items: center;
	gap: 2rem;
	padding: 1.5rem;
	margin: 1.5rem 0;
	border-radius: 12px;
	background: #ffffff;
	border: 1px solid #b2dfdb;
	box-shadow: 0 2px 8px rgba(0, 150, 136, 0.15);
	transition: transform 0.3s, box-shadow 0.3s;
}

.cart-item:hover {
	transform: translateY(-5px);
	box-shadow: 0 4px 12px rgba(0, 150, 136, 0.3);
}

.item-image {
	border-radius: 8px;
	border: 2px solid #4db6ac;
}

.item-details h3 {
	color: #006064;
	margin: 0;
}

.quantity-control {
	display: flex;
	align-items: center;
	gap: 0.5rem;
}

.quantity-btn {
	background: #4db6ac;
	color: #fff;
	border: none;
	padding: 0.5rem 0.8rem;
	border-radius: 4px;
	cursor: pointer;
	transition: background 0.3s;
}

.quantity-btn:disabled {
	background: #b2dfdb;
	cursor: not-allowed;
}

.quantity-btn:hover:enabled {
	background: #00897b;
}

.quantity-input {
	background: #e0f2f1;
	border: 1px solid #4db6ac;
	color: #006064;
	border-radius: 4px;
	padding: 0.3rem;
	text-align: center;
	width: 50px;
}

.item-price {
	color: #006064;
	font-size: 1.2rem;
	font-weight: bold;
}

.delete-btn {
	background: #ff8a80;
	color: #fff;
	border: none;
	padding: 0.5rem 0.8rem;
	border-radius: 4px;
	cursor: pointer;
	transition: background 0.3s;
}

.delete-btn:hover {
	background: #ff5252;
}

.total-section {
	background: #ffffff;
	padding: 1rem;
	border-radius: 12px;
	margin-top: 2rem;
	border: 1px solid #b2dfdb;
	text-align: right;
}

.total-price {
	color: #006064;
	font-size: 2rem;
	margin-bottom: 1rem;
}

.address-section {
	margin-bottom: 1rem;
}

.address-section label {
	color: #006064;
	margin-right: 0.5rem;
}

.address-section select {
	background: #e0f2f1;
	border: 1px solid #4db6ac;
	color: #006064;
	padding: 0.6rem;
	border-radius: 4px;
}

.checkout-btn {
	background: #4db6ac;
	color: #fff;
	font-weight: bold;
	padding: 0.8rem 1.5rem;
	border: none;
	border-radius: 4px;
	cursor: pointer;
	transition: background 0.3s;
}

.checkout-btn:disabled {
	background: #b2dfdb;
	cursor: not-allowed;
}

.checkout-btn:hover:enabled {
	background: #00897b;
}

.empty-cart {
	color: #006064;
	font-size: 1.5rem;
	text-align: center;
	margin-top: 2rem;
}

/* 移动端响应式 */
@media (max-width: 768px) {
	.cart-item {
		grid-template-columns: 1fr;
		padding: 2rem;
	}

	.item-image {
		width: 80%;
		margin: 0 auto;
	}

	.delete-btn {
		margin-top: 1rem;
	}
}
</style>

<route lang="json">
{
"meta": {
"layout": "front"
}
}
</route>
