<?php
ob_start();

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

include 'auth.php';
include "db.php";

/* 📊 DATABASE SE PURCHASE QUOTATIONS LIST NIKALNA */
$quota_query = "
    SELECT 
        pq.id as quota_id, 
        pq.date_issued, 
        pq.valid_till, 
        s.name as supplier_name, 
        pq.grand_total, 
        pq.status
    FROM purchase_quotations pq
    LEFT JOIN suppliers s ON pq.supplier_id = s.id
    ORDER BY pq.date_issued DESC, pq.id DESC
";

$quotations = [];
$table_exists = true;

try {
    $result = $conn->query($quota_query);
    if ($result && $result->num_rows > 0) {
        while($row = $result->fetch_assoc()){
            $quotations[] = $row;
        }
    }
} catch (Exception $e) {
    // Agar database mein abhi table nahi bana, toh system crash nahi hoga
    $table_exists = false; 
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Purchase Estimates / Quotations List</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">

<style>
body{
    background:#eef2f7;
    font-family:'Poppins',sans-serif;
    margin:0;
    padding:0;
    color:#333;
}
.page-wrapper{
    width:95%;
    max-width:1200px;
    margin:40px auto;
}
.page-card{
    background:#fff;
    border-radius:18px;
    box-shadow:0 10px 35px rgba(0,0,0,0.06);
    padding:28px;
}
.page-header{
    display:flex;
    justify-content:space-between;
    align-items:center;
    margin-bottom:25px;
    flex-wrap:wrap;
    gap:15px;
}
.page-header h2{
    margin:0;
    font-size:22px;
    font-weight:700;
    color:#222;
}

.btn-add-new {
    background: linear-gradient(135deg, #0d6efd, #0a58ca);
    color: #fff;
    padding: 9px 18px;
    border-radius: 8px;
    text-decoration: none;
    font-weight: 600;
    font-size: 13px;
    transition: 0.3s;
    box-shadow: 0 3px 10px rgba(13,110,253,0.2);
}
.btn-add-new:hover {
    transform: translateY(-1px);
    opacity: 0.95;
}

.table-wrapper{
    overflow-x:auto;
    margin-top:15px;
}
table{
    width:100%;
    border-collapse:collapse;
    border-radius:12px;
    overflow:hidden;
}
thead{
    background:linear-gradient(90deg,#0d6efd,#0a58ca);
    color:#fff;
}
th{
    padding:14px;
    font-size:13px;
    font-weight:600;
    text-align:left;
}
td{
    padding:14px;
    border-bottom:1px solid #eee;
    background:#fff;
    font-size:13px;
}
tbody tr:hover td{
    background:#f4f8ff;
}
.amount-text{
    font-weight:700;
    color:#222;
}
.date-alert {
    color: #dc3545;
    font-weight: 500;
}

/* 🌟 STATUS BADGES STYLE */
.status-badge {
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 11px;
    font-weight: 600;
    display: inline-block;
}
.status-pending {
    background: #fff3cd;
    color: #856404;
}
.status-confirmed {
    background: #d4edda;
    color: #155724;
}

/* 🌟 ACTION BUTTONS CONFIG */
.actions-group {
    display: flex;
    gap: 6px;
    align-items: center;
    flex-wrap: nowrap;
}
.btn-action {
    padding: 5px 12px;
    text-align: center;
    border-radius: 5px;
    text-decoration: none;
    font-size: 11px;
    font-weight: 600;
    transition: 0.2s;
    color: #fff;
    display: inline-block;
    border: none;
    cursor: pointer;
}
.btn-view { background: #17a2b8; }
.btn-view:hover { background: #138496; }

.btn-confirm { background: #28a745; }
.btn-confirm:hover { background: #218838; }

.btn-attach { background: #6c757d; }
.btn-attach:hover { background: #5a6268; }

.btn-delete { background: #dc3545; }
.btn-delete:hover { background: #c82333; }

.no-data{
    text-align:center;
    padding:40px;
    color:#777;
    font-size:15px;
}
@media(max-width:768px){
    .page-header{
        flex-direction:column;
        align-items:stretch;
        text-align:center;
    }
    .btn-add-new{
        display:block;
    }
}
</style>
</head>
<body>

<div class="page-wrapper">
<div class="page-card">

    <div class="page-header">
        <h2>📋 Purchase Estimates / Quotations List</h2>
        <a href="purchase_quota_add.php" class="btn-add-new">+ Create New Quote Request</a>
    </div>

    <div class="table-wrapper">
        <table>
        <thead>
        <tr>
            <th>Quote No.</th>
            <th>Date Issued</th>
            <th>Valid Till</th>
            <th>Supplier Name</th>
            <th>Amount</th>
            <th>Status</th>
            <th style="width: 260px;">Actions</th>
        </tr>
        </thead>
        <tbody>

        <?php
        if(count($quotations) > 0){
            foreach($quotations as $row){
                $quote_no = "#PQR-" . str_pad($row['quota_id'], 3, '0', STR_PAD_LEFT);
                $date_issued = date('d M Y', strtotime($row['date_issued']));
                $valid_till = date('d M Y', strtotime($row['valid_till']));
                $supplier_name = !empty($row['supplier_name']) ? htmlspecialchars($row['supplier_name']) : "Unknown Supplier";
                $amount = number_format($row['grand_total'], 2);
                
                // Status Class Assigning
                $status_class = (trim(strtolower($row['status'])) == 'confirmed') ? 'status-confirmed' : 'status-pending';
                $status_text = htmlspecialchars($row['status']);

                // Real Activated Links mapping
                $view_link = "purchase_quota_view.php?id=" . $row['quota_id'];
                $confirm_link = "purchase_quota_confirm.php?id=" . $row['quota_id'];
                $attach_link = "purchase_quota_attach.php?id=" . $row['quota_id'];
                $delete_link = "purchase_quota_delete.php?id=" . $row['quota_id'];

                echo "<tr>
                    <td><b>{$quote_no}</b></td>
                    <td>{$date_issued}</td>
                    <td class='date-alert'>{$valid_till}</td>
                    <td><b>{$supplier_name}</b></td>
                    <td class='amount-text'>{$amount} PKR</td>
                    <td><span class='status-badge {$status_class}'>{$status_text}</span></td>
                    <td>
                        <div class='actions-group'>
                            <a href='{$view_link}' class='btn-action btn-view'>View</a>";
                            
                            if(strtolower($row['status']) !== 'confirmed') {
                                echo "<a href='{$confirm_link}' class='btn-action btn-confirm'>Confirm</a>";
                            }
                            
                            echo "<a href='{$attach_link}' class='btn-action btn-attach'>Attach</a>
                            <a href='{$delete_link}' class='btn-action btn-delete' onclick='return confirm(\"Are you sure you want to delete this quote?\")'>Delete</a>
                        </div>
                    </td>
                </tr>";
            }
        } else {
            // Preview Row (Jab tak database mein records na hon, tab tak UI empty nahi lagega)
            echo "<tr>
                <td><b>#PQR-001</b></td>
                <td>18 May 2026</td>
                <td class='date-alert'>18 Jun 2026</td>
                <td><b>Al-Madina Bricks Traders (Sample Preview)</b></td>
                <td class='amount-text'>344,320.00 PKR</td>
                <td><span class='status-badge status-pending'>Pending</span></td>
                <td>
                    <div class='actions-group'>
                        <a href='purchase_quota_view.php?id=1' class='btn-action btn-view'>View</a>
                        <a href='#' class='btn-action btn-confirm' onclick='alert(\"Sample confirmation placeholder\")'>Confirm</a>
                        <a href='#' class='btn-action btn-attach' onclick='alert(\"Attachment feature active on live entries\")'>Attach</a>
                        <a href='#' class='btn-action btn-delete' onclick='alert(\"Cannot delete a sample preview row.\")'>Delete</a>
                    </div>
                </td>
            </tr>";
        }
        ?>

        </tbody>
        </table>
    </div>

</div>
</div>

</body>
</html>