<?php
session_start();
include "config.php";

$message = "";

/* ======================
   SEND OTP
======================*/
if(isset($_POST['send_otp'])){

    $mobile = $_POST['mobile'];

    if(strlen($mobile) != 10){
        $message = "Invalid Mobile Number";
    }else{

        // Generate OTP
        $otp = rand(100000,999999);

        $_SESSION['otp'] = $otp;
        $_SESSION['otp_mobile'] = $mobile;
        $_SESSION['otp_time'] = time(); // store time

        // Demo purpose: showing OTP on screen
        $message = "Your OTP is: ".$otp;

        // In future integrate SMS / WhatsApp API here
    }
}

/* ======================
   VERIFY OTP
======================*/
if(isset($_POST['verify_otp'])){

    $mobile = $_POST['mobile'];
    $user_otp = $_POST['otp'];

    if(!isset($_SESSION['otp'])){
        $message = "Please request OTP first";
    }
    else if($mobile != $_SESSION['otp_mobile']){
        $message = "Mobile number mismatch";
    }
    else if(time() - $_SESSION['otp_time'] > 300){
        unset($_SESSION['otp']);
        $message = "OTP expired";
    }
 else   if($_POST['otp'] == $_SESSION['otp']){

    $_SESSION['patient_mobile'] = $_POST['mobile'];
    unset($_SESSION['otp']);

    header("Location: patient_dashboard.php");
    exit;
}
else{
    $error = "Wrong OTP";
}
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Patient Login</title>
<link rel="stylesheet" href="assets/style.css">

<style>
body{
    background:#f4f6fb;
    font-family:Arial;
}
.login-box{
    width:320px;
    margin:80px auto;
    background:#fff;
    padding:25px;
    border-radius:10px;
    box-shadow:0 0 10px rgba(0,0,0,0.1);
}
h2{
    text-align:center;
}
input{
    width:100%;
    padding:10px;
    margin-top:10px;
    border:1px solid #ccc;
    border-radius:6px;
}
button{
    width:100%;
    margin-top:12px;
    padding:10px;
    background:#4f7cff;
    color:#fff;
    border:none;
    border-radius:6px;
    font-size:16px;
}
.msg{
    text-align:center;
    color:green;
    font-weight:bold;
    margin-bottom:10px;
}
.error{
    text-align:center;
    color:red;
    font-weight:bold;
    margin-bottom:10px;
}
</style>
</head>

<body>

<div class="login-box">

<h2>Patient Login</h2>


<br>
<form action="index.php" method="get">
    <button type="submit" style="background:#28a745;">Go To Home</button>
</form>

<?php
if($message!=""){
   if(strpos($message,'Successful')!==false || strpos($message,'OTP')!==false){
      echo "<div class='msg'>$message</div>";
   }else{
      echo "<div class='error'>$message</div>";
   }
}
?>

<!-- SEND OTP FORM -->
<form method="post">
<input type="text" name="mobile" placeholder="Enter Mobile Number" required>
<button type="submit" name="send_otp">Send OTP</button>
</form>

<hr>

<!-- VERIFY OTP FORM -->
<form method="post">
<input type="text" name="mobile" placeholder="Enter Mobile Number" required>
<input type="text" name="otp" placeholder="Enter OTP" required>
<button type="submit" name="verify_otp">Verify OTP</button>
</form>


<?php if(isset($_SESSION['patient_mobile'])){ ?>
<br>
<form action="patient_dashboard.php" method="get">
    <button type="submit" style="background:#28a745;">Go To Dashboard</button>
</form>
<?php } ?>

</div>

</body>
</html>