<?php include $_SERVER['DOCUMENT_ROOT'] . '/load_header.php';   ?>
<title> <?=$title_message?> </title>
<link rel="stylesheet" href="css/style.css?v=<?=time()?>">
</head>
<body>	
<?php
$num = isset($_REQUEST['num']) ? $_REQUEST['num'] : '';  
$option = isset($_REQUEST['option']) ? $_REQUEST['option'] : '';   // 견적서와 산출서의 다른점을 표현하는 것
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/estimate/fetch_unitprice.php");
$pdo = db_connect();
	try {
		$sql = "select * from {$DB}.{$tablename} where num = ? ";
		$stmh = $pdo->prepare($sql);
		$stmh->bindValue(1, $num, PDO::PARAM_STR);
		$stmh->execute();
		$count = $stmh->rowCount();
		if ($count < 1) { 
			print "검색결과가 없습니다.<br>";
		} else {
			$row = $stmh->fetch(PDO::FETCH_ASSOC);
			include "_row.php";

			// $korean = number_to_korean($totalprice);
		}
	} catch (PDOException $Exception) {
		print "오류: " . $Exception->getMessage();
	}
	// JSON 문자열을 PHP 배열로 디코딩합니다.
	if($major_category == '스크린')
		$decodedEstimateList = json_decode($estimateList, true);
	else
		$decodedEstimateList = json_decode($estimateSlatList, true);
	

// 디코딩된 데이터가 배열인지 확인합니다.
if (!is_array($decodedEstimateList)) {
    echo "데이터가 정상적이지 않습니다. 확인바랍니다.";
    exit;
}

// detailJson 변수가 제대로 설정되었는지 확인
if (!isset($detailJson) || empty($detailJson)) {
    $detailJson = [];
}

echo '<script>';
echo 'var detailJsonData = ' . json_encode($detailJson) . ';';
echo '</script>';

$shutterboxMsg ='';

?>
  
<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="user_name" name="user_name" value="<?= isset($user_name) ? $user_name : '' ?>">
	<input type="hidden" id="update_log" name="update_log" value="<?= isset($update_log) ? $update_log : null ?>">
	<input type="hidden" id="tablename" name="tablename" value="<?= isset($tablename) ? $tablename : '' ?>">
	<input type="hidden" id="header" name="header" value="<?= isset($header) ? $header : '' ?>">	
	<input type="hidden" id="secondordnum" name="secondordnum" value="<?= isset($secondordnum) ? $secondordnum : '' ?>">	
	<input type="hidden" id="detailJson" name="detailJson"> <!-- 라디오버튼 저장하려면 두개의 변수가 필요하다.  -->	
	<input type="hidden" id="estimateSurang" name="estimateSurang"> <!-- 견적가 수량  -->	
	<input type="hidden" id="estimateTotal" name="estimateTotal"> <!-- 견적가 총액  -->	
	<input type="hidden" id="option" name="option" value="<?= isset($option) ? $option : '' ?>">

<div class="container mt-2">
    <div class="d-flex align-items-center justify-content-end mt-3 m-1 mb-4">
	     <span class="badge bg-secondary me-5 fs-5"> ( <?=$major_category?> ) </span>
        <input type="checkbox" id="showlistCheckbox" <?php echo ($option == 'option') ? '' : 'checked'; ?>>
        <label for="showlistCheckbox" class="me-3">소요자재</label>		

		<input type="checkbox" id="showEstimateCheckbox" <?php echo ($option == 'option') ? '' : 'checked'; ?>>
		<label for="showEstimateCheckbox" class="me-3">산출내역서</label>		

		<input type="checkbox" id="showVendorCheckbox" <?php echo ($option == 'option') ? 'checked' : ''; ?>>
		<label for="showVendorCheckbox" class="me-3">업체발송용</label>
		
		<button type="button" class="btn btn-danger btn-sm mx-1 initialBtn" >  <i class="bi bi-arrow-clockwise"></i>  재계산 </button>
		<button  type="button" class="btn btn-dark btn-sm mx-1 saveBtn" > <i class="bi bi-floppy"></i> 산출내역 저장 </button>		
        <button type="button" class="btn btn-dark btn-sm  mx-1" onclick="generatePDF()"> PDF 저장 </button>
		<button type="button" class="btn btn-dark btn-sm mx-1" onclick="sendmail();"> <i class="bi bi-envelope-arrow-up"></i> 전송 </button> 
        <button type="button" class="btn btn-secondary btn-sm  ms-5" onclick="self.close();"> &times; 닫기 </button>&nbsp;
    </div>
	<br>    
     <div class="d-flex align-items-center justify-content-center mt-2 mb-0">
		 <div class="alert alert-primary mb-2" role="alert">
			검사비는 제주도를 제외하고, 5만원으로 수정, '산출내역 저장'버튼을 누른 후 저장해야 금액이 확정됩니다.
		</div>
	</div>
	<div class="d-flex align-items-center justify-content-end mt-0">		
		<table class="table table-bordered mt-0 text-end w-auto">
		<thead >
			<tr>
				<th colspan="5" class="text-end">
					<span class="text-danger me-5"> 💬 수정금액이 있으면 자동금액보다 수정금액이 우선됨.</span>
					<span class="text-secondary">공급가액 기준, VAT별도</span>	
				</th>
			</tr>
			<tr class="table-secondary">
				<th class="text-center">자동금액</th>
				<th class="text-center">수정 금액</th>
				<th class="text-center">차액</th>								
			</tr>
		</thead>						
			<tbody>
				<tr>
					<td>
						<input type="text" id="EstimateFirstSum" name="EstimateFirstSum" class="form-control text-end" readonly 
							value="<?= isset($EstimateFirstSum) && $EstimateFirstSum != 0 ? number_format($EstimateFirstSum) : $EstimateFirstSum ?>">
					</td>
					<td>
						<input type="text" id="EstimateUpdatetSum" name="EstimateUpdatetSum" class="form-control text-end text-primary" readonly 
							value="<?= isset($EstimateUpdatetSum) && $EstimateUpdatetSum != 0 ? number_format($EstimateUpdatetSum) : $EstimateUpdatetSum ?>">
					</td>
					<td>
						<input type="text" id="EstimateDiffer" name="EstimateDiffer" class="form-control text-end text-danger" readonly 
							value="<?= isset($EstimateDiffer) && $EstimateDiffer != 0 ? number_format($EstimateDiffer) : $EstimateDiffer ?>">
					</td>
				</tr>
			</tbody>
		</table>
	</div>
