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

$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
$employee = $_POST["cmbemp"];
$cmbdept = $_POST["cmbdept"];

$month = $_POST["month"];
mysqli_select_db($conn, $zdb);
$zyear = right($zdb, 4);
$myyear = $_SESSION[$myyear];

$yearno = $_POST["year"];

if (!empty($employee)) {
    $emplcond = "AND accode = '$employee'";
}

$mainqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt, DATE_FORMAT(`dateproc`, '%d-%m-%Y')as procdt
                                       FROM `payproc` WHERE  monthno='$month' and deptcode = '$cmbdept' and yearno='$yearno' $emplcond  ORDER BY empname") or die(mysqli_error($conn));

$getno = mysqli_num_rows($mainqry);

if ($getno == 0) {
    browse("No Record Found ");
    echo "<script>window.close()</script>";
}

if ($month == 1) {
    $curr_month = "January";
}
if ($month == 2) {
    $curr_month = "February";
}
if ($month == 3) {
    $curr_month = "March";
}
if ($month == 4) {
    $curr_month = "April";
}
if ($month == 5) {
    $curr_month = "May";
}
if ($month == 6) {
    $curr_month = "June";
}
if ($month == 7) {
    $curr_month = "July";
}
if ($month == 8) {
    $curr_month = "August";
}
if ($month == 9) {
    $curr_month = "September";
}
if ($month == 10) {
    $curr_month = "October";
}
if ($month == 11) {
    $curr_month = "November";
}
if ($month == 12) {
    $curr_month = "December";
}

$getcomp2 = mysqli_query($conn, "SELECT * FROM $zroot.`company`") or die(mysqli_error($conn));
$comp2 = mysqli_fetch_array($getcomp2);
include('include/linkshead.php');
?>
<html>

<head>
    <title>Salary |Slip</title>
    <link href="MyStyle/inv_format.css" rel="stylesheet">

    <style>
        body {
            font-size: 13px;
        }

        .bor_rg {
            border-right: 1px solid black;
        }

        .numeric {
            text-align: right
        }

        .fontnew {
            font-family: 'Verdana';
            font-size: 13px;
            font: 'bold';
        }

        .border {
            border-top-style: solid;
            border-right-style: solid;
            border-bottom-style: solid;
            border-left-style: solid;
            border-width: 1px;
            border-color: black;
        }

        .uppercase {
            text-transform: uppercase;
        }

        table tr td {
            font-size: 15px;
        }

        .table-condensed>tbody>tr>td {
            padding: 3px;
            font-size: 12px;
        }

        .padd {
            padding: 8px;
        }

        .bold {
            font-weight: bold;
        }
        body {
            margin: 0;
            font-family: 'Times New Roman', serif;
            background: #fff;
        }

        .a4-container {
            width: 210mm;
            min-height: 297mm;
            padding: 20mm;
            margin: auto;
            box-sizing: border-box;
            box-shadow: 0 0 8px #ccc;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 14px;
        }

        .border td,
        .border th {
            border: 1px solid #000;
        }

        .padd {
            padding: 6px;
        }

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

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

        .bold {
            font-weight: bold;
        }

        .uppercase {
            text-transform: uppercase;
        }

        .bor_rg {
            border-right: 1px solid #000;
        }

        h5 {
            font-style: italic;
            margin-top: 20px;
            text-align: center;
        }
@media print {
    html, body {
        width: 210mm;
        height: 1400mm;
        margin: 0;
        padding: 0;
        font-size: 16px; /* Increased for better print readability */
        line-height: 1.4;
    }

    .a4-container {
        width: 100%;
        max-width: none;
        padding: 0;
        margin: 0;
        box-shadow: none;
    }

    table {
        width: 96%;
        height: 150px;
        border-collapse: collapse;
        font-size: 50px; /* Uniform font size in table */
    }
}
    </style>
</head>

<body>
    <?php
    $ctr = 0;
    while ($fmain = mysqli_fetch_array($mainqry)) {

        $deptcode = $fmain['deptcode'];
        $getdept = mysqli_query($conn, "SELECT name FROM `dept` WHERE code='$deptcode'") or die(mysqli_error($conn));
        $dept = mysqli_fetch_array($getdept);

        $ctr++;
        if ($ctr == 1) {
    ?>
        <? } ?>
        <br>
        <br>
        <br>
        <br>
        <br>

        <div class="a4-container">
            <!-- Company & Header -->
            <table class="border">
                <tr>
                    <td colspan="4" class="uppercase text-center padd">
                        <div style="font-size: 22px;"><b><?= $comp2['name'] ?></b></div>
                        <div style="font-size: 14px;">
                            <?= $comp2['add1'] . " " . $comp2['add2'] . " " . $comp2['add3']; ?>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="4" class="text-center bold padd" style="font-size: 14px;">
                        Salary For The Month Of - <?= $curr_month . " " . $year; ?>
                    </td>
                </tr>

                <!-- Employee Details -->
                <tr>
                    <td class="bor_rg padd" style="width: 20%;">
                        EmpCode:<br>
                        Name:<br>
                        Designation:<br>
                        No of Days:
                    </td>
                    <td class="padd" style="width: 30%;">
                        <?= $fmain['empcode']; ?><br>
                        <?= $fmain['empname']; ?><br>
                        &nbsp;<br>
                        <?= $fmain['daydetail']; ?>
                    </td>
                    <td class="bor_rg padd text-right" style="width: 20%;">
                        Department:<br>
                        Account No.:<br>
                        P.F. No.:<br>
                        &nbsp;
                    </td>
                    <td class="padd" style="width: 30%;">
                        <?= $dept['name']; ?><br>
                        &nbsp;<br>
                        &nbsp;<br>
                        &nbsp;
                    </td>
                </tr>
            </table>

            <!-- Earnings & Deductions -->
            <table class="border" style="margin-top: 15px;">
                <tr class="bold text-center">
                    <td colspan="2">Earnings</td>
                    <td colspan="2">Deductions</td>
                </tr>
                <tr>
                    <!-- Earnings -->
                    <td class="bor_rg padd" style="width: 20%;">Basic</td>
                    <td class="bor_rg padd text-right" style="width: 20%;"><?= $fmain['basic']; ?></td>
                    <!-- Deductions -->
                    <td class="bor_rg padd" style="width: 30%;">
                        Loan<br>
                        Advance/Other Dedn.
                    </td>
                    <td class="padd text-right" style="width: 30%;">
                        <?= $fmain['loan']; ?><br>
                        <?= $fmain['advance']; ?>
                    </td>
                </tr>

                <!-- Totals -->
                <tr class="bold">
                    <td class="text-right padd bor_rg">Total</td>
                    <td class="text-right padd bor_rg"><?= round($fmain['salary']); ?></td>
                    <td class="text-right padd bor_rg">Total</td>
                    <td class="text-right padd"><?= round($fmain['total_ded']); ?></td>
                </tr>
                <!-- <tr class="bold">
                    <td colspan="2" class="text-start padd bor_rg">Net Salary :- <?= round($fmain['net_sal']); ?></td>
                </tr> -->
                <tr class="bold">
                    <td colspan="4" class="padd text-left">Net Salary :- ₹<?= round($fmain['net_sal']); ?></td>
                </tr>
            </table>

            <br>
            <br>
            <?php
            $mwords = round($qry['billamt']);
            $words = rsword($mwords);
            if ($ctr == 2) {
                echo "</div>";
                echo "</div>";
                $ctr = 0;
            }
            ?>
        </div>
    <? } ?>
</body>

</html>