<template>
    <view class="container">
        <!-- 收货地址部分 -->
        <view class="address-section">
            <view class="address-info" @click="chooseAddress">
                <view class="address-main">
                    <view class="address-name-phone">
                        <text class="name">{{ defAddress.name }}</text>
                        <text class="phone">{{ defAddress.phone }}</text>
                    </view>
                    <view class="address-detail">
                        {{ defAddress.address }}{{ defAddress.house_number }}
                    </view>
                </view>
                <view class="address-arrow"><u-icon name="arrow-right" size="25"></u-icon></view>
            </view>
        </view>

        <!-- 兑换商品信息 -->
        <view class="exchange-section">
            <view class="section-title">兑换商品</view>
            <view class="goods-item">
                <view class="goods-image">
                    <image :src="goodsInfo.goodsInfo.goods_info.commodity_pic" mode="aspectFill"></image>
                </view>
                <view class="goods-info">
                    <view class="goods-name">{{ goodsInfo.specName }}</view>
                    <view class="goods-quantity">{{ goodsInfo.quantity }}件</view>
                </view>
                <view class="goods-points">{{ goodsInfo.price }}积分</view>
            </view>
            <view class="total-section">
                <text>共{{ goodsInfo.quantity }}件商品 共计:</text>
                <text class="total-points">{{ goodsInfo.price * goodsInfo.quantity }}积分</text>
            </view>
        </view>

        <!-- 积分计算部分 -->
        <view class="points-calculation">
            <view class="calculation-item">
                <text>商品小计</text>
                <text>{{ goodsInfo.price * goodsInfo.quantity }}积分</text>
            </view>
            <view class="calculation-item">
                <text>运费抵扣</text>
                <text>0积分</text>
            </view>
        </view>

        <!-- 底部确认兑换按钮 -->
        <view class="bottom-section">
            <view class="points-consumption">
                <text>消耗积分:</text>
                <text class="consumption-points">{{ goodsInfo.price * goodsInfo.quantity }}积分</text>
            </view>
            <button class="confirm-btn" @click="confirmExchange">确认兑换</button>
        </view>
    </view>
</template>

<script>
import { request, NavgateTo } from "../../../utils";
import { apiArr } from "../../../api/shop";
import { apiArr as apiArr2 } from "../../../api/pointShop";

export default {
    data() {
        return {
            defAddress: {},
            goodsInfo: {}
        };
    },
    onLoad(options) {
        if (options.orderInfo) {
            this.goodsInfo = JSON.parse(options.orderInfo);
        }
    },
    onShow() {
        // 检查是否有选中的地址
        const selectedAddress = uni.getStorageSync('selectedAddress');
        if (selectedAddress && Object.keys(selectedAddress).length > 0) {
            this.defAddress = selectedAddress;
            // 清除选中状态，避免重复应用
            uni.removeStorageSync('selectedAddress');
        } else {
            // 如果没有选中的地址，则获取默认地址
            this.getUserAddress();
        }
    },
    methods: {
        // 获取用户默认地址
        getUserAddress() {
            request(apiArr.getUserDefAddress, "POST", {}).then(res => {
                this.defAddress = res.default_address;
            });
        },
        // 选择地址
        chooseAddress() {
            NavgateTo('/packages/shop/address/index');
        },
        // 确认兑换
        confirmExchange() {
            // 检查是否有收货地址
            if (!this.defAddress || Object.keys(this.defAddress).length === 0) {
                uni.showToast({
                    title: '请选择收货地址',
                    icon: 'none'
                });
                return;
            }

            const params = {
                user_id: uni.getStorageSync('userId'),
                mch_id: this.goodsInfo.goodsInfo.mch_id,
                receiving_name: this.defAddress.name,
                receiving_phone: this.defAddress.phone,
                receiving_address: this.defAddress.address + this.defAddress.house_number,
                goods_item: {
                    goods_id: this.goodsInfo.goodsInfo.goods_id,
                    count: this.goodsInfo.quantity,
                    price: this.goodsInfo.price,
                    freight: this.goodsInfo.goodsInfo.freight || 0
                }
            }
            request(apiArr2.pointShopAdd, "POST", params).then(res => {
                const params = {
                    order_id: res.order_id
                }
                request(apiArr2.pointShopPay, "POST", params).then(res => {
                    setTimeout(() => {
                        NavgateTo('/packages/jfShop/orderDetail/index?order_id=' + params.order_id);
                    }, 300);
                })
            })
        }
    }
};
</script>

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