<?php

ob_start();

require_once __DIR__ . '/../library/auth.php';

allow_roles(['admin', 'accountant']);

$school_id          = $_SESSION['school_id'];

$current_session_id = get_current_session_id($pdo);

// ======================================================
// GET RECEIPT NUMBER
// ======================================================

$receipt_no = trim($_GET['receipt_no'] ?? '');

if ($receipt_no === '') {

    die('Invalid receipt number.');
}

// ======================================================
// FETCH RECEIPT
// ======================================================

$stmt = $pdo->prepare("
    SELECT

        fp.id,
        fp.receipt_no,
        fp.paid_amount,
        fp.payment_mode,
        fp.payment_date,
        fp.created_at,

        s.name,

        ss.roll_no,
        ss.section,

        c.class_name,

        sp.father_name,
        sp.father_phone,

        sch.school_name,
        sch.school_address,
        sch.school_phone

    FROM fee_payments fp

    INNER JOIN students s
        ON s.id = fp.student_id

    INNER JOIN student_sessions ss
        ON ss.student_id = s.id
        AND ss.session_id = fp.session_id
        AND ss.school_id = fp.school_id

    INNER JOIN classes c
        ON c.id = ss.class_id

    LEFT JOIN student_parents sp
        ON sp.student_id = s.id
        AND sp.school_id = fp.school_id
        AND sp.session_id = fp.session_id

    LEFT JOIN school_settings sch
        ON sch.school_id = fp.school_id

    WHERE fp.receipt_no = ?
    AND fp.school_id = ?
    AND fp.session_id = ?

    LIMIT 1
");

$stmt->execute([

    $receipt_no,

    $school_id,

    $current_session_id

]);

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

// ======================================================
// VALIDATE RECEIPT
// ======================================================

if (!$receipt) {

    die('Receipt not found.');
}

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

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

    <title>

        Receipt #
        <?= htmlspecialchars($receipt['receipt_no']) ?>

    </title>

    <style>

        *{
            margin:0;
            padding:0;
            box-sizing:border-box;
        }

        body{
            background:#f4f6f9;
            font-family:Arial,sans-serif;
            color:#333;
            padding:20px;
        }

        .receipt-wrapper{
            max-width:850px;
            margin:auto;
        }

        .top-actions{
            text-align:right;
            margin-bottom:15px;
        }

        .print-btn{
            background:#007bff;
            color:#fff;
            border:none;
            padding:10px 18px;
            border-radius:5px;
            cursor:pointer;
            font-size:14px;
        }
		.back-btn{
    background:#6c757d;
    color:#fff;
    border:none;
    padding:10px 18px;
    border-radius:5px;
    cursor:pointer;
    font-size:14px;
    margin-right:10px;
}

.back-btn:hover{
    background:#545b62;
}

        .print-btn:hover{
            background:#0056b3;
        }

        .receipt-card{
            background:#fff;
            border-radius:10px;
            overflow:hidden;
            box-shadow:0 2px 12px rgba(0,0,0,0.08);
            border:1px solid #ddd;
        }

        .receipt-header{
            background:#28a745;
            color:#fff;
            padding:25px;
        }

        .school-name{
            font-size:28px;
            font-weight:bold;
            margin-bottom:8px;
        }

        .school-details{
            font-size:14px;
            line-height:1.7;
        }

        .receipt-meta{
            margin-top:20px;
            display:flex;
            justify-content:space-between;
            flex-wrap:wrap;
            gap:10px;
        }

        .receipt-badge{
            background:#fff;
            color:#28a745;
            padding:10px 15px;
            border-radius:6px;
            font-weight:bold;
            font-size:14px;
        }

        .receipt-body{
            padding:25px;
        }

        .section-title{
            font-size:18px;
            font-weight:bold;
            margin-bottom:15px;
            color:#28a745;
            border-bottom:2px solid #eee;
            padding-bottom:8px;
        }

        .grid{
            display:grid;
            grid-template-columns:1fr 1fr;
            gap:25px;
        }

        .detail-box{
            background:#fafafa;
            border:1px solid #eee;
            border-radius:8px;
            padding:18px;
        }

        .detail-row{
            display:flex;
            justify-content:space-between;
            gap:15px;
            padding:10px 0;
            border-bottom:1px dashed #ddd;
            font-size:14px;
        }

        .detail-row:last-child{
            border-bottom:none;
        }

        .detail-label{
            color:#666;
        }

        .detail-value{
            font-weight:bold;
            text-align:right;
        }

        .amount-section{
            margin-top:30px;
            background:#f8f9fa;
            border:2px dashed #28a745;
            border-radius:10px;
            padding:30px;
            text-align:center;
        }

        .amount-title{
            font-size:16px;
            margin-bottom:10px;
            color:#555;
        }

        .amount{
            font-size:42px;
            font-weight:bold;
            color:#28a745;
        }

        .payment-mode{
            margin-top:10px;
            font-size:16px;
            font-weight:bold;
            color:#444;
        }

        .footer{
            padding:25px;
            text-align:center;
            border-top:1px solid #eee;
            font-size:13px;
            color:#666;
        }

        .signature{
            margin-top:40px;
            text-align:right;
        }

        .signature-line{
            display:inline-block;
            width:220px;
            border-top:1px solid #333;
            padding-top:8px;
            text-align:center;
            font-size:13px;
            color:#444;
        }

        @media(max-width:768px){

            body{
                padding:10px;
            }

            .receipt-header{
                padding:20px;
            }

            .school-name{
                font-size:22px;
            }

            .receipt-body{
                padding:15px;
            }

            .grid{
                grid-template-columns:1fr;
            }

            .amount{
                font-size:34px;
            }

            .receipt-meta{
                flex-direction:column;
            }

            .detail-row{
                flex-direction:column;
                gap:4px;
            }

            .detail-value{
                text-align:left;
            }

            .signature{
                text-align:center;
            }
        }

        @media print{
			.back-btn{
    display:none;
}

            body{
                background:#fff;
                padding:0;
            }

            .top-actions{
                display:none;
            }

            .receipt-card{
                box-shadow:none;
                border:none;
            }

            .print-btn{
                display:none;
            }
        }

    </style>

</head>

<body>

<div class="receipt-wrapper">

    <!-- PRINT BUTTON -->

   <div class="top-actions">

    <button
        type="button"
        class="back-btn"
        onclick="history.back()"
    >

        ← Back

    </button>

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

        🖨 Print Receipt

    </button>

</div>
    <!-- RECEIPT -->

    <div class="receipt-card">

        <!-- HEADER -->

        <div class="receipt-header">

            <div class="school-name">

                <?= htmlspecialchars($receipt['school_name'] ?? 'School Name') ?>

            </div>

            <div class="school-details">

                <?= nl2br(htmlspecialchars($receipt['school_address'] ?? '')) ?>

                <br>

                Phone :
                <?= htmlspecialchars($receipt['school_phone'] ?? '-') ?>

            </div>

            <div class="receipt-meta">

                <div class="receipt-badge">

                    Receipt :
                    <?= htmlspecialchars($receipt['receipt_no']) ?>

                </div>

                <div class="receipt-badge">

                    Date :
                    <?= date(
                        'd M Y',
                        strtotime($receipt['payment_date'])
                    ) ?>

                </div>

            </div>

        </div>

        <!-- BODY -->

        <div class="receipt-body">

            <div class="grid">

                <!-- STUDENT -->

                <div class="detail-box">

                    <div class="section-title">

                        Student Details

                    </div>

                    <div class="detail-row">

                        <div class="detail-label">

                            Student Name

                        </div>

                        <div class="detail-value">

                            <?= htmlspecialchars($receipt['name']) ?>

                        </div>

                    </div>

                    <div class="detail-row">

                        <div class="detail-label">

                            Roll No

                        </div>

                        <div class="detail-value">

                            <?= htmlspecialchars($receipt['roll_no'] ?? '-') ?>

                        </div>

                    </div>

                    <div class="detail-row">

                        <div class="detail-label">

                            Class

                        </div>

                        <div class="detail-value">

                            <?= htmlspecialchars($receipt['class_name']) ?>

                            -
                            <?= htmlspecialchars($receipt['section'] ?? '-') ?>

                        </div>

                    </div>

                    <div class="detail-row">

                        <div class="detail-label">

                            Father Name

                        </div>

                        <div class="detail-value">

                            <?= htmlspecialchars($receipt['father_name'] ?? '-') ?>

                        </div>

                    </div>

                    <div class="detail-row">

                        <div class="detail-label">

                            Phone

                        </div>

                        <div class="detail-value">

                            <?= htmlspecialchars($receipt['father_phone'] ?? '-') ?>

                        </div>

                    </div>

                </div>

                <!-- PAYMENT -->

                <div class="detail-box">

                    <div class="section-title">

                        Payment Details

                    </div>

                    <div class="detail-row">

                        <div class="detail-label">

                            Payment Date

                        </div>

                        <div class="detail-value">

                            <?= date(
                                'd M Y',
                                strtotime($receipt['payment_date'])
                            ) ?>

                        </div>

                    </div>

                    <div class="detail-row">

                        <div class="detail-label">

                            Payment Mode

                        </div>

                        <div class="detail-value">

                            <?= htmlspecialchars($receipt['payment_mode']) ?>

                        </div>

                    </div>

                    <div class="detail-row">

                        <div class="detail-label">

                            Receipt Time

                        </div>

                        <div class="detail-value">

                            <?= date(
                                'h:i A',
                                strtotime($receipt['created_at'])
                            ) ?>

                        </div>

                    </div>

                    <div class="detail-row">

                        <div class="detail-label">

                            Session

                        </div>

                        <div class="detail-value">

                            <?= htmlspecialchars($current_session_id) ?>

                        </div>

                    </div>

                </div>

            </div>

            <!-- AMOUNT -->

            <div class="amount-section">

                <div class="amount-title">

                    Amount Received

                </div>

                <div class="amount">

                    ₹<?= number_format($receipt['paid_amount'], 2) ?>

                </div>

                <div class="payment-mode">

                    <?= strtoupper(
                        htmlspecialchars($receipt['payment_mode'])
                    ) ?>

                    PAYMENT

                </div>

            </div>

        </div>

        <!-- FOOTER -->

        <div class="footer">

            <strong>

                Thank You!

            </strong>

            <br><br>

            This is a computer generated receipt.

            <div class="signature">

                <div class="signature-line">

                    Authorized Signature

                </div>

            </div>

        </div>

    </div>

</div>

</body>

</html>

<?php

ob_end_flush();

?>