<?php 
//die('1233');
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=add_station_det";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "station_detail_table";
$id	 = "";
$vat = "";
$query_string = "";
$page_name      = "Add Station Detail";
$from = $_REQUEST['from'];
if($from == "view_station_details"){
	if(isset($_REQUEST['station_id'])){
		$station_id = $_REQUEST['station_id'];
	}
} 
 $machine_type = utility::getTableFieldById("station_master_table","machine_type","id",$station_id); 
############################### START INSERT ######################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string"));
    $id 			    	= $_REQUEST['id']; 
	$station_id 			= $_REQUEST['station_id'];
	$resource_code 	= $_REQUEST['resource_code']; 
	$resource_description 	= $_REQUEST['resource_description']; 	
	$resource_group_code 	= $_REQUEST['resource_group_code'];
	$no_of_table       = $_REQUEST['no_of_table'];
    $machine_type       = $_REQUEST['machine_type'];
	//DIE;
	$resource_group_description 	= $_REQUEST['resource_group_description']; 
	$hide 	= isset($_REQUEST['hide']) ? 1 : 0; // Added hide field

    $created_by			= $_SESSION['user_id'];
	$created_on			= date("Y-m-d H:i:s");	
	$updated_by			= $_SESSION['user_id'];

	$sql = "station_id = '".$station_id."',resource_code = '".$resource_code."',machine_type = '".$machine_type."',resource_description = '".$resource_description."',resource_group_code = '".$resource_group_code."',resource_group_description = '".$resource_group_description."',no_of_table  = '".$no_of_table ."',hide = '".$hide."',created_on = '".$created_on."', created_by = '".$created_by."', updated_by = '".$updated_by."'";

	$sql=utility::convert_to_crypt_data($sql,$table_name);
	
	$sql= "INSERT INTO $table_name SET ".$sql;
	
	mysqli_query($conn,$sql);
	$station_detail_id = mysqli_insert_id($conn);
	
	if ($no_of_table > 0) {
		for ($i = 1; $i <= $no_of_table; $i++) {
			$virtual_id = $station_detail_id . $i; // concatenate machine_id with table number

			$map_sql = "INSERT INTO pu_mapping_tables (machine_id, no_of_table, virtual_id) 
						VALUES ($station_detail_id, $i, $virtual_id)";
			
			if (!mysqli_query($conn, $map_sql)) {
				echo "Error inserting no_of_table = $i: " . mysqli_error($conn);
			}
		}
	}
	
	$_SESSION['info_message'] = "Recourse Detail added successfully";
	if(isset($from) && $from == "view_station_details"){
	   header("Location: index?page=add_station_det&from=view_station_details&hide_top=1&hide_left=1");
       exit();
	}
	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 Station Detail";
	$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);
	
	$station_id 	        = $row_edit['station_id']; 
    $resource_code 	= $row_edit['resource_code'];
	$resource_group_code 	= $row_edit['resource_group_code']; 
	$resource_description 	= $row_edit['resource_description'];
	$resource_group_description 	= $row_edit['resource_group_description']; 
	$no_of_table 	= $row_edit['no_of_table']; 	
	$machine_type 	= $row_edit['machine_type'];  
	$hide 	= $row_edit['hide']; // Added hide field
    
}
############################ END EDIT ######################################### 


########################## START UPDATE ######################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string"));
	$id						= $_REQUEST['id'];
	
	
	$station_id 	        = $_REQUEST['station_id']; 
    $resource_code 	= $_REQUEST['resource_code']; 
	$resource_description 	= $_REQUEST['resource_description']; 
	$resource_group_code 	= $_REQUEST['resource_group_code']; 
	$machine_type 	= $_REQUEST['machine_type']; 
	$resource_group_description       	= $_REQUEST['resource_group_description']; 
	$no_of_table       	= $_REQUEST['no_of_table']; 
	$hide 	= isset($_REQUEST['hide']) ? 1 : 0; // Added hide field
	$updated_by			= $_SESSION['user_id'];
	 $sql = "station_id = '".$station_id."',resource_code = '".$resource_code."',machine_type = '".$machine_type."',resource_description = '".$resource_description."',resource_group_code = '".$resource_group_code."',resource_group_description = '".$resource_group_description."', no_of_table  = '".$no_of_table ."', hide = '".$hide."', 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);
	if ($no_of_table > 0) {
		$machine_id = $id;

		$delete_sql = "DELETE FROM pu_mapping_tables WHERE machine_id = $machine_id";
		mysqli_query($conn, $delete_sql);

		for ($i = 1; $i <= $no_of_table; $i++) {
			$virtual_id = $machine_id . $i; 
			$map_sql = "INSERT INTO pu_mapping_tables (machine_id, no_of_table, virtual_id) 
						VALUES ($machine_id, $i, $virtual_id)";
			if (!mysqli_query($conn, $map_sql)) {
				echo "Error inserting no_of_table = $i: " . mysqli_error($conn);
			}
		}
	}
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  
	$query_string = "&".$query_string;
	
	$station_detail_id = urlencode(utility::app_crypt($id,'e'));
	
	$_SESSION['info_message'] = "Recourse Detail updated successfully";
	if(isset($from) && $from == "view_station_details"){
	   header("Location: index?page=add_station_det&from=view_station_details&station_detail_id=$station_detail_id&id=$id &hide_top=1&hide_left=1");
       exit();
	}
	
    header("Location: $file_name$query_string");
    exit();
}
#################### END UPDATE ######################### 
?>
<style type="text/css">
<? if(isset($from) && $from == "view_station_details"){?>
.content-wrapper{
	margin-left: 0 !important;
}
.content {
    padding: 10px !important;
}
.card-footer {
    display: block !important;
    background-color: #f4f6f9;
    padding: 10px;
}
<?}?>
em{color:red;font-size: 20px;position: absolute;}
</style>


