<?php require_once('../auth/auth.php');
if(isset($_POST['getid'])) {  
$getid=$_POST['getid'];  
$type=$_POST['type']; 
$checkbox1= implode(",", $_POST['cperc']);

	if ($type=='company') {

		mysqli_query($conn,"UPDATE company SET co_permission='$checkbox1' WHERE coid = '$getid'");

		mysqli_query($conn,"UPDATE user_g SET group_permission='$checkbox1' WHERE coid='$getid' AND group_name='Administrator' ORDER BY ugid ASC") or die(mysqli_error($conn));

		mysqli_query($conn,"UPDATE business SET business_permission='$checkbox1' WHERE coid='$getid' ORDER BY bid ASC") or die(mysqli_error($conn));

		mysqli_query($conn,"UPDATE branch SET branch_permission='$checkbox1' WHERE coid='$getid' ORDER BY brid ASC") or die(mysqli_error($conn)); 

	} elseif($type=='business') {
	  
	    mysqli_query($conn,"UPDATE business SET business_permission='$checkbox1' WHERE bid = '$getid'") or die(mysqli_error($conn));

	    mysqli_query($conn,"UPDATE branch SET branch_permission='$checkbox1' WHERE bid = '$getid' ORDER BY brid ASC") or die(mysqli_error($conn));

	} elseif($type=='branch') {
		
		mysqli_query($conn,"UPDATE branch SET branch_permission='$checkbox1' WHERE brid = '$getid'") or die(mysqli_error($conn));

	} elseif($type=='user_g') {
		  if(!empty($_POST['business_id'] && $_POST['branch_id'])) {

		  	$business_id = implode(",", $_POST['business_id']);
		  	$branch_id = implode(",", $_POST['branch_id']);

		  	mysqli_query($conn,"UPDATE user_g SET group_permission='$checkbox1',bid_per='$business_id',brid_per='$branch_id' WHERE ugid = '$getid'") or die(mysqli_error($conn));

		  } else {
			mysqli_query($conn,"UPDATE user_g SET group_permission='$checkbox1' WHERE ugid = '$getid'") or die(mysqli_error($conn));
	      }
	}
}  
?>