<?php 
include 'db.php';
?>
<?php 
$title = "Invoice Data";
include 'include/header.php';?>

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

        <?php if (!empty($_SESSION['message'])): ?>
        <div id="customAlert" class="custom-alert">
            <?= $_SESSION['message']; unset($_SESSION['message']); ?>
            <span class="close-btn" onclick="closeAlert()">×</span>
        </div>
        <?php endif; ?>


        <div class="card shadow">
            <div class="row">
                <div class="col-md-6">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">All Invoices</h5>

                        <?php
                    $cnt = mysqli_fetch_assoc(mysqli_query($con,
                        "SELECT COUNT(*) total FROM invoices"
                    ));
                ?>
                        <span class="badge">Total:
                            <?= $cnt['total'] ?>
                        </span>
                    </div>
                </div>
                <div class="col-md-6 " style="text-align: end;">
                    <a href="invoice_form.php" class="btn btn-primary">Add New invoice</a>
                </div>
            </div>



            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <!-- <input type="text" id="search" class="form-control mb-3" placeholder="Search invoice no / client / phone"> -->
                    </div>
                    <div class="col-md-6">
                        <a href=""></a>
                        <input type="text" id="search" class="form-control mb-3"
                            placeholder="Search invoice no / client / Date">
                    </div>
                </div>


                <div class="table-responsive">
                    <table class="table table-bordered table-hover" id="invoiceTable">
                        <thead class="table-dark">
                            <tr>
                                <th>#</th>
                                <th>Invoice No</th>
                                <th>Date</th>
                                <th>Client</th>
                                <th>Amount</th>
                                <th>Phone</th>
                                <th>Payment</th>
                                <th width="150">Action</th>
                            </tr>
                        </thead>

                        <tbody>
                            <?php
                                $q = mysqli_query($con,"SELECT * FROM invoices ORDER BY id DESC");
                                $i=1;

                                while($r=mysqli_fetch_assoc($q)){
                                    // Get all payment types for this invoice
                                    $payments_arr = [];
                                    $pay_q = mysqli_query($con,"SELECT payment_type,amount FROM invoice_payments WHERE invoice_id='".$r['id']."'");
                                    while($pay = mysqli_fetch_assoc($pay_q)){
                                        $payments_arr[] = $pay['payment_type'] . " ₹" . number_format($pay['amount'],2);
                                    }
                                    $payment_text = implode(", ", $payments_arr);
                            ?>
                            <tr>
                                <td>
                                    <?= $i++ ?>
                                </td>
                                <td>
                                    <?= htmlspecialchars($r['invoice_no']) ?>
                                </td>
                                <td>
                                    <?= date('d-m-Y',strtotime($r['invoice_date'])) ?>
                                </td>
                                <td>
                                    <?= htmlspecialchars($r['client_name']) ?>
                                </td>
                                <td>₹
                                    <?= number_format($r['final_amount'],2) ?>
                                </td>
                                <td>
                                    <?= htmlspecialchars($r['phone']) ?>
                                </td>
                                <td>
                                    <?= htmlspecialchars($payment_text) ?>
                                </td>

                                <td>
                                    <a href="invoice_view.php?id=<?= $r['id'] ?>" class="btn btn-info btn-sm">
                                        <i class="fa fa-eye"></i>
                                    </a>

                                    <a href="invoice_edit.php?id=<?= $r['id'] ?>" class="btn btn-warning btn-sm">
                                        <i class="fa fa-edit"></i>
                                    </a>

                                    <a href="invoice_delete.php?id=<?= $r['id'] ?>" class="btn btn-danger btn-sm"
                                        onclick="return confirm('Delete this invoice?')">
                                        <i class="fa fa-trash"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php } ?>

                            <?php if(mysqli_num_rows($q)==0){ ?>
                            <tr>
                                <td colspan="8" class="text-center">No invoices found</td>
                            </tr>
                            <?php } ?>

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

            </div>
        </div>

    </div>
</div>

<!-- 🔍 SEARCH -->
<script>
    document.getElementById('search').addEventListener('keyup', function () {
        let value = this.value.toLowerCase();
        document.querySelectorAll('#invoiceTable 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'; ?>