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


/**
 * 我的订单
 */

require_once dirname(__DIR__, 2) . '/core/lib/license.php';
require_once dirname(__DIR__, 2) . '/core/lib/license_guard.php';

$license = new \Core\Lib\License();
if (!\Core\Lib\LicenseGuard::hasFeature('order_product', $license)) {
?>
<div class="page-header">
    <h1 class="page-title">
        <i class="icon-shopping-bag"></i>
        我的订单
    </h1>
</div>

<div class="page-content">
    <div style="background:#fff7ed;border:1px solid #fdba74;border-radius:16px;padding:32px 24px;text-align:center;">
        <div style="display:inline-flex;align-items:center;padding:6px 12px;border-radius:999px;background:#fff;color:#c2410c;border:1px solid #fdba74;font-size:12px;font-weight:700;">需升级授权</div>
        <div style="font-size:24px;font-weight:700;color:#0f172a;margin:18px 0 10px;">订单中心未开放</div>
        <div style="max-width:520px;margin:0 auto;color:#64748b;line-height:1.8;">
            <?php echo htmlspecialchars(\Core\Lib\LicenseGuard::getDeniedMessage('order_product', $license, '当前授权不支持订单功能')); ?>
        </div>
        <div style="margin-top:20px;">
            <a href="http://www.yutoucms.cn/license-center/user.php?act=buy" target="_blank" class="btn btn-primary">立即升级</a>
        </div>
    </div>
</div>
<?php
    return;
}

$page = max(1, intval($_GET['p'] ?? 1));
$page_size = 10;
$offset = ($page - 1) * $page_size;

$status_map = [
    '-1' => ['已取消', 'status-canceled'],
    '0'  => ['待付款', 'status-pending'],
    '1'  => ['已付款', 'status-paid'],
    '2'  => ['已发货', 'status-shipped'],
    '3'  => ['已完成', 'status-completed']
];

// 我的订单
$total = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('order')} WHERE member_id = ?", [$_SESSION['member_id']])['cnt'];
$rows = $db->fetchAll("SELECT * FROM {$db->table('order')} WHERE member_id = ? ORDER BY id DESC LIMIT {$offset}, {$page_size}", [$_SESSION['member_id']]);
$total_pages = ceil($total / $page_size);

// 统计
$pending_count = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('order')} WHERE member_id = ? AND status = 0", [$_SESSION['member_id']])['cnt'] ?? 0;
$paid_count = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('order')} WHERE member_id = ? AND status IN (1,2,3)", [$_SESSION['member_id']])['cnt'] ?? 0;
?>
<div class="page-header">
    <h1 class="page-title">
        <i class="icon-shopping-bag"></i>
        我的订单
    </h1>
</div>