</div>
</form>

<div id="content-to-print">	
<br>
<div class="container mt-1">
<div class="d-flex align-items-center justify-content-center ">
    <table class="table table-sm" style="border-collapse: collapse;">
        <tbody>
            <tr>
                <td class="text-center fw-bold" style="width:8%;" >작성일자</td>                
                <td rowspan="2"  class="text-center align-middle fw-bold fs-4" style="width:60%; border-top:none; border-bottom:none;" > 
				<?php 
				if($option!=='option')
					echo '<span class="badge bg-primary"> ' . $title_message . ' </span>  </td>'; // <span id="updateText" class="text-danger"> </span> 
                   else
					echo '<span class="text-dark"> ' . $title_message_sub . ' </span> </td>';
				?>
								
                <td class="text-center fw-bold"  style="width:10%;" >견적번호</td>
				</tr>
				<tr>
				<td class="text-center" >  <?=$indate?> </td>				
                <td class="text-center fw-bold text-primary" >  <?=$pjnum?>  </td>
            </tr>            
        </tbody>
    </table>   
</div>


<div class="d-flex align-items-center justify-content-center ">
    <table class="table" style="border-collapse: collapse;">
        <tbody>
            <tr>
                <td class="text-center fw-bold yellowBold " style="width:10%;">업체명</td>
                <td class="text-center yellowBold " style="width:40%;"> <?=$secondord?> (귀하) </td>		
                <td rowspan="5" class="text-center align-middle fw-bold" style="width:5%; border-top:none; border-bottom:none;" >공 급 자</td>
                <td class="text-center fw-bold lightgray " > 상호 </td>
                <td class="text-center fw-bold" colspan="3" >㈜ 경동기업 </td>             
                
            </tr>
            <tr>
                <td class="text-center fw-bold">제품명</td>
                <td class="text-center" > <?=$subTitle?>   </td>
				<td class="text-center fw-bold lightgray "  style="width:10%;" >등록번호</td>
                <td class="text-center" style="width:10%;"> 139-87-00333 </td>
                <td class="text-center fw-bold lightgray " >대표자</td>
                <td class="text-center fw-bold">
					<div class="d-flex align-items-center justify-content-center ">
						이 경 호 &nbsp;
						<!-- <img src="../img/daehanstamp.png" alt="도장" style="width:45px; height:45px;"> -->
					</div>
                </td>				
			<tr>
                <td class="text-center fw-bold">현장명</td>
                <td class="text-center fw-bold" > <?=$outworkplace?></td>
                <td class="text-center fw-bold lightgray " > 사업장주소 </td>
                <td colspan="3" class="text-center"> 경기도 김포시 통진읍 옹정로 45-22</td>				
            </tr>
			<tr>				
				<td class="text-center fw-bold">담당자</td>
                <td class="text-center"><?=$secondordman?></td>
                <td class="text-center fw-bold lightgray " > 업 태 </td>
                <td class="text-center" > 제조업 </td>
                <td class="text-center fw-bold lightgray " >종목</td>
                <td class="text-center" > 방화셔터, 금속창호 </td>				
                
            </tr>
			<tr>
                <td  class="text-center fw-bold">연락처</td>
                <td  class="text-center"><?=$secondordmantel?></td>
                <td class="text-center fw-bold lightgray " > TEL. </td>
                <td class="text-center" > 031-983-5130</td>
                <td class="text-center fw-bold lightgray " > FAX </td>
                <td class="text-center" > 02-6911-6315 </td>		                
            </tr>            
        </tbody>
    </table>   
	</div>
	<div class="d-flex align-items-center justify-content-center ">	
		<table class="table" style="border-collapse: collapse;">
			<tbody>
				<tr>
					<!--
					<td class="text-center fw-bold" style="width:250px;" >
					합계금액(부가세 별도)  <br>
					아래와 같이 견적합니다				
					<span class="text-danger"> (VAT 별도) </span>
					</td>
					<td class="text-center  align-middle fs-6 fw-bold" style="width:50px;">  금 </td>				
					<td rowspan="5" class="text-end align-middle fw-bold fs-6" style="width:500px;" > <span id="koreantotalsum"> </span> </td>                
					<td class="text-center fw-bold  align-middle fs-6"  style="width:50px;" > 원 </td>
					-->
				<td colspan="3" class="align-middle text-end fs-6 fw-bold" style="width:250px;"> ( ￦ <span id="totalsum"> </span> )
					    <span class="text-danger"> (VAT 별도) </span>
				</td>
				</tr>
			</tbody>
		</table>
	</div>	