<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
$zcomp = $_SESSION["comp_" . $dbname];

mysqli_select_db($conn, $zdb);

$vouch = $_GET['vouchno'];

$maindb = 'rawmain';
$trandb = 'rawtran';

$getqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as vouchdt FROM `$maindb` WHERE vouchno='$vouch'");
$getdata = mysqli_fetch_array($getqry);

$cmpname = mysqli_query($conn, "SELECT * FROM $zroot.`company` WHERE code='$zcomp'");
$getcmpname = mysqli_fetch_array($cmpname);

$getparty = mysqli_query($conn, "SELECT name FROM accmast WHERE accode='$getdata[party]'");
$showparty = mysqli_fetch_array($getparty);
?>
<!DOCTYPE html>
<html>

    <head>
        <title>Thermal Print</title>

        <style>
            /* body {
                width: 80mm;
                font-family: monospace;
                font-size: 11px;
                margin: 0;
                padding: 5px;
            }
    
            .center {
                text-align: center;
            }
    
            .line {
                border-top: 1px dashed black;
                margin: 5px 0;
            }
    
            .row {
                display: flex;
                justify-content: space-between;
            }
    
            .bold {
                font-weight: bold;
            } */

            body {
                font-family: monospace;
                font-size: 11px;
                margin: 0;
                padding: 0;
            }

            /* Screen view (for preview) */
            .print-area {
                width: 80mm;
                margin: auto;
                padding: 5px;
            }

            @media print {

                html, body {
                    width: 100%;
                    margin: 0;
                    padding: 0;
                }

                .print-area {
                    width: 72mm;          /* 🔥 REDUCED from 80mm */
                    margin: 0 auto;
                    padding: 0 2mm;       /* 🔥 LEFT + RIGHT SAFE SPACE */
                    box-sizing: border-box;
                    padding-bottom: 20mm;
                }

                @page {
                    size: 80mm auto;
                    margin: 0;
                }

                .print-space {
                    height: 25mm;   /* 🔥 adjust: 20–30mm */
                    display: block;
                }
            }

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

            .line {
                border-top: 1px dashed black;
                margin: 5px 0;
            }

            .row {
                display: flex;
                justify-content: space-between;
                align-items: flex-start;
                margin-bottom: 6px;   /* 👈 spacing between items */
            }

            .bold {
                font-weight: bold;
            }

            .row {
                display: flex;
                justify-content: space-between;
                align-items: flex-start;
            }

            .item-name {
                width: 40%;          /* adjust width */
                word-wrap: break-word;
                white-space: normal; /* allow wrapping */
            }

            .qty, .rate, .amt {
                width: 20%;
                text-align: right;
            }
            .lblitem {
                width: 40%;          /* adjust width */
            }
            .lblrest {
                width: 20%;          /* adjust width */
            }
            .text-right {
                text-align: right;
            }

        </style>
    </head>

    <body onload="window.print()">

        <div class="print-area">
            <div class="center bold">
                <?php echo $getcmpname['name']; ?><br>
            </div>

            <div class="center">
                Mumbai<br>
            </div>

            <div class="line"></div>

            <div class="row">
                <span>Voucher: <?php echo $getdata['vouchno']; ?></span>
                <span class="text-right">Date: <?php echo $getdata['vouchdt']; ?></span>
            </div>

            <div>
                Customer: <?php echo $showparty['name']; ?><br>
            </div>

            <div class="line"></div>

            <div class="row bold">
                <span class="lblitem">Item</span>
                <span class="text-right lblrest">Qty</span>
                <span class="text-right lblrest">Rate</span>
                <span class="text-right lblrest">Amt</span>
            </div>

            <div class="line"></div>
            <?php
            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno='$vouch'");

            while ($row = mysqli_fetch_array($gettran)) {

                $item = mysqli_fetch_array(mysqli_query(
                                $conn,
                                "SELECT name FROM rawmat WHERE code='$row[item]'"
                        ));

                echo "<div class='row'>
        <div class='item-name'>" . $item['name'] . "</div>
        <div class='qty'>$row[qty]</div>
        <div class='rate'>$row[rate]</div>
        <div class='amt'>$row[amount]</div>
      </div>";
            }
            ?>
            <div class="line"></div>

            <div class="row bold">
                <span>Total</span>
                <span></span>
                <span></span>
                <span><?php echo $getdata['netamt']; ?></span>
            </div>

            <?php if (!empty($getdata['remark1'])) { ?>
                <div class="line"></div>
                <div>
                    Remark: <?php echo $getdata['remark1'] . $getdata['remark2']; ?>
                </div>
            <?php } ?>

            <div class="line"></div>

            <div class="center">
                Thank You! Visit Again!
            </div>
            <div class="print-space"></div>
        </div>

    </body>

</html>