<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "item_management";
$id	 = "";
$vat = "";
$query_string = "";
$page_name      = "Add Item";
$from = $_REQUEST['from'];
################################################## START INSERT ##################################################

if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string"));
	  $max_files = 10; 
	  if (!empty($_FILES['images']['name'][0])) {
		$total_files = count($_FILES['images']['name']);
	  }

		if ($total_files > $max_files) {
			$_SESSION['info_message'] = "You can only upload a maximum of 10 images.";
			header("Location: index?page=add");
			exit();
		}
	$part_number				= $_REQUEST['part_number'];
	$product_description	    = $_REQUEST['product_description'];
	$part_status				= $_REQUEST['part_status'];
	$usual_source				= $_REQUEST['usual_source'];
	$stock_uom					= $_REQUEST['stock_uom'];
	$drawing_number				= $_REQUEST['drawing_number'];
	$product_group				= $_REQUEST['product_group'];
	$man_lead_time				= $_REQUEST['man_lead_time'];
	$standard_cost_ebq			= $_REQUEST['standard_cost_ebq'];
	$pack_quantity			    = $_REQUEST['pack_quantity'];
	$pack_weight				= $_REQUEST['pack_weight'];
	$pallet_quantity			= $_REQUEST['pallet_quantity'];
	$standard_cost				= $_REQUEST['standard_cost'];
	$nominal_code_sales			= $_REQUEST['nominal_code_sales'];
	$nominal_code_purchase		= $_REQUEST['nominal_code_purchase'];
	$date_time_amended			= utility::setFormatedDate($_REQUEST['date_time_amended']);
	$date_time_amended_utc		=  utility::setFormatedDate($_REQUEST['date_time_amended_utc']);
	$packing_code				= $_REQUEST['packing_code'];
	$recycle_code				= $_REQUEST['recycle_code'];
	$drawing_issue				= $_REQUEST['drawing_issue'];
	$uph						= $_REQUEST['uph'];
	$cnc_program_no				= $_REQUEST['cnc_program_no'];
	$customer_code				= $_REQUEST['customer_code'];
	$master_sample_location		= $_REQUEST['master_sample_location'];
	$tool_location				= $_REQUEST['tool_location'];
	$checking_fixture_location	= $_REQUEST['checking_fixture_location'];
	$type						= $_REQUEST['type'];
	$spec						= $_REQUEST['spec'];
	$grade						= $_REQUEST['grade'];
	$format						= $_REQUEST['format'];
	$density					= $_REQUEST['density'];
	$mpe						= $_REQUEST['mpe'];
	$length_mm					= $_REQUEST['length_mm'];
	$width_mm					= $_REQUEST['width_mm'];
	$thickness					= $_REQUEST['thickness'];
	$mks						= $_REQUEST['mks'];	
	$gross						= $_REQUEST['gross'];
	$net						= $_REQUEST['net'];
	$scrap_percent				= $_REQUEST['scrap_percent'];
	$uom						= $_REQUEST['uom'];
	$price_uom					= $_REQUEST['price_uom'];	
	$unit_cost					= $_REQUEST['unit_cost'];
	$scrap_value				= $_REQUEST['scrap_value'];
	$type_2						= $_REQUEST['type_2'];
	
	
	$created_by					= $_SESSION['user_id'];
	$created_on					= date("Y-m-d H:i:s");	
	$updated_by					= $_SESSION['user_id'];

	$sql = "part_number = '".$part_number."', product_description = '".$product_description."', part_status = '".$part_status."', usual_source = '".$usual_source."', stock_uom = '".$stock_uom."', drawing_number = '".$drawing_number."', product_group = '".$product_group."', man_lead_time = '".$man_lead_time."', standard_cost_ebq = '".$standard_cost_ebq."', pack_quantity = '".$pack_quantity."', pack_weight = '".$pack_weight."', pallet_quantity = '".$pallet_quantity."', standard_cost = '".$standard_cost."', nominal_code_sales = '".$nominal_code_sales."', nominal_code_purchase = '".$nominal_code_purchase."', date_time_amended = '".$date_time_amended."', date_time_amended_utc = '".$date_time_amended_utc."', packing_code = '".$packing_code."', recycle_code = '".$recycle_code."', drawing_issue = '".$drawing_issue."', uph = '".$uph."', cnc_program_no = '".$cnc_program_no."', customer_code = '".$customer_code."', master_sample_location = '".$master_sample_location."', tool_location = '".$tool_location."', checking_fixture_location = '".$checking_fixture_location."', type = '".$type."', spec = '".$spec."', grade = '".$grade."', format = '".$format."', density = '".$density."', mpe = '".$mpe."', length_mm = '".$length_mm."', width_mm = '".$width_mm."', thickness = '".$thickness."', mks = '".$mks."', gross = '".$gross."', net = '".$net."', scrap_percent = '".$scrap_percent."', uom = '".$uom."', price_uom = '".$price_uom."', unit_cost = '".$unit_cost."', scrap_value = '".$scrap_value."', type_2 = '".$type_2."', created_on = '".$created_on."', created_by = '".$created_by."', updated_by = '".$updated_by."'";

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

	mysqli_query($conn,$sql);
	$product_id = mysqli_insert_id($conn);
	
	$target_dir = "../product_images/";
		for ($i = 0; $i < $total_files; $i++) {
			$image_name = $_FILES['images']['name'][$i];
			$tmp_name = $_FILES['images']['tmp_name'][$i];
			$imageFileType = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
			if ($imageFileType != "jpg" && $imageFileType != "jpeg" && $imageFileType != "png" && $imageFileType != "gif") {
				$_SESSION['info_message'] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
				
			}
			$fname = pathinfo($image_name, PATHINFO_FILENAME);
			$target_file = $target_dir . $fname . "." . $imageFileType;
			$counter = 1;
			while (file_exists($target_file)) {
				$target_file = $target_dir . $fname . "_" . $counter . "." . $imageFileType;
				$counter++;
			}
			if (move_uploaded_file($tmp_name, $target_file)) {
			  $sql_img = "INSERT INTO item_images (product_id, product_image) VALUES ('$product_id', '$target_file')";
				mysqli_query($conn, $sql_img);
			}
		}
		if($part_number !=''){
			$sql="insert into product_name set pro_id='".$product_id."',group_name='".$product_description."',group_number='".$part_number."',group_code='33'";
			mysqli_query($conn,$sql);
			$group_id=mysqli_insert_id($conn);
	    }
		
	mysqli_query($conn,"UPDATE item_management SET part_number_id = '$group_id' WHERE id = '$product_id' ");
		if(isset($from) && $from == "add_new_item_progress"){
		$_SESSION['info_message'] = "Item added successfully";
		 header("Location: index?page=add&hide_top=1&hide_left=1");
		 //$_SESSION['new_sku_number_id'] = "$item_id";
          exit();
        }
		$_SESSION['info_message'] = "Item added successfully";
		header("Location: $file_name");
		exit();
	}

