<?php 
include('nf_config.php');
include('menu_side.php');


	$newrecid1 = date("YmdHis");
	$newrecid2 = date("Y")*date("m")*date("d")+date("s")+rand(10,100);
	$newrecid3 = date("d")*date("d")*date("s")+date("s")+rand(100,10000);
	$newrecid4 = uniqid();
	$newrecid = $newrecid1.$newrecid2.$newrecid3.$newrecid4;	


include('r_dbc.php');
 		$sql = "select desc_manager
		from ccmanager_id
		where pid = '".$pid."'
		and mid = '".$mid."'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
      while($row = mysqli_fetch_assoc($result)) {
		$desc_manager = $row["desc_manager"];
		}
	}
mysqli_close($conn);		
		
if(isset($_REQUEST['submit']) and $_REQUEST['submit']!=""){
	extract($_REQUEST);
	if($code_center==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=un');
		exit;
	}
	elseif($code_categ==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue');
		exit;
	}
	else{

		$rab_datec = $rab_date;
		$rab_datec = substr($rab_datec,6,4)."-".substr($rab_datec,3,2)."-".substr($rab_datec,0,2);	
		
		
		$userCount	=	$db->getQueryCount('ccmanager','cid');
		if($userCount[0]['total']<2000000){
			$data	=	array(
							'pid'=>$pid,
							'mid'=>$mid,
							'code_center'=>$code_center,
							'code_categ'=>$code_categ,							
							'newrecid'=>$newrecid
						);
			$insert	=	$db->insert('ccmanager',$data);
			if($insert){
				header('location:nf_manager.php?msg=ras&pid='.$pid.'&mid='.$mid.'&xuid='.$xuid.'&lang='.$lang);
				exit;
			}else{
				header('location:nf_manager.php?msg=rna&pid='.$pid.'&mid='.$mid.'&xuid='.$xuid.'&lang='.$lang);
				exit;
			}
		}else{
			header('location:'.$_SERVER['PHP_SELF'].'?msg=dsd&pid='.$pid.'&mid='.$mid.'&xuid='.$xuid.'&lang='.$lang);
			exit;
		}
	}
}
?>

<!doctype html>

<html lang="en-US" class="no-js">

<head>

	<meta charset="UTF-8">

	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

	<title><?php echo $desc_manager." ".$nrecords ?></title>

	

    <link rel="stylesheet" href="./bootstrap/css/bootstrap.css" >
    <link rel="stylesheet" href="./fontawesome/css/all.css" >

 <link rel="stylesheet" href="./jqueryui/themes/smoothness/jquery-ui.css">
 <script src="./jqueryui/jquery-1.10.2.js"></script>
<script src="./jqueryui/jquery-ui.js"></script>

<!-- include summernote css/js -->
 <link href="./summernote/summernote-bs4.css" rel="stylesheet">
 <script src="./summernote/summernote-bs4.js"></script>

<script>
  $(function() {
	$( "#rab_date" ).datepicker({dateFormat: "dd-mm-yy"})
  });
</script>	

<?php include('nf_chosen01.php'); ?>	

</head>



