<template>
    <view class="container">
        <!-- 进度条 -->
        <view class="progress-container" v-if="currentAfterSale.process_status === 1">
            <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 class="progress-line"></view>
            <view class="progress-item">
                <text class="progress-text">退款结束</text>
            </view>
        </view>

        <view class="progress-container" v-if="currentAfterSale.process_status === 2">
            <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" v-if="currentAfterSale.process_status === 1">
            <text class="status-title">请等待商家处理</text>
            <text class="status-desc"><text style="font-weight: bold;">2天</text>后商家未处理将自动同意</text>
            <text class="status-desc">您已成功发起退款申请，请耐心等待商家处理</text>
        </view>

        <view class="status-tip" v-if="currentAfterSale.review_status === 3">
            <text class="status-title">商家拒绝申请,请您处理</text>
            <text class="status-desc"><text style="color: #f63b08;">2天</text>后未处理将自动关闭</text>
        </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 ?
                            currentAfterSale.refund_amount.toFixed(2) : '0.00' }}</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.commodity_order_item[0].count }}</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 ?
                    currentAfterSale.refund_amount.toFixed(2) : '0.00') }}</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" v-if="currentAfterSale.process_status === 1">
            <button class="cancel-btn" @click="cancelRefund">撤销申请</button>
            <button class="modify-btn" @click="modifyRefund">修改申请</button>
            <button class="urge-btn" @click="urgeProcess">催处理</button>
        </view>

        <u-popup ref="popup" :show="showPopup" @close="closePopup" :mask-close-able="false"
            :close-on-click-overlay="false">
            <view class="popup-content">
                <view class="success-icon">
                    <view class="check-circle">
                        <text class="check-mark">✓</text>
                    </view>
                </view>
                <view class="popup-title">已为您催处理</view>
                <view class="popup-desc">平台客服已帮您催促卖家，07月26日20:49前卖家未处理系统将自动退款。</view>
                <view class="popup-buttons">
                    <button class="continue-btn" @click="closePopup">继续联系卖家</button>
                    <button class="know-btn" @click="closePopup">我知道了</button>
                </view>
            </view>
        </u-popup>
    </view>
</template>

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

export default {
    data() {
        return {
            currentAfterSale: {},
            pickerDefaultDate: new Date(),
            showPopup: false
        };
    },
    created() {
        // 初始化日期选择器默认值为当前申请时间
        this.pickerDefaultDate = new Date(this.currentAfterSale.create_time);
    },
    onShow() {
        const storageAfterSale = uni.getStorageSync('afterSaleItem');
        if (storageAfterSale) {
            this.currentAfterSale = storageAfterSale;
            uni.removeStorageSync('afterSaleItem');
        }
    },
    onLoad(options) {
        const item = JSON.parse(options?.item);
        console.log("🚀 ~ onLoad ~ item:", item)
        this.currentAfterSale = item;
    },
    // 添加onBackPress生命周期钩子，处理左上角返回按钮点击事件
    onBackPress() {
        // 确保使用uni.navigateBack()返回上一页，触发index页面的onShow生命周期
        uni.navigateBack();
        return true; // 阻止默认返回行为
    },
    methods: {
        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')}`;
        },

        // 复制退款编号
        copyRefundNo() {
            // 使用uni-app的复制API
            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)}`);
        },

        // 催处理
        urgeProcess() {
            this.showPopup = true;
        },

        // 关闭弹窗
        closePopup() {
            this.showPopup = false;
        }
    }
};
</script>

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