/* ####################################### edit section ############################# */
	if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$id = $_REQUEST['id'];
	
	$form_action 	= "update";
	$button_value 	= "Update";
	$page_name      = "Edit Item";
	$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);
	$row_edit = utility::validate_crypt_data($row_edit);
	
	
	$part_number				= $row_edit['part_number'];
	$product_description		= $row_edit['product_description'];
	$part_status				= $row_edit['part_status'];
	$usual_source				= $row_edit['usual_source'];
	$stock_uom					= $row_edit['stock_uom'];
	$drawing_number				= $row_edit['drawing_number'];
	$product_group				= $row_edit['product_group'];
	$man_lead_time				= $row_edit['man_lead_time'];
	$standard_cost_ebq			= $row_edit['standard_cost_ebq'];
	$pack_quantity			    = $row_edit['pack_quantity'];
	$pack_weight				= $row_edit['pack_weight'];
	$pallet_quantity			= $row_edit['pallet_quantity'];
	$standard_cost				= $row_edit['standard_cost'];
	$nominal_code_sales			= $row_edit['nominal_code_sales'];
	$nominal_code_purchase		= $row_edit['nominal_code_purchase'];
	$date_time_amended			= $row_edit['date_time_amended'];
	$date_time_amended_utc		= $row_edit['date_time_amended_utc'];
	$packing_code				= $row_edit['packing_code'];
	$recycle_code				= $row_edit['recycle_code'];
	$drawing_issue				= $row_edit['drawing_issue'];
	$uph						= $row_edit['uph'];
	$cnc_program_no				= $row_edit['cnc_program_no'];
	$customer_code				= $row_edit['customer_code'];
	$master_sample_location		= $row_edit['master_sample_location'];
	$tool_location				= $row_edit['tool_location'];
	$checking_fixture_location	= $row_edit['checking_fixture_location'];
	$type						= $row_edit['type'];
	$spec						= $row_edit['spec'];
	$grade						= $row_edit['grade'];
	$format						= $row_edit['format'];
	$density					= $row_edit['density'];
	$mpe						= $row_edit['mpe'];
	$length_mm					= $row_edit['length_mm'];
	$width_mm					= $row_edit['width_mm'];
	$thickness					= $row_edit['thickness'];
	$mks						= $row_edit['mks'];	
	$gross						= $row_edit['gross'];
	$net						= $row_edit['net'];
	$scrap_percent				= $row_edit['scrap_percent'];
	$uom						= $row_edit['uom'];
	$price_uom					= $row_edit['price_uom'];	
	$unit_cost					= $row_edit['unit_cost'];
	$scrap_value				= $row_edit['scrap_value'];
	$type_2						= $row_edit['type_2'];
		
}
/* ###################Update ##################################################### */


