<?php
    include('../db/connection.php');
    include('mail_configuration.php');

    date_default_timezone_set("Asia/Kolkata");
	$datetime = date("Y-m-d H:i:s");	

    $email = trim($_POST['email'] ?? '');
    $mobile = trim($_POST['mobile'] ?? '');

    $stmt = $conn->prepare("
        SELECT id 
        FROM user 
        WHERE deletion = 0 AND otp_registration = 'no' 
        AND email_id = ? AND mobile = ?
    ");
    $stmt->bind_param("ss", $email, $mobile);
    $stmt->execute();
    $result = $stmt->get_result();

    if (mysqli_num_rows($result) > 0) {
        $otp = rand(1011,9999);
        
        $update_sql = "UPDATE user 
                    SET otp = ?, otp_expire_time = ? 
                    WHERE email_id = ? AND mobile = ?";
        $stmt_update = $conn->prepare($update_sql);
        $stmt_update->bind_param("ssss",$otp, $datetime, $email, $mobile);
        if ($stmt_update->execute()) {            
            $message = $otp.' is the retroEV One-Time-Password (OTP) to verify your phone Number . Please enter this to proceed. B2L';
            $baseUrl = "http://cloud.smsindiahub.in/api/mt/SendSMS";
            $params = [
                "user" => "retroev",
                "password" => "retro123Ev$123",
                "number"       => $mobile,  
                "text"  => $message,
                "channel" => "Trans",         // your sender ID
                "flashsms" => 0,
                "senderid" => "BLMTEC",
                "DCS" => 0,
                // maybe other params like “route”, “DLR” etc based on docs
            ];
            $url = $baseUrl . '?' . http_build_query($params);
            $response = file_get_contents($url);
            $responseData = json_decode($response, true);

            $sanitized_emailid=stripcslashes($email);
            $url_link=$_SERVER['SERVER_NAME'];
            $email=$sanitized_emailid;
            $fittmail=$main_org_mailid;
            $to = "$email";
            $subject = 'mPragati: OTP for Secure Login';
            $headers = "From: " . strip_tags($fittmail) . "\r\n";
            $headers .= "Reply-To: ". strip_tags($fittmail) . "\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
            $url=$url_link."/CRF/dist/img/mPragati_Logo_V6.avif";
            
            $message = '<html><body style="background: #fff">';
            $message .= '<table style="width: 60%;margin: auto;">';
            $message .= "<tbody>";
            $message .= "<tr><td><img style='height:66px;text-align:left;margin-bottom:3%' src='$url'></td></tr>";
            $message .= "<tr><td><h2 style='text-align:left;font-size:30px;margin-top:0;margin-bottom:0'>OTP for Secure Login</h2></td></tr>";
            $message .= "<tr><td><p style='text-align:justify;font-size:18px;font-weight:500;margin-bottom:6%'>Please use the one time password (OTP) below for secure login into your mPragati account with email ID ".$email.".</p></td></tr>";
            $message .= "<tr><td><span style='background:lightgray;padding:4%;text-align:center;display:block;font-size:30px;font-weight:600;margin-bottom:5%'>". strip_tags($otp) ."</span></td></tr>";
            $message .= "<tr><td><p style='text-align:justify;font-size:18px;margin-top:2%'>If you haven't requested this email, there's nothing to worry about-you can safely ignore it.</p></td></tr>";
            $message .= '</tbody><table>';
            $message .= '</body></table>';
            $retval = mail ($to,$subject,$message,$headers);
            if($retval==true) {
                echo "11";
            } else {
                echo "33";
            }
        } else {
            echo "00";
        }
    } else {
        echo "00";
    }

?>