<?php
// Include necessary configuration and functions
include "top.php";

// Fetch bill ID
$bill_id = mysqli_real_escape_string($con, $_GET['bill_id']);

// Data fetching logic similar to getbills.php
$getd = f(q("SELECT * FROM cbill WHERE blid='$bill_id'"));
if (!$getd) {
    die("Bill not found.");
}

$getcust = f(q("SELECT * FROM adduser WHERE id='$getd[bl_cid]'"));
$getcom = f(q("SELECT * FROM tbl_company WHERE c_id=$getcust[com_id]"));
$getloged = f(q("SELECT * FROM tbl_login WHERE l_uid='$getd[bl_user]'"));
$getcustline = f(q("SELECT * FROM tbl_login WHERE l_id='$getcust[line_ids]'"));

// Calculations
$currentc = $getd['bl_amt'];
$prevdue = $getd['bl_prevdue'];
$disc = $getd['bl_disc'];

$showGst = ($getcom['isgst'] == '1');

// GST Calculation if applicable
if ($showGst) {
    $gstvalu = (floatval($currentc) * 100) / 118;
    $gstamt = floatval($currentc) - floatval($gstvalu);
    $packamt = $gstvalu;
    $gst = $gstamt;
    $gsth = round(floatval($gst) / 2, 2);
} else {
    $packamt = $currentc;
    $gst = 0;
}

$toalcharges = (floatval($currentc) - floatval($disc)) + floatval($prevdue);
$duess = floatval($toalcharges) - floatval($getd['bl_payamt']);

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Thermal Bill - <?php echo $getd['bl_no']; ?></title>
    <style>
        /* General Styles */
        body {
            margin: 0;
            padding: 0;
            font-family: 'Courier New', Courier, monospace;
            font-size: 11px;
            color: #000;
        }

        /* Container for 58mm paper size */
        .receipt {
            width: 50mm;
            /* Narrower than 58mm for margins */
            margin: 0 auto;
            padding: 2mm;
        }

        /* Typography */
        .text-center {
            text-align: center;
        }

        .text-right {
            text-align: right;
        }

        .bold {
            font-weight: bold;
        }

        .uppercase {
            text-transform: uppercase;
        }

        .large {
            font-size: 14px;
        }

        .xlarge {
            font-size: 16px;
        }

        /* Tables */
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 2mm 0;
        }

        th,
        td {
            text-align: left;
            padding: 0.5mm 0;
            line-height: 1.2;
            vertical-align: top;
        }

        /* Separators */
        .divider {
            border-bottom: 1px dashed #000;
            margin: 2mm 0;
        }

        /* UI elements (hidden during print) */
        .no-print {
            display: flex;
            justify-content: center;
            padding: 10px;
            background: #f0f0f0;
            position: sticky;
            top: 0;
            border-bottom: 1px solid #ccc;
        }

        .btn-print {
            padding: 8px 16px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-family: sans-serif;
            font-weight: bold;
        }

        @media print {
            .no-print {
                display: none;
            }

            body {
                margin: 0;
            }

            .receipt {
                width: 100%;
            }
        }

        @page {
            size: auto;
            margin: 0mm;
        }
    </style>
</head>

<body>

    <div class="no-print">
        <button class="btn-print" onclick="window.print()">Print Receipt</button>
    </div>

    <div class="receipt">
        <!-- Company Header -->
        <div class="text-center">
            <div class="xlarge bold uppercase"><?php echo $getcom['c_name']; ?></div>
            <div><?php echo $getcom['c_address']; ?></div>
            <div>Ph:
                <?php echo $getcom['c_phone']; ?><?php if ($getcom['c_mobile'] != '0')
                       echo " / " . $getcom['c_mobile']; ?>
            </div>
            <?php if ($getcom['c_emailalt'] != '') { ?>
                <div>GSTN: <?php echo $getcom['c_emailalt']; ?></div>
            <?php } ?>
        </div>

        <div class="divider"></div>

        <!-- Bill Basic Info -->
        <table>
            <tr>
                <td class="bold">Bill No:</td>
                <td class="text-right"><?php echo $getd['blid']; ?></td>
            </tr>
            <!-- <tr>
            <td class="bold">Date:</td>
            <td class="text-right"><?php echo date("d-m-Y", strtotime($getd['bl_dt'])); ?></td>
        </tr> -->
            <tr>
                <td class="bold">Period:</td>
                <td class="text-right">

                    <?php echo date("d-m-Y", strtotime($getd['bl_stdt'])) . " - " . date("d-m-Y", strtotime($getd['bl_expiry'])); ?>
                </td>
            </tr>
        </table>

        <div class="divider"></div>

        <!-- Customer Info -->
        <div class="bold uppercase"><?php echo $getcust['name']; ?></div>
        <div>A/C No: <?php echo $getcust['custid']; ?></div>
        <div>Mob: <?php echo $getcust['phone']; ?></div>
        <?php if ($getcust['vc'] != '') { ?>
            <div> <?php echo $getcust['vc']; ?></div><?php } ?>
        <?php if ($getcust['stb'] != '') { ?>
            <div> <?php echo $getcust['stb']; ?></div><?php } ?>

        <div class="divider"></div>

        <!-- Billing Table -->
        <table>
            <tr>
                <td class="bold">Description</td>
                <td class="text-right bold">Amount</td>
            </tr>
            <tr>
                <td>Base Package</td>
                <td class="text-right"><?php echo round($packamt, 2); ?></td>
            </tr>
            <?php if ($disc > 0) { ?>
                <tr>
                    <td>Discount (-)</td>
                    <td class="text-right"><?php echo round($disc, 2); ?></td>
                </tr>
            <?php } ?>
            <?php if ($showGst) { ?>
                <tr>
                    <td>GST (18%)</td>
                    <td class="text-right"><?php echo round($gst, 2); ?></td>
                </tr>
            <?php } ?>
            <tr>
                <td>Previous Due</td>
                <td class="text-right"><?php echo round($prevdue, 2); ?></td>
            </tr>
            <tr class="large bold">
                <td>TOTAL PAYABLE</td>
                <td class="text-right"><?php echo round($toalcharges, 2); ?></td>
            </tr>
        </table>

        <div class="divider"></div>

        <!-- Payment Status -->
        <table>
            <tr>
                <td class="bold">Amount Paid:</td>
                <td class="text-right"><?php echo round($getd['bl_payamt'], 2); ?></td>
            </tr>
            <tr>
                <td class="bold">Pay Date:</td>
                <td class="text-right"><?php echo date("d-m-Y", strtotime($getd['bl_paydt'])); ?></td>
            </tr>
            <tr class="large bold">
                <td>BALANCE DUE:</td>
                <td class="text-right"><?php echo round($duess, 2); ?></td>
            </tr>
        </table>

        <div class="divider"></div>

        <div class="text-center" style="margin-top: 5mm;">
            <div class="bold">Thank You!</div>
            <div style="font-size: 9px;">Collector: <?php echo $getcustline['l_name']; ?></div>
        </div>
    </div>

    <script>
        // Auto-open print dialog maybe?
        // window.onload = function() { window.print(); }
    </script>

</body>

</html>