<script src="<?php echo $siteURL ?>/plugins/password/PassRequirements.js"></script>
<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view_pro_template";
$form_action	= "insert";
$button_value 	= "Save";
$table_name 	= "product_template";
$id	 = "";
$vat = "";
$query_string = "";
$page_name      = "Add Product Template";
$organisation_date = date("d-m-Y");
$from = $_REQUEST['from'];
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string","admin_id"));   
	
    $product_template_name 					= $_REQUEST['product_template_name']; 
    $part_number 							= $_REQUEST['part_number'];
	$part_number_revision 					= $_REQUEST['part_number_revision'];
	$part_number_category 					= $_REQUEST['part_number_category'];
	$stock_part 							= $_REQUEST['stock_part'];
	$current_product_range					= $_REQUEST['current_product_range'];
	$is_taxable 							= $_REQUEST['is_taxable'];
	$sales_item_code 						= $_REQUEST['sales_item_code'];
	$sales_price 							= $_REQUEST['sales_price'];
	$product_group							= $_REQUEST['product_group'];
	$product_group_short_code 				= $_REQUEST['product_group_short_code'];
	$parent_product_groups 					= $_REQUEST['parent_product_groups'];
	$description_external 					= $_REQUEST['description_external'];
	$description_internal 					= $_REQUEST['description_internal'];
	$currency								= $_REQUEST['currency'];
	$part_checked 							= $_REQUEST['part_checked'];
	$part_checked_date 						= $_REQUEST['part_checked_date'];
	$part_requires_attention 				= $_REQUEST['part_requires_attention'];
	$part_attention_notes 					= $_REQUEST['part_attention_notes'];
	$components_costs 						= $_REQUEST['components_costs'];
	$costing_method 						= $_REQUEST['costing_method'];
	$labour_hours 							= $_REQUEST['labour_hours'];
	$labour_hours_total_parts 				= $_REQUEST['labour_hours_total_parts'];
	$total_cost 							= $_REQUEST['total_cost'];
	$bill_of_materials 						= $_REQUEST['bill_of_materials'];
	$where_used 							= $_REQUEST['where_used'];
	$qr_code 								= $_REQUEST['qr_code'];
	$barcode 								= $_REQUEST['barcode'];
	$image1									= $_REQUEST['image1'];
	$image2 								= $_REQUEST['image2'];
	$image3 								= $_REQUEST['image3']; 
	
	$created_on								= date("Y-m-d H:i:s");	    
	$created_by 							=$_SESSION['admin_user_id'];
    
	
	$sql = "product_template_name = '".$product_template_name."',part_number = '".$part_number."',part_number_revision = '".$part_number_revision."',part_number_category = '".$part_number_category."',stock_part = '".$stock_part."',current_product_range = '".$current_product_range."',is_taxable = '".$is_taxable."',sales_item_code = '".$sales_item_code."',sales_price = '".$sales_price."',product_group = '".$product_group."',product_group_short_code = '".$product_group_short_code."', parent_product_groups = '".$parent_product_groups."', description_external = '".$description_external."',description_internal = '".$description_internal."',currency = '".$currency."',part_checked = '".$part_checked."',part_checked_date = '".$part_checked_date."',part_requires_attention = '".$part_requires_attention."',part_attention_notes = '".$part_attention_notes."',components_costs = '".$components_costs."',costing_method = '".$costing_method."',labour_hours = '".$labour_hours."',labour_hours_total_parts = '".$labour_hours_total_parts."',total_cost = '".$total_cost."',bill_of_materials = '".$bill_of_materials."',where_used = '".$where_used."',qr_code = '".$qr_code."',barcode = '".$barcode."',image1 = '".$image1."',image2 = '".$image2."',image3 = '".$image3."', created_on = '".$created_on."',  created_by = '".$created_by."'";
	
	$sql=utility::convert_to_crypt_data($sql,$table_name);
	$sql= "INSERT INTO $table_name SET ".$sql;
	
	mysqli_query($conn,$sql);
	$pro_temp_id = mysqli_insert_id($conn);
