<?php 
include 'db.php';
?>
<?php $title = "All Clients"; include 'include/header.php';?>


<div id="page-wrapper">
    <div id="page-inner">

        <div class="card shadow">

            <!-- HEADER -->
            <div class="row">
                <div class="col-md-6">
                    <div class="card-header">
                        <h5 class="mb-0">All Clients</h5>
                        <?php
                        $cnt = mysqli_fetch_assoc(mysqli_query(
                            $con,
                            "SELECT COUNT(DISTINCT phone) total FROM invoices"
                        ));
                        ?>
                        <span class="badge">Total: <?= $cnt['total'] ?></span>
                    </div>
                </div>
                <div class="col-md-6 text-end p-3">
                    <input type="text" id="search" class="form-control w-75 d-inline"
                        placeholder="Search client / phone / city">
                </div>
            </div>

            <!-- TABLE -->
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered table-hover" id="clientTable">
                        <thead class="table-dark">
                            <tr>
                                <th>#</th>
                                <th>Client Name</th>
                                <th>Phone</th>
                                <th>City</th>
                                <th>State</th>
                                <th>Total Invoices</th>
                                <th>Total Business</th>
                                <!-- <th width="120">Action</th> -->
                            </tr>
                        </thead>
                        <tbody>

                        <?php
                        $q = mysqli_query($con,"
                            SELECT 
                                client_name,
                                phone,
                                city,
                                state,
                                COUNT(id) AS total_invoices,
                                SUM(final_amount) AS total_amount
                            FROM invoices
                            GROUP BY phone
                            ORDER BY client_name ASC
                        ");

                        $i = 1;
                        while($r = mysqli_fetch_assoc($q)):
                        ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><?= htmlspecialchars($r['client_name']) ?></td>
                                <td><?= htmlspecialchars($r['phone']) ?></td>
                                <td><?= htmlspecialchars($r['city']) ?></td>
                                <td><?= htmlspecialchars($r['state']) ?></td>
                                <td class="text-center"><?= $r['total_invoices'] ?></td>
                                <td>₹ <?= number_format($r['total_amount'],2) ?></td>
                                <!-- <td class="text-center">
                                    <a href="client_invoices.php?phone=<?= urlencode($r['phone']) ?>"
                                       class="btn btn-info btn-sm">
                                        View
                                    </a>
                                </td> -->
                            </tr>
                        <?php endwhile; ?>

                        <?php if(mysqli_num_rows($q) == 0): ?>
                            <tr>
                                <td colspan="8" class="text-center">No clients found</td>
                            </tr>
                        <?php endif; ?>

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

        </div>

    </div>
</div>

<!-- 🔍 SEARCH -->
<script>
document.getElementById('search').addEventListener('keyup', function () {
    let value = this.value.toLowerCase();
    document.querySelectorAll('#clientTable tbody tr').forEach(tr => {
        tr.style.display = tr.innerText.toLowerCase().includes(value) ? '' : 'none';
    });
});
</script>

<style>
.badge {
    background: #0d6efd;
    padding: 6px 12px;
    border-radius: 20px;
    font-weight: bold;
}
.table td, .table th {
    vertical-align: middle;
}
</style>

<?php include 'include/footer.php'; ?>
