<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
require("includes/config.php");
require("includes/function.php");

$formname = 'Journal Voucher';
$maindb   = 'jv';
$trandb   = 'jvtran';

$zdb    = $_SESSION["zdbyear_" . $dbname];
$compcd = $_SESSION["comp_"    . $dbname];
mysqli_select_db($conn, $zdb);

$vouchno = isset($_GET['vouchno']) ? (int)$_GET['vouchno'] : 0;

$result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `vouchno`='$vouchno'") or die(mysqli_error($conn));
$get    = mysqli_fetch_array($result);
if (!$get) $get = [];

$lines = [];
$totdebit = 0; $totcredit = 0;
$tres = mysqli_query($conn, "SELECT t.*, a.`name` AS accname
                              FROM `$trandb` t
                              LEFT JOIN `accmast` a ON a.`accode` = t.`accode`
                              WHERE t.`vouchno`='$vouchno' ORDER BY t.`srl` ASC") or die(mysqli_error($conn));
while ($row = mysqli_fetch_assoc($tres)) {
    $lines[] = $row;
    $totdebit  += (float)$row['dr'];
    $totcredit += (float)$row['cr'];
}

$compres  = mysqli_query($conn, "SELECT `name` FROM `$dbname`.`company` WHERE `code`='$compcd'");
$comprow  = $compres ? mysqli_fetch_assoc($compres) : null;
$compname = $comprow ? $comprow['name'] : '';

$vouchdt_disp = (!empty($get['vouchdt']) && $get['vouchdt'] != '0000-00-00') ? date('d-m-Y', strtotime($get['vouchdt'])) : '';

function pf($v) {
    if ($v === null || $v === '' || (float)$v == 0) return '';
    $v = (float)$v;
    return ($v == (int)$v) ? (string)(int)$v : number_format($v, 2);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $formname; ?> - <?php echo $vouchno; ?></title>
<style>
    body { font-family: Arial, sans-serif; font-size:13px; color:#1e293b; margin:20px; }
    h2 { text-align:center; margin:0 0 4px; }
    .sub { text-align:center; color:#64748b; margin-bottom:16px; }
    table { width:100%; border-collapse:collapse; margin-top:10px; }
    th, td { border:1px solid #cbd5e1; padding:6px 8px; font-size:12px; }
    th { background:#f1f5f9; text-align:center; }
    td.num { text-align:right; }
    .hdr-table td { border:none; padding:3px 6px; font-size:12.5px; }
    .hdr-label { color:#64748b; font-weight:600; width:140px; }
    tfoot td { font-weight:700; background:#f8fafc; }
    @media print { .no-print { display:none; } }
</style>
</head>
<body>
    <h2><?php echo htmlspecialchars($compname); ?></h2>
    <div class="sub"><?php echo $formname; ?></div>

    <table class="hdr-table">
        <tr>
            <td class="hdr-label">Voucher No</td><td>JV#<?php echo $vouchno; ?></td>
            <td class="hdr-label">Date</td><td><?php echo htmlspecialchars($vouchdt_disp); ?></td>
        </tr>
        <tr>
            <td class="hdr-label">Narration</td><td colspan="3"><?php echo htmlspecialchars($get['narr'] ?? ''); ?></td>
        </tr>
    </table>

    <table>
        <thead>
            <tr>
                <th style="width:6%;">Sr</th>
                <th>Account</th>
                <th style="width:18%;">Debit</th>
                <th style="width:18%;">Credit</th>
            </tr>
        </thead>
        <tbody>
            <?php $sr = 0; foreach ($lines as $ln): $sr++; ?>
            <tr>
                <td style="text-align:center;"><?php echo $sr; ?></td>
                <td><?php echo htmlspecialchars($ln['accname'] ?? ''); ?></td>
                <td class="num"><?php echo pf($ln['dr']); ?></td>
                <td class="num"><?php echo pf($ln['cr']); ?></td>
            </tr>
            <?php endforeach; ?>
            <?php if (empty($lines)): ?>
            <tr><td colspan="4" style="text-align:center;color:#94a3b8;">No line items.</td></tr>
            <?php endif; ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="2" style="text-align:right;">Total</td>
                <td class="num"><?php echo number_format($totdebit, 2); ?></td>
                <td class="num"><?php echo number_format($totcredit, 2); ?></td>
            </tr>
            <tr>
                <td colspan="2" style="text-align:right;">Amount in Words</td>
                <td colspan="2"><?php echo function_exists('rsword') ? rsword(round($totdebit)) : ''; ?></td>
            </tr>
        </tfoot>
    </table>

    <div class="no-print" style="margin-top:16px;text-align:center;">
        <button onclick="window.print()" style="padding:8px 18px;border:none;border-radius:6px;background:#2563eb;color:#fff;cursor:pointer;">Print</button>
    </div>
</body>
</html>
