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

if(isset($_REQUEST['submit']) and $_REQUEST['submit']!=""){
	extract($_REQUEST);
	if($code_categ_group==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=un');
		exit;
	}
	elseif($code_categ==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue');
		exit;
	}	
	elseif($desc_categ==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue');
		exit;
	}
	elseif($um==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue');
		exit;
	}	
	/*elseif($comm_categ==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue');
		exit;
	}*/	
	else{
		
		$userCount	=	$db->getQueryCount('categs','cid');
		if($userCount[0]['total']<2000000){
			$data	=	array(
							'pid'=>$pid,
							'code_categ_group'=>$code_categ_group,
							'code_categ'=>$code_categ,							
							'desc_categ'=>$desc_categ,
							'um'=>$um,							
							'comm_categ'=>$comm_categ,			
							'isgroup'=>$isgroup										
						);
			$insert	=	$db->insert('categs',$data);
			if($insert){
				header('location:nf_categs.php?msg=ras&pid='.$pid.'&xuid='.$xuid.'&lang='.$lang);
				exit;
			}else{
				header('location:nf_categs.php?msg=rna&pid='.$pid.'&xuid='.$xuid.'&lang='.$lang);
				exit;
			}
		}else{
			header('location:'.$_SERVER['PHP_SELF'].'?msg=dsd&pid='.$pid.'&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 $n_categs ?></title>

	

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

<?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> $field_mand </div>';

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

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> $field_mand </div>';

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

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> $field_mand </div>';

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

			echo	'<div class="alert alert-success"><i class="fa fa-thumbs-up"></i> $recadded </div>';

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

			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> $recaddedno <strong> $tryagain </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." ".$n_categ_group;  ?></strong> <a href="nf_categs.php?pid=<?php echo $pid ?>&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." ".$n_categs;  ?></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">
					
						<div class="mb-3">

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

							<!-- input type="text" name="code_categ_group" id="code_categ_group" class="form-control" placeholder="<?php echo $nenter." ".$n_categ_group ?>" required -->

									<?php 
									echo "<select name='code_categ_group' id='code_categ_group' class='chosen form-control' > ";
									include('r_dbc.php');
											$sql = "select code_categ_group,desc_categ_group
											from categs_groups c
											where pid = '".$pid."'
											order by 1";
									$result = mysqli_query($conn, $sql);
									if (mysqli_num_rows($result) > 0) {
										  while($row = mysqli_fetch_assoc($result)) {
											printf ("<option value = '%s' > %s %s </option>", $row["code_categ_group"],$row["code_categ_group"],$row["desc_categ_group"]);
											}
										}
									mysqli_close($conn);
									echo "</select>"; 
									?>

						</div>						
					

						<div class="mb-3">

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

							<input type="text" name="code_categ" id="code_categ" class="form-control" placeholder="<?php echo $nenter." ".$n_categ ?>" required>

						</div>						

						<div class="mb-3">

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

							<input type="text" name="desc_categ" id="desc_categ" class="form-control" placeholder="<?php echo $nenter." ".$ndesc ?>" required>

						</div>
						
						<div class="mb-3">

							<label><?php echo $num ?></label>

									<?php 
									echo "<select name='um' id='um' class='form-control' > ";
									include('r_dbc.php');
											$sql = "select um
											from um
											order by 1";
									$result = mysqli_query($conn, $sql);
									if (mysqli_num_rows($result) > 0) {
										  while($row = mysqli_fetch_assoc($result)) {
											printf ("<option value = '%s' > %s </option>", $row["um"],$row["um"]);
											}
										}
									mysqli_close($conn);
									echo "</select>"; 
									?>

						</div>						
						

						<div class="mb-3">

							<label><?php echo $n_comms ?></label>

							<input type="text" name="comm_categ" id="comm_categ" class="form-control" placeholder="<?php echo $nenter." ".$n_comms ?>">

						</div>												

						<div class="mb-3">

							<label><?php echo $nsubgroupd ?></label>

									<?php 
									echo "<select name='isgroup' id='isgroup' class='form-control chosen' > ";
									include('r_dbc.php');
											$sql = "select 'GRP0' as code_categ,upper('".$nnot." ".$ngroupd."') as desc_categ1
											UNION
											SELECT 'GRP1' as code_categ,upper('".$ngroupd."') as desc_categ11
											UNION
											select code_categ,concat(code_categ,'-',desc_categ) as desc_categ1
											from categs
											where pid = '".$pid."'
											and isgroup = 'GRP1'
											order by 1";
									$result = mysqli_query($conn, $sql);
									if (mysqli_num_rows($result) > 0) {
										  while($row = mysqli_fetch_assoc($result)) {
											printf ("<option value = '%s' > %s </option>", $row["code_categ"],$row["desc_categ1"]);
											}
										}
									mysqli_close($conn);
									echo "</select>"; 
									?>

						</div>																		
				
							<input type="hidden" id="pid"  name="pid"  value="<?php echo $pid ?>"  >
							<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." ".$n_categ ?></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>