<template>
    <view class="container">
        <!-- 进度条 -->
        <view class="progress-container">
            <view class="progress-item">
                <text class="progress-text">商家处理</text>
            </view>
            <view class="progress-line"></view>
            <view class="progress-item active">
                <text class="progress-text">寄回商品</text>
            </view>
            <view class="progress-line"></view>
            <view class="progress-item">
                <text class="progress-text">退款结束</text>
            </view>
        </view>

        <!-- 状态提示 -->
        <view class="status-tip">
            <text class="status-title">自行寄回</text>
            <text class="status-desc"><text style="color: #ff481f;">2天</text>后未寄出将撤销退货申请</text>
            <view class="status-desc2">需您自行联系快递公司退回，请不要邮寄到付</view>
            <view class="info-item" style="border: none;">
                <text class="info-label">商家地址</text>
                <text class="info-value">{{ currentAfterSale.supplier_address }}</text>
                <text class="copy-icon" @click="copyAdress"></text>
            </view>

            <button class="addOrderIdBtn" @click="addOrderId" v-if="true">填写单号</button>
            <view class="logistics-info" v-else>
                <text>退货物流：</text>
                <text v-if="false"></text>
            </view>
        </view>

        <view class="hr"></view>

        <!-- 商品信息 -->
        <view class="goods-info">
            <view class="goods-item">
                <view class="asGoodTag tag-img"
                    v-if="currentAfterSale.commodity_order_item[0].is_support_same_day === 1">当日达</view>
                <image class="goods-image" :src="currentAfterSale.commodity_order_item[0].commodity_pic"></image>
                <view class="goods-details">
                    <text class="goods-name">
                        <text class="asGoodTag asGoodTag1"
                            v-if="currentAfterSale.commodity_order_item[0].is_support_same_day === 1">当日达</text>
                        {{ currentAfterSale.commodity_order_item[0].goods_name }}
                        <text class="refund-amount">退款：¥{{ currentAfterSale.refund_amount.toFixed(2) }}</text>
                    </text>
                    <text class="goods-desc">{{ currentAfterSale.commodity_order_item[0].goods_spec }}</text>
                    <text class="goods-price">
                        {{ '¥' + currentAfterSale.commodity_order_item[0].sales_price.toFixed(2) + '/个' }}
                        <text class="goods-count">X{{ currentAfterSale.after_sales_goods.split('@')[0] }}
                        </text>
                    </text>
                </view>
            </view>
        </view>

        <!-- 退款信息 -->
        <view class="refund-info">
            <view class="info-item">
                <text class="info-label">服务类型<text style="color: #fc3811;">*</text></text>
                <text class="info-value">{{ getServiceTypeText(currentAfterSale.after_sales_type) }}</text>
                <text class="info-arrow"></text>
            </view>
            <view class="info-item">
                <text class="info-label">退款原因<text style="color: #fc3811;">*</text></text>
                <text class="info-value">{{ currentAfterSale.after_sales_reason }}</text>
                <text class="info-arrow"></text>
            </view>
            <view class="info-item">
                <text class="info-label">退款金额<text style="color: #fc3811;">*</text></text>
                <text class="info-value amount">{{ '¥' + currentAfterSale.refund_amount.toFixed(2) }}</text>
            </view>
            <view class="info-item">
                <text class="info-label">申请时间<text style="color: #fc3811;">*</text></text>
                <text class="info-value">{{ formatDate(currentAfterSale.create_time) }}</text>
            </view>
            <view class="info-item">
                <text class="info-label">退款编号<text style="color: #fc3811;">*</text></text>
                <text class="info-value">{{ currentAfterSale.after_sales_no }}</text>
                <text class="copy-icon" @click="copyRefundNo"></text>
            </view>
        </view>

        <!-- 操作按钮 -->
        <view class="action-buttons">
            <!-- 填写完物流单号后不显示图片和平台介入按钮 -->
            <view>
                <image class="bottomImg" src="https://static.hshuishang.com/secdBack_bottom.png"
                    alt="" />
            </view>
            <button class="modify-btn" @click="modifyRefund">平台介入</button>
            <button class="cancel-btn" @click="cancelRefund">撤销申请</button>
        </view>
    </view>
</template>

<script>
import {
    isPhone,
    picUrl,
    request,
    upload,
    NavgateTo
} from '../../../utils';
import { apiArr } from "../../../api/afterSale";

export default {
    data() {
        return {
            currentAfterSale: {},
            pickerDefaultDate: new Date(),
            logisticsInfo: null
        };
    },
    created() {
        // 初始化日期选择器默认值为当前申请时间
        this.pickerDefaultDate = new Date(this.currentAfterSale.create_time);
    },
    onLoad(options) {
        const item = JSON.parse(options?.item);
        console.log("🚀 ~ onLoad ~ item:", item)
        this.currentAfterSale = item;
        this.getLogisticsInfo();
    },
    onShow() {
        // 页面显示时检查是否有新的物流信息
        this.getLogisticsInfo();
    },
    methods: {
        // 获取物流信息
        getLogisticsInfo() {
            const info = uni.getStorageSync('logisticsInfo');
            if (info) {
                this.logisticsInfo = info;
                // 可选：清空storage，避免下次进入还显示旧数据
                // uni.removeStorageSync('logisticsInfo');
            }
        },

        getServiceTypeText(type) {
            return type === 1 ? '仅退款' : (type === 2 ? '退货退款' : '换货');
        },

        // 格式化日期
        formatDate(dateString) {
            if (!dateString) return '';
            const date = new Date(dateString);
            return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}:${String(date.getSeconds()).padStart(2, '0')}`;
        },

        // 赋值商家地址
        copyAdress() {
            uni.setClipboardData({
                data: this.currentAfterSale.supplier_address,
                success: () => {
                    uni.showToast({
                        title: '复制成功',
                        icon: 'success'
                    });
                },
                fail: () => {
                    uni.showToast({
                        title: '复制失败',
                        icon: 'none'
                    });
                }
            });
        },

        addOrderId() {
            NavgateTo(`../returnLogistics/index?item=${JSON.stringify(this.currentAfterSale)}`);
        },

        // 复制退款编号
        copyRefundNo() {
            uni.setClipboardData({
                data: this.currentAfterSale.after_sales_no,
                success: () => {
                    uni.showToast({
                        title: '复制成功',
                        icon: 'success'
                    });
                },
                fail: () => {
                    uni.showToast({
                        title: '复制失败',
                        icon: 'none'
                    });
                }
            });
        },

        // 撤销申请
        cancelRefund() {
            uni.showModal({
                title: "确定要撤销本次售后申请吗？",
                showCancel: true,
                cancelText: "暂不撤销",
                confirmText: "确认撤销",
                confirmColor: "#ff4d4f",
                success: (res) => {
                    if (res.confirm) {
                        const params = {
                            id: this.currentAfterSale.id,
                        }
                        request(apiArr.revokeApply, "POST", params).then((res) => {
                            uni.showToast({
                                title: '订单撤销成功',
                                icon: 'success'
                            });
                        });
                    }
                },
            });
        },

        // 修改申请
        modifyRefund() {
            // NavgateTo(`../apply/index?item=${JSON.stringify(this.currentAfterSale)}`);
        },
    }
};
</script>

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