<?php
session_start();
if(!isset($_SESSION['user']) && !isset($_SESSION['user_id'])){ header("Location: login.php"); exit(); }
include "db.php";

$result = $conn->query("SELECT * FROM quotations ORDER BY id DESC");
?>

<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <title>سجل عروض الأسعار</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Segoe UI', Tahoma, sans-serif; }
        body { display: flex; direction: rtl; background: #0b0f19; color: #f8fafc; min-height: 100vh; }
        .sidebar { width: 260px; height: 100vh; background: #111424; padding: 30px 20px; position: fixed; right: 0; top: 0; border-left: 1px solid #1e293b; }
        .sidebar img { width: 150px; display: block; margin: 0 auto 40px auto; }
        .sidebar a { display: block; color: #94a3b8; text-decoration: none; padding: 12px 15px; margin-bottom: 8px; border-radius: 8px; font-size: 14px; transition: all 0.3s; }
        .sidebar a:hover, .sidebar a.active { background: #1e293b; color: #ffffff; }
        
        .main { margin-right: 260px; padding: 40px; width: 100%; }
        .header-section { display: flex; justify-content: space-between; align-items: center; margin-bottom: 30px; }
        h2 { color: #ffffff; border-right: 5px solid #00f2fe; padding-right: 15px; }
        
        .btn-new { background: linear-gradient(90deg, #00f2fe, #4facfe); color: #111424; padding: 12px 20px; border-radius: 8px; text-decoration: none; font-weight: bold; font-size: 14px; }
        
        .table-container { background: #111424; border-radius: 12px; border: 1px solid #1e293b; overflow: hidden; box-shadow: 0 10px 30px rgba(0,0,0,0.3); }
        table { width: 100%; border-collapse: collapse; text-align: right; font-size: 14px; }
        th { background: #1e293b; color: #94a3b8; padding: 16px; border-bottom: 1px solid #334155; }
        td { padding: 16px; color: #e2e8f0; border-bottom: 1px solid #1e293b; }
        tr:hover td { background: #161b30; }
        
        .btn-print { background: #1e293b; color: #00f2fe; border: 1px solid #334155; padding: 6px 14px; border-radius: 6px; text-decoration: none; font-size: 13px; transition: 0.3s; }
        .btn-print:hover { background: #00f2fe; color: #111424; }
    </style>
</head>
<body>

<div class="sidebar">
    <img src="cropped-Logo-scaled-214x61.png" alt="Glob Network">
    <a href="dashboard.php">Dashboard</a>
    <a href="quotations.php" class="active">عروض الأسعار</a>
    <a href="sales.php">المبيعات</a>
</div>

<div class="main">
    <div class="header-section">
        <h2>أرشيف عروض الأسعار الصادرة</h2>
        <a href="add_quotation.php" class="btn-new">➕ إنشاء عرض سعر جديد</a>
    </div>

    <div class="table-container">
        <table>
            <thead>
                <tr>
                    <th>رقم العرض</th>
                    <th>اسم العميل / الشركة</th>
                    <th>تاريخ الإصدار</th>
                    <th>الضريبة المضافة</th>
                    <th>إجمالي قيمة العرض</th>
                    <th>الإجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php if($result->num_rows > 0): ?>
                    <?php while($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td style="color:#00f2fe; font-weight:bold;">#<?php echo $row['id']; ?></td>
                            <td><?php echo htmlspecialchars($row['customer_name']); ?></td>
                            <td><?php echo date('Y-m-d', strtotime($row['created_at'])); ?></td>
                            <td><?php echo $row['tax_percentage']; ?> %</td>
                            <td style="color:#10b981; font-weight:bold;"><?php echo number_format($row['grand_total'], 2); ?> ج.م</td>
                            <td>
                                <a href="view_quotation.php?id=<?php echo $row['id']; ?>" target="_blank" class="btn-print">🖨️ طباعة ومعاينة العرض</a>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="6" style="text-align: center; color: #94a3b8; padding: 40px;">لا توجد عروض أسعار صادرة حتى الآن.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

</body>
</html>