<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view_purchase_list";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "purchase_list";
$page_name      = "Add Purchase List";
$id	 = "";
$vat = "";
$query_string = "";
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string"));
	
    $item_id 	= $_REQUEST['item_id']; 
    $quantity_requested 	= $_REQUEST['quantity_requested']; 
	
    $sql= "INSERT INTO $table_name (item_id,quantity_requested) VALUES('$item_id','$quantity_requested')";
    mysqli_query($conn,$sql);
	
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	$_SESSION['info_message'] = "$tmp 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 Purchase List";
	$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);

	$item_id 	= $row_edit['item_id']; 
	$quantity_requested 		= $row_edit['quantity_requested']; 
	$required_date 		= $row_edit['required_date'];
	$work_order_id 		= $row_edit['work_order_id']; 
}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string"));
	 
	$id					= $_REQUEST['id'];
	$item_id 			= $_REQUEST['item_id'];
	$quantity_requested 			= $_REQUEST['quantity_requested'];
	

	$sql= "UPDATE $table_name SET item_id = '$item_id' ,quantity_requested = '$quantity_requested' 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_purchase_list", $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	$_SESSION['info_message'] = "$tmp 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'];
			$query_string = str_ireplace("page=add", "page=view_purchase_list", $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="col-lg-6">
                      <label for="Item No." class="control-label">Item No.<em>*</em></label>
                      <div class="">
                        <? $gsi->getPartNumberListNew("item_id", $item_id, "100%", false, "form-control select2 item_id", "required"); ?>
                      </div>
                    </div>
					<div class="col-lg-6">
                      <label for="Quantity" class="control-label">Quantity<em>*</em></label>
                      <div class="">
                        <input type="text" id="quantity_requested" name="quantity_requested" value="<?php echo $quantity_requested;?>" class="form-control" placeholder="Quantity" required>
                      </div>
                    </div>
					<div class="col-lg-6">
                      <label for="part_num_internal" class=" control-label">Required Date<em></em></label>
                      <div class="">
                      <input type="text" class="form-control datepicker" id="required_date" name="required_date" value="<?php (isset($required_date) && $required_date!="") ? utility::getFormatedDate($required_date) : "";?>" placeholder="DD-MM-YYYY">
                      </div>
                    </div>
					<div class="col-lg-6">
                      <label for="part_num_category" class="control-label">WO NO.<em>*</em></label>
                      <div class="">
                        <input type="text" id="work_order_id" name="work_order_id" value="<?php echo $work_order_id;?>" class="form-control" placeholder="WO No." required>
                      </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;
	});
	
});
</script>