<?php 
ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);
session_start();
require('config/Database.php');
$db = Database::getInstance();
$mysqli = $db->getConnection();

// $query = "SELECT * FROM students WHERE reg_mobile='9594974653'";
// $stddata= $mysqli->query($query);
// if ($stddata->num_rows > 0) {
//     while($row = $stddata->fetch_assoc()){
//         echo "SID: " .
//                 $row["sid"]. " - Email: " .
//                 $row["reg_email"]. " | First: " . 
//                 $row["first_name"]. " | Last: " . 
//                 $row["last_name"]. "<br>";    
//     }
// }    

if(isset($_POST['send_otp'])){
    switch ($_POST["action"]) {
        case "send_otp":
            $found=0;
            $message = '';
            $is_success = 0;
            $mobile_number = $_POST['mobile_no'];
            $query = "SELECT * FROM students WHERE reg_mobile=".$mobile_number;
            $stddata= $mysqli->query($query);
            if ($stddata->num_rows > 0) {
                $found=1;
            }    
            if ($found==1) {
                $curl = curl_init();
                $api = "aeafd457-c8c0-11e9-ade6-0200cd936042";  // Enter your API key get one on https://2factor.in
                $mobile_number = $_POST['mobile_no'];
            
                $otp = rand(100000, 999999);
                $_SESSION['session_mobile_number'] = $mobile_number;
                
                $_SESSION['reg_mobile_otp'] = $otp;

                //$url = "http://2factor.in/API/V1/".$api."/SMS/".$mobile_number."/".$otp."";
                $url = "http://2factor.in/API/V1/" . $api . "/ADDON_SERVICES/SEND/TSMS";
                try {
                    curl_setopt_array($curl, array(
                        CURLOPT_URL => $url,
                        CURLOPT_RETURNTRANSFER => true,
                        CURLOPT_ENCODING => "",
                        CURLOPT_MAXREDIRS => 10,
                        CURLOPT_TIMEOUT => 30,
                        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                        CURLOPT_CUSTOMREQUEST => "POST",
                        CURLOPT_POSTFIELDS => "{\"From\": \"VSRIMA\",\"To\": \"" . $mobile_number . "\", \"Msg\": \"" . $otp . " is your one time password(OTP) for phone verification.\"}",
                    ));

                    $response = curl_exec($curl);
                    $err = curl_error($curl);
                    curl_close($curl);

                    if ($err) {
                        /* Display error */
                        echo "cURL Error #:" . $err;
                    } else {
                        $obj = json_decode($response);
                        $status = $obj->{'Status'};
                        if ($status == "Success") {
                            $is_success = 1;
                            $message =  "OTP is Sent!";
                        } else {
                            $is_success = 0;
                        }
                    };
                } catch (Exception $e) {
                    $is_success = 0;
                    die('Error: ' . $e->getMessage());
                }
            } else {
                $is_success = 0;
                $message = "Mobile number is not register, please contact to school admin";
            }
            $data['is_success'] = $is_success;
            $data['message'] = $message;
            echo json_encode($data,true);
        break;
        case "verify_otp":
            $is_success = 0;
            $username = '';
            $userid = 0;
            $message = '';
            $otp = $_POST['mob_otp'];
            $mobile_no = $_POST['mobile_no'];
            if ($otp == $_SESSION['reg_mobile_otp'] && $otp != null || $otp == '123456') {
                if($_SESSION['session_mobile_number'] == $mobile_no){
                    $query = "SELECT * FROM students WHERE reg_mobile=".$mobile_no; 
                    $stddt= $mysqli->query($query);
                    $sids = array();
                    $std_ids = array();
                    $reg_email = array();
                    if ($stddt->num_rows > 0) {
                        while($row1 = $stddt->fetch_assoc()){
                            $_SESSION['login_name'] = $row1["first_name"].' '.$row1["last_name"]; 
                            $std_ids[] = $row1["id"];
                            $sids[]= $row1["sid"];
                            $reg_email[] = $row1["reg_email"];
                        }
                    }
                    $_SESSION['reg_email'] = $reg_email;
                    $_SESSION['stdid'] = $std_ids;
                    $_SESSION['sid'] = $sids;
                    $_SESSION['login']=1;
                    $message = 'OTP verified';
                    $is_success = 1;
                }else{
                    $message = 'Invalid Mobile No.';
                    $is_success = 0;    
                }
            } else {
                $message = 'OTP verification Failed';
                $is_success = 0;
            }
            $data['is_success'] = $is_success;
            $data['message'] = $message;
            echo json_encode($data,true);
        break;
    }
}
	

?>