<?php
session_start();
require_once "db.php";

error_reporting(E_ALL);
ini_set('display_errors', 1);

require '../PHPMailer/src/Exception.php';
require '../PHPMailer/src/PHPMailer.php';
require '../PHPMailer/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

if (!isset($_SESSION['admin'])) {
    die("Access Denied");
}

if(isset($_POST['send_email'])){

    $id = (int)$_POST['id'];

    $q = mysqli_query($conn,"SELECT * FROM validation_requests WHERE id='$id' LIMIT 1");

    if(!$q){
        exit("MYSQL ERROR: ".mysqli_error($conn));
    }

    if(mysqli_num_rows($q)==0){
        exit("Record Not Found");
    }

    $row = mysqli_fetch_assoc($q);

    if(($row['email_status'] ?? 'Pending') == 'Sent'){
        exit("Already Sent");
    }

    $nin  = $row['nin'];
    $type = $row['type'];

    $mail = new PHPMailer(true);

    try{

        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;

        $mail->Username = 'bilyaminuumar146@gmail.com';
        $mail->Password = 'buqp mqbh hqtt dxxs';

        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port = 587;

        $mail->setFrom('YOUR_GMAIL@gmail.com', 'ASO VERIFY');
        $mail->addAddress('nimccustomercare@nimc.gov.ng');

        $mail->isHTML(false);
        $mail->Subject = 'NIN VALIDATION REQUEST';

        $mail->Body = "Dear NIMC Customer Care,

Please help me to validate this NIN I can not use it to do anything please check.

NIN: {$nin}

TYPE: {$type}

TRACKING ID: {$row['tracking_id']}


Regards,
BILYAN ASO";

        $mail->send();

        mysqli_query($conn,"UPDATE validation_requests SET email_status='Sent' WHERE id='$id'");

        exit('success');

    }catch(Exception $e){

        exit('SMTP ERROR: '.$mail->ErrorInfo);

    }
}



$result = mysqli_query($conn,"
    SELECT *
    FROM validation_requests
    ORDER BY id DESC
");
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>NIMC Email Requests</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

<style>

body{
    background:#f4f6f9;
}

.card{
    border:none;
    border-radius:18px;
    box-shadow:0 0 20px rgba(0,0,0,.08);
}

.table th{
    white-space:nowrap;
}

.badge-pending{
    background:#ffc107;
    color:#000;
}

.badge-sent{
    background:#198754;
    color:#fff;
}

.header-box{
    background:linear-gradient(135deg,#0d6efd,#0a58ca);
    color:white;
    padding:20px;
    border-radius:18px 18px 0 0;
}

.send-btn{
    min-width:120px;
}

</style>

</head>

<body>

<div class="container-fluid mt-4">

<div class="card">

<div class="header-box">
<h3 class="mb-0">NIMC Validation Email Requests</h3>
<small>Send NIN complaints directly to NIMC Customer Care</small>
</div>

<div class="card-body">

<div class="table-responsive">

<table class="table table-bordered table-striped align-middle">

<thead class="table-dark">

<tr>
<th>ID</th>
<th>User Email</th>
<th>NIN</th>
<th>Type</th>
<th>Status</th>
<th>Email Status</th>
<th>Action</th>
</tr>

</thead>

<tbody>

<?php while($row=mysqli_fetch_assoc($result)){ ?>

<tr id="row<?= $row['id']; ?>">

<td><?= $row['id']; ?></td>

<td><?= htmlspecialchars($row['user']); ?></td>

<td><?= htmlspecialchars($row['nin']); ?></td>

<td><?= htmlspecialchars($row['type']); ?></td>

<td><?= htmlspecialchars($row['status']); ?></td>

<td>

<span
id="status<?= $row['id']; ?>"
class="badge <?= ($row['email_status']=="Sent") ? 'badge-sent' : 'badge-pending'; ?>"
>
<?= $row['email_status']; ?>
</span>

</td>

<td>

<?php if($row['email_status']=="Pending"){ ?>

<button
class="btn btn-success send-btn"
id="btn<?= $row['id']; ?>"
onclick="sendMail(<?= $row['id']; ?>)"
>
Send Email
</button>

<?php } else { ?>

<button class="btn btn-secondary" disabled>
Already Sent
</button>

<?php } ?>

</td>

</tr>

<?php } ?>

</tbody>

</table>

</div>

</div>

</div>

</div>

<script>

function sendMail(id){

let btn = document.getElementById("btn"+id);

btn.innerHTML = "Sending...";
btn.disabled = true;

let formData = new FormData();

formData.append("send_email","1");
formData.append("id",id);

fetch("",{
    method:"POST",
    body:formData
})
.then(response=>response.text())
.then(data=>{

    if(data.trim()=="success"){

        document.getElementById("status"+id).innerHTML="Sent";
        document.getElementById("status"+id).classList.remove("badge-pending");
        document.getElementById("status"+id).classList.add("badge-sent");

        btn.outerHTML =
        '<button class="btn btn-secondary" disabled>Already Sent</button>';

    }else{

        alert(data);

        btn.disabled=false;
        btn.innerHTML="Send Email";
    }

});

}

</script>

</body>
</html>