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

$mode = isset($_POST['mode']) ? $_POST['mode'] : '';
$num = isset($_POST['num']) ? $_POST['num'] : '';
$tablename = isset($_POST['tablename']) ? $_POST['tablename'] : '';

require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
$pdo = db_connect();

try {
        $sql = "SELECT * FROM {$DB}.$tablename WHERE num=? ";
        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $num, PDO::PARAM_INT);      
        $stmh->execute();
        $row = $stmh->fetch(PDO::FETCH_ASSOC);

        include '_row.php';
    } catch (PDOException $Exception) {
        echo "오류: ".$Exception->getMessage();
        exit;
}

$title_message = '케이스 절곡 전개';

// echo '<pre>';
// print_r($row);
// echo '</pre>';

// 기존에 저장된 값이 있는 경우 해당 값을 설정
$selected_exit_directionFlat = isset($row['exit_direction']) ? $row['exit_direction'] : '양면 점검구';

?>

<style>
.image-container {
	position: relative;
	display: inline-block;
}
.image-container img {
	display: block;
}
	.input-overlay {
		position: absolute;
		background: rgba(255, 255, 255, 0.5);
		border: 1px solid blue;
		width: 50px;
		color: blue;
		/* font-weight: bold; */
	}
	
.modal-body {
    max-height: 750px; /* 원하는 높이 설정 */
    max-width: 1200px; /* 원하는 너비 설정 */
    overflow-x: auto; /* 가로 스크롤 가능 */
    overflow-y: auto; /* 세로 스크롤 가능 */
}

.table-container {
    width: 100%;    
}

#dynamicTable {
    table-layout: fixed;
    width: 1000px; 
}

#dynamicTable th:first-child,
#dynamicTable td:first-child {
    width: 100px;
    min-width: 100px;
    max-width: 100px;
}

#dynamicTable th:nth-child(2),
#dynamicTable td:nth-child(2) {
    width: 900px;
    min-width: 900px;
    max-width: 900px;
}

.input-container {
    width: 100%;    
    white-space: nowrap;
    display: flex;
    align-items: center;
	border : none;
}

.input-container input,
.input-container span {
    display: inline-block;
    margin-right: 5px;
}

.input-container input[type="text"] {
    width: 30px;	
	height:30px;
}	

</style>

<div class="container-fluid">    
        <div class="card justify-content-center">
            <div class="card-header text-center">
				<div class="row">
					<div class="col-sm-2">
					</div>
					<div class="col-sm-8">
						<div class="d-flex align-items-center justify-content-center">
							<span class="text-center fs-5"><?=$title_message?></span>
							<button class="btn btn-dark btn-sm ms-5 me-2" onclick="generatePDF()"> PDF 저장 </button>	
							<button class="btn btn-dark btn-sm " onclick="viewWork('<?=$num?>');return false"> 절곡 작업지시서 </button>	
						</div>
					</div>
					<div class="col-sm-2">
					<div class="d-flex align-items-center justify-content-end">
						<button type="button" class="btn btn-outline-dark btn-sm me-2 closeBtn"> <ion-icon name="close-circle-outline"></ion-icon> 닫기 </button>				
					</div>
					</div>
					</div>
            </div>
            <div class="card-body">			   
				<div class="row mb-3">
					<div class="col text-center">
						<span class="badge fs-5 
							<?php 
								if($selected_exit_directionFlat === '양면 점검구') {
									echo 'bg-primary';
								} elseif($selected_exit_directionFlat === '밑면 점검구') {
									echo 'bg-success';
								} elseif($selected_exit_directionFlat === '후면 점검구') {
									echo 'bg-danger';
								}
							?>">
							<?php echo $selected_exit_directionFlat; ?>
						</span>
					</div>
				</div>

<div class="d-flex align-items-center justify-content-center m-2">
    <div class="image-container mb-5">
        <img id="checkImageFlat" alt="Image">
        
		<span id="display_front_bottom_width" class="input-overlay text-success fs-6" style="top: 350px; left: 110px;" > 전면밑 <?=$front_bottom_width?> </span>
		<span id="display_rail_width" class="input-overlay text-danger fs-6" style="top: 410px; left: 180px;" > 레일폭 <?=$rail_width?> </span>
    </div>
