<?php 
require 'config_db.php';
$data = json_decode(file_get_contents('php://input'), true);
if($data['user_type'] == ''  or $data['given_member_id'] == '')
{
    $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");
}
else
{
    $mode = strip_tags(mysqli_real_escape_string($con,$data['mode']));
    
    $user_type = strip_tags(mysqli_real_escape_string($con,$data['user_type']));
    $created_by = strip_tags(mysqli_real_escape_string($con,$data['created_by']));
    $date_id = strip_tags(mysqli_real_escape_string($con,$data['date_id']));
    
    $g_id = strip_tags(mysqli_real_escape_string($con,$data['given_member_id']));
    $g_name = strip_tags(mysqli_real_escape_string($con,$data['given_name']));
    $g_mobile = strip_tags(mysqli_real_escape_string($con,$data['given_mobile']));
    
    $r_id = strip_tags(mysqli_real_escape_string($con,$data['received_member_id']));
    $r_name = strip_tags(mysqli_real_escape_string($con,$data['received_name']));
    $r_mobile = strip_tags(mysqli_real_escape_string($con,$data['received_mobile']));
    
    
    $business_value = strip_tags(mysqli_real_escape_string($con,$data['business_value']));
    $business_date = strip_tags(mysqli_real_escape_string($con,$data['business_date']));
    $date_time = strip_tags(mysqli_real_escape_string($con,$data['date_time']));
    $refer = strip_tags(mysqli_real_escape_string($con,$data['for_refer']));

    if($mode == 1) {
        
        $attendance = $con->query("select * from business_given_received where date_id = '$date_id' 
                            and given_member_id = '$g_id' and received_member_id = '$r_id' and business_value = '$business_value'");
        if ($attendance->num_rows != 0) {
            $returnArr = array("ResponseCode" => "300", "Result" => "false", "ResponseMsg" => "Already Business Data Added");
        } else {
            $sql = "INSERT INTO `business_given_received`(`created_by`, `date_id`, `given_member_id`, `given_name`, `given_mobile`, 
            `received_member_id`, `received_name`, `received_mobile`, `for_referral`, `business_date`, `business_value`, `date_time`) 
            VALUES ('$created_by','$date_id','$g_id','$g_name','$g_mobile','$r_id',
            '$r_name','$r_mobile','$refer','$business_date','$business_value','$date_time')";
            $result = mysqli_query($con,$sql);
            
            if($result) {
                $returnArr = array(
        		"ResponseCode" => "200",
        		"Result" => "true",
        		"ResponseMsg" => "Business Data Added",
        		"data" => []);
            } else {
                $returnArr = array(
        		"ResponseCode" => "302",
        		"Result" => "false",
        		"ResponseMsg" => "Business Data Error!",
        		"data" => []);
            }
        }
        
    } else if($mode == 2) {
        
        $attendance = $con->query("select * from business_given_received where date_id = '$date_id' 
                            and given_member_id = '$r_id' and received_member_id = '$g_id' and business_value = '$business_value'");
        if ($attendance->num_rows != 0) {
            $returnArr = array("ResponseCode" => "300", "Result" => "false", "ResponseMsg" => "Already Business Data Added");
        } else {
            $sql = "INSERT INTO `business_given_received`(`created_by`, `date_id`, `given_member_id`, `given_name`, `given_mobile`, 
            `received_member_id`, `received_name`, `received_mobile`, `for_referral`, `business_date`, `business_value`, `date_time`) 
            VALUES ('$created_by','$date_id','$r_id','$r_name','$r_mobile','$g_id',
            '$g_name','$g_mobile','$refer','$business_date','$business_value','$date_time')";
            $result = mysqli_query($con,$sql);
            
            if($result) {
                $returnArr = array(
        		"ResponseCode" => "200",
        		"Result" => "true",
        		"ResponseMsg" => "Business Data Added",
        		"data" => []);
            } else {
                $returnArr = array(
        		"ResponseCode" => "302",
        		"Result" => "false",
        		"ResponseMsg" => "Business Data Error!",
        		"data" => []);
            }
        }
    }
    
}
echo json_encode($returnArr);