<div class="page-content">
    <!-- 统计卡片 -->
    <div class="order-stats">
        <div class="stat-card">
            <div class="stat-icon">📦</div>
            <div class="stat-info">
                <div class="stat-value"><?php echo $total; ?></div>
                <div class="stat-label">全部订单</div>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">⏱</div>
            <div class="stat-info">
                <div class="stat-value"><?php echo $pending_count; ?></div>
                <div class="stat-label">待付款</div>
            </div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">✅</div>
            <div class="stat-info">
                <div class="stat-value"><?php echo $paid_count; ?></div>
                <div class="stat-label">已付款</div>
            </div>
        </div>
    </div>

    <!-- 订单列表 -->
    <?php if (empty($rows)): ?>
    <div class="empty-state">
        <div class="empty-state-icon">📋</div>
        <div class="empty-state-text">暂无订单记录</div>
    </div>
    <?php else: ?>
    <div class="order-list">
        <?php foreach ($rows as $row): ?>
        <?php $rowProducts = $db->fetchAll("SELECT op.*, p.litpic as product_thumb, p.id as product_id FROM {$db->table('order_product')} op LEFT JOIN {$db->table('product')} p ON op.product_id = p.id WHERE op.order_id = ? LIMIT 3", [$row['id']]); ?>
        <?php $firstProduct = !empty($rowProducts) ? $rowProducts[0] : null; ?>
        <div class="order-item">
            <div class="order-header">
                <span class="order-sn">订单号：<?php echo htmlspecialchars($row['order_sn']); ?></span>
                <span class="order-time"><?php echo date('Y-m-d H:i', $row['create_time'] ?? time()); ?></span>
            </div>
            <div class="order-body">
                <?php foreach ($rowProducts as $p): ?>
                <div class="order-product-item" style="display:flex;align-items:center;gap:12px;padding:8px 0;border-bottom:1px solid #f0f0f0;">
                    <a href="/public/index.php?m=product&id=<?php echo $p['product_id']; ?>" target="_blank" style="flex-shrink:0;">
                        <img src="<?php echo htmlspecialchars($p['product_image'] ?: $p['product_thumb'] ?: '/template/agrismart/images/no-product.jpg'); ?>" alt="" style="width:56px;height:56px;object-fit:cover;border-radius:6px;border:1px solid #e5e7eb;">
                    </a>
                    <div style="flex:1;min-width:0;">
                        <a href="/public/index.php?m=product&id=<?php echo $p['product_id']; ?>" target="_blank" style="color:#1a1a1a;text-decoration:none;font-size:14px;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden;"><?php echo htmlspecialchars($p['product_title'] ?: $p['product_name']); ?></a>
                        <div style="font-size:12px;color:#6b7280;margin-top:4px;">¥<?php echo number_format(floatval($p['total_price'] ?? $p['price'] * $p['quantity']), 2); ?> × <?php echo $p['quantity']; ?></div>
                    </div>
                </div>
                <?php endforeach; ?>
                <div class="order-info-row" style="display:flex;justify-content:space-between;align-items:center;padding-top:12px;">
                    <div class="order-amount">
                        <span class="amount-label">¥</span>
                        <span class="amount-value"><?php echo number_format(isset($row['pay_price']) ? floatval($row['pay_price']) : floatval($row['total_amount'] ?? 0), 2); ?></span>
                    </div>
                    <div class="order-status">
                        <?php
                        $s = $status_map[intval($row['status'])] ?? ['未知', ''];
                        ?>
                        <span class="status <?php echo $s[1]; ?>"><?php echo $s[0]; ?></span>
                    </div>
                    <div class="order-actions">
                        <?php if ($row['status'] == 0): ?>
                        <a href="/public/order/pay.php?order_id=<?php echo $row['id']; ?>" class="btn btn-primary btn-sm">去付款</a>
                        <?php else: ?>
                        <a href="?page=order_detail&id=<?php echo $row['id']; ?>" class="btn btn-outline btn-sm">查看</a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>

    <?php if ($total_pages > 1): ?>
    <div class="pagination">
        <?php if ($page > 1): ?><a href="?page=myorder&p=<?php echo $page - 1; ?>">上一页</a><?php endif; ?>
        <?php for ($i = 1; $i <= $total_pages; $i++): ?>
        <a href="?page=myorder&p=<?php echo $i; ?>" class="<?php echo $i === $page ? 'current' : ''; ?>"><?php echo $i; ?></a>
        <?php endfor; ?>
        <?php if ($page < $total_pages): ?><a href="?page=myorder&p=<?php echo $page + 1; ?>">下一页</a><?php endif; ?>
    </div>
    <?php endif; ?>
    <?php endif; ?>
</div>

<style>
.order-stats { display:flex; gap:16px; margin-bottom:24px; }
.stat-card { flex:1; background:#fff; border:1px solid #e5e7eb; border-radius:12px; padding:16px 20px; display:flex; align-items:center; gap:14px; }
.stat-icon { font-size:28px; }
.stat-info .stat-value { font-size:22px; font-weight:700; color:#1a1a1a; }
.stat-info .stat-label { font-size:13px; color:#6b7280; margin-top:2px; }
.order-list { display:flex; flex-direction:column; gap:16px; }
.order-item { background:#fff; border:1px solid #e5e7eb; border-radius:12px; overflow:hidden; }
.order-header { display:flex; justify-content:space-between; padding:12px 16px; background:#f9fafb; border-bottom:1px solid #e5e7eb; font-size:13px; color:#6b7280; }
.order-body { padding:12px 16px; }
.order-info-row { }
.order-amount { font-weight:600; color:#1a1a1a; }
.order-amount .amount-label { font-size:13px; }
.order-amount .amount-value { font-size:18px; color:#f5a623; }
.order-status .status { padding:3px 10px; border-radius:20px; font-size:12px; }
.status-pending { background:#fef3c7; color:#92400e; }
.status-paid { background:#d1fae5; color:#065f46; }
.status-shipped { background:#dbeafe; color:#1e40af; }
.status-completed { background:#ecfdf5; color:#065f46; }
.status-canceled { background:#f3f4f6; color:#6b7280; }
.btn-sm { padding:6px 14px; font-size:13px; border-radius:8px; }
.btn-primary { background:#10b981; color:#fff; }
.btn-outline { border:1px solid #d1d5db; color:#374151; }
.empty-state { text-align:center; padding:60px; background:#fff; border:1px solid #e5e7eb; border-radius:12px; }
.empty-state-icon { font-size:48px; margin-bottom:12px; }
.empty-state-text { color:#6b7280; font-size:15px; }
.pagination { display:flex; justify-content:center; gap:8px; margin-top:20px; }
.pagination a { padding:8px 16px; border:1px solid #d1d5db; border-radius:8px; color:#374151; font-size:14px; }
.pagination a.current { background:#10b981; color:#fff; border-color:#10b981; }
.pagination a:hover { background:#f3f4f6; }
</style>
