<?php
header("Content-Type: application/json");

require_once "../config.php";

$email = $_POST['email'] ?? '';
$mobile = $_POST['mobile'] ?? '';
$role = $_POST['role'] ?? '';
$newpassword = isset($_POST['newpassword']) ? md5($_POST['newpassword']) : '';

/// Future Use Admin to Update Password
// if (!empty($role) && ($role) == 'admin') {
//     $update = "UPDATE admin SET Password=:newpassword WHERE AdminEmail=:email";
//     $chngpwd1 = $dbh->prepare($update);
//     $chngpwd1->bindParam(':email', $email, PDO::PARAM_STR);
//     $chngpwd1->bindParam(':newpassword', $newpassword, PDO::PARAM_STR);
//     $chngpwd1->execute();
//     echo json_encode([
//         'status' => 'success',
//         "message" => "Password updated successfully"
//     ]);
// } 
if (!empty($role) && ($role) == 'student') {
    $update = "UPDATE tblstudents SET Password=:newpassword WHERE EmailId=:email AND MobileNumber=:mobile";
    $chngpwd1 = $dbh->prepare($update);
    $chngpwd1->bindParam(':email', $email, PDO::PARAM_STR);
    $chngpwd1->bindParam(':mobile', $mobile, PDO::PARAM_STR);
    $chngpwd1->bindParam(':newpassword', $newpassword, PDO::PARAM_STR);
    $chngpwd1->execute();
    echo json_encode([
        'status' => 'success',
        "message" => "Password updated successfully"
    ]);
} else if (!empty($role) && ($role) == 'staff') {
    $update = "UPDATE tblstaff SET Password=:newpassword WHERE EmailId=:email AND MobileNumber=:mobile";
    $chngpwd1 = $dbh->prepare($update);
    $chngpwd1->bindParam(':email', $email, PDO::PARAM_STR);
    $chngpwd1->bindParam(':mobile', $mobile, PDO::PARAM_STR);
    $chngpwd1->bindParam(':newpassword', $newpassword, PDO::PARAM_STR);
    $chngpwd1->execute();
    echo json_encode([
        'status' => 'success',
        "message" => "Password updated successfully"
    ]);
} else {
    echo json_encode(["status" => "error", "message" => "Update Failed"]);
}

// if (!empty($email) && !empty($mobile) && !empty($newpassword)) {
//     $sql = "SELECT EmailId FROM tblstudents WHERE EmailId=:email and MobileNumber=:mobile";
//     $query = $dbh->prepare($sql);
//     $query->bindParam(':email', $email, PDO::PARAM_STR);
//     $query->bindParam(':mobile', $mobile, PDO::PARAM_STR);
//     $query->execute();

//     $row = $query->fetch(PDO::FETCH_ASSOC);

//     if ($row) {
//         echo json_encode([
//             'status' => 'success',
//             "message" => "Password updated successfully"
//         ]);
//         $update = "UPDATE tblstudents SET Password=:newpassword WHERE EmailId=:email AND MobileNumber=:mobile";
//         $chngpwd1 = $dbh->prepare($update);
//         $chngpwd1->bindParam(':email', $email, PDO::PARAM_STR);
//         $chngpwd1->bindParam(':mobile', $mobile, PDO::PARAM_STR);
//         $chngpwd1->bindParam(':newpassword', $newpassword, PDO::PARAM_STR);
//         $chngpwd1->execute();
//     } else {
//         $sql = "SELECT EmailId FROM tblstaff WHERE EmailId=:email and MobileNumber=:mobile";
//         $query = $dbh->prepare($sql);
//         $query->bindParam(':email', $email, PDO::PARAM_STR);
//         $query->bindParam(':mobile', $mobile, PDO::PARAM_STR);
//         $query->execute();

//         $row = $query->fetch(PDO::FETCH_ASSOC);

//         if ($row) {
//             echo json_encode([
//                 'status' => 'success',
//                 "message" => "Password updated successfully"
//             ]);
//             $update = "UPDATE tblstaff SET Password=:newpassword WHERE EmailId=:email AND MobileNumber=:mobile";
//             $chngpwd1 = $dbh->prepare($update);
//             $chngpwd1->bindParam(':email', $email, PDO::PARAM_STR);
//             $chngpwd1->bindParam(':mobile', $mobile, PDO::PARAM_STR);
//             $chngpwd1->bindParam(':newpassword', $newpassword, PDO::PARAM_STR);
//             $chngpwd1->execute();
//         } else {
//             echo json_encode([
//                 'status' => 'error',
//                 'message' => 'Invalid User',
//                 'email' => $email,
//                 'mobile' => $mobile
//             ]);
//             echo json_encode(["status" => "error", "message" => "Invalid credentials - $email -- $password"]);
//         }
//     }
// } else {
//     echo json_encode(["status" => "error", "message" => "Invalid input ss"]);
// }
