<?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 = '감리업체 주소록'; 
 ?>
 
<title> <?=$title_message?> </title>

</head>

<body>	
	 
<?php

$option = isset($_REQUEST['option']) ? $_REQUEST['option'] : '';  
// 담당자 추가하면 option=add
$search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';  
$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';  
$header = isset($_REQUEST['header']) ? $_REQUEST['header'] : '';  
  
$tablename = 'phonebook_SU';
  	  
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
$pdo = db_connect();
    
$num = isset($_REQUEST["num"]) ? $_REQUEST["num"] : '';

// 수정일 경우
if($num>0)
{	     
	try{
	 $sql = "select * from ". $DB . "." . $tablename . " where num=?";
	 $stmh = $pdo->prepare($sql);  
	 $stmh->bindValue(1, $num, PDO::PARAM_STR);      
	 $stmh->execute();            
	  
	 $row = $stmh->fetch(PDO::FETCH_ASSOC); 	 
		include '_row.php';

	 }catch (PDOException $Exception) {
	   print "오류: ".$Exception->getMessage();
	 }      				 

	 $mode = 'update';
		 
}
else
{
	include '_request.php';
	
	$mode = 'insert';
	$representative_name = $search;
	$manager_name = $search;
	$phone = '';	
	$represent = '대표코드';  // 대표코드
	$registdate = date('Y-m-d');  // 현재일자 기록
} 
?>	


<form id="board_form" name="board_form" method="post" enctype="multipart/form-data">			 

<input type="hidden" id="mode" name="mode" value="<?= isset($mode) ? $mode : '' ?>">             
<input type="hidden" id="num" name="num" value="<?= isset($num) ? $num : '' ?>"> 	
<input type="hidden" id="tablename" name="tablename" value="<?= isset($tablename) ? $tablename : '' ?>"> 	
<input type="hidden" id="update_log" name="update_log" value="<?= isset($update_log) ? $update_log : '' ?>"> 		
<input type="hidden" id="parentnum" name="parentnum" value="<?= isset($parentnum) ? $parentnum : '' ?>"> 	
<input type="hidden" id="header" name="header" value="<?= isset($header) ? $header : '' ?>"> 		
<input type="hidden" id="represent" name="represent" value="<?= isset($represent) ? $represent : '' ?>"> 		
<input type="hidden" id="registdate" name="registdate" value="<?= isset($registdate) ? $registdate : '' ?>"> 	
	


<div class="container-fluid" >					
	<div class="d-flex align-items-center justify-content-center" >
	<div class="card justify-content-center" >
		<div class="card-header text-center">
			<span class="text-center fs-5" > <?=$title_message?>  </span>								
		</div>
	<div class="card-body">                                
		<div class="row justify-content-center text-center">  		 
			<div class="d-flex align-items-center justify-content-center m-2">
				<table class="table table-bordered" >
					<tbody>
						<tr>
							<td class="text-center fw-bold" style="width:150px;" >감리업체 명</td>
							<td class="text-center"> 
									<input type="text" class="form-control" id="vendor_name" name="vendor_name" style="width:250px;" value="<?=$vendor_name?>">    
							</td>				
							<td class="text-center fw-bold" style="width:170px;" >대표자 성함</td>
							<td class="text-center fw-bold" >
								<input type="text" class="form-control" id="representative_name" name="representative_name"  style="width:200px;" value="<?=$representative_name?>">  
							</td>
						</tr>														
						<tr>
							<td class="text-center fw-bold">주소</td>
							<td colspan="3" class="text-center"> 
								<input type="text" class="form-control text-start" id="address" name="address" autocomplete="off" value="<?=$address?>">   							
						</tr>
						<tr>				
							<td  class="text-center fw-bold">전화</td>
							<td class="text-center"> 
								<input type="text" class="form-control" id="phone" name="phone" autocomplete="off" value="<?=$phone?>">  
							</td>							
							<td class="text-center fw-bold" > 모바일 </td>	
							<td class="text-center"> 
								<input type="text" class="form-control" id="mobile" name="mobile" autocomplete="off" value="<?=$mobile?>"> 
							</td>																					
						</tr>
						<tr>				
							<td  class="text-center fw-bold">이메일</td>
							<td class="text-center"> 
								<input type="email" class="form-control" id="email" name="email" autocomplete="off" value="<?=$email?>">       
							</td>							
							<td class="text-center fw-bold" > 팩스 </td>	
							<td class="text-center"> 
								<input type="text" class="form-control" id="fax" name="fax" autocomplete="off" value="<?=$fax?>">    
							</td>																					
						</tr>
						<tr>				
							<td  class="text-center fw-bold">담당자명</td>
							<td class="text-center"> 
								<input type="text" class="form-control" id="manager_name" name="manager_name" autocomplete="off" value="<?=$manager_name?>">       
							</td>							
							<td class="text-center fw-bold" > 담당자Tel </td>	
							<td class="text-center"> 
								<input type="text" class="form-control" id="contact_info" name="contact_info" autocomplete="off" value="<?=$contact_info?>">      
							</td>																					
						</tr>
							
						<tr>
							<td class="text-center fw-bold">비고</td>
							<td colspan="3" class="text-center"> 
								<textarea class="form-control" id="note" name="note" autocomplete="off" ><?=$note?></textarea>    							
						</tr>						
						
					</tbody>
				</table>   
			</div>		 
	 		
	</div>

	<div class="d-flex justify-content-center">

		<button type="button" id="saveBtn"  class="btn btn-dark btn-sm me-1">
			<i class="bi bi-floppy-fill"></i> 저장
		</button>	
		<?php if(intval($num) > 0 ) : ?>
			<button id="deleteBtn" class="btn btn-danger btn-sm ms-1 me-1" type="button" > <i class="bi bi-trash"></i>  삭제  </button>	
		<?php endif; ?>				
		<button type="button"  id="closeBtn" class="btn btn-outline-dark btn-sm ms-1 me-1">
			&times; 닫기
		</button>
		</div>
	</div>
		</div>
	</div>