</div>
<div id="content-to-print">    
    <div class="row justify-content-center text-center">
        <div class="d-flex align-items-center justify-content-center m-2">
            <table class="table ">
                <tbody>
                    <tr>
                        <td class="text-center fs-6 fw-bold" colspan="6" >
                            <div class="d-flex align-items-center justify-content-center">
                                <span class="text-center fs-6 ms-1 me-1"> (<?= $selected_exit_directionFlat?>) 케이스 &nbsp;&nbsp;
                                <span class="text-center fs-6 ms-1 me-1"> 전면 밑 : <?=$front_bottom_width?> &nbsp;&nbsp;  레일폭 : <?=$rail_width?> &nbsp;&nbsp;&nbsp;&nbsp; </span> 
                                <span class="text-center fs-6 ms-1 me-1 text-primary"> 가로(폭): </span> 
                                <span class="text-primary ms-1 me-1"><?=$box_width?></span>
                                <span class="text-center fs-6 ms-1 me-1"> x </span> 
                                <span class="text-center fs-6 ms-1 me-1 text-danger"> 세로(높이): </span> 
                                <span class="text-danger"><?=$box_height?></span>
                            </div>
                        </td>
                    </tr>                           
                </tbody>
            </table>
        </div>
    </div>				
    <div class="row justify-content-center text-center">
	
<?php
// 공통인 것
$box_height = isset($row['box_height']) ? $row['box_height'] : 0;
$front_bottom_width = isset($row['front_bottom_width']) ? $row['front_bottom_width'] : 0;

// 케이스 전개도 함수
require_once($_SERVER['DOCUMENT_ROOT'] . "/shutterbox/fun_case.php");
$productData = getCasePlate($selected_exit_directionFlat, $box_width, $box_height, $rail_width, $front_bottom_width);
?>

<div class="table-container">
    <table class="table">
        <thead>
            <tr>
                <th class="w100px">번호</th>
                <th class="w80px">구분</th>
                <th>상세내역</th>
            </tr>
        </thead>
<tbody id="tableBody">
    <?php foreach ($productData as $productIndex => $product): ?>
        <?php 
        // 첫 번째 행에 번호를 표시 (1, 2, 3... inputValues의 길이만큼)
        echo '<tr>';
        echo '<td rowspan="6" class="lightgray">' . $product['label'] . '</td>';
        echo '<td class="lightgray">번호</td>';
        echo '<td class="input-container" style="border:none!important; border-bottom: 1px solid black!important;">';
        foreach ($product['inputValues'] as $index => $value) {
            echo '<span class="form-control text-center" style="width: 30px;">' . ($index + 1) . '</span>';
        }
        echo '</td>';
        echo '</tr>';

        // 입력 값 행
        echo '<tr>';
        echo '<td class="lightgray">입력</td>';
        echo '<td class="input-container" style="border:none!important; border-bottom: 1px solid black!important;">';
        foreach ($product['inputValues'] as $value) {
            echo '<span class="yellowBold text-center" style="width: 30px;">' . $value . '</span>';
        }
        echo '</td>';
        echo '</tr>';

        // 연신율 행
        echo '<tr>';
        echo '<td class="lightgray">연신율 (-)</td>';
        echo '<td class="input-container" style="border:none!important; border-bottom: 1px solid black!important;">';
        foreach ($product['bendingRates'] as $rate) {
            echo '<span class="text-center" style="width: 30px;">' . $rate . '</span>';
        }
        echo '</td>';
        echo '</tr>';

        // 합계 행
        echo '<tr>';
        echo '<td class="lightgray">합계</td>';
        echo '<td class="input-container" style="border:none!important; border-bottom: 1px solid black!important;">';
        foreach ($product['sums'] as $sum) {
            echo '<span class="orangeBlackBold text-center" style="width: 30px;">' . $sum . '</span>';
        }
        echo '</td>';
        echo '</tr>';

        // 음영 행
        echo '<tr>';
        echo '<td class="lightgray">음영</td>';
        echo '<td class="input-container" style="border:none!important; border-bottom: 1px solid black!important;">';
        foreach ($product['colors'] as $color) {
            echo '<span  style="width: 10px; margin-left:10px; margin-right:15px;">' . ($color ? 'O' : '&nbsp;') . '</span>';
        }
        echo '</td>';
        echo '</tr>';

        // A각 표시 행
        echo '<tr>';
        echo '<td class="lightgray">A각 표시</td>';
        echo '<td class="input-container" style="border:none!important; border-bottom: 1px solid black!important;">';
        foreach ($product['aAngles'] as $angle) {
            echo '<span style="width: 10px; margin-left:10px; margin-right:15px;">' . ($angle ? 'O' : '&nbsp;') . '</span>';
        }
        echo '</td>';
        echo '</tr>';
        ?>
    <?php endforeach; ?>
	</tbody>	  
    </table>
</div>					   
</div>
</div>
</div>    
</div>    
</div>