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

if(isset($_REQUEST['editId']) and $_REQUEST['editId']!=""){
	$row	=	$db->getAllRecords('centers','*',' AND cid="'.$_REQUEST['editId'].'"');
}

if(isset($_REQUEST['submit']) and $_REQUEST['submit']!=""){
	extract($_REQUEST);
	if($code_center_group==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=un&editId='.$_REQUEST['editId']);
		exit;
	}elseif($code_center==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue&editId='.$_REQUEST['editId']);
		exit;
	}elseif($desc_center==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue&editId='.$_REQUEST['editId']);
		exit;
	}
	/* elseif($comm_center==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue&editId='.$_REQUEST['editId']);
		exit;
	} */
	
	$data	=	array(
					'code_center_group'=>$code_center_group,
					'code_center'=>$code_center,					
					'desc_center'=>$desc_center,
					'umc'=>$umc,
					'comm_center'=>$comm_center,
					'isgroup'=>$isgroup
					);
	$update	=	$db->update('centers',$data,array('cid'=>$editId));
	if($update){
		header('location: nf_centers.php?msg=rus&pid='.$pid.'&xuid='.$xuid.'&lang='.$lang);
		exit;
	}else{
		header('location: nf_centers.php?msg=rnu&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_centers ?></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 field!</div>';
		}elseif(isset($_REQUEST['msg']) and $_REQUEST['msg']=="ue"){
			echo	'<div class="alert alert-danger"><i class="fa fa-exclamation-triangle"></i> $field_mand field!</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>';
		}
		?>
		<div class="card">
			<div class="card-header"><i class="fa fa-fw fa-plus-circle"></i> <strong> <?php echo $nedit." ".$n_center;  ?> </strong> <a href="nf_centers.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_centers;  ?></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">
					
					<?php 
		
						$cidx 			= $row[0]['cid']; 
						$code_centerx = $row[0]['code_center']; 
						$desc_centerx = $row[0]['desc_center']; 
						$umcx 		  = $row[0]['umc']; 						
						$comm_centerx = $row[0]['comm_center']; 						
						$isgroupx 		= $row[0]['isgroup']; 						
					
					?>
						<div class="mb-3">
							<label><?php echo $n_center_group ?><span class="text-danger">*</span></label>
							<!-- input type="text" name="code_center_group" id="code_center_group" class="form-control" value="<?php echo $row[0]['code_center_group']; ?>" placeholder="<?php echo $nenter." ".$n_center_group; ?>" required -->

									<?php 
									echo "<select name='code_center_group' id='code_center_group' class='chosen form-control' > ";
									include('r_dbc.php');
											$sql = "select 1 as ordr,code_center_group,desc_center_group
											from centers_groups c
											where pid = '".$pid."'
											and code_center_group = '".$row[0]['code_center_group']."'
											union
											select 2 as ordr,code_center_group,desc_center_group
											from centers_groups c
											where pid = '".$pid."'
											and code_center_group != '".$row[0]['code_center_group']."'
											order by 1,2";
									$result = mysqli_query($conn, $sql);
									if (mysqli_num_rows($result) > 0) {
										  while($row = mysqli_fetch_assoc($result)) {
											printf ("<option value = '".$row["code_center_group"]."' > ".$row["code_center_group"]." ".$row["desc_center_group"]." </option>");
											}
										}
									mysqli_close($conn);
									echo "</select>"; 
									?>
							
						</div>											
					
					
						<div class="mb-3">
							<label><?php echo $n_center ?><span class="text-danger">*</span></label>
							<input type="text" name="code_center" id="code_center" class="form-control" value="<?php echo $code_centerx ?>"  required>
						</div>						
						<div class="mb-3">
							<label><?php echo $ndesc ?><span class="text-danger">*</span></label>
							<input type="text" name="desc_center" id="desc_center" class="form-control" value="<?php echo $desc_centerx  ?>"  required>
						</div>
						<div class="mb-3">
							<label><?php echo $numc ?></label>

									<?php 
									echo "<select name='umc' id='umc' class='form-control' > ";
									include('r_dbc.php');
											$sql = "select 1 as ordr,umc
											from um
											where umc = '".$umcx."'
											and length(umc)>0
											union
											select 2 as ordr,umc
											from um
											where umc != '".$umcx."'
											and length(umc)>0
											order by 1,2";
									$result = mysqli_query($conn, $sql);
									if (mysqli_num_rows($result) > 0) {
										  while($row = mysqli_fetch_assoc($result)) {
											printf ("<option value = '".$row["umc"]."' > ".$row["umc"]." </option>");
											}
										}
									mysqli_close($conn);
									echo "</select>"; 
									?>
							
						</div>			
					
						<div class="mb-3">
							<label><?php echo $ncomms ?></label>
							<input type="text" name="comm_center" id="comm_center" class="form-control" value="<?php echo $comm_centerx ?>"  >
						</div>						

						<div class="mb-3">

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

									<?php 
									
									
									
											$sql = "select 1 as ordr, code_center,concat(code_center,'-',desc_center) as desc_center1
											from centers
											where pid = '".$pid."'
											and code_center = '".$isgroupx."'
											UNION
											select 2 as ordr, code_center,concat(code_center,'-',desc_center) as desc_center1
											from centers
											where pid = '".$pid."'
											and isgroup = 'GRP1'
											and code_center != '".$isgroupx."'									
											order by ordr";																				
									
									
									
									echo "<select name='isgroup' id='isgroup' class='form-control chosen' > ";
									
																		
																		
									if ($isgroupx == 'GRP0') 
										{ 
											$desc_center_isgroup = strtoupper($nnot." ".$ngroupd); 
											printf ("<option value = '%s' > %s </option>", $isgroupx,$desc_center_isgroup);											
											printf ("<option value = 'GRP1' > %s </option>", strtoupper($ngroupd));																						
											include('r_dbc.php');
											$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_center"],$row["desc_center1"]);
													}
												}
											mysqli_close($conn);											
										}
									else if ($isgroupx == 'GRP1') 
										{ 
											$desc_center_isgroup = strtoupper($ngroupd); 
											printf ("<option value = '%s' > %s </option>", $isgroupx,$desc_center_isgroup);										
											printf ("<option value = 'GRP0' > %s </option>", strtoupper($nnot." ".$ngroupd));											
											include('r_dbc.php');
											$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_center"],$row["desc_center1"]);
													}
												}
											mysqli_close($conn);											
											}
									else {									
											include('r_dbc.php');
											$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_center"],$row["desc_center1"]);
													}
												}
											mysqli_close($conn);
									printf ("<option value = 'GRP0' > %s </option>", strtoupper($nnot." ".$ngroupd));											
									printf ("<option value = 'GRP1' > %s </option>", strtoupper($ngroupd));											
									}
								
									echo "</select>"; 
									?>

						</div>												

						
						<div class="mb-3">
							<input type="hidden" name="editId" id="editId" value="<?php echo $_REQUEST['editId']?>">
							<button type="submit" name="submit" value="submit" id="submit" class="btn btn-primary"><i class="fa fa-fw fa-edit"></i> <?php echo $nupdate." ".$n_center;  ?></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>