<?php 
require 'config_db.php';
$data = json_decode(file_get_contents('php://input'), true);
if($data['user_type'] == ''  or $data['member_id'] == '')
{
    $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");
}
else
{
    $mode = strip_tags(mysqli_real_escape_string($con,$data['mode']));
    $member_id = strip_tags(mysqli_real_escape_string($con,$data['member_id']));
    $user_type = strip_tags(mysqli_real_escape_string($con,$data['user_type']));
    $member_name = strip_tags(mysqli_real_escape_string($con,$data['member_name']));
    $member_mobile = strip_tags(mysqli_real_escape_string($con,$data['member_mobile']));
    $sub_name = strip_tags(mysqli_real_escape_string($con,$data['sub_name']));
    $scan_date = strip_tags(mysqli_real_escape_string($con,$data['scan_date']));
    $scan_time = strip_tags(mysqli_real_escape_string($con,$data['scan_time']));
    $date_id = strip_tags(mysqli_real_escape_string($con,$data['date_id']));

    if($mode == 1) {
        
        $token = strip_tags(mysqli_real_escape_string($con,$data['token']));
        $status = $con->query("select * from app_attendance_key where att_token = '$token'");
        if ($status->num_rows != 0) {
            if ($row1 = $status->fetch_assoc()) {
                $created_by = $row1['id'];
                $otp = $row1['att_key'];
                
                $attendance = $con->query("select * from attendance_data where date_id = '$date_id' and member_id = '$member_id'");
                if ($attendance->num_rows != 0) {
                    $returnArr = array("ResponseCode" => "300", "Result" => "false", "ResponseMsg" => "Already Attendance Added");
                } else {
                    $sql = "INSERT INTO `attendance_data`(`created_by`,`date_id`,`member_id`,`name`,`mobile`,`sub_name`,`otp`,`scan_date`,`scan_time`) 
                    VALUES ('$created_by','$date_id','$member_id','$member_name','$member_mobile','$sub_name','$otp','$scan_date','$scan_time')";
                    $result = mysqli_query($con,$sql);
                    
                    if($result) {
                        $returnArr = array(
                		"ResponseCode" => "200",
                		"Result" => "true",
                		"ResponseMsg" => "Attendance Added Successfully",
                		"data" => []);
                    } else {
                        $returnArr = array(
                		"ResponseCode" => "302",
                		"Result" => "false",
                		"ResponseMsg" => "Rescan the QR!!",
                		"data" => []);
                    }
                }
            } else {
                $returnArr = array("ResponseCode" => "301", "Result" => "false", "ResponseMsg" => "OTP Expired!");
            }
        } else {
            $returnArr = array("ResponseCode" => "302", "Result" => "false", "ResponseMsg" => "OTP Expired! Rescan the QR!!");
        }
        
        
    } else if($mode == 2) {
        $attendance = $con->query("select * from attendance_data where date_id = '$date_id' and member_id = '$member_id'");
        if ($attendance->num_rows != 0) {
            $returnArr = array("ResponseCode" => "300", "Result" => "false", "ResponseMsg" => "Already Attendance Added");
        } else {
            $sql = "INSERT INTO `attendance_data`(`created_by`,`date_id`,`member_id`,`name`,`mobile`,`sub_name`,`otp`,`scan_date`,`scan_time`) 
                    VALUES ('0','$date_id','$member_id','$member_name','$member_mobile','$sub_name','123456','$scan_date','$scan_time')";
            $result = mysqli_query($con,$sql);
            
            if($result) {
                $returnArr = array(
        		"ResponseCode" => "200",
        		"Result" => "true",
        		"ResponseMsg" => "Attendance Added Successfully",
        		"data" => []);
            } else {
                $returnArr = array(
        		"ResponseCode" => "201",
        		"Result" => "false",
        		"ResponseMsg" => "Try Again!!",
        		"data" => []);
            }
        }
    } else if($mode == 3) {
        
        $token = strip_tags(mysqli_real_escape_string($con,$data['token']));
        $status = $con->query("select * from app_attendance_key where att_key = '$token'");
        if ($status->num_rows != 0) {
            if ($row1 = $status->fetch_assoc()) {
                $created_by = $row1['id'];
                $otp = $row1['att_key'];
                
                $attendance = $con->query("select * from attendance_data where date_id = '$date_id' and member_id = '$member_id'");
                if ($attendance->num_rows != 0) {
                    $returnArr = array("ResponseCode" => "300", "Result" => "false", "ResponseMsg" => "Already Attendance Added");
                } else {
                    $sql = "INSERT INTO `attendance_data`(`created_by`,`date_id`,`member_id`,`name`,`mobile`,`sub_name`,`otp`,`scan_date`,`scan_time`) 
                    VALUES ('$created_by','$date_id','$member_id','$member_name','$member_mobile','$sub_name','$otp','$scan_date','$scan_time')";
                    $result = mysqli_query($con,$sql);
                    
                    if($result) {
                        $returnArr = array(
                		"ResponseCode" => "200",
                		"Result" => "true",
                		"ResponseMsg" => "Attendance Added Successfully",
                		"data" => []);
                    } else {
                        $returnArr = array(
                		"ResponseCode" => "302",
                		"Result" => "false",
                		"ResponseMsg" => "Rescan the QR!!",
                		"data" => []);
                    }
                }
            } else {
                $returnArr = array("ResponseCode" => "301", "Result" => "false", "ResponseMsg" => "OTP Expired!");
            }
        } else {
            $returnArr = array("ResponseCode" => "302", "Result" => "false", "ResponseMsg" => "OTP Expired! Rescan the QR!!");
        }
        
        
    } 
    
}
echo json_encode($returnArr);