<?php
include "../config.php";
require_once "../fun/money_format.php";
$user_id = $_SESSION['user_id'];
$emp_id = $_SESSION['emp_id'];
$user_role = $_SESSION['user_role'];

if (!isset($_SESSION['emp_id'])) {
    echo "<script>location.href='../index';</script>";
}
// if($_SESSION['user_role']!=0){
//     echo "<script>location.href='../404';</script>";
// }

function AmountInWords(float $amount)
{
    $amount_after_decimal = round($amount - ($num = floor($amount)), 2) * 100;
    // Check if there is any number after decimal
    $amt_hundred = null;
    $count_length = strlen($num);
    $x = 0;
    $string = array();
    $change_words = array(
        0 => '', 1 => 'One', 2 => 'Two',
        3 => 'Three', 4 => 'Four', 5 => 'Five', 6 => 'Six',
        7 => 'Seven', 8 => 'Eight', 9 => 'Nine',
        10 => 'Ten', 11 => 'Eleven', 12 => 'Twelve',
        13 => 'Thirteen', 14 => 'Fourteen', 15 => 'Fifteen',
        16 => 'Sixteen', 17 => 'Seventeen', 18 => 'Eighteen',
        19 => 'Nineteen', 20 => 'Twenty', 30 => 'Thirty',
        40 => 'Forty', 50 => 'Fifty', 60 => 'Sixty',
        70 => 'Seventy', 80 => 'Eighty', 90 => 'Ninety'
    );
    $here_digits = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');

    while ($x < $count_length) {
        $get_divider = ($x == 2) ? 10 : 100;
        $amount = floor($num % $get_divider);
        $num = floor($num / $get_divider);
        $x += $get_divider == 10 ? 1 : 2;

        if ($amount) {
            $add_plural = (($counter = count($string)) && $amount > 9) ? 's' : null;
            $amt_hundred = ($counter == 1 && $string[0]) ? ' and ' : null;
            $string[] = ($amount < 21) ? $change_words[$amount] . ' ' . $here_digits[$counter] . $add_plural . ' 
                ' . $amt_hundred : $change_words[floor($amount / 10) * 10] . ' ' . $change_words[$amount % 10] . ' 
                ' . $here_digits[$counter] . $add_plural . ' ' . $amt_hundred;
        } else {
            $string[] = null;
        }
    }
    $implode_to_Rupees = implode('', array_reverse($string));
    $get_paise = ($amount_after_decimal > 0) ? "And " . ($change_words[$amount_after_decimal / 10] . " 
        " . $change_words[$amount_after_decimal % 10]) . ' Paise' : '';
    return ($implode_to_Rupees ? $implode_to_Rupees . 'Rupees ' : '') . $get_paise;
}

$id = mysqli_real_escape_string($db, base64_decode($_GET['id']));
$data = mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM appraisal_letter WHERE id='$id'"));

####################################################################
$approved_by = json_decode($data['approved_by'], true);
$is_all_approved = false;

$approvals = !empty($data['approved_by']) ? json_decode($data['approved_by'], true) : [];
if (!empty($approvals)) {
    $lastSubarray = end($approvals);
    $lastStatuses = is_array($lastSubarray) ? array_column($lastSubarray, 'status') : [];
} else {
    $lastStatuses = [];
}
$is_all_approved = count(array_filter($lastStatuses, fn($val) => $val == 1)) === count($lastStatuses);
if ($user_role!=0 && (empty($lastStatuses) || !$is_all_approved)){
    // add watermark
    echo "<style>
        .watermark {
            position: fixed;
            top: 50%;
            left: 50%;
            width: fit-content;
            transform: translate(-50%, -50%) rotate(45deg);
            font-size: 60px;
            display: none;
            color: rgba(0, 0, 0, 0.2);
            pointer-events: none;
            display: none;
            mix-blend-mode: darken;
        }
    </style>

    <div class=\"watermark\"><img src='{$url}letters/unathorized.png'></div>

    <script>
        // JavaScript to control printing
        window.onload = function() {
            document.querySelector('.watermark').style.display = 'none'; // Hide watermark on the screen

            // Show watermark on every printed page
            window.onbeforeprint = function() {
                document.querySelector('.watermark').style.display = 'block';
            };

            // Hide watermark after printing is done
            window.onafterprint = function() {
                document.querySelector('.watermark').style.display = 'none';
            };
        };
    </script>";
}
####################################################################
if(($_SESSION['emp_id']=='NI406' && !$is_all_approved)){
    echo "<script>location.href='../404';</script>";
}

if ($_SESSION['emp_id']!='NI406' && ($_SESSION['user_role'] != 0 && $data['added_by']!=$_SESSION['emp_id'] && !isset($lastSubarray[$_SESSION['emp_id']]) )) {
    echo "<script>location.href='../404';</script>";
}

$total_per_month = round($data['basic_salary']/12) + round($data['hra']/12) + round($data['sp_allowance']/12) + round($data['books_per']/12);
$total_per_annum = $data['basic_salary'] + $data['hra'] + $data['sp_allowance'] + $data['books_per'] + $data['retention_bonus'];

$for_deduction_month = $total_per_month;
$for_deduction_annum = $total_per_annum;

$addOn = json_decode($data['add-on'],true);
$deduction = json_decode($data['deduction'],true);

?>

<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/png" href="https://www.jobaaj.com/images/">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.3.0/font/bootstrap-icons.css">
    <title>Appraisal Letter</title>
    <style>
        * {
            font-size: 14px;
        }

        @media print {
            @page {
                margin-left: 0.6in;
                margin-right: 0.6in;
                margin-top: 0.1in;
                margin-bottom: 0.2in;
            }
        }

        table.report-container {
            page-break-after: always;
        }

        thead.report-header {
            display: table-header-group;
        }

        tfoot.report-footer {
            display: table-footer-group;
        }
    </style>

</head>

<body>



    <table class="report-container container">

        <thead class="report-header">
            <tr>
                <th class="report-header-cell p-1 mb-0">
                    <div class="header-info m-3 mb-0">
                        <div class="d-flex justify-content-between mb-0">
                            <div class=""> <i class="fa-solid fa-phone"></i>&nbsp;+91 73035 12500</div>
                            <div class=""><i class="fa-sharp fa-solid fa-globe"></i>&nbsp;www.nishtyainfotech.com</div>
                            <div class=""><i class="fa-solid fa-envelope"></i>&nbsp;contact@nishtyainfotech.com</div>
                        </div>
                        <div class="d-flex flex-row">
                            <span><img src="<?php echo $url; ?>/letters/nishtya_transparent.png" style="width:5rem" alt=""></span>
                            <div style="margin-left: 1rem;">
                            <h3 class="fw-bold mb-1">NISHTYA INFOTECH PVT. LTD </h3>
                                <p class="text-right"> 379, Vardhman Grand Plaza, Plot No.7, <br>
                                    Manglam Place, Sector-3, Rohini <br>
                                    New Delhi- 110085 <br>
                                </p>
                            </div>
                            <span><img src="<?php echo $url; ?>/letters/1.png" style="width:6rem;margin-left:8rem;" alt=""></span>
                        </div>
                    </div>
                </th>
            </tr>
        </thead>

        <tbody class="report-content">
            <tr>
                <td class="report-content-cell">
                    <div class="main p-3 pt-0">

                        <table class="mt-3">

                            <tr>
                                <td><strong>Date:</strong> <?php echo date("d M Y", strtotime($data['issue_date'])); ?></td>

                            </tr>
                        </table>

                        <h1 class="mt-2 text-center mb-3"><u>Letter Of Appraisal</u></h1> <br>


                        <p>Dear <?php echo $data['first_name']; ?>, <br><br>
                            We would like to extend this appraisal letter for your dedication and contribution to the organization, Your
                            performance for this year has exceeded the company's expectations, It proves your commitment towards Nishtya
                            Infotech. We are pleased to appraise your annual CTC from <strong>Rs. <?php echo IND_money_format($data['old_ctc']); ?></strong> to <strong>Rs. <?php echo IND_money_format($data['new_ctc']); ?></strong> (i.e <strong><?php echo round((($data['new_ctc']-$data['old_ctc'])/$data['old_ctc'])*100,2); ?>%</strong> Increment to CTC), please refer to the attached Annexure I for detailed CTC computation. This revision is effective from <strong><?php echo date("d M Y",strtotime($data['revision_date'])); ?></strong>. 
                            <br><br>
                            The revised compensation package has been customized for you taking into consideration your performance,
                            company policies, and related factors. <br>Your compensation package is unique to you and not for comparison with other employees of the company.<br><br>
                            Note: After this appraisal in case you decide to leave the service of the organization, you will be required to give
                            notice as per company policy. As per the revised policy, your notice period is <strong><?php echo $data['notice_period']; ?> days</strong>. All the other terms and
                            conditions of your employment remain unchanged. <br><br>We at Nishtya Infotech hope that you will continue your work with the same dedication and meet all the challenges
                            in the upcoming year. We have the privilege to have you with us and personally congratulate you on your
                            contribution and look forward to your participation in the growth of the company. <br><br>
                            Best wishes for a long, happy, and rewarding career with us. <br><br>
                        </p> <br>
                        Sincerely, <br>
                        For Nishtya Infotech Pvt. Ltd. <br>


                        <span><img src="<?php echo $url; ?>/letters/akhilesh_sign.jpeg" style="width:8rem; mix-blend-mode: hard-light;" alt=""></span><br>
                        Akhilesh Kumar Saha <br>
                        Authorized Signatory <br>
                        Snr. Finance Manager <br>
                        <strong>Nishtya Infotech Pvt. Ltd.</strong>
                    </div>
                </td>
            </tr>
        </tbody>
        
    </table>
    
    <table class="report-container container">
        <thead class="report-header">
            <tr>
                <th class="report-header-cell p-1 mb-0">
                    <div class="header-info m-3 mb-0">
                        <div class="d-flex justify-content-between mb-0">
                            <div class=""> <i class="fa-solid fa-phone"></i>&nbsp;+91 73035 12500</div>
                            <div class=""><i class="fa-sharp fa-solid fa-globe"></i>&nbsp;www.nishtyainfotech.com</div>
                            <div class=""><i class="fa-solid fa-envelope"></i>&nbsp;contact@nishtyainfotech.com</div>
                        </div>
                        <div class="d-flex flex-row">
                            <span><img src="<?php echo $url; ?>/letters/nishtya_transparent.png" style="width:5rem" alt=""></span>
                            <div style="margin-left: 1rem;">
                            <p class="h1 fw-bold mb-1">NISHTYA INFOTECH PVT. LTD </p>
                                <p class="text-right"> 379, Vardhman Grand Plaza, Plot No.7, <br>
                                    Manglam Place, Sector-3, Rohini <br>
                                    New Delhi- 110085 <br>
                                </p>
                            </div>
                            <span><img src="<?php echo $url; ?>/letters/1.png" style="width:6rem;margin-left:8rem;" alt=""></span>
                        </div>
                    </div>
                </th>
            </tr>
        </thead>
        
        <tbody class="report-content">
            <tr>
                <td class="report-content-cell">
                <div class="main p-3 pt-0">
                        <h6 style="text-align: center;">Annexure I</h6>
                        <table>
                            <tr>
                                <th>Name of Employee</th>
                                <td> : <?php echo $data['first_name'].' '.$data['last_name']; ?></td>

                            </tr>
                            <tr>
                                <th>Designation</th>
                                <td> : <?php echo $data['position']; ?></td>
                            </tr>
                            <tr>
                                <th>Date of Joining</th>
                                <td class="mb-0"> : <?php echo date("d-M-Y", strtotime($data['joining_date'])); ?></td>
                            </tr>
                            <tr>
                                <th>Location</th>
                                <td> : Agra</td>

                            </tr>
                            <tr>
                                <th>Level</th>
                                <td> : <?php echo $data['level']; ?></td>
                            </tr>
                        </table>

                        <table class="mt-2" style="width:100%; border: 1px solid black;">

                            <tr>
                                <th style="border: 1px solid black;border-collapse: collapse;text-align: center;">Particular/Component</th>
                                <th style="border: 1px solid black;border-collapse: collapse;text-align: center;">Per Month &#8377;</th>
                                <th style="border: 1px solid black;border-collapse: collapse;text-align: center;">Per Annum &#8377;</th>
                            </tr>

                            <tr>
                                <td style="border: 1px solid black;border-collapse: collapse;text-align: left;font-weight:bold;">Fixed Pay</td>
                                <td style="border: 1px solid black;border-collapse: collapse;text-align: center;"></td>
                                <td style="border: 1px solid black;border-collapse: collapse;text-align: center;"></td>
                            </tr>
                            <tr>
                                <td style="border: 1px solid black;border-collapse: collapse;text-align: left;">&nbsp;&nbsp;Basic Salary</td>
                                <td style="border: 1px solid black;border-collapse: collapse;text-align: center;"><?php echo IND_money_format(round($data['basic_salary']/12)); ?> </td>
                                <td style="border: 1px solid black;border-collapse: collapse;text-align: center;"><?php echo IND_money_format($data['basic_salary']); ?></td>
                            </tr>
                            <tr>
                                <td style="border: 1px solid black;border-collapse: collapse;text-align: left;">&nbsp;&nbsp;House Rent Allowance</td>
                                <td style="border: 1px solid black;border-collapse: collapse;text-align: center;"><?php echo IND_money_format(round($data['hra']/12)); ?></td>
                                <td style="border: 1px solid black;border-collapse: collapse;text-align: center;"><?php echo IND_money_format($data['hra']); ?></td>
                            </tr>
                            <tr>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: left;">&nbsp;&nbsp;Special Allowance</td>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"><?php echo IND_money_format(round($data['sp_allowance']/12)); ?></td>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"><?php echo IND_money_format($data['sp_allowance']); ?></td>
                            </tr>
                            <tr>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: left;">&nbsp;&nbsp;Books & Periodicals </td>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"><?php echo IND_money_format(round($data['books_per']/12)); ?></td>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"><?php echo IND_money_format($data['books_per']); ?></td>
                            </tr>
                            <tr>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: left;">&nbsp;&nbsp;Retention Bonus </td>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"><?php echo IND_money_format(round($data['retention_bonus']/12)); ?></td>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"><?php echo IND_money_format($data['retention_bonus']); ?></td>
                            </tr>
                           
                            <tr>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: left;font-weight:bold;">Total Fixed Salary (A)</td>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"><?php echo IND_money_format($total_per_month); ?></td>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"><?php echo IND_money_format($total_per_annum); ?></td>
                            </tr>
                            <?php
                            $addOn_month = 0;
                            $addOn_annum = 0;
                            foreach ($addOn as $key => $value) {
                                if(!empty($key)){
                            ?>
                                <tr>
                                <?php if (strtolower($key) == "variable pay") { ?>
                                    <td style="border: 1px solid black;border-collapse: collapse; text-align: left; font-weight:bold;"><?php echo $key; ?></td>
                                <?php } else { ?>
                                    <td style="border: 1px solid black;border-collapse: collapse; text-align: left;">&nbsp;&nbsp;<?php echo $key; ?></td>
                                <?php } ?>

                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"> 
                                    <?php echo is_numeric($value)? IND_money_format(round($value/12)): $value; ?>
                                </td>

                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;">
                                <?php echo is_numeric($value)? IND_money_format(round($value)): $value; ?>
                                </td>
                                </tr>
                            <?php 
                                $total_per_month += round($value/12);
                                $total_per_annum += round($value);
                            }
                        } ?>

                            <tr>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: left;font-weight:bold;">Total Gross CTC* (Excluding Incentives)</td>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"> <?php echo IND_money_format($total_per_month); ?></td>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"><?php  echo IND_money_format($total_per_annum); ?></td>
                            </tr>

                            <?php
                            foreach($deduction as $key => $value){
                                if(!empty($key)){
                            ?>
                            <tr>
                                <td style="border: 1px solid black;border-collapse: collapse;text-align: left;font-weight:bold;">Deduction (B)</td>
                                <td style="border: 1px solid black;border-collapse: collapse;text-align: center;"></td>
                                <td style="border: 1px solid black;border-collapse: collapse;text-align: center;"></td>
                            </tr>
                            <?php
                            break;
                                }}
                            $deduction_month = 0;
                            $deduction_annum = 0;
                            foreach($deduction as $key => $value){
                                if(!empty($key)){
                            ?>
                                <tr>
                                    <td style="border: 1px solid black;border-collapse: collapse; text-align: left;">&nbsp;&nbsp;<?php echo $key; ?></td>
                                    <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"> <?php echo IND_money_format(round($value/12)); ?></td>
                                    <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"><?php echo IND_money_format($value); ?></td>

                                    <?php 
                                        $deduction_month += round($value/12);
                                        $deduction_annum += $value;
                                    ?>
                                </tr>
                            <?php }} ?>

                            <tr>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: left;font-weight:bold">
                                <?php if($deduction_month!=0)  {?>
                                    Net Pay (A-B)
                                <?php } else { ?>
                                    Net Pay (A)
                                <?php } ?>
                                </td>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"> <?php echo IND_money_format($for_deduction_month -  $deduction_month); ?></td>
                                <td style="border: 1px solid black;border-collapse: collapse; text-align: center;"><?php echo IND_money_format($for_deduction_annum -  $deduction_annum); ?></td>
                            </tr>
                        </table>

                        <?php if (!empty($data['incentive_term'])) { ?>
                            <h6 class="mt-2 mb-0">Performance Incentive^^</h6>
                            <span class="mb-0" id="spaceDIV"><?php echo $data['incentive_term']; ?></span>
                        <?php } ?>

                        
                        <h6 class="mt-2 mb-0">Total CTC*</h6>
                        <ul>
                            <li> Total Cost to Company is a total of Gross Annual Fixed and Annual Variable Pay plus Annual Bonus at 100%
                                payout as per the clause mentioned above.</li>
                            <li> PF,ESI, Income Tax, and Local Tax will be deducted as per the prevailing rules.(If applicable) <br>
                                For Nishtya Infotech Pvt. Ltd.</li>
                        </ul>

                        <span><img src="<?php echo $url; ?>/letters/akhilesh_sign.jpeg" style="width:8rem; mix-blend-mode: hard-light;" alt=""></span><br>
                        Akhilesh Kumar Saha <br>
                        Authorized Signatory <br>
                        Snr. Finance Manager <br>
                        <strong>Nishtya Infotech Pvt. Ltd.</strong>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>

</body>
<script>
    window.print();
</script>
<script>
    document.addEventListener("contextmenu", (event) => {
        event.preventDefault();
    });
    document.addEventListener('keydown', function(e) {
        // F12
        if (e.keyCode === 123) {
            e.preventDefault();
        }

        // Block Ctrl+Shift+I / J / C
        if (e.ctrlKey && e.shiftKey && ['I', 'i', 'J', 'j', 'C', 'c'].includes(e.key)) {
            e.preventDefault();
            return false;
        }

        // Block Ctrl+U
        if (e.ctrlKey && ['U', 'u'].includes(e.key)) {
            e.preventDefault();
            return false;
        }
    });

    setInterval(() => {
        const before = new Date().getTime();
        debugger;
        const after = new Date().getTime();
        if (after - before > 100) {
            document.body.innerHTML = "<h1 style='color:red;text-align:center;margin-top:20%'>Unauthorized Access Detected. Closing Page...</h1>";
            setTimeout(() => {
                window.location.href = "about:blank"; // or just hang the page
            }, 2000);
        }
    }, 100);
</script>
</html>