<?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'];

if ($user_type == '' || $member_id == '') {
	$returnArr = array("ResponseCode" => "401", "Result" => "false", "ResponseMsg" => "Something Went wrong try again !");
} else {
    $bus_give = 0;
    $sql1 = "select SUM(business_value) AS total_business from business_given_received where given_member_id='$member_id' and status = '1'";
    $result1 = mysqli_query($con,$sql1);
    if($row1 = mysqli_fetch_assoc($result1)){
        $bus_give = $row1['total_business'];
        if($bus_give == null) $bus_give = 0;
    }
    
    $bus_receive = 0;
    $sql2 = "select SUM(business_value) AS total_business from business_given_received where received_member_id='$member_id' and status = '1'";
    $result2 = mysqli_query($con,$sql2);
    if($row2 = mysqli_fetch_assoc($result2)){
        $bus_receive = $row2['total_business'];
        if($bus_receive == null) $bus_receive = 0;
    }
    
    $bus_refferal = 0;
    $sql3 = "select SUM(business_value) AS total_business from business_given_received where given_member_id='$member_id' and for_referral = '1' and status = '1'";
    $result3 = mysqli_query($con,$sql3);
    if($row3 = mysqli_fetch_assoc($result3)){
        $bus_refferal = $row3['total_business'];
        if($bus_refferal == null) $bus_refferal = 0;
    }
    
    $visitors = $con->query("select * from visitors where member_id='$member_id' and status = '1'")->num_rows;
    
    $sql4 = "select COUNT(DISTINCT date_id) AS total_attendance from attendance_data where status = '1'";
    $result4 = mysqli_query($con,$sql4);
    if($row4 = mysqli_fetch_assoc($result4)){
        $total_attendance = $row4['total_attendance'];
        if($total_attendance == null) $total_attendance = 0;
    } else {
        $total_attendance = 0;
    }
    
    $member_attendance = $con->query("select * from attendance_data where member_id='$member_id' and status = '1'")->num_rows;
    if($member_attendance == null) $member_attendance = 0;
    
    $giver_details = array();
	$all_givers = array();
    $top_giver = "SELECT given_member_id,SUM(business_value) AS total_amount from business_given_received GROUP BY given_member_id ORDER BY total_amount DESC LIMIT 3";
    $g_result = mysqli_query($con,$top_giver);
     while($rowg = mysqli_fetch_assoc($g_result)) {
         $g_member_id = $rowg['given_member_id'];
         $member_query = $con->query("select * from app_users where member_id = '$g_member_id' Order by id DESC");
		if ($rowgg = $member_query->fetch_assoc()) {
		    $giver_details['name'] = $rowgg['name'];
		    $giver_details['logo'] = $rowgg['logo'];
		}
		$giver_details['amount'] = "₹ ".$rowg['total_amount'];
		$all_givers[] = $giver_details;
     }
     
    $receiver_details = array();
	$all_receivers = array();
    $top_receiver = "SELECT received_member_id,SUM(business_value) AS total_amount from business_given_received GROUP BY received_member_id ORDER BY total_amount DESC LIMIT 3";
    $r_result = mysqli_query($con,$top_receiver);
     while($rowr = mysqli_fetch_assoc($r_result)) {
         $r_member_id = $rowr['received_member_id'];
         $member_query = $con->query("select * from app_users where member_id = '$r_member_id' Order by id DESC");
		if ($rowrr = $member_query->fetch_assoc()) {
		    $receiver_details['name'] = $rowrr['name'];
		    $receiver_details['logo'] = $rowrr['logo'];
		}
		$receiver_details['amount'] = "₹ ".$rowr['total_amount'];
		$all_receivers[] = $receiver_details;
     }
    
    // $users = $con->query("select * from users where shop_id='$shop_id' and status = '1'")->num_rows;
    // $i = 1;
    // $sql = "SELECT * FROM invoices where shop_id='$shop_id' order by id DESC limit 10";
    // $result = mysqli_query($con,$sql);
    // while($rows = mysqli_fetch_assoc($result))
    // {
    //     $is_invoice = $rows['is_invoice'];
    //     if($is_invoice == 1)
    //     {
    //         $datee = $rows['invoice_date'];
    //         $noo = $rows['invoice_no'];
    //     }
    //     else if($is_invoice == 0)
    //     {
    //         $datee = $rows['estimate_date'];
    //         $noo = $rows['estimate_no'];
    //     }
    //     $p['s_no'] = $i++;
    //     $p['order_date'] = $datee;
    //     $p['order_no'] = $noo;
    // 	$p['order_id'] = $rows['order_id'];
    // 	$p['name'] = $rows['cus_name'];
    // 	$p['amount']  = $rows['rounded_total'];
    	
    //     $re_orders[] = $p;
    // }
  
    $rows = array('business_given'=>$bus_give,'business_received'=>$bus_receive,'business_referral'=>$bus_refferal,'visitors'=>$visitors,
    'total_attendance'=>$total_attendance,'member_attendance'=>$member_attendance,'top_givers'=>$all_givers,'top_receivers'=>$all_receivers);
}

echo json_encode($rows);
mysqli_close($con);

?>