//	echo $pro_temp_id;die;
	$_SESSION['info_message'] = "Product Template added sucessfully";
	header('Location: index?page=add_pro_attr&pro_temp_id=' . $pro_temp_id);
	exit();

	
}
################################################# END INSERT #################################################
  
    $part_number 						=	1;      
	$part_number_revision 				=	1;     
	$part_number_category 				=	1;      
	$stock_part 						=	1;      
	$current_product_range				=	1;      
	$is_taxable 						=	1;      
	$sales_item_code 					=	1;     
	$sales_price 						=	1;      
	$product_group						=	1;     
	$product_group_short_code 			=	1;     
	$parent_product_groups 				=	1;     
	$description_external 				=	1;     
	$description_internal 				=	1;      
	$currency							=	1;      
	$part_checked 						=	1;      
	$part_checked_date 					=	1;      
	$part_requires_attention 			=	1;      
	$part_attention_notes 				=	1;    
	$components_costs 					=	1;      
	$costing_method 					=	1;      
	$labour_hours 						=	1;      
	$labour_hours_total_parts 			=	1;      
	$total_cost 						=	1;     
	$bill_of_materials 					=	1;      
	$where_used 						=	1;
	$qr_code 							=	1;
	$barcode 							=	1;
	$image1								=	1;
	$image2 							=	1; 
	
