<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "stock_category";
$page_name      = "Add Stock Category";
$id	 = "";
$vat = "";
$query_string = "";

################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string"));
	
    $stock_category 	       	    = $_REQUEST['stock_category']; 
    $stock_category_description 	= $_REQUEST['stock_category_description']; 
	$default_commodity_code 		= $_REQUEST['default_commodity_code']; 
	$default_nominal_for_purchases  = $_REQUEST['default_nominal_for_purchases']; 
	$is_expedite_po       			= $_REQUEST['is_expedite_po']; 
	$project_cc       				= $_REQUEST['project_cc']; 
	
    $created_by			= $_SESSION['user_id'];
	$created_on			= date("Y-m-d H:i:s");	
	$updated_by			= $_SESSION['user_id'];

	$sql = "stock_category = '".$stock_category."',stock_category_description = '".$stock_category_description."',default_commodity_code = '".$default_commodity_code."',default_nominal_for_purchases  = '".$default_nominal_for_purchases."',is_expedite_po = '".$is_expedite_po."',project_cc = '".$project_cc."',created_on = '".$created_on."', created_by = '".$created_by."', updated_by = '".$updated_by."'";

	$sql= "INSERT INTO $table_name SET ".$sql;

	mysqli_query($conn,$sql);
	
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	$_SESSION['info_message'] = "Stock Category added successfully";
    header("Location: $file_name");
    exit();
}
################################################# END INSERT #################################################

################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$id = $_REQUEST['id'];

	$form_action 	= "update";
	$button_value 	= "Update";
	$page_name      = "Edit Stock Category";
	$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	
	
    $sql= "SELECT * FROM $table_name WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);

	$stock_category 	            = $row_edit['stock_category']; 
    $stock_category_description 	= $row_edit['stock_category_description']; 
	$default_commodity_code 		= $row_edit['default_commodity_code'];
	$default_nominal_for_purchases  = $row_edit['default_nominal_for_purchases']; 
	$is_expedite_po  				= $row_edit['is_expedite_po'];  
	$project_cc						= $row_edit['project_cc'];  
}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string"));
	 
	$id								= $_REQUEST['id'];
	$stock_category 		        = $_REQUEST['stock_category']; 
    $stock_category_description 	= $_REQUEST['stock_category_description']; 
	$default_commodity_code 		= $_REQUEST['default_commodity_code']; 
	$default_nominal_for_purchases  = $_REQUEST['default_nominal_for_purchases'];
	$is_expedite_po       			= $_REQUEST['is_expedite_po'];
	$project_cc       				= $_REQUEST['project_cc']; 
	$updated_by						= $_SESSION['user_id'];
	
    $sql = "stock_category = '".$stock_category."',stock_category_description = '".$stock_category_description."',default_commodity_code = '".$default_commodity_code."',default_nominal_for_purchases  = '".$default_nominal_for_purchases."',is_expedite_po = '".$is_expedite_po."',project_cc = '".$project_cc."', updated_by = '".$updated_by."'";
	$sql= "UPDATE $table_name SET ".$sql." WHERE id = '".$id."'";
    mysqli_query($conn,$sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace("page=add", "page=view", $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	$_SESSION['info_message'] = "Stock Category updated successfully";
    header("Location: $file_name$query_string");
    exit();
}
################################################# END UPDATE ################################################# 
?>
<style>
em{color:red;font-size: 20px;position: absolute;}
</style>
<section class="content-header">
	<div class="container-fluid">
		<div class="row mb-2">
			<div class="col-sm-6">
				<h1>
					<?=$page_name;?>
				</h1>
			</div>
			<div class="col-sm-6">
			<?
			$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
			if(isset($from) && $from == "view_stock_category_details"){
				$query_string = str_ireplace("page=add", "page=view_stock_category_details", $query_string);
			}else {
				$query_string = str_ireplace("page=add", "page=view", $query_string);
			}
				
			?>
			<button type="button" id="back" name="back" value="back"  style="float:right;margin-right:5px;margin-bottom:8px;" class="btn btn-primary" onclick="window.location.href = 'index?<?=$query_string;?>'">Back</button>
			</div>
		</div>
	</div>
</section>
<section class="content">
	<div class="row">
		<div class="col-lg-12">
			<!-- general form elements -->
              <div class="card box-primary">
                <form class="form-horizontal" method="post" name="form1" id="form1" action="index?page=add&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="card-body">
				    <div class="row">
                    <div class="form-group col-lg-4">
                      <label for="Category" class="control-label">Category<em>*</em></label>
                      <div class="">
                        <input type="stock_category" id="stock_category" name="stock_category" value="<?php echo $stock_category;?>" class="form-control" placeholder="Category" required>
                      </div>
                    </div>
					<div class="form-group col-lg-4">
						<label for="Stock Category Description" class="control-label">Stock Category Description<em></em></label>
						<div class="">
							<input type="text" id="stock_category_description" name="stock_category_description" value="<?php echo $stock_category_description;?>" class="form-control" placeholder="Stock Category Description">
						</div>
					</div>
					<div class="form-group col-lg-4">
						<label for="Default Nominal For Purchases" class="control-label">Default Nominal For Purchases<em></em></label>
						<div class="">
							<input type="text" id="default_nominal_for_purchases" name="default_nominal_for_purchases" value="<?php echo $default_nominal_for_purchases;?>" class="form-control" placeholder="Default Nominal For Purchases">
						</div>
					</div>
					</div>
					<div class="row">
					<div class="form-group col-lg-4">
						<label for="Default Commodity Code" class="control-label">Default Commodity Code<em></em></label>
						<div class="">
							<input type="text" id="default_commodity_code" name="default_commodity_code" value="<?php echo $default_commodity_code;?>" class="form-control" placeholder="Default Commodity Code">
						</div>
					</div>
					<div class="form-group col-lg-4">
						<label for="Expedite P/O's" class="control-label">Expedite P/O's<em></em></label>
						<div class="">
							<? $gsi->getYesNo("is_expedite_po", $is_expedite_po, "100%", false, "form-control"); ?>
						</div>
					</div>
					<div class="form-group col-lg-4">
						<label for="Project CC" class="control-label">Project CC<em></em></label>
						<div class="">
							<input type="text" id="project_cc" name="project_cc" value="<?php echo $project_cc;?>" class="form-control" placeholder="Project CC" >
						</div>
					</div>
					</div>
                  </div><!-- /.box-body -->
                  <div class="card-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
                    <button type="submit" id="submit" name="submit" value="<?php echo $button_value;?>" class="btn btn-primary">Submit
						<i class="fas fa-sync-alt fa-spin" style="display:none;" id="page_load_div"></i>
					</button>
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
</section>
<script type="text/javascript">
$(document).ready(function(){
	$("#form1").submit(function() {
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
		//return true;
	});
	document.getElementById('stock_category').addEventListener('input', function() {
    this.value = this.value.toUpperCase();
  });

});
</script>