<?php

session_start();

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

// ======================================================
// VALIDATE ID
// ======================================================

$invoice_id = (int) ($_GET['id'] ?? 0);

if ($invoice_id <= 0) {

    die('Invalid Invoice ID');
}

// ======================================================
// FETCH INVOICE
// ======================================================

$stmt = $pdo->prepare("
    SELECT
        i.*,

        s.name AS school_name,
        s.address,
        s.phone,
        s.email

    FROM invoices i

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

    WHERE i.id = ?
    LIMIT 1
");

$stmt->execute([$invoice_id]);

$invoice = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$invoice) {

    die('Invoice not found');
}

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    >

    <title>

        Print Invoice

    </title>

    <!-- Bootstrap -->

    <link
        rel="stylesheet"
        href="../../assets/plugins/bootstrap/css/bootstrap.min.css"
    >

    <!-- Font Awesome -->

    <link
        rel="stylesheet"
        href="../../assets/plugins/fontawesome-free/css/all.min.css"
    >

    <style>

        body {

            background: #f4f6f9;
            font-family: Arial, sans-serif;
        }

        .invoice-box {

            max-width: 900px;
            margin: 30px auto;
            background: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }

        .invoice-title {

            font-size: 28px;
            font-weight: bold;
        }

        .invoice-status {

            padding: 8px 14px;
            border-radius: 4px;
            color: #fff;
            font-weight: bold;
            display: inline-block;
        }

        .paid {

            background: #28a745;
        }

        .pending {

            background: #ffc107;
            color: #000;
        }

        .cancelled {

            background: #dc3545;
        }

        @media print {

            .no-print {

                display: none !important;
            }

            body {

                background: #fff;
            }

            .invoice-box {

                box-shadow: none;
                margin: 0;
            }
        }

    </style>

</head>

<body>

<div class="invoice-box">

    <!-- HEADER -->

    <div class="row mb-4">

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

            <h2 class="invoice-title">

                SaaS ERP Invoice

            </h2>

            <p class="mb-1">

                <strong>

                    Invoice No:

                </strong>

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

            </p>

            <p class="mb-1">

                <strong>

                    Date:

                </strong>

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

            </p>

        </div>

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

            <?php

            $statusClass = 'pending';

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

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

            ?>

            <span class="invoice-status <?= $statusClass; ?>">

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

            </span>

        </div>

    </div>

    <hr>

    <!-- SCHOOL DETAILS -->

    <div class="row mb-4">

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

            <h5>

                Billed To

            </h5>

            <p class="mb-1">

                <strong>

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

                </strong>

            </p>

            <p class="mb-1">

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

            </p>

            <p class="mb-1">

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

            </p>

            <p class="mb-1">

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

            </p>

        </div>

    </div>

    <!-- INVOICE TABLE -->

    <div class="table-responsive mb-4">

        <table class="table table-bordered">

            <thead class="thead-dark">

                <tr>

                    <th>

                        Plan

                    </th>

                    <th>

                        Billing Cycle

                    </th>

                    <th>

                        Start Date

                    </th>

                    <th>

                        End Date

                    </th>

                    <th>

                        Amount

                    </th>

                </tr>

            </thead>

            <tbody>

                <tr>

                    <td>

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

                    </td>

                    <td>

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

                    </td>

                    <td>

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

                    </td>

                    <td>

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

                    </td>

                    <td>

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

                    </td>

                </tr>

            </tbody>

        </table>

    </div>

    <!-- PAYMENT DETAILS -->

    <div class="row mb-4">

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

            <h5>

                Payment Details

            </h5>

            <p class="mb-1">

                <strong>

                    Method:

                </strong>

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

            </p>

            <p class="mb-1">

                <strong>

                    Remarks:

                </strong>

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

            </p>

        </div>

    </div>

    <!-- TOTAL -->

    <div class="row">

        <div class="col-md-12 text-right">

            <h3>

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

            </h3>

        </div>

    </div>

    <hr>

    <!-- FOOTER -->

    <div class="text-center mt-4">

        <p class="mb-0">

            Thank you for using our School ERP SaaS.

        </p>

    </div>

    <!-- PRINT BUTTON -->

    <div class="text-center mt-4 no-print">

        <button
            onclick="window.print();"
            class="btn btn-primary"
        >

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

            Print Invoice

        </button>

        <a
            href="view_invoices.php"
            class="btn btn-secondary"
        >

            Back

        </a>

    </div>

</div>

</body>

</html>