<?php
include "../db.php";
require '../auth.php';
restrictAccess(['Sub Associate']);
date_default_timezone_set('Asia/Kolkata');
$currentTime = date('M j, Y g:i a');

if (isset($_GET['id'])) {
    $id = intval($_GET['id']);
    $result = mysqli_query($con, "SELECT * FROM student_bill WHERE sb_id = $id");

    if ($row = mysqli_fetch_assoc($result)) {
        // Get student name
        $student = '';
        $student_q = mysqli_query($con, "SELECT uname FROM users WHERE uid = '{$row['sb_for']}'");
        if ($stu = mysqli_fetch_assoc($student_q)) {
            $student = $stu['uname'];
        }

         // Get course name
        $course = '';
        $product_sql = mysqli_query($con, "SELECT * FROM product WHERE pid = '{$row['sb_course']}'");
        if($p_row = mysqli_fetch_assoc($product_sql)){
            $course_q = mysqli_query($con, "SELECT course_name FROM clg_course WHERE sl_no = '{$p_row['product']}'");
            if ($cr = mysqli_fetch_assoc($course_q)) {
                $course = $cr['course_name'];
            }
        }

        // Get college name
        $college = '';
        $college_q = mysqli_query($con, "SELECT uname FROM users WHERE uid = '{$row['sb_college']}'");
        if ($clg = mysqli_fetch_assoc($college_q)) {
            $college = $clg['uname'];
        }

        // Get department
        $department = '';
        $dept_q = mysqli_query($con, "SELECT cat_name FROM clg_course_cat WHERE sl_no = '{$row['sb_dept']}'");
        if ($dept = mysqli_fetch_assoc($dept_q)) {
            $department = $dept['cat_name'];
        }

        $type = ($row['sb_by'] == '1' && $_SESSION["uid"] == 1) ? "Original" : "Duplicate";

        $date = date("M d, Y", strtotime($row['sb_date']));
?>
<!DOCTYPE html>
<html>

<head>
    <title>Invoice - <?= $row['sb_invoice'] ?></title>
    <style>
    body {
        font-family: 'Segoe UI', sans-serif;
        margin: 30px;
        color: #333;
        background: #fff;
    }

    /* .invoice-box {
        max-width: 850px;
        margin: auto;
        padding: 30px 40px;
        background: #fff;
        border: 1px solid #ddd;
        box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
    } */

    .top-section {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .logo {
        height: 60px;
    }

    .invoice-title {
        font-size: 14px;
        font-weight: bold;
        text-align: right;
    }

    .meta {
        margin-top: 20px;
        display: flex;
        justify-content: space-between;
        font-size: 15px;
    }

    .meta div {
        width: 48%;
        line-height: 1.6;
    }

    table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 30px;
    }

    table th,
    table td {
        border: 1px solid #ccc;
        padding: 10px;
        text-align: center;
        font-size: 14px;
    }

    table th {
        background-color: #007bff;
        color: #fff;
        font-weight: 600;
    }

    .amount-total {
        text-align: right;
        font-size: 16px;
        font-weight: bold;
        margin-top: 15px;
    }

    .footer {
        margin-top: 40px;
        font-size: 14px;
        text-align: center;
        color: #888;
    }

    .proof-image {
        margin-top: 20px;
        text-align: right;
    }

    .proof-image img {
        border: 1px solid #ccc;
        height: 150px;
    }
    </style>
</head>

<body onload="window.print()">
    <div class="invoice-box">
        <div class="top-section">
            <div style="display:flex;align-items: center;width: 80%;">
                <img src="../assets/image/logo-pims.png" class="logo" alt="Logo">
                <h4
                    style="font-size: 28px;line-height: 25px;position: relative;left: 10px;text-transform: uppercase;color: #293061;">
                    Penguin Institute of Management Studies</h4>
            </div>
            <div class="invoice-title">
                <span style="background: #293061;color: #fff;padding: 2px 4px;border-radius: 4px;"><?= $type ?></span>
                Invoice<br>
                Invoice ID: <?= $row['sb_invoice'] ?>
            </div>
        </div>

        <div class="meta">
            <div>
                <strong>To:</strong> <?= $student ?><br>
                <?= $college ?><br>
                <strong>Course:</strong> <?= $course ?><br>
                <strong>Department:</strong> <?= $department ?>
            </div>
            <div style="text-align: right;">
                <strong>Date:</strong> <?= $date ?>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <th>Payment Remark</th>
                    <th>Price (₹)</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <?php 
                        $highlight = '';
                        $additionalInfo = '';

                        // Check and add extra info or highlight if not Cash
                        if ($row["sb_mode"] == "Cash") {
                            $additionalInfo .= htmlspecialchars($row['sb_remark']) . "<br>";
                        }
                        if ($row["sb_mode"] != "Cash") {
                            $highlight = 'style="background-color: #ffffcc; padding: 2px;"'; // light yellow highlight
                        }

                        // Add UPI transaction ID if not UPI
                        if ($row["sb_mode"] == "UPI" && !empty($row["sb_remark"])) {
                            $additionalInfo .= "<strong>UPI Txn ID:</strong> " . htmlspecialchars($row["sb_remark"]) . "<br>";
                        }

                        // Add Cheque/DD number
                        if (in_array($row["sb_mode"], ['Cheque/DD']) && !empty($row["sb_remark"])) {
                            $additionalInfo .= "<strong>Cheque/DD No.:</strong> " . htmlspecialchars($row["sb_remark"]) . "<br>";
                        }
                        ?>

                        <div <?= $highlight ?>>
                            <?= $additionalInfo ?>
                        </div>

                    </td>
                    <td><?= number_format($row['sb_amount'], 2) ?></td>
                </tr>
            </tbody>
        </table>

        <div class="amount-total">
            Total: ₹<?= number_format($row['sb_amount'], 2) ?>
        </div>
        <?php if(!empty($row['sb_description'])){?>
        <strong>Description:</strong> <?= nl2br($row['sb_description']) ?><br>
        <?php } if(!empty($row['sb_purpus'])){?>
        <em><strong>Purpose:</strong> <?= nl2br($row['sb_purpus']) ?></em>
        <?php }?>


        <div class="proof-image">
            <br>
            <span>_______________________</span> <br>
            <strong>Signature</strong>
        </div>

        <div class="footer">
            Thank you<br>
            © Penguin Institute of Management Studies
        </div>
    </div>
</body>

</html>
<?php
    } else {
        echo "Invalid Bill ID.";
    }
}
?>