<link rel="stylesheet" href="../css/sweet-alert.css" /> 
<!doctype html>
<?php 
include("../connection.php");
include("../user.php");
$user=$_SESSION['user'];

	
$role_name=$_REQUEST['role_name'];
$ho_admin=$_REQUEST['ho_admin'];
$menu=$_REQUEST['menu'];
$cnt=count($menu);
$check=mysqli_num_rows(mysqli_query($con,"SELECT code FROM role_master ORDER BY code desc"));
//$tab_id=$_REQUEST['id'];
if($check==0)
{
$role_code="ROLE-001";
}
else
{
$contract_res=mysqli_fetch_array(mysqli_query($con,"SELECT code FROM role_master ORDER BY code desc"));
$auto_num=$contract_res['code'];
$no=explode("-",$auto_num);
$no1=$no[1]+1;
$sid= str_pad($no1,3,'0',STR_PAD_LEFT);
$role_code=$no[0]."-".$sid;
}
	$n=mysqli_query($con,"insert into role_master (code,ho_admin_access,role_name,status,created_by)values ('$role_code','$ho_admin','$role_name','0','1')");
for($r=0;$r<$cnt;$r++)
{
	$ra="new".($r+1);
	$new=$_REQUEST[$ra];
	$me=explode(":",$menu[$r]);
	$detail=mysqli_query($con,"insert into role_detail(code,menu_id,submenu_id,view_only,edit_only,all_only,created_by)values('$role_code','$me[0]','$me[1]','$new','','','$user')");
	//echo "insert into role_detail (code,menu_id,submenu_id,view_only,edit_only,all_only,created_by)values ('$role_code','$me[0]','$me[1]','$new','','','$user')";	
}

if($n)
{	
?>
<script>
setTimeout(function() 
{
swal({
title: "Successfully",
text: "Completed!",
type: "success"
}, function() {
window.location = "role_master.php";
});
}, 100);
</script>  
<?php
}	
else
{
?>
<script>
setTimeout(function() {
swal({
title: "Not",
text: "Completed!",
type: "error"
}, function() {
window.location = "role_master.php";
});
}, 100);
</script> 	  
<?Php
}	
?>
<script src="../js/sweet-alert.js"></script>