<?php

error_reporting(0);
require_once("../model/config.inc.php"); 
require_once("../model/Database.class.php"); 


/************************************* INSERT ********************************************/

if($_POST['action']=="Add")
{
	$active_menu = $_POST['checkboxesChecked'];
	$activemenu = explode(',',$active_menu);
	$result ='';
	$select_userroll=$pdo_conn->prepare("SELECT COUNT(userroll_id) FROM userroll WHERE roll_name LIKE '".$_POST['roll_name']."' and delete_status!='1'");
    $select_userroll->execute();
    $userroll = $select_userroll->fetchAll();

	if($userroll[0]['COUNT(userroll_id)']==0)
	{
		$sql = "INSERT INTO userroll (roll_name) VALUES (:roll_name)";
		$pdo_statement = $pdo_conn->prepare($sql);
			
		$result = $pdo_statement->execute(array(':roll_name'=>$_POST['roll_name']));
	}
	else
	{
		echo "error";
	}
	if($result == TRUE){
		
	  $lastInsertId = $pdo_conn->lastInsertId(); 
	  
		$sql = "SELECT * FROM userform WHERE delete_status !='1'";
		$prepare = $pdo_conn->prepare($sql);
		$exc = $prepare->execute();
	  	if($exc == TRUE){
			$list = $prepare->fetchAll();
			foreach($list as $value){
				
				if (in_array($value['userform_id'], $activemenu))
				{
				  $status = '1';
				}else{
				  $status = '0';
				}
				$sql = "INSERT INTO userformrights (roll_id, form_id, status) VALUES (:roll_id, :form_id, :status)";
				$pdo_statement = $pdo_conn->prepare($sql);
			
				$result = $pdo_statement->execute(array(':roll_id'=>$lastInsertId, ':form_id'=>$value['userform_id'], ':status'=>$status));
			}
		}
	  echo '1';
	}
	else { echo $pdo_statement->errorinfo(); }	
}

/************************************* UPDATE ********************************************/
if($_POST['action']=="Update")
{
	$result ='';
	$active_menu = $_POST['checkboxesChecked'];
	$activemenu = explode(',',$active_menu);
	
	$select_userroll=$pdo_conn->prepare("SELECT COUNT(userroll_id) FROM userroll WHERE roll_name LIKE '".$_POST['roll_name']."' AND userroll_id!='".$_POST['userroll_id']."'");
    $select_userroll->execute();
    $userroll = $select_userroll->fetchAll();

	if($userroll[0]['COUNT(userroll_id)']==0)
	{
		$pdo_statement=$pdo_conn->prepare("update userroll set roll_name='".$_POST['roll_name']."' where userroll_id=".$_POST['userroll_id']);
		$result = $pdo_statement->execute();
	}
	else
	{
		echo "error";
	}
	if($result == TRUE) {
		$pdo_delete = "DELETE FROM userformrights WHERE roll_id='".$_POST['userroll_id']."'";
		$result_delete=$pdo_conn->exec($pdo_delete);
		
		/*****************Insert latest*******/		
		
		$lastInsertId = $_POST['userroll_id']; 
	  
		$sql = "SELECT * FROM userform WHERE delete_status !='1'";
		$prepare = $pdo_conn->prepare($sql);
		$exc = $prepare->execute();
	  	if($exc == TRUE){
			$list = $prepare->fetchAll();
			foreach($list as $value){
				
				if (in_array($value['userform_id'], $activemenu))
				{
				  $status = '1';
				}else{
				  $status = '0';
				}
				$sql = "INSERT INTO userformrights (roll_id, form_id, status) VALUES (:roll_id, :form_id, :status)";
				$pdo_statement = $pdo_conn->prepare($sql);
			
				$result = $pdo_statement->execute(array(':roll_id'=>$lastInsertId, ':form_id'=>$value['userform_id'], ':status'=>$status));
			}
		}
	  echo '1';
	}else{ print_r($pdo_statement->errorinfo()); }
	
}

/************************************* DELETE ********************************************/

if($_POST['action']=="Delete")
{
	
	$pdo_statement="update userroll set delete_status='1' where userroll_id=".$_POST['userroll_id'];
	$result=$pdo_conn->exec($pdo_statement);
	
	$pdo_statement1="update userformrights set delete_status='1' where roll_id=".$_POST['userroll_id'];
	$result1=$pdo_conn->exec($pdo_statement1);
	if(!empty($result)) {
		echo $msg = "Successfully Deleted";
	}
	//else { print_r($pdo_statement->errorinfo()); }
	
}

?>