<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");  

if (!isset($_SESSION["level"]) || $_SESSION["level"] > 5) {
    sleep(1);
    header("Location:" . $WebSite . "login/login_form.php"); 
    exit;
}   

include $_SERVER['DOCUMENT_ROOT'] . '/load_header.php';

// 첫 화면 표시 문구
$title_message = '차량 관리 목록'; 
?>

<link href="css/style.css" rel="stylesheet">   
<title> <?=$title_message?> </title>

</head>
<body>		 

<?php
// 세무사아이디면 다른 메뉴 연결
if($_SESSION["userid"] == '0266771300') {
	include $_SERVER['DOCUMENT_ROOT'] . '/myheader_accountant.php'; 
}
else {
	include $_SERVER['DOCUMENT_ROOT'] . '/myheader.php'; 
}
?>   

<?php

$header = isset($_REQUEST['header']) ? $_REQUEST['header'] : '';  

function checkNull($strtmp) {
    return !($strtmp === null || trim($strtmp) === '');
}

$search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';  
$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';

$tablename = 'car'; // 'car' 테이블 사용

require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
$pdo = db_connect();
 
$order_by = "ORDER BY manufacturing_date ASC, purchase_date ASC ";
	
if (checkNull($search)) {
	$sql = "SELECT * FROM ".$DB.".".$tablename." 
        WHERE searchtag LIKE '%$search%' AND is_deleted IS NULL " . $order_by;	
} else {
	$sql = "SELECT * FROM ".$DB.".".$tablename." WHERE is_deleted IS NULL " . $order_by;
}

try {  	  	  
	$stmh = $pdo->query($sql); 
	$total_row = $stmh->rowCount();			  
?>	

<form id="board_form" name="board_form" method="post" enctype="multipart/form-data">			 
	<input type="hidden" id="num" name="num">             	
	<input type="hidden" id="mode" name="mode" value="<?=$mode?>">             	
	<input type="hidden" id="tablename" name="tablename" value="<?= isset($tablename) ? $tablename : '' ?>">				

<div class="container-fluid">
<div class="card justify-content-center text-center mt-5">

	<div class="card-header">
		<div class="d-flex justify-content-center align-items-center">  
			<h5>  <?=$title_message?> </h5> 
			<button type="button" class="btn btn-dark btn-sm mx-2"  onclick='location.reload();' > <i class="bi bi-arrow-clockwise"></i> </button>  	   		  				
		</div>							
	</div>
	<div class="card-body">								
		<div class="d-flex justify-content-center text-center align-items-center mb-2">										 
			▷ <?= $total_row ?> &nbsp; 
			<div class="inputWrap30">			
				<input type="text" id="search" class="form-control" style="width:150px;" name="search" value="<?=$search?>" onKeyPress="if (event.keyCode==13){ enter(); }">
				<button class="btnClear"></button>
			</div>							
			&nbsp;&nbsp;
			<button class="btn btn-outline-dark btn-sm" type="button" id="searchBtn"> <i class="bi bi-search"></i> </button> &nbsp;&nbsp;&nbsp;&nbsp;			
			<button id="newBtn" type="button" class="btn btn-dark btn-sm me-2"> <i class="bi bi-pencil-square"></i> 신규 </button>				
		</div>		
		
	<div class="table-responsive">	
		<table class="table table-hover" id="myTable">		 			
		<thead class="table-primary">
			<th class="text-center w50px">번호</th>
			<th class="text-center w100px">차량번호</th>			
			<th class="text-center w50px">담당자</th>					
			<th class="text-center w120px">보험사</th>
			<th class="text-center w100px">보험사 연락처</th> <!-- 추가: 보험사 연락처 -->
			<th class="text-center w100px">최초등록일</th>
			<th class="text-center w100px">구매일자</th>
			<th class="text-center w80px">구매 유형</th> <!-- 추가: 구매 유형 -->
			<th class="text-center w150px">엔진오일 교환 주기</th> <!-- 추가: 엔진오일 교환 주기 -->   			
			<th class="text-center w150px">엔진오일교환일</th>   
			<th class="text-center w150px">정비 정보</th>					
			<th class="text-center w200px">비고</th>
		</thead>
		<tbody>		      	 
		<?php  		
		$start_num = $total_row;  			    
		while($row = $stmh->fetch(PDO::FETCH_ASSOC)) {
			// 엔진오일 교환일 JSON 디코딩
			$engine_oil_data = json_decode($row['engine_oil_change_data'], true);
			$maintenance_data = json_decode($row['maintenance_data'], true);
		?>					 
		<tr onclick="redirectToView('<?=$row['num']?>')">  
			<td class="text-center"><?= $start_num ?></td>
			<td class="text-center text-primary fw-bold"><?= htmlspecialchars($row['vehicle_number'], ENT_QUOTES, 'UTF-8') ?>
				<?= htmlspecialchars($row['vehicle_type'], ENT_QUOTES, 'UTF-8') ?></td>
			<td class="text-center fw-bold"><?= htmlspecialchars($row['responsible_person'], ENT_QUOTES, 'UTF-8') ?></td>
			<td class="text-center"><?= htmlspecialchars($row['insurance'], ENT_QUOTES, 'UTF-8') ?></td>
			<td class="text-center"><?= htmlspecialchars($row['insurance_contact'], ENT_QUOTES, 'UTF-8') ?></td> <!-- 추가: 보험사 연락처 -->
			<td class="text-center"><?= $row['manufacturing_date'] ?></td>
			<td class="text-center"><?= $row['purchase_date'] ?></td>
			<td class="text-center">
				<?php
				if (isset($row['purchase_type'])) {
					switch ($row['purchase_type']) {
						case 'lease':
							echo '리스';
							break;
						case 'rent':
							echo '렌트';
							break;
						case 'company_own':
							echo '회사 소유';
							break;
						default:
							echo '정보 없음';
							break;
					}
				} else {
					echo '정보 없음';
				}
				?>
			</td>

			<td class="text-center"><?= htmlspecialchars($row['engine_oil_change_cycle'], ENT_QUOTES, 'UTF-8') ?></td> <!-- 추가: 엔진오일 교환 주기 -->

			<td class="text-center">
			<?php 
				if (!empty($engine_oil_data) && is_array($engine_oil_data)) {
					echo '<ul>';
					foreach ($engine_oil_data as $oil) {
						if (!empty($oil['engine_oil_change_date']) || !empty($oil['mileage'])) {
							echo '<li>';
							echo '' . htmlspecialchars($oil['engine_oil_change_date'], ENT_QUOTES, 'UTF-8') . ', ';
							echo '주행거리 : ' . htmlspecialchars($oil['mileage'], ENT_QUOTES, 'UTF-8') . ' km';
							echo '</li>';
						}
					}
					echo '</ul>';
				} else {
					echo '정보 없음';
				}
			?>
			</td>
			<!-- 정비 정보 표시 -->
			<td class="text-center">
			<?php 
				if (!empty($maintenance_data) && is_array($maintenance_data)) {
					echo '<ul>';
					foreach ($maintenance_data as $maintenance) {
						if (!empty($maintenance['maintenance_date']) && !empty($maintenance['maintenance_record'])) {
							echo '<li>' . htmlspecialchars($maintenance['maintenance_date'], ENT_QUOTES, 'UTF-8') . ': ' 
								. htmlspecialchars($maintenance['maintenance_record'], ENT_QUOTES, 'UTF-8') . '</li>';
						}
					}
					echo '</ul>';
				} else {
					echo '정보 없음';
				}
			?>
			</td>
			
			<td class="text-center"><?= htmlspecialchars($row['note'], ENT_QUOTES, 'UTF-8') ?></td>
		</tr>
		<?php
			$start_num--;
		} 
		} catch (PDOException $Exception) {
			print "오류: ".$Exception->getMessage();
		}  
		?>		 
		</tbody>			
			
		</table>

		</div>
	</div>
