<?php
session_start();
$where 					= " WHERE 1=1 ";
$next_page_condition 	= "";
$paging ="";

if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
}
if(isset($_REQUEST["from"]) && $_REQUEST["from"] !=""){
	$_SESSION["from"] = $_REQUEST["from"];
}
if(isset($_REQUEST['id']) && $_REQUEST['id'] !=""){
	$where .= " AND id = ".$_REQUEST['id']." ";
}
$station_id = utility::app_crypt($_REQUEST['station_id'],'d');
if($_REQUEST['id'] != $station_id){
	die("Invaild URL");
}
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "station_master_table";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ";
$rs = mysqli_query($conn,$query);
$station_row = mysqli_fetch_assoc($rs);
$station_row=utility::validate_crypt_data($station_row);
?>
<style>
	.form-group .control-label {
		padding-top: 0px;
	}

	.content {
		min-height: 0px;
	}

	.content-header h1 {
		float: left;
		padding-bottom: 10px;
	}
</style>
<section class="content-header">
	<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12 col-md-6 col-sm-6">
				<div class="col-sm-10">
					<h4 style="float:left;clear:right;">
						Resource Station Details - <span style='color:#ed5c5c;'><?= $station_row['station'];?></span>
					</h4>
				</div>
				<?
				$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
				$query_string = str_ireplace("page=view_station_details", "page=view", $query_string);
			?>
				<button type="button" id="back" name="back" value="back" style="float:right;margin-bottom: 15px;"
				class="btn btn-primary" onclick="window.location.href = 'index?<?=$query_string.$next_page_condition.$paging;?>'">Back</button>

				<? if((isset($_REQUEST["from"]) && $_REQUEST["from"] == "view") || (isset($_REQUEST["from"]) && $_REQUEST['from'] == "view_station_details")){ 
			  ?>
				<a href="index?page=add&from=view_station_details&action=edit&station_id=<?=urlencode(utility::app_crypt($_REQUEST['id'],'e'));?>&id=<?=$_REQUEST['id'].$next_page_condition;?>" style="float:right;margin-right:5px;" class="btn btn-primary">Edit</a>
				<? } ?>
			</div>
		</div>
	</div>
</section>
<section class="content">
	<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12 no-padding">
				<div class="row">
					<!----------------------- Message ------------------------------->
					<? if(isset($_SESSION['info_message'])){ ?>
					<div class="col-sm-12 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>
					<? } ?>
					<!----------------------- End Message ------------------------------->
					<!-- general form elements -->
					<? 
						$col_width = 12;
					?>
					<div class="col-lg-12 col-md-12 col-sm-12 col-xs-<?=$col_width;?>">
						<div class="box box-primary card">
							<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="box-body">
									<div class="row">
										<div class="col-lg-12 col-sm-12">
										    <div class="row">
											<div class="form-group col-lg-6">
												<label for="Resource Station" class="col-sm-4 control-label">Resource Station</label>
												<div class="col-sm-8">
													<? echo ucfirst($station_row['station']); ?>
												</div>
											</div>
											<div class="form-group col-lg-6">
												<label for="Facility Rate OEM" class="col-sm-4 control-label">Facility
													Rate OEM</label>
												<div class="col-sm-8">
													<? echo ucfirst($station_row['facility_rate_oem']); ?>
												</div>
											</div>
											</div>
											<div class="row">
											<div class="form-group col-lg-6">
												<label for="Labour Rate OEM" class="col-sm-4 control-label">Labour Rate
													OEM</label>
												<div class="col-sm-8">
													<? echo ucfirst($station_row['labour_rate_oem']); ?>
												</div>
											</div>
											<div class="form-group col-lg-6">
												<label for="Has Set Time" class="col-sm-4 control-label">Has Set Time</label>
												<div class="col-sm-8">
													<? echo  $gsi->getYesNoById($station_row['has_set_time']); ?>
												</div>
											</div>
											</div>
											<div class="row">
											<div class="form-group col-lg-6">
												<label for="Has Operation Time" class="col-sm-4 control-label">Has
													Operation Time</label>
												<div class="col-sm-8">
													<? echo  $gsi->getYesNoById($station_row['has_operation_time']); ?>
												</div>
											</div>
											<div class="form-group col-lg-6 hide">
												<label for="Hrs Week" class="col-sm-4 control-label">Hrs Week</label>
												<div class="col-sm-8">
													<?php echo $station_row["hrs_week"]; ?>
												</div>
											</div>
											</div>
										</div>

									</div>

								</div><!-- /.box-body -->
							</form>
						</div><!-- /.box -->
					</div><!-- /.box -->
				</div>
			</div>
		</div>
	</div>
