<?php
session_start();
include('includes/config.php');

if(!isset($_SESSION['user_id'])){
    header("Location: login_user.php");
    exit();
}

$id_user = $_SESSION['user_id'];


$pesanan_saya = mysqli_query($conn, "
    SELECT * FROM pesanan 
    WHERE id_user = $id_user 
    AND status NOT IN ('Selesai', 'Gagal', 'Dibatalkan') 
    ORDER BY id DESC
");


$riwayat_pesanan = mysqli_query($conn, "
    SELECT * FROM pesanan 
    WHERE id_user = $id_user 
    AND status IN ('Selesai', 'Gagal', 'Dibatalkan') 
    ORDER BY id DESC
");
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Pesanan Saya - Alfateeh Sepatuku</title>
    <style>
        body { font-family: 'Poppins', sans-serif; background: #f5f7fa; margin: 0; padding: 0; }
        .container { max-width: 1000px; margin: 40px auto; background: #fff; padding: 30px; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
        h2 { color: #1e88e5; border-bottom: 2px solid #1e88e5; padding-bottom: 10px; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        table th, table td { padding: 12px; border-bottom: 1px solid #eee; text-align: left; }
        
        
        .status { padding: 6px 12px; border-radius: 6px; color: #fff; font-size: 0.85rem; font-weight: bold; }
        .st-proses { background: #ff9800; } 
        .st-kirim { background: #2196f3; }  
        .st-selesai { background: #4caf50; } 
        
        .btn-detail { background: #1e88e5; color: #fff; padding: 6px 12px; border-radius: 5px; text-decoration: none; font-size: 0.85rem; }
        .btn-selesai { background: #4caf50; color: #fff; padding: 6px 12px; border-radius: 5px; text-decoration: none; font-size: 0.85rem; font-weight: bold; margin-left: 5px; }
    </style>
</head>
<body>

<div class="container">
    <h2>📦 Pesanan Sedang Berjalan</h2>
    <table>
        <thead>
            <tr>
                <th>No. Pesanan</th>
                <th>Total</th>
                <th>Status</th>
                <th>Aksi</th>
            </tr>
        </thead>
        <tbody>
            <?php while($row = mysqli_fetch_assoc($pesanan_saya)): ?>
            <tr>
                <td><strong><?= $row['no_pesanan'] ?></strong></td>
                <td>Rp <?= number_format($row['total'], 0, ',', '.') ?></td>
                <td>
                    <?php 
                        
                        $status_db = $row['status']; 
                        $class = "st-proses"; 

                        if (strpos(strtolower($status_db), 'kirim') !== false) {
                            $class = "st-kirim"; 
                        } elseif (strpos(strtolower($status_db), 'selesai') !== false) {
                            $class = "st-selesai"; 
                        }
                    ?>
                    <span class="status <?= $class ?>"><?= $status_db ?></span>
                </td>
                <td>
                    <a href="status_pesanan.php?id=<?= $row['id'] ?>" class="btn-detail">Detail</a>
                    
                    <?php if(strpos(strtolower($row['status']), 'kirim') !== false): ?>
                        <a href="proses_selesai.php?id=<?= $row['id'] ?>" class="btn-selesai" onclick="return confirm('Sudah terima barang?')">Selesai</a>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>

    <h2 style="margin-top: 50px;">📜 Riwayat Pesanan</h2>
    <table>
        <?php while($row = mysqli_fetch_assoc($riwayat_pesanan)): ?>
            <tr>
                <td><?= $row['no_pesanan'] ?></td>
                <td>Rp <?= number_format($row['total'], 0, ',', '.') ?></td>
                <td><span class="status st-selesai"><?= $row['status'] ?></span></td>
                <td><a href="status_pesanan.php?id=<?= $row['id'] ?>" class="btn-detail">Detail</a></td>
            </tr>
        <?php endwhile; ?>
    </table>
</div>

</body>
</html>