</div>


</form>
</body>
</html>

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

ajaxRequest_write = null;
/* ESC 키 누를시 팝업 닫기 */
// $(document).keydown(function(e){
		// //keyCode 구 브라우저, which 현재 브라우저
		// var code = e.keyCode || e.which;
		
		// if (code == 27) { // 27은 ESC 키번호
			// self.close();
		// }
// });

$(document).ready(function(){	  
	 
	// 창닫기 버튼
	$("#closeBtn").on("click", function() {
		self.close();
	});	
	
// 저장 버튼 서버에 저장하고 Ecount 전송함
$("#saveBtn").on("click", function() {
	
	var header = $("#header").val();
			
	let msg = '저장완료';
	
		if (ajaxRequest_write !== null) {
			ajaxRequest_write.abort();
		}		 
		ajaxRequest_write = $.ajax({
			url: "process.php",
			type: "post",		
			data: $("#board_form").serialize(),								
			success : function( data ){		

		    console.log(data);			
														
			 Toastify({
					text: msg ,
					duration: 3000,
					close:true,
					gravity:"top",
					position: "center",
					backgroundColor: "#4fbe87",
				}).showToast();			
				
					$(opener.location).attr("href", "javascript:reloadlist();");	

				setTimeout(function() {
					  // 창 닫기
					   self.close();								   
				   }, 500);				

			},
			error : function( jqxhr , status , error ){
				console.log( jqxhr , status , error );
				   } 			      		
		});												

	});			
	

// 삭제버튼
$("#deleteBtn").on("click", function() {	
		// DATA 삭제버튼 클릭시
			Swal.fire({ 
				   title: '해당 DATA 삭제', 
				   text: " DATA 삭제는 신중하셔야 합니다. '\n 정말 삭제 하시겠습니까?", 
				   icon: 'warning', 
				   showCancelButton: true, 
				   confirmButtonColor: '#3085d6', 
				   cancelButtonColor: '#d33', 
				   confirmButtonText: '삭제', 
				   cancelButtonText: '취소' })
				   .then((result) => { if (result.isConfirmed) { 
				   
						$("#mode").val("delete");	

						// console.log('mode 전송전 :' , $("#mode").val() );	
													
						if (ajaxRequest_write !== null) {
							ajaxRequest_write.abort();
						}		 
						ajaxRequest_write = $.ajax({
							url: "process.php",
							type: "post",		
							data: $("#board_form").serialize(),								
							success : function(data){	
								console.log(data);	
								ajaxRequest_write = null;
								 Toastify({
										text: "파일 삭제 완료!",
										duration: 3000,
										close:true,
										gravity:"top",
										position: "center",
										backgroundColor: "#4fbe87",
									}).showToast();		
										
								  setTimeout(function() {
											opener.location.reload();	
											window.close();
								   }, 1000);										 
												
								},
								error : function( jqxhr , status , error ){
									console.log( jqxhr , status , error );
								} 			      		
							   });												
				   }
				   });	
	});	
	
});	  // end of ready
	
			
	
</script>