<template>
    <view class="container">
        <view v-for="(item, index) in currentAfterSale" :key="index">
            <view class="after-sale-item">
                <!-- 头部信息 -->
                <view class="header">
                    <view class="company-info">
                        <view class="company-name">{{ item.commodity_order_item[0].goods_name }}</view>
                        <text class="after-sale-no">{{ item.after_sales_type === 1 ? '退款' : (item.after_sales_type === 2
                            ? '退货退款' : '换货') }}</text>
                    </view>
                    <text class="order-time">提交订单：{{ formatDate(item.create_time) }}</text>
                </view>

                <!-- 商品信息 -->
                <view class="goods-info" v-for="(ite, index) in item.commodity_order_item" :key="index">
                    <view class="asGoodTag tag-img" v-if="ite.is_support_same_day === 1">当日达</view>
                    <image :src="ite.commodity_pic" class="goods-image"></image>
                    <view class="goods-details">
                        <text class="goods-name">
                            <text class="asGoodTag asGoodTag1" v-if="ite.is_support_same_day === 1">当日达</text>
                            {{ ite.goods_name }}
                            <text class="refund-amount">退款：¥{{ ite.sales_price }}</text>
                        </text>
                        <text class="goods-desc">{{ ite.goods_spec }}</text>
                        <view class="price-count">
                            <text class="goods-price">¥{{ ite.sales_price }}/个</text>
                            <text class="goods-count">x{{ ite.count }}</text>
                        </view>
                    </view>
                </view>

                <!-- 退款状态 -->
                <view class="status-container" v-if="showStatus">
                    <view class="status-item" @click="pendingPage(item)">
                        <text class="status-label">{{ getStatusText(item) }}</text>
                        <text class="status-desc">商家将在<text style="color: #e73b05;">{{
                            calculateProcessingTime(item.create_time) }}</text>内处理</text>
                        <view class="arrow-right"></view>
                    </view>
                </view>

                <!-- 操作按钮 -->
                <view class="action-buttons" v-if="showStatus">
                    <button class="modify-btn" @click="modifyApplication(item)">修改申请</button>
                    <button class="cancel-btn" @click="cancelApplication(item)">撤销申请</button>
                </view>
            </view>
        </view>

    </view>
</template>

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

export default {
    props: {
        afterSaleList: {
            type: Array,
            default: () => []
        },
        order_cate: {
            type: Number,
            default: 1,
        },
    },
    data() {
        return {
            currentAfterSale: this.afterSaleList,
            showStatus: true,
        };
    },
    watch: {
        afterSaleList: {
            handler(newVal) {
                this.currentAfterSale = newVal;
            },
            deep: true
        }
    },
    methods: {
        getList() {
            const params = {
                user_id: uni.getStorageSync('userId'),
                order_cate: this.order_cate,
            }
            request(apiArr.afterSalesList, "POST", params).then((res) => {
                res.after_sales_list.forEach(item => {
                    item.commodity_order_item?.forEach(good => {
                        good.commodity_pic = picUrl + good.commodity_pic;
                    })
                })
                this.currentAfterSale = res.after_sales_list;
            });
        },
        getStatusText(item) {
            if (item.after_sales_status === 2) {
                return '已撤销';
            }
            if (item.process_status === 2) {
                this.showStatus = false;
                return '已完成';
            } else if (item.process_status === 3) {
                return '已取消';
            }
            const status = item.review_status;
            const statusMap = {
                1: '商家待处理',
                2: '商家已同意',
                3: '商家已拒绝',
            };
            return statusMap[status] || '未知状态';
        },
        calculateProcessingTime(createTime) {
            return '2天';
        },
        formatDate(dateString) {
            if (!dateString) return '';
            const date = new Date(dateString);
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            const hours = String(date.getHours()).padStart(2, '0');
            const minutes = String(date.getMinutes()).padStart(2, '0');
            const seconds = String(date.getSeconds()).padStart(2, '0');
            return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        },
        modifyApplication(item) {
            NavgateTo(`../apply/index?item=${JSON.stringify(item)}`);
        },
        cancelApplication(item) {
            uni.showModal({
                title: '提示',
                content: '确定要撤销退款申请吗？',
                success: (res) => {
                    if (res.confirm) {
                        const params = {
                            id: item.id,
                        }
                        request(apiArr.revokeApply, "POST", params).then((res) => {
                            this.getList();
                            uni.showToast({
                                title: '订单撤销成功',
                                icon: 'success',
                                // duration: 1500,
                                // success: () => {
                                //     this.$emit('revokeApply');
                                // }
                            });
                        });
                    }
                }
            });
        },
        pendingPage(item) {
            // NavgateTo(`/packages/myOrders/sendBack/index?item=${JSON.stringify(item)}`);

            if (item.after_sales_type === 3) {
                if (item.review_status === 2) {
                    NavgateTo(`/packages/myOrders/sendBack/index?item=${JSON.stringify(item)}`); //自行寄回
                    return;
                }
                NavgateTo(`/packages/myOrders/changeInfo/index?item=${JSON.stringify(item)}`)
            } else {
                if (item.refund_completed_time) {
                    NavgateTo(`/packages/myOrders/refundOver/index?item=${JSON.stringify(item)}`); //退款成功
                    return;
                }
                NavgateTo(`/packages/myOrders/pending/index?item=${JSON.stringify(item)}`);
            }
        }
    },
};
</script>

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