




						
<?php
session_start();
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/functions.php';
check_admin();

// Handle delete
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $stmt = $conn->prepare("DELETE FROM invoices WHERE id = ?");
    $stmt->bind_param('i', $id);
    $stmt->execute();
    redirect('manage_invoices.php');
}

// Handle search and filters
$search = trim($_GET['search'] ?? '');
$status_filter = trim($_GET['status'] ?? '');
$date_from = trim($_GET['from'] ?? '');
$date_to = trim($_GET['to'] ?? '');

$where = [];
if ($search !== '') {
    $esc = $conn->real_escape_string($search);
    $where[] = "(invoice_number LIKE '%$esc%' OR client_name LIKE '%$esc%')";
}
if ($status_filter !== '') {
    $esc = $conn->real_escape_string($status_filter);
    $where[] = "status = '$esc'";
}
if ($date_from !== '') {
    $esc = $conn->real_escape_string($date_from);
    $where[] = "date >= '$esc'";
}
if ($date_to !== '') {
    $esc = $conn->real_escape_string($date_to);
    $where[] = "date <= '$esc'";
}
$whereClause = '';
if (!empty($where)) {
    $whereClause = 'WHERE ' . implode(' AND ', $where);
}

// Fetch invoices
$invoices = [];
$query = "SELECT * FROM invoices $whereClause ORDER BY date DESC";
$res = $conn->query($query);
if ($res && $res->num_rows > 0) {
    while ($row = $res->fetch_assoc()) {
        $invoices[] = $row;
    }
}

// Unique statuses for filter dropdown
$statusOptions = [];
$resStatus = $conn->query("SELECT DISTINCT status FROM invoices");
if ($resStatus) {
    while ($row = $resStatus->fetch_assoc()) {
        $statusOptions[] = $row['status'];
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Invoices - Admin</title>
    <link rel="stylesheet" href="../assets/css/style.css">
    <!-- Include FontAwesome for icons -->
    <link rel="stylesheet" href="../assets/fontawesome/fontawesome-all.min.css">
</head>
<body>
<div class="admin-wrapper">
    <div class="admin-sidebar">
        <h2>Admin Panel</h2>
        <!-- Sidebar toggle for collapse -->
        <span class="toggle-btn" onclick="toggleSidebar()"><i class="fas fa-bars"></i></span>
        <a href="dashboard.php"><i class="fas fa-tachometer-alt"></i><span class="label">Dashboard</span></a>
        <a href="manage_sliders.php"><i class="fas fa-sliders-h"></i><span class="label">Manage Sliders</span></a>
        <a href="manage_services.php"><i class="fas fa-cogs"></i><span class="label">Manage Services</span></a>
        <a href="manage_products.php"><i class="fas fa-box-open"></i><span class="label">Manage Products</span></a>
        <a href="manage_blogs.php"><i class="fas fa-blog"></i><span class="label">Manage Blogs</span></a>
        <a href="manage_gallery.php"><i class="fas fa-images"></i><span class="label">Manage Gallery</span></a>
        <a href="manage_clients.php"><i class="fas fa-users"></i><span class="label">Manage Clients</span></a>
        <a href="manage_appointments.php"><i class="fas fa-calendar-check"></i><span class="label">Appointments</span></a>
        <a href="manage_service_bookings.php"><i class="fas fa-tools"></i><span class="label">Service Bookings</span></a>
        <a href="manage_parts_bookings.php"><i class="fas fa-cubes"></i><span class="label">Parts Bookings</span></a>
        <a href="manage_enquiries.php"><i class="fas fa-envelope"></i><span class="label">Enquiries</span></a>
        <a href="manage_invoices.php" class="active"><i class="fas fa-file-invoice-dollar"></i><span class="label">Invoices</span></a>
        <a href="logout.php"><i class="fas fa-sign-out-alt"></i><span class="label">Logout</span></a>
    </div>
    <div class="admin-content">
        <h1>Invoices</h1>
        <p><a href="invoice_form.php" class="btn-primary" style="padding:8px 15px;display:inline-block;">Add New Invoice</a></p>
        <form method="get" action="" style="margin-bottom:20px;display:flex;flex-wrap:wrap;gap:10px;align-items:flex-end;">
            <div class="form-group" style="margin-right:10px;">
                <label>Search</label>
                <input type="text" name="search" value="<?php echo e($search); ?>" placeholder="Invoice # or Client name">
            </div>
            <div class="form-group" style="margin-right:10px;">
                <label>Status</label>
                <select name="status">
                    <option value="">All</option>
                    <?php foreach ($statusOptions as $status): ?>
                        <option value="<?php echo e($status); ?>" <?php echo $status_filter === $status ? 'selected' : ''; ?>><?php echo e(ucfirst($status)); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group" style="margin-right:10px;">
                <label>From</label>
                <input type="date" name="from" value="<?php echo e($date_from); ?>">
            </div>
            <div class="form-group" style="margin-right:10px;">
                <label>To</label>
                <input type="date" name="to" value="<?php echo e($date_to); ?>">
            </div>
            <div class="form-group">
                <button type="submit" class="btn-primary" style="padding:8px 15px;">Filter</button>
            </div>
        </form>
        <table class="table">
            <tr>
                <th>ID</th>
                <th>Invoice #</th>
                <th>Client Name</th>
                <th>Amount (₹)</th>
                <th>Date</th>
                <th>Status</th>
                <th>Actions</th>
            </tr>
            <?php foreach ($invoices as $inv): ?>
            <tr>
                <td><?php echo $inv['id']; ?></td>
                <td><?php echo e($inv['invoice_number']); ?></td>
                <td><?php echo e($inv['client_name']); ?></td>
                <td><?php echo number_format($inv['amount'], 2); ?></td>
                <td><?php echo date('Y-m-d', strtotime($inv['date'])); ?></td>
                <td><?php echo e(ucfirst($inv['status'])); ?></td>
                <td class="actions" style="white-space:nowrap;">
                    <a href="invoice_view.php?id=<?php echo $inv['id']; ?>" title="View PDF"><i class="fas fa-file-pdf"></i></a>
                    <a href="invoice_form.php?id=<?php echo $inv['id']; ?>" title="Edit" style="margin-left:8px;"><i class="fas fa-edit"></i></a>
                    <a href="manage_invoices.php?delete=<?php echo $inv['id']; ?>" title="Delete" onclick="return confirm('Delete this invoice?');" style="margin-left:8px;"><i class="fas fa-trash"></i></a>
                </td>
            </tr>
            <?php endforeach; ?>
        </table>
        <footer class="admin-footer">
            &copy; 2026 Blueswirlz Engineers & Consultants. All rights reserved.
        </footer>
    </div>
    </div>
</b