<?php
session_start();
require_once '../config.php';

error_reporting(E_ALL);
ini_set('display_errors', 1);

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

$userId = (int)$_SESSION['user_id'];

/* =========================
   FETCH ORDER HISTORY
========================= */

$orders = [];

$stmt = $conn->prepare("
    SELECT 
        o.id,
        o.qty,
        o.notes,
        o.status,
        o.created_at,

        p.product_name

    FROM orders o

    LEFT JOIN products p
    ON p.id = o.product_id

    WHERE o.mr_id = ?

    ORDER BY o.id DESC
");

$stmt->bind_param("i", $userId);
$stmt->execute();

$res = $stmt->get_result();

if($res){
    $orders = $res->fetch_all(MYSQLI_ASSOC);
}

$stmt->close();


function e($v){
    return htmlspecialchars($v ?? '', ENT_QUOTES, 'UTF-8');
}

function badge($status){

    $status = strtolower(trim($status));

    if($status == 'approved'){
        return 'approved';
    }

    if($status == 'rejected'){
        return 'rejected';
    }

    return 'pending';
}
?>

<!doctype html>
<html lang="en">

<head>

<meta charset="utf-8">

<meta name="viewport"
content="width=device-width, initial-scale=1">

<title>Order History</title>

<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap"
rel="stylesheet">

<style>
:root{

    --sidebar-width:260px;

    --bg:#f4f7fb;
    --card:#ffffff;

    --text:#0f172a;
    --muted:#64748b;

    --primary:#334155;

    --success:#16a34a;
    --danger:#dc2626;
    --warning:#f59e0b;

    --border:#dbe4ee;

    --shadow:
        0 4px 18px rgba(15,23,42,.05);

    --radius:18px;
}

*{
    box-sizing:border-box;
}

body{
    margin:0;
    font-family:'Inter',sans-serif;
    background:#f1f5f9;
    color:var(--text);
}

/* PAGE */

.page{
    margin-left:var(--sidebar-width);
    padding:25px;
    min-height:100vh;
}

/* HERO */

.hero{
    background:#fff;
    border:1px solid var(--border);
    border-radius:20px;
    padding:18px 22px;
    margin-bottom:14px;
    box-shadow:var(--shadow);
}

.hero h1{
    margin:0;
    font-size:20px;
    font-weight:800;
    display:flex;
    align-items:center;
    gap:10px;
    color:#0f172a;
}

.hero h1 i{
    color:#334155;
}

.hero p{
    margin:6px 0 0;
    color:#64748b;
    font-size:13px;
}

/* CARD */

.card{
    background:#fff;
    border-radius:20px;
    border:1px solid var(--border);
    overflow:hidden;
    box-shadow:var(--shadow);
}

/* TABLE */

.table-wrap{
    overflow:auto;
    cursor:grab;
    user-select:none;
    scroll-behavior:smooth;
}

.table-wrap.active{
    cursor:grabbing;
}

table{
    width:100%;
    border-collapse:collapse;
    min-width:780px;
}

thead{
    background:#334155;
}

th{
    padding:14px 16px;
    text-align:left;
    color:#fff;
    font-size:12px;
    font-weight:700;
    white-space:nowrap;
}

td{
    padding:14px 16px;
    border-bottom:1px solid #eef2f7;
    font-size:13px;
    color:#1e293b;
    background:#fff;
    white-space:nowrap;
    transition:.15s ease;
}

tbody tr:hover td{
    background:#f8fbff;
}

/* PRODUCT */

.product-name{
    font-weight:600;
    color:#0f172a;
}

/* NOTES */

.notes{
    color:#64748b;
    max-width:180px;
    overflow:hidden;
    text-overflow:ellipsis;
}

/* BADGE */

.badge{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    min-width:80px;
    padding:6px 10px;
    border-radius:999px;
    font-size:10px;
    font-weight:700;
}

.pending{
    background:#fef3c7;
    color:#92400e;
}

.approved{
    background:#dcfce7;
    color:#166534;
}

.rejected{
    background:#fee2e2;
    color:#991b1b;
}

/* DATE */

.date{
    color:#64748b;
    font-size:12px;
    font-weight:500;
}

/* EMPTY */

.empty{
    text-align:center;
    padding:30px;
    color:#64748b;
    font-size:14px;
}

/* SCROLLBAR */

.table-wrap::-webkit-scrollbar{
    height:7px;
}

.table-wrap::-webkit-scrollbar-track{
    background:#e2e8f0;
}

.table-wrap::-webkit-scrollbar-thumb{
    background:#94a3b8;
    border-radius:20px;
}

/* MOBILE */

@media(max-width:992px){

    .page{
        margin-left:0;
        padding:80px 12px 12px;
    }
}

@media(max-width:768px){

    .hero{
        padding:16px;
        border-radius:18px;
    }

    .hero h1{
        font-size:18px;
    }

    .hero p{
        font-size:12px;
    }

    th{
        font-size:11px;
        padding:12px;
    }

    td{
        font-size:12px;
        padding:12px;
    }

    .badge{
        min-width:70px;
        font-size:10px;
    }
}
</style>
</head>

<body>

<?php include 'salesexecutivesidebar.php'; ?>
<?php include '../header.php'; ?>

<div class="page">

    <!-- HERO -->

    <div class="hero">

        <h1>

            <i class="fa-solid fa-clock-rotate-left"></i>

            Order History

        </h1>

        <p>
            View all your previous order requests.
        </p>

    </div>

    <!-- CARD -->

    <div class="card">

        <div class="table-wrap">

            <table>

                <thead>

                    <tr>

                        <th>ID</th>
                        <th>Product</th>
                        <th>Qty</th>
                        <th>Notes</th>
                        <th>Status</th>
                        <th>Date</th>

                    </tr>

                </thead>

                <tbody>

                <?php if(!empty($orders)): ?>

                    <?php foreach($orders as $row): ?>

                        <tr>

                            <td>
                                <?php echo (int)$row['id']; ?>
                            </td>

                            <td>
                                <?php echo e($row['product_name']); ?>
                            </td>

                            <td>
                                <?php echo (int)$row['qty']; ?>
                            </td>

                            <td>
                                <?php echo e($row['notes']); ?>
                            </td>

                            <td>

                                <span class="badge <?php echo badge($row['status']); ?>">

                                    <?php echo e($row['status']); ?>

                                </span>

                            </td>

                            <td>

                                <?php
                                echo date(
                                    "d M Y",
                                    strtotime($row['created_at'])
                                );
                                ?>

                            </td>

                        </tr>

                    <?php endforeach; ?>

                <?php else: ?>

                    <tr>

                        <td colspan="6" class="empty">

                            No orders found.

                        </td>

                    </tr>

                <?php endif; ?>

                </tbody>

            </table>

        </div>

    </div>

</div>

</body>
</html>