<?php 
require 'config_db.php';
$data = json_decode(file_get_contents('php://input'), true);
$type = $data['user_type'];
$mode = $data['mode'];
if($type == '')
{
	$returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went wrong try again !");
}
else
{
    $p = array();
	$q = array();
    if($mode == 0) { // get all users
        $cy = $con->query("select * from app_users Order by id DESC");
        while($row = $cy->fetch_assoc())
    	{
    		$p['id'] = $row['id'];
    		$p['member_id'] = $row['member_id'];
    		$p['name'] = $row['name'];
    		$p['logo'] = $row['logo'];
    		$p['company'] = $row['company'];
    		$p['user_name'] = $row['user_name'];
    		$p['mobile'] = $row['mobile'];
    		$p['password'] = $row['password'];
    		$p['type'] = $row['type'];
    		$p['status'] = $row['status'];
    		$q[] = $p;
    	}
	    $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Success","data"=>$q);
	    
    } else if($mode == 1) { // get all active users
        $p1 = array();
    	$act = array();
        $cy = $con->query("select * from app_users where status='1' Order by id DESC");
        while($row = $cy->fetch_assoc())
    	{
    		$p['id'] = $row['id'];
    		$p['member_id'] = $row['member_id'];
    		$p['name'] = $row['name'];
    		$p['logo'] = $row['logo'];
    		$p['company'] = $row['company'];
    		$p['user_name'] = $row['user_name'];
    		$p['mobile'] = $row['mobile'];
    		$p['password'] = $row['password'];
    		$p['type'] = $row['type'];
    		$p['status'] = $row['status'];
    		$q[] = $p;
    	}
    	$cy1 = $con->query("select * from activities where status='1' Order by id DESC");
        while($row1 = $cy1->fetch_assoc())
    	{
    		$p1['id'] = $row1['id'];
    		$p1['name'] = $row1['name'];
    		$p1['total_limit'] = $row1['total_limit'];
    		$p1['status'] = $row1['status'];
    		$act[] = $p1;
    	}
    	$returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Success","data"=>$q,"activities"=>$act);
    	
    } else if($mode == 2) { // update user role,status
        $userid = strip_tags(mysqli_real_escape_string($con, $data['id']));
        $member_id = strip_tags(mysqli_real_escape_string($con, $data['member_id']));
        $name = strip_tags(mysqli_real_escape_string($con, $data['name']));
        $status = strip_tags(mysqli_real_escape_string($con, $data['status']));
        $role = strip_tags(mysqli_real_escape_string($con, $data['role']));
        
        $role_val = 2;
        if($role == "Admin"){
            $role_val = 1;
        }
        
        $status_val = 0;
        if($status == "Active"){
            $status_val = 1;
        }

        $update_app_user_sql = "update app_users set 
                                    `type`='$role_val',`status`='$status_val'
                                        where id = '$userid' and member_id = '$member_id'";

        $result = $con->query($update_app_user_sql);
        if ($result)
            $returnArr = array("ResponseCode" => "200", "Result" => "true", "ResponseMsg" => "User Status updated");
        else
            $returnArr = array("ResponseCode" => "201", "Result" => "false", "ResponseMsg" => "Failure");
    }
}
echo json_encode($returnArr);
mysqli_close($con);
?> 	