if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	
	utility::addslashesOnAllInput(array("query_string","admin_id"));
	$id  						= $_REQUEST['id'];
	
    $part_number				= $_REQUEST['part_number'];
	$product_description	    = $_REQUEST['product_description'];
	$part_status				= $_REQUEST['part_status'];
	$usual_source				= $_REQUEST['usual_source'];
	$stock_uom					= $_REQUEST['stock_uom'];
	$drawing_number				= $_REQUEST['drawing_number'];
	$product_group				= $_REQUEST['product_group'];
	$man_lead_time				= $_REQUEST['man_lead_time'];
	$standard_cost_ebq			= $_REQUEST['standard_cost_ebq'];
	$pack_quantity			    = $_REQUEST['pack_quantity'];
	$pack_weight				= $_REQUEST['pack_weight'];
	$pallet_quantity			= $_REQUEST['pallet_quantity'];
	$standard_cost				= $_REQUEST['standard_cost'];
	$nominal_code_sales			= $_REQUEST['nominal_code_sales'];
	$nominal_code_purchase		= $_REQUEST['nominal_code_purchase'];
	$date_time_amended			= utility::setFormatedDate($_REQUEST['date_time_amended']);
	$date_time_amended_utc		=  utility::setFormatedDate($_REQUEST['date_time_amended_utc']);
	$packing_code				= $_REQUEST['packing_code'];
	$recycle_code				= $_REQUEST['recycle_code'];
	$drawing_issue				= $_REQUEST['drawing_issue'];
	$uph						= $_REQUEST['uph'];
	$cnc_program_no				= $_REQUEST['cnc_program_no'];
	$customer_code				= $_REQUEST['customer_code'];
	$master_sample_location		= $_REQUEST['master_sample_location'];
	$tool_location				= $_REQUEST['tool_location'];
	$checking_fixture_location	= $_REQUEST['checking_fixture_location'];
	$type						= $_REQUEST['type'];
	$spec						= $_REQUEST['spec'];
	$grade						= $_REQUEST['grade'];
	$format						= $_REQUEST['format'];
	$density					= $_REQUEST['density'];
	$mpe						= $_REQUEST['mpe'];
	$length_mm					= $_REQUEST['length_mm'];
	$width_mm					= $_REQUEST['width_mm'];
	$thickness					= $_REQUEST['thickness'];
	$mks						= $_REQUEST['mks'];	
	$gross						= $_REQUEST['gross'];
	$net						= $_REQUEST['net'];
	$scrap_percent				= $_REQUEST['scrap_percent'];
	$uom						= $_REQUEST['uom'];
	$price_uom					= $_REQUEST['price_uom'];	
	$unit_cost					= $_REQUEST['unit_cost'];
	$scrap_value				= $_REQUEST['scrap_value'];
	$type_2						= $_REQUEST['type_2'];
	
	
	$updated_by		 		    = $_SESSION['user_id'];
	
	$sql = "part_number = '".$part_number."', product_description = '".$product_description."', part_status = '".$part_status."', usual_source = '".$usual_source."', stock_uom = '".$stock_uom."', drawing_number = '".$drawing_number."', product_group = '".$product_group."', man_lead_time = '".$man_lead_time."', standard_cost_ebq = '".$standard_cost_ebq."', pack_quantity = '".$pack_quantity."', pack_weight = '".$pack_weight."', pallet_quantity = '".$pallet_quantity."', standard_cost = '".$standard_cost."', nominal_code_sales = '".$nominal_code_sales."', nominal_code_purchase = '".$nominal_code_purchase."', date_time_amended = '".$date_time_amended."', date_time_amended_utc = '".$date_time_amended_utc."', packing_code = '".$packing_code."', recycle_code = '".$recycle_code."', drawing_issue = '".$drawing_issue."', uph = '".$uph."', cnc_program_no = '".$cnc_program_no."', customer_code = '".$customer_code."', master_sample_location = '".$master_sample_location."', tool_location = '".$tool_location."', checking_fixture_location = '".$checking_fixture_location."', type = '".$type."', spec = '".$spec."', grade = '".$grade."', format = '".$format."', density = '".$density."', mpe = '".$mpe."', length_mm = '".$length_mm."', width_mm = '".$width_mm."', thickness = '".$thickness."', mks = '".$mks."', gross = '".$gross."', net = '".$net."', scrap_percent = '".$scrap_percent."', uom = '".$uom."', price_uom = '".$price_uom."', unit_cost = '".$unit_cost."', scrap_value = '".$scrap_value."', type_2 = '".$type_2."', updated_by = '".$updated_by."'";
	$sql= "UPDATE $table_name SET ".$sql." WHERE id = '".$id."'";
    mysqli_query($conn,$sql);
	
	$target_dir = "../product_images/";
	$total_files = count($_FILES['images']['name']);
	for ($i = 0; $i < $total_files; $i++) {
		$image_name = $_FILES['images']['name'][$i];
		$tmp_name = $_FILES['images']['tmp_name'][$i];
		$imageFileType = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
		if ($imageFileType != "jpg" && $imageFileType != "jpeg" && $imageFileType != "png" && $imageFileType != "gif") {
			$_SESSION['info_message'] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

		}
		$fname = pathinfo($image_name, PATHINFO_FILENAME);
		$target_file = $target_dir . $fname . "." . $imageFileType;
		$counter = 1;
		while (file_exists($target_file)) {
			$target_file = $target_dir . $fname . "_" . $counter . "." . $imageFileType;
			$counter++;
		}
		if (move_uploaded_file($tmp_name, $target_file)) {
			$sql_img = "INSERT INTO item_images (product_id, product_image) VALUES ('$id', '$target_file')";
			mysqli_query($conn, $sql_img);
		}
	}

	$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;
	
	$item_id = urlencode(utility::app_crypt($id,'e'));
     	if($part_number !=''){
			mysqli_query($conn,"UPDATE product_name SET group_name = '$product_description' ,group_number = '$part_number' WHERE pro_id = '$id' ");
	
			mysqli_query($conn,$sql);
			$group_id=mysqli_insert_id($conn);
	    }
	
	$_SESSION['info_message'] = "Item updated successfully";
	if(isset($from) && $from == "view_item_progress"){
		header("Location: index?page=view_item_progress&from=view&item_id=$item_id&id=$id");
		exit();
	}
	
    header("Location: $file_name$query_string");
    exit();
}
################################################# END UPDATE ################################################# 
?>
<style>
em{color:red;font-size: 20px;position: absolute;}

