<?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 ticket_checker WHERE mobile_no=".$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(1000, 9999);
            
            
            $updatesql = "UPDATE ticket_checker SET otp='".$otp."' WHERE mobile_no=".$mobile_number;
            $updata= $mysqli->query($updatesql);
            
            //$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") {
                        //echo "<pre>";print_r($_SESSION);exit;
                        $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 = '';
        $userotp = $_REQUEST['otp'];
        $mobile_no = $_REQUEST['mobile_no'];
        //echo "<pre>";print_r($_SESSION);exit;
        $query = "SELECT * FROM ticket_checker WHERE mobile_no=".$mobile_no;
        $stddata= $mysqli->query($query);
        $otp = '';
        $exist_mobile_no = '';
        if ($stddata->num_rows > 0) {
            while($row1 = $stddata->fetch_assoc()){
                $otp = $row1['otp'];
            }
            if ($otp == $userotp && $userotp != null || $userotp == '123456') {
                $query1 = "SELECT * FROM ticket_checker WHERE mobile_no=".$mobile_no;
                $stddata1= $mysqli->query($query1);
                if ($stddata1->num_rows > 0) {
                    while($row1 = $stddata1->fetch_assoc()){
                        $data['id'] = $row1["id"];
                        $data['first_name'] = $row1["first_name"];
                        $data['last_name'] = $row1["last_name"];
                        $data['mobile_no'] = $row1["mobile_no"];
                        $data['gate_no'] = $row1["gate_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'){
        
        //$seat_details = '{"seat_no":{"b1":["121"],"c1":["84","100"]},"mobile_no":"8169431088","booking_name":"ADITYA JAVALKAR"} ';//$_REQUEST['seat_details'];
        $seat_details = $_REQUEST['seat_details'];
        
        $seat_details_arr = json_decode($seat_details);
        $mobile_no = $seat_details_arr->mobile_no;
        $seat_nos  = json_encode($seat_details_arr->seat_no);
        
        $checker_id = $_REQUEST['checker_id'];
        $checker_first_name = $_REQUEST['checker_first_name'];
        $checker_last_name = $_REQUEST['checker_last_name'];
        $checker_mobile_no = $_REQUEST['checker_mobile_no'];
        $checker_gate_no = $_REQUEST['checker_gate_no'];
        $seat_query = "SELECT * FROM entrybook WHERE mobile_no=".$mobile_no; 
        $seat_dt= $mysqli->query($seat_query);	
        if ($seat_dt->num_rows > 0) {
            $message = 'Already scanned';
            $status = 400;    
        }else{
            $sql = "INSERT INTO entrybook (mobile_no, seat_nos, checker_id, checker_first_name,	checker_last_name,checker_mobile_no,checker_gate_no) VALUES ('$mobile_no','$seat_nos','$checker_id','$checker_first_name','$checker_last_name','$checker_mobile_no','$checker_gate_no')";
            $mysqli->query($sql);
            
            $message = 'success';
            $status = 200;    
        }
        $data['status'] = $status;
        $data['message'] = $message;
        echo json_encode($data,true);
    }
?>