</div>		
</form>

<!-- 페이지 로딩 -->
<script>
$(document).ready(function(){	
	var loader = document.getElementById('loadingOverlay');
	if(loader)
		loader.style.display = 'none';
});

var ajaxRequest_write = null;
var dataTable;

$(document).ready(function() {			
	dataTable = $('#myTable').DataTable({
		"paging": true,
		"ordering": true,
		"searching": true,
		"pageLength": 50,
		"lengthMenu": [25, 50, 100, 200, 500, 1000],
		"language": {
			"lengthMenu": "Show _MENU_ entries",
			"search": "Live Search:"
		},
		"order": []
	});

	var savedPageNumber = getCookie('carpageNumber');
	if (savedPageNumber) {
		dataTable.page(parseInt(savedPageNumber) - 1).draw(false);
	}

	dataTable.on('page.dt', function() {
		var carpageNumber = dataTable.page.info().page + 1;
		setCookie('carpageNumber', carpageNumber, 10);
	});

	$('#myTable_length select').on('change', function() {
		var selectedValue = $(this).val();
		dataTable.page.len(selectedValue).draw();
		savedPageNumber = getCookie('carpageNumber');
		if (savedPageNumber) {
			dataTable.page(parseInt(savedPageNumber) - 1).draw(false);
		}
	});
});

function restorePageNumber() {
	var savedPageNumber = getCookie('carpageNumber');
	location.reload(true);
}

function enter() {
	$("#board_form").submit();	       		
}

$(document).keydown(function(e){
	var code = e.keyCode || e.which;
	if (code == 27) { 
		self.close();
	}
});

function redirectToView(num) {
	var title = '<?php echo $title_message; ?>';
	var tablename = $("#tablename").val();
	popupCenter('write_form.php?mode=modify&num=' + num + '&tablename=' + tablename,  title , 1100, 900);    
}

$("#searchBtn").on("click", function() {
	$("#board_form").submit();
});	

$("#newBtn").on("click", function() {		
	var title = '<?php echo $title_message; ?>';
	var tablename = $("#tablename").val();
	popupCenter('write_form.php?tablename=' + tablename,  title +'신규등록' , 1100, 900);   	
	
});

$("#closeBtn").on("click", function() {
	self.close();
});	
		
function reloadlist() {
	$("#board_form").submit();				
}	

$(document).ready(function(){    
   // 방문기록 남김
   var title = '<?php echo $title_message; ?>';
   saveMenuLog(title);
});
</script>

</body>
</html>