<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-5-20
 */
require_once dirname(__DIR__, 2) . '/core/lib/refund_service.php';

\Core\Lib\RefundService::ensureTable($db);
$statusMap = \Core\Lib\RefundService::statusMap();
$page = max(1, intval($_GET['p'] ?? 1));
$pageSize = 10;
$offset = ($page - 1) * $pageSize;
$memberId = intval($_SESSION['member_id'] ?? 0);
$total = intval($db->fetch("SELECT COUNT(*) AS cnt FROM {$db->table('order_refund')} WHERE member_id = ?", [$memberId])['cnt'] ?? 0);
$rows = $db->fetchAll("SELECT * FROM {$db->table('order_refund')} WHERE member_id = ? ORDER BY id DESC LIMIT {$offset}, {$pageSize}", [$memberId]);
$totalPages = max(1, (int) ceil($total / $pageSize));
?>
<div class="page-header">
    <h1 class="page-title"><i class="icon-rotate-ccw"></i> 我的售后</h1>
</div>
<div class="page-content">
    <?php if (empty($rows)): ?>
    <div class="empty-state">
        <div class="empty-state-icon">↩</div>
        <div class="empty-state-text">暂无售后/退款申请</div>
    </div>
    <?php else: ?>
    <table class="table">
        <thead>
            <tr>
                <th>订单号</th>
                <th>退款金额</th>
                <th>申请原因</th>
                <th>状态</th>
                <th>申请时间</th>
                <th>后台备注</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($rows as $row): ?>
            <?php $status = $statusMap[intval($row['status'] ?? 0)] ?? ['未知', 'badge-gray']; ?>
            <tr>
                <td><?php echo htmlspecialchars($row['order_sn'] ?? '-'); ?></td>
                <td>¥<?php echo number_format(floatval($row['amount'] ?? 0), 2); ?></td>
                <td style="max-width:260px;word-break:break-word;"><?php echo htmlspecialchars($row['reason'] ?? '-'); ?></td>
                <td><span class="badge <?php echo $status[1]; ?>"><?php echo $status[0]; ?></span></td>
                <td><?php echo !empty($row['create_time']) ? date('Y-m-d H:i', intval($row['create_time'])) : '-'; ?></td>
                <td style="max-width:260px;word-break:break-word;"><?php echo htmlspecialchars(($row['admin_remark'] ?? '') !== '' ? $row['admin_remark'] : '-'); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php if ($totalPages > 1): ?>
    <div class="pagination">
        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
        <a href="?page=refund&p=<?php echo $i; ?>" class="<?php echo $i === $page ? 'current' : ''; ?>"><?php echo $i; ?></a>
        <?php endfor; ?>
    </div>
    <?php endif; ?>
    <?php endif; ?>
</div>