<? if($from != "view_station_details"){?>
<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_enquiry_progress"){
				$query_string = str_ireplace("page=add", "page=view_enquiry_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-success" onclick="window.location.href = 'index?<?=$query_string;?>'">Back</button>
	  </div>
	</div>
  </div><!-- /.container-fluid -->
</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']);?>
				<? if($from == "view_station_details"){?>
				<script type="text/javascript">
				$(document).ready(function(){
					setTimeout(function(){
						window.parent.jQuery("#myModal .close_button").click();
					},1000);
				});
				</script>
				<? } ?>
			  </div>
			  <? } ?>
			<!----------------------- End Message ------------------------------->
			<!-- general form elements -->
              <div class="card">
                <form method="post" name="form1" id="form1" action="index?page=add_station_det&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="row">
						
					
					<div class="form-group col-sm-4">
                      <label for="Resource Code" class="control-label">Resource Code<em>*</em></label>
                      <div class="">
                        <input type="text" id="resource_code" name="resource_code" value="<?php echo $resource_code;?>" class="form-control" placeholder="Resource Code" required>
                      </div>
                    </div>
					<div class="form-group col-sm-4">
                      <label for="Resource Description" class="control-label">Resource Description<em>*</em></label>
                      <div class="">
                        <input type="text" id="resource_description" name="resource_description" value="<?php echo $resource_description;?>" class="form-control" placeholder="Resource Description" required>
                      </div>
                    </div>
					<div class="form-group col-lg-4">
					  <label for="Machine Type" class="control-label">Machine Type<em>*</em></label>
					  <div class="">
					  <?php echo  $machinetype = $gsi->getmachineTypeList("machine_type", $machine_type, "100%", false, "form-control select2", "required"); ?>
					  </div>
					</div>
					</div>
                     <div class="row">
					
					<div class="form-group col-sm-4">
                      <label for="Resource Group Code" class="control-label">Resource Group Description<em>*</em></label>
                      <div class="">
                        <input type="text" id="resource_group_description" name="resource_group_description" value="<?php echo $resource_group_description;?>" class="form-control" placeholder="Resource Group Description" required>
                      </div>
                    </div>
					<div class="form-group col-sm-4">
                      <label for="Resource Group Code" class="control-label">Resource Group Code<em>*</em></label>
                      <div class="">
                        <input type="text" id="resource_group_code" name="resource_group_code" value="<?php echo $resource_group_code;?>" class="form-control" placeholder="Resource Group Code" required>
                      </div>
                    </div>
					<? $display_div = ($machine_type == 4) ? "block" : "none"; ?>
					<div class="col-lg-4" id="otherCustomerFields" style="display:<?=$display_div;?>;">
						<div class="form-group">
						  <label for="No of Table" class="control-label">No of Table<em>*</em></label>
						  <div class="">
							<input type="number" id="no_of_table" name="no_of_table" value="<?php echo $no_of_table;?>" class="form-control" placeholder="No of Table"  step="1">
						  </div>
						</div>
				    </div>
					<div class="form-group col-lg-4">
					<label class="control-label d-block">Hide From Gantt Chart</label>
					<div class="form-check">
						<input type="checkbox" id="hide" name="hide" value="1" class="form-check-input" <?php echo ($hide == 1) ? 'checked' : ''; ?>
						>&nbsp;&nbsp;
						<label class="form-check-label" for="hide">
							Yes
						</label>
					</div>
				</div>

					</div>
					
					</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="from" name="from" value="<?php echo $from;?>">
					<input type="hidden" id="station_id" name="station_id" value="<?php echo $station_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">
$(function(){
	$('#form1').submit(function(){
		//$(this).addClass('disabled');
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
	});

});
</script>