<?php
require_once '../config.php';
require_once '../fun/money_format.php';

$loi_reference_id = base64_decode($_GET['loi_id']) ?? null;

if (!$loi_reference_id) {
    echo "<script>window.close();history.back();</script>";
    exit;
}
$data = mysqli_query($db, "SELECT * FROM `letters_of_intent` WHERE `loi_reference_id`='$loi_reference_id'");

if (mysqli_num_rows($data) == 0) {
    echo "<script>window.close();history.back();</script>";
    exit;
}

$loi_content = mysqli_fetch_assoc($data);
$mail_log = json_decode($loi_content['mail_log'], true);
// print_r($loi_content);

if((isset($_GET['mail_status']) && $_GET['mail_status'] === 'sent') && (isset($mail_log['status']) && $mail_log['status'] === 'Sent')) {
    $content = preg_replace('/("|\')https:\/\/www\.jobaaj\.com\/loi_acceptance\/\S*("|\')/', 'javascript:;', $loi_content['content']);
}else{
    $total_payout = json_decode($loi_content['total_payout'], true);

    $name = $loi_content['applicant_name'] ?? 'Candidate';
    $email = $loi_content['applicant_email'] ?? 'candidate@gmail.com';
    $company_name = "Jobaaj Group";
    $parent_company_name = 'Nishtya India Private Limited';
    $doj = $loi_content['date_of_joining'] ?? '';
    $job_name = ucwords($loi_content['offer_position'] ?? '');
    $trial_period = $loi_content['trial_period_days'] ?? 0;
    $internship_period = $loi_content['internship_period'] ?? 0;

    $trial_pay_per_month = $total_payout['trial_period_stipend'] ?? 0;
    $internship_payout = $total_payout['internship_stipend'] ?? [];
    $ctc = $total_payout['offer_ctc'] ?? 0;

    // convert money format when not empty
    $formatted_trial_pay = !empty($trial_pay_per_month) ? IND_money_format($trial_pay_per_month) : '';
    $formatted_internship_pay = !empty($internship_payout) ? array_map('IND_money_format', $internship_payout) : [];
    $formatted_ctc = !empty($ctc) ? IND_money_format($ctc) : '';

    $subject = $loi_content['subject'] ?? "Letter of Intent for $job_name at $company_name";
    $loi_template = $loi_content['employment_type'] ?? 'default';

    // ################################################################### Create LOI template
    if ($loi_template === 'internship') {
        $ctc = ['internship_amount' => $formatted_internship_pay, 'internship_period'=>$internship_period];
    }
    $templateFile = "./loi_{$loi_template}.php";
    if (!file_exists($templateFile)) {
        echo json_encode(["status" => 500 ,"message" => "Template file not found."]);
        exit;
    }
    require_once $templateFile;
    $loi_data = array(
        $loi_reference_id,
        $doj,
        $name,
        $job_name,
        $ctc,
        $trial_period,
        $trial_pay_per_month,
        $company_name,
        $parent_company_name
    );
    $content = generateLOIContent($loi_data);
    $content = preg_replace('/("|\')https:\/\/www\.jobaaj\.com\/loi_acceptance\/\S*("|\')/', 'javascript:;', $content);
    // ################################################################### Create LOI template
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $loi_content['loi_subject']; ?></title>
</head>

<body>
    <?php echo $content; ?>
</body>

</html>