<?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>";
}

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']));
$sql = "SELECT 
    t1.id, 
    t1.added_by, 
    t1.approved_by, 
    t1.issue_date, 
    t1.duration_start, 
    t1.duration_end,
    t1.domain,
    t1.stipend,

    t2.name, 
    t2.joining_date, 
    t3.designation_name AS position, 
    t4.name AS mentor
FROM internship_letter t1 
INNER JOIN users t2 ON t1.employee_id=t2.employee_id 
INNER JOIN designations t3 ON t2.designation=t3.id 
INNER JOIN users t4 ON t4.employee_id=t2.leader
WHERE t1.id='$id'";
$data = mysqli_fetch_assoc(mysqli_query($db, $sql));
// print_r($data);
$first_name = explode(' ', $data['name'])[0];
$last_name = explode(' ', $data['name'])[1];

####################################################################
$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>";
}

?>

<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>Internship Offer Letter</title>
    <style>
        * {
            font-size: 13px;
        }

        /* @page { size: auto;  margin: 0mm; } */
        @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;
        }

        .stipend p {
            margin-bottom: 0;
        }
    </style>

</head>

<body>

    <table class="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=" mt-0"><i class="fa-sharp fa-solid fa-globe"></i>&nbsp;www.jobaajgroup.com
                            </div>
                            <div class=""><i class="fa-solid fa-envelope"></i>&nbsp;contact@jobaaj.com</div>
                        </div>
                        <div class="d-flex flex-row">
                            <span><img src="<?php echo $url; ?>letters/jobaaj_transparent.png" style="width:10rem" alt=""></span>
                            <div style="margin-left: 1rem;">
                                <h3 class="fw-bold text-right mb-0 mt-3">NISHTYA INFOTECH PVT. LTD </h3>
                                <p class="text-right mb-0 mt-1"><i class="bi bi-geo-alt-fill"></i>&nbsp; Reg: 379, Vardhman Grand Plaza, Plot No.7, Manglam Place, Sector-3, Rohini New Delhi- 110085 <br></p>
                                <p class="text-right mb-0 mt-1"><i class="bi bi-geo-alt-fill"></i>&nbsp; Corp Office: Office no. 810 & 811, 8th Floor, Corporate Park, Sanjay Palace, Civil Lines, Agra, Uttar Pradesh 282002<br></p>
                                <p class="text-right mb-0">CIN No: U72900DL2020PTC369688<br>

                                </p>
                            </div>
                        </div>
                    </div>
                    </div>

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

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

                            </tr>
                        </table>

                        <h2 class="mt-2 text-center mb-3">Internship Offer Letter</h2>

                        <p><strong> Dear <?php echo $first_name; ?></strong></p>

                        <p>With reference to your application, regarding the above subject, we are glad to offer you an
                            internship in the position of <strong>"<?php echo $data['position']; ?>"</strong> position</p>
                        <p>In this Internship, you will be working on the following terms and conditions:
                        </p>
                        <ol>
                            <li> Your period of training will be from <?php echo date("d-F-Y", strtotime($data['duration_start'])); ?> to <?php echo date("d-F-Y", strtotime($data['duration_end'])); ?>. During your
                                internship, you will be assigned
                                various projects in the <strong>“<?php echo $data['domain']; ?>”</strong> domain</li>
                            <li>Your Mentor for the period and the project will be <?php echo $data['mentor']; ?>.</li>
                            <li class="stipend">
                                <?php echo $data['stipend']; ?>
                            </li>
                            </li>
                            <li>During your internship, you will be given Training and Certification.</li>
                            <li>After the completion of this internship, based on your Performance you will be offered
                                full-time employment at our
                                company</li>
                            <li>After accepting this offer letter, you must complete the total internship period,
                                otherwise, your certificate of
                                internship/letter of recommendation will be revoked.</li>
                            <strong>
                                <li>In case you opt to leave the organization after joining but before completing
                                    the internship period, then you must
                                    inform your Manager/ Leader (1 week) prior. If you fail to do so any amount payable
                                    to you on account for
                                    stipend shall be forfeited by the Company</li>
                            </strong>
                            <li>During internship period you will be allowed 1.5 monthly leave on pro rata basis.</li>
                        </ol>

                        <p><strong>At-Will Employment:</strong>Your employment with the Company is "at will," Thus, you
                            or the Company may terminate our
                            employment relationship at any time, with or without cause or advance notice. In its sole
                            discretion, the Company
                            reserves the right to change your compensation and/or employee benefits at any time on a
                            prospective basis.</p>
                        <br><br>

                        <p><strong>Additional Agreements:</strong>As a condition of your employment, you agree to
                            execute any additional agreements required by
                            the Company at the start of your employment. This includes any agreements that relate to
                            your national ID verification,
                            confidentiality, or intellectual property assignment obligations to the Company. You further
                            agree that at all times of your
                            employment (and afterward as applicable), you will be bound by, and fully comply with these
                            additional agreements.
                        </p>

                        <p>Please review and return via email to confirm your acceptance.</p>
                        <p>We are excited by the prospect of you joining our team & best wishes for a long, happy, and rewarding career with us</p>

                        <p> Yours sincerely,</p>
                        <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>