<?php
session_start();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';


header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
header('Content-Type: application/json; charset=utf-8');

// Validate required parameters
if (empty($_POST['user']) || empty($_POST['password'])) {
    echo json_encode([
        'status' => false,
        'success' => 'Missing required parameters',
    ]);
    exit;
}


include 'config.php';

function check_input($par) {
    $par = trim($par);
    $par = stripslashes($par);
    $par = htmlspecialchars($par);
    return $par;
}

$user = check_input($_POST['user']);
$password = check_input($_POST['password']);
$user = mysqli_escape_string($con, $user);
$password = mysqli_escape_string($con, $password);
$pass = sha1($password);
$code = rand(1000, 9999);

$sql = "SELECT * FROM users WHERE (email = '$user' AND password = '$pass') OR (phone = '$user' AND password = '$pass')";
$result = $con->query($sql) or die("error: Server error " . mysqli_error($con));
$count = mysqli_num_rows($result);

if ($count == 1) {
    $rows = mysqli_fetch_array($result);
    $firstname = $rows['firstname'];
    $lastname = $rows['lastname'];
    $expiry = $rows['code_expiry'];
    $auth = $rows['auth'];
    $email = $rows['email'];
    $user_id = $rows['user_id'];
    $account_type = $rows['account_type'];
    $stat = $rows['status'];

    if ($stat == "Deleted") {
        $suc = "Account Deleted";
        $status = false;
    } elseif ($stat == "Suspended") {
        $suc = "Account Suspended";
        $status = false;
    } elseif ($stat == "Verified" || $stat == "Completed") {
        $suc = "Successful";
        $status = true;
    } else {
        $subject = "Your Verification Code!";
        $message = '<!DOCTYPE HTML>
        <html>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
        </head>
        <body>
        <div style="background-color:#eee; border:solid; border-width:thin; border-color:#EEE; font-family:Verdana, Geneva, sans-serif">
        <div style="background-color:white; width: 80%; margin:auto; font-size:13px; text-align: center; padding:1.5em;">
       <p>Dear '.$firstname.'</p>
<p>You just created an account on Eduabroad.</p>
<p>Your verification code is '.$code.'</p>
 </div>
        </div>
        </body>
        </html>';

        $mail = new PHPMailer();
        require 'mail_head.php';
        $mail->setFrom('dev@diimdevs.com', 'Eduabroad');
        $mail->addAddress($email);
        $mail->addBcc("diimtech@gmail.com");
        $mail->Subject = $subject;
        $mail->msgHTML($message);
        $mail->Send();

        $update = $con->query("UPDATE users SET ver_code = '$code' WHERE email = '$email'") or die("error: Server error " . mysqli_error($con));
        $suc = "Account Not Verified";
        $status = false;
    }
} else {
    $suc = "Wrong email or password";
    $status = false;
}

$con->close();

$output = [
    'status' => $status,
    'success' => $suc,
    'user_id' => $user_id ?? null,
    'email' => $email ?? null,
    'auth' => $auth ?? null,
];
echo json_encode($output);
?>