################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$id = $_REQUEST['id'];
	
	$form_action 	= "update";
	$button_value 	= "Update";
	$page_name      = "Edit Product Template";
	$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);
	
    $product_template_name 					= $row_edit['product_template_name']; 
    $part_number 							= $row_edit['part_number'];
	$part_number_revision 					= $row_edit['part_number_revision'];
	$part_number_category 					= $row_edit['part_number_category'];
	$stock_part 							= $row_edit['stock_part'];
	$current_product_range					= $row_edit['current_product_range'];
	$is_taxable 							= $row_edit['is_taxable'];
	$sales_item_code 						= $row_edit['sales_item_code'];
	$sales_price 							= $row_edit['sales_price'];
	$product_group							= $row_edit['product_group'];
	$product_group_short_code 				= $row_edit['product_group_short_code'];
	$parent_product_groups 					= $row_edit['parent_product_groups'];
	$description_external 					= $row_edit['description_external'];
	$description_internal 					= $row_edit['description_internal'];
	$currency								= $row_edit['currency'];
	$part_checked 							= $row_edit['part_checked'];
	$part_checked_date 						= $row_edit['part_checked_date'];
	$part_requires_attention 				= $row_edit['part_requires_attention'];
	$part_attention_notes 					= $row_edit['part_attention_notes'];
	$components_costs 						= $row_edit['components_costs'];
	$costing_method 						= $row_edit['costing_method'];
	$labour_hours 							= $row_edit['labour_hours'];
	$labour_hours_total_parts 				= $row_edit['labour_hours_total_parts'];
	$total_cost 							= $row_edit['total_cost'];
	$bill_of_materials 						= $row_edit['bill_of_materials'];
	$where_used 							= $row_edit['where_used'];
	$qr_code 								= $row_edit['qr_code'];
	$barcode 								= $row_edit['barcode'];
	$image1									= $row_edit['image1'];
	$image2 								= $row_edit['image2'];
	$image3 								= $row_edit['image3']; 
    
    
}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string","admin_id"));
	$id= $_REQUEST['id'];	
	
	 $product_template_name 				= $_REQUEST['product_template_name']; 
    $part_number 							= $_REQUEST['part_number'];
	$part_number_revision 					= $_REQUEST['part_number_revision'];
	$part_number_category 					= $_REQUEST['part_number_category'];
	$stock_part 							= $_REQUEST['stock_part'];
	$current_product_range					= $_REQUEST['current_product_range'];
	$is_taxable 							= $_REQUEST['is_taxable'];
	$sales_item_code 						= $_REQUEST['sales_item_code'];
	$sales_price 							= $_REQUEST['sales_price'];
	$product_group							= $_REQUEST['product_group'];
	$product_group_short_code 				= $_REQUEST['product_group_short_code'];
	$parent_product_groups 					= $_REQUEST['parent_product_groups'];
	$description_external 					= $_REQUEST['description_external'];
	$description_internal 					= $_REQUEST['description_internal'];
	$currency								= $_REQUEST['currency'];
	$part_checked 							= $_REQUEST['part_checked'];
	$part_checked_date 						= $_REQUEST['part_checked_date'];
	$part_requires_attention 				= $_REQUEST['part_requires_attention'];
	$part_attention_notes 					= $_REQUEST['part_attention_notes'];
	$components_costs 						= $_REQUEST['components_costs'];
	$costing_method 						= $_REQUEST['costing_method'];
	$labour_hours 							= $_REQUEST['labour_hours'];
	$labour_hours_total_parts 				= $_REQUEST['labour_hours_total_parts'];
	$total_cost 							= $_REQUEST['total_cost'];
	$bill_of_materials 						= $_REQUEST['bill_of_materials'];
	$where_used 							= $_REQUEST['where_used'];
	$qr_code 								= $_REQUEST['qr_code'];
	$barcode 								= $_REQUEST['barcode'];
	$image1									= $_REQUEST['image1'];
	$image2 								= $_REQUEST['image2'];
	$image3 								= $_REQUEST['image3'];
	
	$updated_by								= $_SESSION['user_id'];
	
	
	$sql = "product_template_name = '".$product_template_name."',part_number = '".$part_number."',part_number_revision = '".$part_number_revision."',part_number_category = '".$part_number_category."',stock_part = '".$stock_part."',current_product_range = '".$current_product_range."',is_taxable = '".$is_taxable."',sales_item_code = '".$sales_item_code."',sales_price = '".$sales_price."',product_group = '".$product_group."',product_group_short_code = '".$product_group_short_code."', parent_product_groups = '".$parent_product_groups."', description_external = '".$description_external."',description_internal = '".$description_internal."',currency = '".$currency."',part_checked = '".$part_checked."',part_checked_date = '".$part_checked_date."',part_requires_attention = '".$part_requires_attention."',part_attention_notes = '".$part_attention_notes."',components_costs = '".$components_costs."',costing_method = '".$costing_method."',labour_hours = '".$labour_hours."',labour_hours_total_parts = '".$labour_hours_total_parts."',total_cost = '".$total_cost."',bill_of_materials = '".$bill_of_materials."',where_used = '".$where_used."',qr_code = '".$qr_code."',barcode = '".$barcode."',image1 = '".$image1."',image2 = '".$image2."',image3 = '".$image3."', updated_by = '".$updated_by."'";
	$sql=utility::convert_to_crypt_data($sql,$table_name);
	$sql= "UPDATE $table_name SET ".$sql." WHERE id = '".$id."'";
    mysqli_query($conn,$sql);
	
	$_SESSION['info_message'] = "Product Template updated sucessfully";
    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_customer_details"){
				$query_string = str_ireplace("page=add", "page=view_customer_details", $query_string);
			}else {
				$query_string = str_ireplace("page=add_pro_template", "page=view_pro_template", $query_string);
			}
				
			?>
                <button type="button" id="back" name="back" value="back"
                    style="float:right;margin-right:5px;margin-bottom:8px;" class="btn btn-success"
                    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>
            <? } ?>
            <? if(isset($_SESSION['exit_message'])){ ?>
            <div class="alert alert-danger alert-dismissable">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                <? echo $_SESSION['exit_message']; unset($_SESSION['exit_message']);?>
            </div>
            <? } ?>
            <!----------------------- End Message ------------------------------->
            <!-- general form elements -->
            <div class="card">
                <form class="" method="post" name="form1" id="form1"
                    action="index?page=add_pro_template&action=<? echo $form_action; ?>"
                    onsubmit="return validateForm(this)" enctype="multipart/form-data">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-lg-12 col-sm-12">
                                <div class="form-group">
                                    <label class="control-label"
                                        for="First Name">Product Template Name<em>*</em>&nbsp;&nbsp;&nbsp;<span id="msg_print"
                                            class="ps-5 text-danger"></span></label>
                                    <div class="">
                                        <input type="text" id="product_template_name" name="product_template_name"
                                            class="form-control" value="<?php echo $product_template_name;?>"
                                            placeholder="Product Template Name" required>
                                    </div>
                                </div>
                            </div>
                        </div>
						<div class="row">
							<div class="text-center col-lg-12 mb-2">
								<strong class="h3"> Fixed Fields</strong>
								<hr style="background-color:#fa4a14; height:1px;">
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="part_number" name="part_number" value="1" <?php echo ($part_number == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Part Number</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="part_number_revision" name="part_number_revision" value="1" <?php echo ($part_number_revision == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Part Number Revision</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="part_number_category" name="part_number_category" value="1" <?php echo ($part_number_category == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Part Number Category</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="stock_part" name="stock_part" value="1" <?php echo ($stock_part == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Stock Part</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="current_product_range" name="current_product_range" value="1" <?php echo ($current_product_range == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Current Product Range</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="is_taxable" name="is_taxable" value="1" <?php echo ($is_taxable == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Is Taxable</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="sales_item_code" name="sales_item_code" value="1" <?php echo ($sales_item_code == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Sales Item Code</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="sales_price" name="sales_price" value="1" <?php echo ($sales_price == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Sales Price</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="product_group" name="product_group" value="1" <?php echo ($product_group == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Product Group</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="product_group_short_code" name="product_group_short_code" value="1" <?php echo ($product_group_short_code == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Product Group Short Code</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="parent_product_groups" name="parent_product_groups" value="1" <?php echo ($parent_product_groups == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Parent Product Groups</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="description_external" name="description_external" value="1" <?php echo ($description_external == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Description External</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="description_internal" name="description_internal" value="1" <?php echo ($description_internal == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Description Internal</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="currency" name="currency" value="1" <?php echo ($currency == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Currency</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="part_checked" name="part_checked" value="1" <?php echo ($part_checked == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Part Checked</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="part_checked_date" name="part_checked_date" value="1" <?php echo ($part_checked_date == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Part Checked Date</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="part_requires_attention" name="part_requires_attention" value="1" <?php echo ($part_requires_attention == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Part Requires Attention</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="part_attention_notes" name="part_attention_notes" value="1" <?php echo ($part_attention_notes == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Part Attention Notes</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="components_costs" name="components_costs" value="1" <?php echo ($components_costs == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Components Costs</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="costing_method" name="costing_method" value="1" <?php echo ($costing_method == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Costing Method</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="labour_hours" name="labour_hours" value="1" <?php echo ($labour_hours == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Labour Hours</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="labour_hours_total_parts" name="labour_hours_total_parts" value="1" <?php echo ($labour_hours_total_parts == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Labour Hours Total Parts</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="total_cost" name="total_cost" value="1" <?php echo ($total_cost == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Total Cost</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="bill_of_materials" name="bill_of_materials" value="1" <?php echo ($bill_of_materials == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Bill of Materials</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="where_used" name="where_used" value="1" <?php echo ($where_used == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Where Used</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="qr_code" name="qr_code" value="1" <?php echo ($qr_code == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">QR Code</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="barcode" name="barcode" value="1" <?php echo ($barcode == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Barcode</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="image1" name="image1" value="1" <?php echo ($image1 == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Image1</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="image2" name="image2" value="1" <?php echo ($image2 == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Image2</label>
								</div>
							</div>

							<div class="col-lg-3">
								<div>
									<input type="checkbox" class="" id="image3" name="image3" value="1" <?php echo ($image3 == 1) ? 'checked' : ''; ?> style="height: 17px; width: 17px;">
									<label class="control-label">Image3</label>
								</div>
							</div>
						</div>

						<!-- /.row -->
                    </div><!-- /.box-body -->
                    <div class="card-footer text-center">
                        <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=""
                            class="btn btn-primary"><?php echo $button_value;?>
                            <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">
$('#form1').submit(function(event) {
    var inputValue = $.trim($("#product_template_name").val());
    $("#product_template_name").val(inputValue);
    if (inputValue === "") {
        var SpaceMsg = "Field Cannot be Accept Spaces!";
        $("#msg_print").html(SpaceMsg);
        event.preventDefault();
    } else {
        $("#submit").attr("disabled", true);
        $('#page_load_div').show();
    }
});
</script>