<?php 

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

include('r_dbc.php');
 		$sql = "select desc_manager,expenses
		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"];
		$expenses 		= $row["expenses"];		
		}
	}
mysqli_close($conn);			
		

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

if(isset($_REQUEST['submit']) and $_REQUEST['submit']!=""){
	extract($_REQUEST);
	if($code_center==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=un&editId='.$_REQUEST['editId']);
		exit;
	}elseif($code_categ==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue&editId='.$_REQUEST['editId']);
		exit;
	}elseif($rab_date==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue&editId='.$_REQUEST['editId']);
		exit;
	}elseif($rab_qty==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue&editId='.$_REQUEST['editId']);
		exit;
	}elseif($rab_uc==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue&editId='.$_REQUEST['editId']);
		exit;
	}elseif($rab_number==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue&editId='.$_REQUEST['editId']);
		exit;		
	}
	/*elseif($invoice==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue&editId='.$_REQUEST['editId']);
		exit;		
	}elseif($comms_html==""){
		header('location:'.$_SERVER['PHP_SELF'].'?msg=ue&editId='.$_REQUEST['editId']);
		exit;		
	}*/

		$rab_datec = $rab_date;
		$rab_datec = substr($rab_datec,6,4)."-".substr($rab_datec,3,2)."-".substr($rab_datec,0,2);	
	
	$data	=	array(
							'code_center'=>$code_center,
							'code_categ'=>$code_categ,							
							'rab_date'=>$rab_datec,
							'rab_qty'=>$rab_qty,							
							'rab_uc'=>$rab_uc,							
							'rab_number'=>$rab_number,							
							'invoice'=>$invoice,							
							'comms_html'=>$comms_html,
							'newrecid'=>$newrecid
						);
					
	$update	=	$db->update('ccmanager',$data,array('cid'=>$editId));
	if($update){
		header('location: nf_manager.php?msg=rus&pid='.$pid.'&mid='.$mid.'&xuid='.$xuid.'&lang='.$lang);
		exit;
	}else{
		header('location: nf_manager.php?msg=rnu&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="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote-bs4.css" rel="stylesheet">
 <script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote-bs4.js"></script>

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

</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']=="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>';
		}
		?>
		<div class="card">
			<div class="card-header"><i class="fa fa-fw fa-plus-circle"></i> <strong> <?php echo $nview." ".$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." ".$nrecords;  ?></a></div>
			<div class="card-body">
				
				<div class="col-sm-6">

					<form method="post">
					
					<?php 
						$code_centerx 	= $row[0]['code_center']; 
						$code_categx 	= $row[0]['code_categ']; 						
						$rab_datex 		= $row[0]['rab_date'];
						$rab_datex 		= substr($rab_datex,8,2)."-".substr($rab_datex,5,2)."-".substr($rab_datex,0,4);
						$rab_qtyx 		= $row[0]['rab_qty']; 						
						$rab_ucx 		= $row[0]['rab_uc']; 												
						$rab_numberx	= $row[0]['rab_number']; 						
						$invoicex 		= $row[0]['invoice']; 						
						$comms_htmlx 	= $row[0]['comms_html']; 						
					
					?>
						<div class="mb-3">
							<label><?php echo $n_center ?></label>

									<?php 
									echo "<select name='code_center' id='code_center' class='form-control' > ";
									include('r_dbc.php');
											$sql = "select 1 as ordr,code_center,desc_center
											from centers c
											where pid = '".$pid."'
											and code_center = '".$code_centerx."' ".$user_cc1."
											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"]."' > ".$row["code_center"]." ".$row["desc_center"]." </option>");
											}
										}
									mysqli_close($conn);
									echo "</select>"; 
									?>
							
						</div>											
						
						<div class="mb-3">
							<label><?php echo $n_categ ?></label>

									<?php 
									echo "<select name='code_categ' id='code_categ' class='form-control' > ";
									include('r_dbc.php');
											$sql = "select 1 as ordr,code_categ,desc_categ,IF(um='--', '', um) as cum
											from categs c
											where pid = '".$pid."'
											and code_categ = '".$code_categx."' ".$user_ct1."
											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_categ"]."' > ".$row["code_categ"]." ".$row["desc_categ"]." ".$row["cum"]."</option>");
											}
										}
									mysqli_close($conn);
									echo "</select>"; 
									?>
							
						</div>																
					

						<?php
							/* CONTROL FOR UM APPEAR BASED ON CATEGORY */
							include('r_dbc.php');
											$sql = "select c.um
											from categs c, ccmanager m
											where m.pid = '".$pid."'
											and m.pid = c.pid
											and m.code_categ = c.code_categ
											and c.code_categ = '".$code_categx."'";
									$result = mysqli_query($conn, $sql);
									if (mysqli_num_rows($result) > 0) {
										  while($row = mysqli_fetch_assoc($result)) {
											$um_check = $row["um"];
											}
										}
									mysqli_close($conn);
						?>
					

						<div class="mb-3">
							<label><?php echo $n_rab_date ?></label>
							<input type="text" name="rab_date" id="rab_date" class="form-control" value="<?php echo $rab_datex  ?>"  disabled>
						</div>
						
						<?php if ($um_check != "--") { ?>
						<div class="mb-3">
							<label><?php echo $n_rab_qty ?></label>
							<input type="number" name="rab_qty" id="rab_qty" class="form-control" step=".0001" value="<?php echo $rab_qtyx ?>"  disabled>
						</div>						

						<div class="mb-3">
							<label><?php echo $n_rab_uc ?></label>
							<input type="number" name="rab_uc" id="rab_uc" class="form-control" step=".0001" value="<?php echo $rab_ucx ?>"  disabled>
						</div>												
						
						
						<?php } 
						if ($um_check == "--") {						
						?>
							<input type="hidden" name="rab_qty" id="rab_qty" value="1">
						<?php } ?>

							
						<div class="mb-3">
							<label><?php echo $n_rab_number ?></label>
							<input type="number" name="rab_number" id="rab_number" class="form-control" step=".01" value="<?php echo $rab_numberx ?>"  disabled>
						</div>					
							<?php if ($expenses == 1) { ?>						
						<div class="mb-3">
							<label><?php echo $n_invoice ?></label>
							<input type="text" name="invoice" id="invoice" class="form-control" value="<?php echo $invoicex ?>"  disabled>
						</div>								
							<?php } ?>
						<div class="mb-3">
							<label><?php echo $ncomms ?></label>
							<?php echo $comms_htmlx ?>
								<script>
								  $('#summernote').summernote({
									tabsize: 3,
									height: 200
								  });
								</script>

						</div>																								
						


						
						<div class="mb-3">
							<input type="hidden" name="rab_uc" id="rab_uc" value="0">						
							<input type="hidden" name="newrecid" id="newrecid" value="-">
							<input type="hidden" name="editId" id="editId" value="<?php echo $_REQUEST['editId']?>">
		
						</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>
</html>