fieldset {
    border-top: 4px solid #dedede; 
    border-right: 1px solid #dedede;
    border-bottom: 1px solid #dedede; 
    border-left: 1px solid #dedede; 
    padding: 15px;
    margin-bottom: 10px;
}

legend {
    width: auto;
    color: #a423a4;
}
.image-item {
    display: flex;
    align-items: center;
    margin-right: 10px;
}

.image-item img {
    margin-right: 5px;
}

.image-container {
    display: flex;
    flex-wrap: wrap;
}
</style>
<?if($_REQUEST['from'] !='add_new_item_progress'){?>
<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_client_progress"){
				$query_string = str_ireplace("page=add", "page=view_client_progress", $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-12">
			<!----------------------- Message ------------------------------->
			<? if(isset($_SESSION['info_message'])){ ?>
			  <div class="alert alert-info alert-dismissable">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
				<? echo $_SESSION['info_message']; unset($_SESSION['info_message']);?>
			  </div>
			  
			   <script type="text/javascript">
				$(document).ready(function(){
					setTimeout(function(){
						window.parent.jQuery("#myModal .close_button2").click();
						window.location.href = 'index?page=view_quotation_progress';
					},500);
				});
            </script>
			<?} ?>
			<!----------------------- End Message ------------------------------->
			<!-- general form elements -->
              <div class="card">
                <form class="" 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-sm-4 form-group">
						<label class="control-label">Part Number <em>*</em></label>
						<div>
							<input type="text" class="form-control" id="part_number" name="part_number" value="<?=$part_number; ?>" placeholder="Part Number" repl>
						</div>
					</div>
					<div class="col-sm-4 form-group">
						<label class="control-label">Product Description <em>*</em></label>
						<div>
							<input type="text" class="form-control" id="product_description" name="product_description" value="<?=$product_description; ?>" placeholder="Product Description" repl>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Part Status <em>*</em></label>
						<div>
							<select class="form-control" id="part_status" name="part_status" repl>
								<option value="Active" <?=($part_status == 'Active' ? 'selected' : '');?>>Active</option>
								<option value="Redundant" <?=($part_status == 'Redundant' ? 'selected' : '');?>>Redundant</option>
							</select>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Usual Source</label>
						<div>
							<select class="form-control" id="usual_source" name="usual_source">
								<option value="Manufactured" <?=($usual_source == 'Manufactured' ? 'selected' : '');?>>Manufactured</option>
								<option value="Bought-In" <?=($usual_source == 'Bought-In' ? 'selected' : '');?>>Bought-In</option>
							</select>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Stock UOM <em>*</em></label>
						<div>
							<input type="text" class="form-control" id="stock_uom" name="stock_uom" value="<?=$stock_uom; ?>" placeholder="Stock UOM" repl oninput="this.value = this.value.replace(/[^a-zA-Z\s]/g, '')">
						</div>
					</div>


					<div class="col-sm-4 form-group">
						<label class="control-label">Drawing Number</label>
						<div>
							<input type="text" class="form-control" id="drawing_number" name="drawing_number" value="<?=$drawing_number; ?>" placeholder="Drawing Number">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Product Group</label>
						<div>
							<? $gsi->getProductGroupList("product_group",$product_group,"100%",false,"form-control select2",""); ?>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Man Lead Time (Days)</label>
						<div>
							<input type="text" class="form-control" id="man_lead_time" name="man_lead_time" value="<?=$man_lead_time; ?>" placeholder="Manufacturing Lead Time" oninput="this.value = this.value.replace(/[^0-9.]/g, '')">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Standard Cost EBQ (£) <em>*</em></label>
						<div>
							<input type="text" class="form-control" id="standard_cost_ebq" name="standard_cost_ebq" value="<?=$standard_cost_ebq; ?>" placeholder="Standard Cost EBQ (£)" repl oninput="this.value = this.value.replace(/[^0-9.]/g, '')">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Pack Quantity <em>*</em></label>
						<div>
							<input type="text" class="form-control" id="pack_quantity" name="pack_quantity" value="<?=$pack_quantity; ?>" placeholder="Pack Quantity" repl oninput="this.value = this.value.replace(/[^0-9.]/g, '')">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Pack Weight</label>
						<div>
							<input type="text" class="form-control" id="pack_weight" name="pack_weight" value="<?=$pack_weight; ?>" placeholder="Pack Weight" oninput="this.value = this.value.replace(/[^0-9.]/g, '')">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Pallet Quantity</label>
						<div>
							<input type="text" class="form-control" id="pallet_quantity" name="pallet_quantity" value="<?=$pallet_quantity; ?>" placeholder="Pallet Quantity"oninput="this.value = this.value.replace(/[^0-9.]/g, '')">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Standard Cost (£) <em>*</em></label>
						<div>
							<input type="text" class="form-control" id="standard_cost" name="standard_cost" value="<?=$standard_cost; ?>" placeholder="Standard Cost (£)" repl>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Nominal Code Sales</label>
						<div>
							<input type="text" class="form-control" id="nominal_code_sales" name="nominal_code_sales" value="<?=$nominal_code_sales; ?>" placeholder="Nominal Code Sales">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Nominal Code Purchase</label>
						<div>
							<input type="text" class="form-control" id="nominal_code_purchase" name="nominal_code_purchase" value="<?=$nominal_code_purchase; ?>" placeholder="Nominal Code Purchase">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Date Time Amended</label>
						<div>
							<input type="text" class="form-control datepicker" id="date_time_amended" name="date_time_amended" value="<?php (isset($date_time_amended) && $date_time_amended !="") ? utility::getFormatedDate($date_time_amended) : "";?>"placeholder="DD-MM-YYYY"  autocomplete="off"> 
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Date Time Amended UTC</label>
						<div>
							<input type="text" class="form-control datepicker" id="date_time_amended_utc" name="date_time_amended_utc" value="<?php (isset($date_time_amended_utc) && $date_time_amended_utc !="") ? utility::getFormatedDate($date_time_amended_utc) : "";?>"placeholder="DD-MM-YYYY"  autocomplete="off"> 
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Packing Code</label>
						<div>
							<input type="text" class="form-control" id="packing_code" name="packing_code" value="<?=$packing_code; ?>" placeholder="Packing Code">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Recycle Code</label>
						<div>
							<input type="text" class="form-control" id="recycle_code" name="recycle_code" value="<?=$recycle_code; ?>" placeholder="Recycle Code">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Drawing Issue</label>
						<div>
							<input type="text" class="form-control" id="drawing_issue" name="drawing_issue" value="<?=$drawing_issue; ?>" placeholder="Drawing Issue">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">UPH</label>
						<div>
							<input type="text" class="form-control" id="uph" name="uph" value="<?=$uph; ?>" placeholder="UPH">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">CNC Program No</label>
						<div>
							<input type="text" class="form-control" id="cnc_program_no" name="cnc_program_no" value="<?=$cnc_program_no; ?>" placeholder="CNC Program No">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Customer Code</label>
						<div>
							<input type="text" class="form-control" id="customer_code" name="customer_code" value="<?=$customer_code; ?>" placeholder="Customer Code">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Master Sample Location</label>
						<div>
							<input type="text" class="form-control" id="master_sample_location" name="master_sample_location" value="<?=$master_sample_location; ?>" placeholder="Master Sample Location">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Tool Location</label>
						<div>
							<input type="text" class="form-control" id="tool_location" name="tool_location" value="<?=$tool_location; ?>" placeholder="Tool Location">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Checking Fixture Location</label>
						<div>
							<input type="text" class="form-control" id="checking_fixture_location" name="checking_fixture_location" value="<?=$checking_fixture_location; ?>" placeholder="Checking Fixture Location">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Type <em>*</em></label>
						<div>
							<input type="text" class="form-control" id="type" name="type" value="<?=$type; ?>" placeholder="Type" repl>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Spec</label>
						<div>
							<input type="text" class="form-control" id="spec" name="spec" value="<?=$spec; ?>" placeholder="Spec">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Grade</label>
						<div>
							<input type="text" class="form-control" id="grade" name="grade" value="<?=$grade; ?>" placeholder="Grade">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Format<em>*</em></label>
						<div>
							<input type="text" class="form-control" id="format" name="format" value="<?=$format; ?>" placeholder="Format" repl>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Density (g/cm3)<em>*</em></label>
						<div>
							<input type="text" class="form-control" id="density" name="density" value="<?=$density; ?>" placeholder="Density" repl>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">MPE<em>*</em></label>
						<div>
							<input type="text" class="form-control" id="mpe" name="mpe" value="<?=$mpe; ?>" placeholder="MPE" repl>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Length (mm)<em>*</em></label>
						<div>
							<input type="text" class="form-control" id="length_mm" name="length_mm" value="<?=$length_mm; ?>" placeholder="Length (mm)" repl>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Width (mm)<em>*</em></label>
						<div>
							<input type="text" class="form-control" id="width_mm" name="width_mm" value="<?=$width_mm; ?>" placeholder="Width (mm)" repl>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Thickness (mm)<em>*</em></label>
						<div>
							<input type="text" class="form-control" id="thickness" name="thickness" value="<?=$thickness; ?>" placeholder="Thickness" repl>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">MKS</label>
						<div>
							<input type="number" class="form-control" id="mks" name="mks" value="<?=$mks; ?>" placeholder="MKS">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Gross (£)</label>
						<div>
							<input type="text" class="form-control" id="gross" name="gross" value="<?=$gross; ?>" placeholder="Gross (£)">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Net  (£)</label>
						<div>
							<input type="text" class="form-control" id="net" name="net" value="<?=$net; ?>" placeholder="Net  (£)">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Scrap Percent (%)</label>
						<div>
							<input type="text" class="form-control" id="scrap_percent" name="scrap_percent" value="<?=$scrap_percent; ?>" placeholder="Scrap Percent (%)">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">UOM <em>*</em></label>
						<div>
							<input type="text" class="form-control" id="uom" name="uom" value="<?=$uom; ?>" placeholder="UOM" repl>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Price UOM<em>*</em></label>
						<div>
							<input type="text" class="form-control" id="price_uom" name="price_uom" value="<?=$price_uom; ?>" placeholder="Price UOM" repl>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Unit cost (£)<em>*</em></label>
						<div>
							<input type="text" class="form-control" id="unit_cost" name="unit_cost" value="<?=$unit_cost; ?>" placeholder="Unit cost" repl>
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Scrap Value</label>
						<div>
							<input type="text" class="form-control" id="scrap_value" name="scrap_value" value="<?=$scrap_value; ?>" placeholder="Scrap Value">
						</div>
					</div>

					<div class="col-sm-4 form-group">
						<label class="control-label">Type 2</label>
						<div>
							<input type="text" class="form-control" id="type_2" name="type_2" value="<?=$type_2; ?>" placeholder="Type 2">
						</div>
					</div>
					<div class="col-sm-4 form-group">
						<label for="images">Select Images (Up to 10)</label>
						<div>
							<input type="file" name="images[]" id="images" class="form-control" multiple accept=".jpg,.jpeg,.png,.gif">						
						</div>
					</div>
				<?php				
					$dir = "../product_images/";
					if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit') {
						?>
						<div class="table-responsive">
							<table id="example1" class="table">
								<thead>
									<tr>
										<!--<th>#</th>-->
										<th class="col-lg-10">Item Images</th>
										<!--<th class="col-lg-2">Delete</th>-->
									</tr>
								</thead>
							</table>
							<div style="max-height:400px; overflow-y:auto;">
								<table id="example1" class="table">
									<tbody>
										<?php
										$dir = "../product_images/";
										$result = mysqli_query($conn, "SELECT id,product_image FROM item_images WHERE product_id='$id'");

										while ($row = mysqli_fetch_assoc($result)) {
											$row = utility::validate_crypt_data($row);
											$product_images = $row['product_image'];
											$product_images_id = $row['id'];

											if (is_file($dir . $product_images)) {
												$image_src = "../" . $dir . $product_images;
											} else {
												$image_src = "placeholder.png";
											}
										?>
										<tr id="image_record_no_<?=$row['id'];?>">
											<td>
												<a href="<?= $image_src ?>" target="_blank">
													<img id="image-item" src="<?= $image_src ?>" width="100">
												</a>
											</td>
											<td>
												<span href="" class="image_delete" alt="<?=$row['id'];?>" data-product_image="<?=$row['product_image'];?>" style="color:#007bff;cursor:pointer"><i class="fa fa-fw fa-trash text-danger"></i></span>
												<i class="fas fa-sync-alt fa-spin" style="display:none;" id="delete_image_load_<?=$row['id'];?>"></i>
												<i class="fa fa-check" id="delete_image_done_<?=$row['id'];?>" style="color:#bad23a;font-size: 20px;display:none;"></i>
											</td>
										</tr>
										<?php } ?>
									</tbody>
								</table>
							</div>
						</div>
						<?php } ?>


					</div>
			     </div><!-- /.box-body -->
                  <div class="card-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" id="from" name="from" value="<?php echo $from;?>">
					<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">

$(function(){
	$('#form1').submit(function(){
		//$(this).addClass('disabled');
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
	});	
	
	$(".image_delete").click(function(){	
		if(confirm('Are you sure! You want to delete') == true){
			var imageid = $(this).attr("alt");
			var product_image = $(this).attr("data-product_image");
			if(imageid != ""){
				$('#delete_image_load_'+imageid).show();
				var form_data   = {'imageid':imageid,'product_image':product_image};       
				$.ajax({
					async: true,
					type: "POST",
					//url: site_url+"/modules/item_management/index.php?page=ajax_actions&action=delete_item_images",
					url: site_url+"/item_management/index?page=ajax_actions&action=delete_item_images",
					data: form_data,
					success: function(result){    
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$('#delete_image_done_'+imageid).show();
							$('#delete_image_load_'+imageid).hide();
							
							setTimeout(function() {
								$('#delete_image_done_'+imageid).hide();
								$('#image_record_no_'+imageid).remove();
							},1000);
						}
					}
				});
			}
		}else{
			return false;
		}
	});
	
	
});
</script>