<?php
require_once("../../public/connection.php");
require_once("../../public/functions.php");

////////// POST ACTION TO VIEW ALL THE UPLOADED ADMINISTRATIVE INFORMATION ////////////
$query = " SELECT * FROM `administratives` ";
$run_query = mysqli_query($connection, $query);

$admin_name = '';
$admin_motto = '';
$admin_logo = '';
$admin_stamp = '';

if(mysqli_num_rows($run_query) > 0){
	while($result = mysqli_fetch_assoc($run_query)){
		$administratives_id = $result['id'];
		$admin_name = $result['school_name'];
		$admin_motto = $result['school_motto'];
		$admin_logo = $result['school_logo'];
		$admin_stamp = $result['school_stamp'];
		//echo "<img src='$admin_logo' />";
	}
}

if (!empty($_GET['invoice_id'])) {
    $invoice_id = inject_checker($connection, $_GET['invoice_id']); // Securely sanitize input

    // Fetch the main invoice details
    $invoiceQuery = "SELECT * FROM `invoice_order` WHERE `invoice_id` = '{$invoice_id}'";
    $invoiceResult = mysqli_query($connection, $invoiceQuery);

    if ($invoiceResult && mysqli_num_rows($invoiceResult) > 0) {
        $invoiceValues = mysqli_fetch_assoc($invoiceResult);

        // Fetch associated invoice items
        $itemsQuery = "SELECT * FROM `invoice_item_order` WHERE `invoice_id` = '{$invoice_id}'";
        $itemsResult = mysqli_query($connection, $itemsQuery);

        $invoiceItems = [];
        if ($itemsResult) {
            while ($item = mysqli_fetch_assoc($itemsResult)) {
                $invoiceItems[] = $item;
            }
        }

        // Format the invoice date
        $invoiceDate = date("d/M/Y", strtotime($invoiceValues['order_date']));

        // Build the HTML output
        echo '<table width="100%" border="1" cellpadding="5" cellspacing="0">
            <tr>
                <td colspan="2" align="center" style="font-size:18px"><b>SCHOOL INVOICE</b></td>
            </tr>
            <tr>
                <td colspan="2">
                    <table width="100%" cellpadding="5">
                        <tr>
                            <td width="65%">
                                From,<br />
                                <b>BILLER (BILL FROM)</b><br />
                                ' . $admin_name . '<br />
                                ' . $admin_motto . '<br />
								' . $invoiceValues['term'] . '<br />
                                ' . $invoiceValues['session'] . '<br />                            

                            </td>
                            <td width="35%"> 
                                To,<br />
                                <b>RECEIVER (BILL TO)</b><br />
                                Name : ' . $invoiceValues['receiver_name'] . '<br />
                                Class : ' . $invoiceValues['receiver_class'] . '<br />
                                Invoice No. : ' . $invoiceValues['invoice_id'] . '<br />
                                Invoice Date : ' . $invoiceDate . '<br />
                            </td>
                        </tr>
                    </table>
                    <br />
                    <table width="100%" border="1" cellpadding="5" cellspacing="0">
                        <tr>
                            <th align="left">SN</th>
                            <th align="left">Item Name</th>
                            <th align="left">Quantity</th>
                            <th align="left">Price</th>
                            <th align="left">Actual Amt.</th> 
                        </tr>';

        $count = 0;   
        foreach ($invoiceItems as $invoiceItem) {
            $count++;
            echo '<tr>
                <td align="left">' . $count . '</td>
                <td align="left">' . $invoiceItem["item_name"] . '</td>
                <td align="left">' . $invoiceItem["item_quantity"] . '</td>
                <td align="left">' . $invoiceItem["item_price"] . '</td>
                <td align="left">' . $invoiceItem["item_final_amount"] . '</td>   
            </tr>';
        }
		
        echo '<tr>
            <td align="right" colspan="5"><b>Sub Total</b></td>
            <td align="left"><b>' . $invoiceValues['sub_total'] . '</b></td>
        </tr>
        <tr>
            <td align="right" colspan="5">Amount Paid:</td>
            <td align="left">' . $invoiceValues['total_amount_paid'] . '</td>
        </tr>
        <tr>
            <td align="right" colspan="5"><b>Amount Due:</b></td>
            <td align="left">' . $invoiceValues['total_amount_due'] . '</td>
        </tr>
		<tr>
            <td align="left" colspan="6"><b>Note: ' . $invoiceValues['note'] . '</b></td>
        </tr>
        </table>
        </td>
        </tr>
        </table>';
    } else {
        echo "Invoice not found.";
    }
} else {
    echo "Invalid invoice ID.";
}
?>
<div style="text-align: center; margin-top: 20px;">
    <button id="printButton" style="
        background-color: #4CAF50; /* Green background */
        color: white; /* White text */
        padding: 15px 30px; /* Padding for the button */
        font-size: 16px; /* Larger font size */
        border: none; /* Remove border */
        border-radius: 5px; /* Rounded corners */
        cursor: pointer; /* Pointer cursor on hover */
        box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2); /* Subtle shadow effect */
        transition: all 0.3s ease-in-out; /* Smooth hover effect */
        margin-right: 10px; /* Space between buttons */
    " onclick="printInvoice()">🖨️ Print Invoice</button>
    
    <button id="cancelButton" style="
        background-color: #f44336; /* Red background */
        color: white; /* White text */
        padding: 15px 30px; /* Padding for the button */
        font-size: 16px; /* Larger font size */
        border: none; /* Remove border */
        border-radius: 5px; /* Rounded corners */
        cursor: pointer; /* Pointer cursor on hover */
        box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2); /* Subtle shadow effect */
        transition: all 0.3s ease-in-out; /* Smooth hover effect */
    " onclick="cancelInvoice()">❌ Cancel</button>
</div>

<style>
    @media print {
        #printButton, #cancelButton {
            display: none; /* Hide both buttons during printing */
        }
    }
</style>

<script>
    function printInvoice() {
        window.print(); // Trigger print functionality
    }

    function cancelInvoice() {
        // Redirect the user to the previous page or a specific URL
        if (document.referrer) {
            window.location.href = document.referrer; // Go back to the previous page
        } else {
            window.location.href = '/'; // Default to homepage if no referrer
        }
    }

    // Optional: Add hover effects for buttons
    const printButton = document.getElementById('printButton');
    printButton.addEventListener('mouseover', () => {
        printButton.style.backgroundColor = '#45a049'; // Darker green
        printButton.style.transform = 'scale(1.05)'; // Slightly larger on hover
    });
    printButton.addEventListener('mouseout', () => {
        printButton.style.backgroundColor = '#4CAF50'; // Reset to original color
        printButton.style.transform = 'scale(1)'; // Reset size
    });

    const cancelButton = document.getElementById('cancelButton');
    cancelButton.addEventListener('mouseover', () => {
        cancelButton.style.backgroundColor = '#e53935'; // Darker red
        cancelButton.style.transform = 'scale(1.05)'; // Slightly larger on hover
    });
    cancelButton.addEventListener('mouseout', () => {
        cancelButton.style.backgroundColor = '#f44336'; // Reset to original color
        cancelButton.style.transform = 'scale(1)'; // Reset size
    });
</script>