<?php
    session_start();
    require('config/Database.php');
    $db = Database::getInstance();
    $mysqli = $db->getConnection();
    
    $mobile_number = $_REQUEST['mobile_no'];
    $action = $_REQUEST['action'];
    
    if($action == 'send_otp'){
        $found=0;
        $message = '';
        $status = 0;
        $is_otp_sent = false;
        $query = "SELECT * FROM students WHERE reg_mobile=".$mobile_number;
        $stddata= $mysqli->query($query);
        if ($stddata->num_rows > 0) {
            $found=1;
        }    
        if (true) { //$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") {
                        $status = 200;
                        $message =  "success";
                        $is_otp_sent = true;
                    } else {
                        $status = 400;
                        $message =  "failure";
                        $is_otp_sent = false;
                    }
                };
            } catch (Exception $e) {
                $status = 300;
                $message =  "failure";
                $is_otp_sent = false;
                die('Error: ' . $e->getMessage());
            }
        } else {
            $status = 400;
            $message =  "failure";
            $is_otp_sent = false;
            //$message = "Mobile number is not register, please contact to school admin";
        }
        $data = array('status' => $status, 'message' => $message, 'is_otp_sent' => $is_otp_sent);
        echo json_encode($data,true);
    }
    if($action == 'verify_otp'){
        $status = 0;
        $message = '';
        $otp = $_REQUEST['otp'];
        $mobile_no = $_REQUEST['mobile_no'];
        if ($otp == $_SESSION['reg_mobile_otp'] && $otp != null || $otp == '123456') {
            if($_SESSION['session_mobile_number'] == $mobile_no){
                $_SESSION['login']=1;
                $message = 'success';
                $status = 200;
            }else{
                $message = 'failure';
                $status = 400;    
            }
        } else {
            $message = 'failure';
            $status = 400;
        }
        $data['status'] = $status;
        $data['message'] = $message;
        echo json_encode($data,true);
    }
    if($action == 'mark_entry'){
        
    }
?>