<?php
// admin/leads.php

$servername = "localhost";
$username = "demoptma_publications";
$password = "dj$JN)hJerjG"; // your DB password
$dbname = "demoptma_publications";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("DB Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM invoices ORDER BY id DESC";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="shortcut icon" href="../assets/img/favicon.png" type="image/x-icon">
    <title>Invoices | WOS Publications UK</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.1/css/all.min.css" integrity="sha512-2SwdPD6INVrV/lHTZbO2nodKhrnDdJK9/kg2XD1r9uGqPo1cUbujc+IYdlYdEErWNu69gVcYgdxlmVmzTWnetw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <style>
        body {
            display: flex;
            min-height: 100vh;
            background: #f4f6f9;
            font-family: "Poppins", sans-serif;
        }
        .sidebar {
            width: 250px;
            background: #043773;
            color: #ecf0f1;
            padding-top: 20px;
            height: 100vh;
            position: fixed;
        }
        .sidebar h2 {
            text-align: center;
            margin-bottom: 30px;
        }
        .sidebar a {
            display: block;
            color: #ecf0f1;
            padding: 20px 20px;
            text-decoration: none;
            font-family: "Poppins", sans-serif;
            border-bottom: 0.7px solid #ffffff47;
        }
        .sidebar a:hover {
            background: #34495e;
        }
        .content {
            flex-grow: 1;
            margin-left: 250px;
            padding: 30px;
        }
        table {
            background: #fff;
        }
        .json-preview {
            max-width: 300px;
            white-space: pre-wrap;
            word-break: break-all;
            font-size: 13px;
            background: #f8f9fa;
            padding: 8px;
            border-radius: 4px;
        }
        
        .content h1 {
            font-family: "Poppins", sans-serif;
            color: #043773;
        }
        
        /*.create-invoice {*/
        /*    text-align: end;*/
        /*}*/
        
        .create-invoice a {
            background-color: #043773;
            color: white;
            padding: 0.5rem 0.7rem;
            font-family: "Inter", sans-serif;
        }
        
        .table-dark {
            --bs-table-bg: #043773 !important;
        }
        
        table th, table td {
            font-size: 0.9rem !important;
            font-family: "Inter", sans-serif;
            padding: 1rem 2rem !important;
        }
        
        .view-inv {
            background-color: #043773;
            padding: 0.5rem 0.4rem;
            border: none;
        }

        .sidebar-logo {
            filter: brightness(0.5) invert(1);
        }
    </style>
</head>
<body>

<div class="sidebar">
    <div class="sidebar-logo" style="display:flex;justify-content:center;padding-bottom:3rem;">
        <img src="../assets/img/logo-new.png" width="70%">
    </div>
    
    <!---->
    
    <a href="invoice.php" class="active">Invoice</a>
</div>

<div class="content">
    <div class="content-header" style="display:flex;justify-content:space-between;align-items:center;padding-top: 5rem; padding-bottom: 1rem;">
        <h1>Invoices <i class="fa-solid fa-file-invoice" style="color: #043773;"></i></h1>
        <div class="mb-3 create-invoice">
            <a href="create_invoice.php" class="btn btn-success">Create New Invoice <i class="fa-solid fa-circle-plus" style="color: #ffffff;"></i></a>
        </div>
    </div>


    <table class="table table-bordered table-striped mt-3">
        <thead class="table-dark">
            <tr>
                <th>ID</th>
                <!--<th>Merchant</th>-->
                <th>Agent</th>
                <th>Client Name</th>
                <!--<th>Client Email</th>-->
                <!--<th>Client Phone</th>-->
                <!--<th>Package</th>-->
                <th>Amount</th>
                <th>Payment Status</th>
                <!--<th>Transaction ID</th>-->
                <th>Checkout Link</th>
                <th>Edit Invoice</th>
                <th>Created At</th>
            </tr>
        </thead>
        <tbody>
        <?php if ($result->num_rows > 0): ?>
            <?php while($row = $result->fetch_assoc()): ?>
                <tr>
                    <td><?= $row['id'] ?></td>
                    <!--<td><?= htmlspecialchars($row['merchant']) ?></td>-->
                    <td><?= htmlspecialchars($row['agent_name']) ?></td>
                    <td><?= htmlspecialchars($row['client_name']) ?></td>
                    <!--<td><?= htmlspecialchars($row['client_email']) ?></td>-->
                    <!--<td><?= htmlspecialchars($row['client_phone']) ?></td>-->
                    <!--<td><?= htmlspecialchars($row['package']) ?></td>-->
                    <td>$<?= number_format($row['amount'], 2) ?></td>
                    <td><?= ucfirst($row['payment_status']) ?></td>
                    <!--<td><?= $row['transaction_id'] ?? '-' ?></td>-->
                    <td>
                        <a href="../checkout.php?reg=<?= $row['id'] ?>" target="_blank" class="btn btn-primary btn-sm view-inv">
                            View Invoice
                        </a>
                    </td>
                    <td>
                        <a href="edit_invoice.php?id=<?= $row['id'] ?>" 
                           class="btn btn-warning btn-sm">
                            Edit
                        </a>
                    </td>
                    <td><?= $row['created_at'] ?></td>
                </tr>
            <?php endwhile; ?>
        <?php else: ?>
            <tr><td colspan="12" class="text-center">No invoices found.</td></tr>
        <?php endif; ?>
        </tbody>
    </table>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.1/js/all.min.js" integrity="sha512-6BTOlkauINO65nLhXhthZMtepgJSghyimIalb+crKRPhvhmsCdnIuGcVbR5/aQY2A+260iC1OPy1oCdB6pSSwQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</body>
</html>
