<?php

session_start();

require_once __DIR__ . '/../../library/config.php';
require_once __DIR__ . '/../../library/super_admin_auth.php';

$page_title = 'View Invoices';

require_once __DIR__ . '/../includes/header.php';
require_once __DIR__ . '/../includes/sidebar.php';

// ======================================================
// FETCH INVOICES
// ======================================================

$stmt = $pdo->prepare("
    SELECT
        i.id,
        i.invoice_no,
        i.plan_name,
        i.amount,
        i.billing_cycle,
        i.start_date,
        i.end_date,
        i.payment_status,
        i.payment_method,
        i.created_at,

        s.name AS school_name

    FROM invoices i

    INNER JOIN schools s
        ON s.id = i.school_id

    ORDER BY i.id DESC
");

$stmt->execute();

$invoices = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<!-- Content Header -->

<section class="content-header">

    <div class="container-fluid">

        <div class="row mb-2">

            <div class="col-sm-6">

                <h1>

                    Invoice Management

                </h1>

            </div>

            <div class="col-sm-6 text-right">

                <a
                    href="create_invoice.php"
                    class="btn btn-primary"
                >

                    <i class="fas fa-plus"></i>

                    Create Invoice

                </a>

            </div>

        </div>

    </div>

</section>

<!-- Main Content -->

<section class="content">

    <div class="container-fluid">

        <div class="card card-primary">

            <div class="card-header">

                <h3 class="card-title">

                    All Invoices

                </h3>

            </div>

            <div class="card-body">

                <div class="table-responsive">

                    <table class="table table-bordered table-hover table-striped">

                        <thead class="thead-dark">

                            <tr>

                                <th width="60">

                                    #

                                </th>

                                <th>

                                    Invoice No

                                </th>

                                <th>

                                    School

                                </th>

                                <th>

                                    Plan

                                </th>

                                <th>

                                    Amount

                                </th>

                                <th>

                                    Billing Cycle

                                </th>

                                <th>

                                    Status

                                </th>

                                <th>

                                    Validity

                                </th>

                                <th width="180">

                                    Actions

                                </th>

                            </tr>

                        </thead>

                        <tbody>

                            <?php if (!empty($invoices)): ?>

                                <?php foreach ($invoices as $index => $invoice): ?>

                                    <tr>

                                        <!-- SERIAL -->

                                        <td>

                                            <?= $index + 1; ?>

                                        </td>

                                        <!-- INVOICE NO -->

                                        <td>

                                            <strong>

                                                <?= htmlspecialchars($invoice['invoice_no']); ?>

                                            </strong>

                                        </td>

                                        <!-- SCHOOL -->

                                        <td>

                                            <?= htmlspecialchars($invoice['school_name']); ?>

                                        </td>

                                        <!-- PLAN -->

                                        <td>

                                            <?= htmlspecialchars($invoice['plan_name']); ?>

                                        </td>

                                        <!-- AMOUNT -->

                                        <td>

                                            <span class="badge badge-success p-2">

                                                ₹<?= number_format($invoice['amount'], 2); ?>

                                            </span>

                                        </td>

                                        <!-- BILLING -->

                                        <td>

                                            <span class="badge badge-info p-2">

                                                <?= htmlspecialchars($invoice['billing_cycle']); ?>

                                            </span>

                                        </td>

                                        <!-- STATUS -->

                                        <td>

                                            <?php

                                            $statusClass = 'secondary';

                                            if ($invoice['payment_status'] === 'Paid') {
                                                $statusClass = 'success';
                                            }

                                            elseif ($invoice['payment_status'] === 'Pending') {
                                                $statusClass = 'warning';
                                            }

                                            elseif ($invoice['payment_status'] === 'Cancelled') {
                                                $statusClass = 'danger';
                                            }

                                            ?>

                                            <span class="badge badge-<?= $statusClass; ?> p-2">

                                                <?= htmlspecialchars($invoice['payment_status']); ?>

                                            </span>

                                        </td>

                                        <!-- VALIDITY -->

                                        <td>

                                            <?= date(
                                                'd-m-Y',
                                                strtotime($invoice['start_date'])
                                            ); ?>

                                            <br>

                                            <small>

                                                to

                                            </small>

                                            <br>

                                            <?= date(
                                                'd-m-Y',
                                                strtotime($invoice['end_date'])
                                            ); ?>

                                        </td>

                                        <!-- ACTIONS -->

                                        <td>

                                            <a
                                                href="print_invoice.php?id=<?= $invoice['id']; ?>"
                                                class="btn btn-sm btn-info"
                                            >

                                                <i class="fas fa-eye"></i>

                                            </a>

                                            <a
                                                href="edit_invoice.php?id=<?= $invoice['id']; ?>"
                                                class="btn btn-sm btn-warning"
                                            >

                                                <i class="fas fa-edit"></i>

                                            </a>

                                            <a
                                                href="delete_invoice.php?id=<?= $invoice['id']; ?>"
                                                class="btn btn-sm btn-danger"
                                                onclick="return confirm('Delete this invoice?')"
                                            >

                                                <i class="fas fa-trash"></i>

                                            </a>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>

                            <?php else: ?>

                                <tr>

                                    <td
                                        colspan="9"
                                        class="text-center text-muted"
                                    >

                                        No invoices found.

                                    </td>

                                </tr>

                            <?php endif; ?>

                        </tbody>

                    </table>

                </div>

            </div>

        </div>

    </div>

</section>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>