</section>
<section class="content">
	<div class="container-fluid">
		<div class="row">
			<div class="col-12">
				<div id="" class="card">
					<div class="card-header">
						<h3 class="card-title" style="color: #9c4893; font-size: 24px;">Recourse Detail</h3>
						<div class="card-tools">
							<div class="input-group input-group-sm" style="">
								<a href="" data-toggle="modal" data-target="#myModal"
									class="btn btn-primary add_new_station" alt="<?=$_REQUEST['id'];?>"
									data-station_name="<?=ucwords($station_row['station']);?>"
									style="float:right;margin-right:35px;margin-bottom:5px;">Add Recourse Detail</a>
							</div>
						</div>
					</div>
					<?
					$query = "SELECT * FROM station_detail_table WHERE station_id = '$station_id' ";
					$sql=mysqli_query($conn,$query);
					$result=mysqli_num_rows($sql);
					?>
					<div class="card-body table-responsive" style="height: auto;">
						<? if($result>0){ ?>
						<div class="box box-primary" id="overflow_div">
							<!-- Start  records header -->
							<table id="example1" class="table table-bordered table-striped">
								<thead>
									<tr>
										<th>Resource Code</th>
										<th>Resource Description</th>
										<th>Resource Group Code</th>
										<th>Resource Group Description</th>	
										<th>Machine Tab</th>
										<th>Edit</th>
										<th>Delete</th>
									</tr>
								</thead>
								<tbody>
									<!-- End records header -->
									<?
								while($row = mysqli_fetch_array($sql)){
							?>
									<!-- Start data -->
									<tr id="station_record_no_<?=$row['id'];?>">
										<td><?php echo $row['resource_code'];?></td>
										<td><?php echo $row['resource_description'];?></td>
										<td><?php echo $row['resource_group_code'];?></td>
										<td><?php echo $row['resource_group_description'];?></td>
										<td>
										<?php 
										$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') 
													? "https://" 
													: "http://";

										 $url = $protocol . $_SERVER['HTTP_HOST'] . "/thermotec/machine_tab/index.php?l=2&code=" . urlencode($row['resource_code']);
										?>
											<a href="<?php echo $url; ?>" style="color: blue;">
												<?php echo htmlspecialchars($row['resource_code']); ?>
											</a>
										</td>
										<td>
											<a href="" data-toggle="modal" data-target="#myModal" class="edit_station"
												alt="<?=$row['id'];?>" data-station_id="<?=$row['station_id'];?>"
												data-station_name="<?=ucwords($station_row['station']);?>"><i
													class="fa fa-fw fa-edit text-primary"></i></a>
										</td>
										<td>
											<span href="" class="station_detail_delete" alt="<?=$row['id'];?>"
												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_station_detail_load_<?=$row['id'];?>"></i>
											<i class="fa fa-check" id="delete_station_detail_done_<?=$row['id'];?>"
												style="color:#bad23a;font-size: 20px;display:none;"></i>
										</td>
									</tr>
									<!-- End data -->
									<? } ?>
								</tbody>
							</table>
						</div>
						<? }else{ ?>
						<div class="box box-primary p-15">No Recourse Detail found...</div>
						<? } ?>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>
<style>
	#overflow_div::-webkit-scrollbar
    {
      width: 5px;  /* for vertical scrollbars */
      height: 5px; /* for horizontal scrollbars */
    }
 
    #overflow_div::-webkit-scrollbar-track
    {
      background:#E7E8E9;
    }
 
    #overflow_div::-webkit-scrollbar-thumb
    {
      background: #3c8dbc;
    }
	#overflow_div{
		height:200px;
		overflow-y: auto;
	}
	@media print{
		body{
			visibility: hidden;
		  }
		  #overflow_div{
			height:auto;
		  }
		  .printable{
			visibility: visible;
		  }
		  .printable{
			position: absolute;
			left: 0;
			top: 0;
		  }
	}
</style>
<script type="text/javascript">
$(function () {  
		$(".add_new_station").click(function(){	
			var station_name = $(this).attr("data-station_name");	
			$("#myModalLabel").html("Recourse Detail for <span style='color:Tomato'>"+station_name+"</span>");
			var hide_top_left = "&hide_top=1&hide_left=1";
			var station_id = $(this).attr("alt");
			var from = "&from=view_station_details";
			$('.modal-dialog').css({"max-width":"60%"});
			$('.modal-content').css({"height": "auto","max-height": "80vh","overflow-y": "auto"}); // Changed this line
			$(".modal_iframe").addClass("contactpopup").attr("src","<?=$config->frontEndURL?>/admin/station/index?page=add_station_det&station_id="+station_id+from+hide_top_left);
		});

		$(".edit_station").click(function(){
			var station_name = $(this).attr("data-station_name");	
			$("#myModalLabel").html("Recourse Detail for <span style='color:Tomato'>"+station_name+"</span>");
			var hide_top_left = "&hide_top=1&hide_left=1";
			var station_id = $(this).attr("data-station_id");			
			var station_detail_id = $(this).attr("alt");
			$('.modal-dialog').css({"max-width":"60%"});
			$('.modal-content').css({"height": "auto","max-height": "80vh","overflow-y": "auto"}); // Changed this line		
			$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/station/index?page=add_station_det&action=edit&from=view_station_details&station_id="+station_id+"&id="+station_detail_id+hide_top_left);
		});
	

	$(".station_detail_delete").click(function(){	
		if(confirm('Are you sure you want to delete?') == true){
			var station_detail_id = $(this).attr("alt");
			if(station_detail_id != ""){
				$('#delete_station_detail_load_'+station_detail_id).show();
				var form_data   = {'station_detail_id':station_detail_id};       
				$.ajax({
					async: true,
					type: "POST",
					url: site_url+"/station/index?page=ajax_actions&action=delete_station_detail",
					data: form_data,
					success: function(result){    
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$('#delete_station_detail_done_'+station_detail_id).show();
							$('#delete_station_detail_load_'+station_detail_id).hide();
							
							setTimeout(function() {
								$('#delete_station_detail_done_'+station_detail_id).hide();
								$('#station_record_no_'+station_detail_id).remove();
							},1000);
						}
					}
				});
			}
		}else{
			return false;
		}
	});	
	
	$(".close_button").click(function(){
		$(".modal_iframe").attr("src","");
		location.reload();
	});
	
	
});
</script>
