<?php
require 'config_db.php';
$data = json_decode(file_get_contents('php://input'), true);
$user_type = $data['user_type'];
$mode = $data['mode'];
$member_id = $data['member_id'];
$start_date = $data['start_date'];
$end_date = $data['end_date'];

if ($user_type == '' || $member_id == '') {
	$returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Something Went wrong try again !");
} else {
    // get all visitors
	if ($mode == 1) {
	    if($user_type == 2) {
	        $visitor_query = $con->query("SELECT * 
                                        FROM attendance_data 
                                        WHERE member_id = '$member_id' AND STR_TO_DATE(scan_date, '%d-%m-%Y') 
                                              BETWEEN STR_TO_DATE('$start_date', '%d-%m-%Y') 
                                              AND STR_TO_DATE('$end_date', '%d-%m-%Y') 
                                        ORDER BY id DESC;
                                        ");
	    } else {
	        if($member_id == "000000")
	            $visitor_query = $con->query("SELECT * 
                                        FROM attendance_data 
                                        WHERE STR_TO_DATE(scan_date, '%d-%m-%Y') 
                                              BETWEEN STR_TO_DATE('$start_date', '%d-%m-%Y') 
                                              AND STR_TO_DATE('$end_date', '%d-%m-%Y') 
                                        ORDER BY id DESC;
                                        ");
	        else 
	            $visitor_query = $con->query("SELECT * 
                                        FROM attendance_data 
                                        WHERE member_id = '$member_id' AND STR_TO_DATE(scan_date, '%d-%m-%Y') 
                                              BETWEEN STR_TO_DATE('$start_date', '%d-%m-%Y') 
                                              AND STR_TO_DATE('$end_date', '%d-%m-%Y') 
                                        ORDER BY id DESC;
                                        ");
	    }
		$visitor_details = array();
		$all_visitors = array();
		while ($row1 = $visitor_query->fetch_assoc()) {
			$visitor_details['id'] = $row1['id'];
			$visitor_details['member_id'] = $row1['member_id'];
			$visitor_details['name'] = $row1['name'];
			$visitor_details['mobile'] = $row1['mobile'];
			$visitor_details['sub_name'] = $row1['sub_name'];
			$visitor_details['scan_date'] = $row1['scan_date'];
			$visitor_details['scan_time'] = $row1['scan_time'];
			$all_visitors [] = $visitor_details;
		}

		$returnArr = array(
			"ResponseCode" => "200",
			"Result" => "true",
			"ResponseMsg" => "Success",
			"data" => $all_visitors
		);
	}
}

echo json_encode($returnArr);
mysqli_close($con);
?>