<body>

	

  	<div class="container">


		<?php

		if(isset($_REQUEST['msg']) and $_REQUEST['msg']=="un"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> User name is mandatory field!</div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="ue"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> User email is mandatory field!</div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="up"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> User phone is mandatory field!</div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="ras"){

			echo	'<div class="alert alert-success"><i class="fa fa-thumbs-up"></i> Record added successfully!</div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="rna"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> Record not added <strong>Please try again!</strong></div>';

		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="dsd"){

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> Please delete a user and then try again <strong>We set limit for security reasons!</strong></div>';

		}

		?>

		<div class="card">

			<div class="card-header"><i class="fa fa-fw fa-plus-circle"></i> <strong><?php echo $nadd." ".$desc_manager." ".$nrecords;  ?></strong> <a href="nf_manager.php?pid=<?php echo $pid ?>&mid=<?php echo $mid ?>&xuid=<?php echo $xuid ?>&lang=<?php echo $lang ?>" class="float-end btn btn-dark btn-sm"><i class="fa fa-fw fa-globe"></i> <?php echo $nbrowse." ".$desc_manager." ".$nrecords;  ?></a></div>

			<div class="card-body">

				

				<div class="col-sm-6">

					<h6 class="card-title"><?php echo $nfields_with ?> <span class="text-danger">*</span> <?php echo $nmandatory ?></h6>

					<form method="post" action="nf_manager_add_cc2.php?pid=<?php echo $pid ?>&mid=<?php echo $mid ?>&xuid=<?php echo $xuid ?>&lang=<?php echo $lang ?>" >


						<div class="mb-3">

							<label><?php echo $n_center ?><span class="text-danger">*</span></label>

									<?php 
									echo "<select name='code_center' id='code_center' class='form-control chosen' > ";
									include('r_dbc.php');
											$sql = "select g.code_center_group,g.desc_center_group,c.code_center,c.desc_center
											from centers c, centers_groups g
											where c.pid = '".$pid."'
											and c.pid = g.pid and c.code_center_group = g.code_center_group ".$user_cc1." 
											and c.isgroup != 'GRP1'
											order by 1,3";
									$result = mysqli_query($conn, $sql);
									if (mysqli_num_rows($result) > 0) {
										  while($row = mysqli_fetch_assoc($result)) {
											printf ("<option value = '%s' >%s %s -->> %s %s </option>", $row["code_center"],$row["code_center_group"],$row["desc_center_group"],$row["code_center"],$row["desc_center"]);
											}
										}
									mysqli_close($conn);
									echo "</select>"; 
									?>

						</div>						

						<div class="mb-3">

							<label><?php echo $n_categ ?><span class="text-danger">*</span></label>

									<?php 
									echo "<select name='code_categ' id='code_categ' class='form-control chosen' > ";
									include('r_dbc.php');
											$sql = "select g.code_categ_group, g.desc_categ_group, c.code_categ,c.desc_categ,IF(c.um='--', '', concat('u.m.:',c.um)) as cum
											from categs c, categs_groups g
											where c.pid = '".$pid."'
											and c.pid = g.pid
											and c.code_categ_group = g.code_categ_group ".$user_ct1."											
											order by 1,3";
									$result = mysqli_query($conn, $sql);
									if (mysqli_num_rows($result) > 0) {
										  while($row = mysqli_fetch_assoc($result)) {
											printf ("<option value = '%s' > %s %s -->> %s %s -->> %s</option>", $row["code_categ"],$row["code_categ_group"],$row["desc_categ_group"],$row["code_categ"],$row["desc_categ"],$row["cum"]);
											}
										}
									mysqli_close($conn);
									echo "</select>"; 
									?>

						</div>



							<input type="hidden" id="newrecid"  name="newrecid"  value="<?php echo $newrecid ?>"  >						
							<input type="hidden" id="pid"  name="pid"  value="<?php echo $pid ?>"  >
							<input type="hidden" id="mid"  name="mid"  value="<?php echo $mid ?>"  >							
							<input type="hidden" id="xuid" name="xuid" value="<?php echo $xuid ?>" >
							<input type="hidden" id="lang" name="lang" value="<?php echo $lang ?>" >						

						<div>
							<button type="submit" name="submit" value="submit" id="submit" class="btn btn-primary"><i class="fa fa-fw fa-plus-circle"></i><?php echo $nadd." ".$nrecords ?></button>

						</div>

					</form>

				</div>

			</div>

		</div>

	</div>



    <script src="./jquery-3.4.1.js"></script>
    <script src="popper.min.js" ></script>
    <script src="./bootstrap/js/bootstrap.min.js" ></script>    


    

</body>
<?php include('nf_chosen02.php'); ?>
</html>