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

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

// 기본값 세팅
$type = $_POST['exit_direction'] ?? '양면 점검구';
$box_width = isset($_POST['box_width']) ? floatval($_POST['box_width']) : 500;
$box_height = isset($_POST['box_height']) ? floatval($_POST['box_height']) : 380;
$rail_width = isset($_POST['rail_width']) ? floatval($_POST['rail_width']) : 70;
$front_bottom_width = isset($_POST['front_bottom_width']) ? floatval($_POST['front_bottom_width']) : 50;
$blocks = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $blocks = getCasePlate($type, $box_width, $box_height, $rail_width, $front_bottom_width);
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <title>점검구 형태별 기본 전개도</title>
</head>
<body>
<div class="container py-4">
  <h3 class="mb-4">점검구 형태별 기본 전개도 자동 생성</h3>
  <div class="card mb-4">
    <div class="card-header">
      <h5 class="mb-0">조건 입력</h5>
    </div>
    <div class="card-body">
      <form method="post" class="row g-3">
        <div class="col-md-3">
          <label class="form-label">점검구 형태</label>
          <select name="exit_direction" class="form-select" style="height: calc(1.5em + 0.75rem + 2px);">
            <option value="양면 점검구"<?= $type==='양면 점검구'?' selected':''?>>양면 점검구</option>
            <option value="후면 점검구"<?= $type==='후면 점검구'?' selected':''?>>후면 점검구</option>
            <option value="밑면 점검구"<?= $type==='밑면 점검구'?' selected':''?>>밑면 점검구</option>
          </select>
        </div>
        <div class="col-md-2">
          <label class="form-label">박스폭</label>
          <input type="number" name="box_width" value="<?=htmlspecialchars($box_width)?>" class="form-control" style="height: calc(1.5em + 0.75rem + 2px);">
        </div>
        <div class="col-md-2">
          <label class="form-label">박스높이</label>
          <input type="number" name="box_height" value="<?=htmlspecialchars($box_height)?>" class="form-control" style="height: calc(1.5em + 0.75rem + 2px);">
        </div>
        <div class="col-md-2">
          <label class="form-label">레일폭</label>
          <input type="number" name="rail_width" value="<?=htmlspecialchars($rail_width)?>" class="form-control" style="height: calc(1.5em + 0.75rem + 2px);">
        </div>
        <div class="col-md-2">
          <label class="form-label">전면하단폭</label>
          <input type="number" name="front_bottom_width" value="<?=htmlspecialchars($front_bottom_width)?>" class="form-control" style="height: calc(1.5em + 0.75rem + 2px);">
        </div>
        <div class="col-md-1 d-flex align-items-end">
          <button type="submit" class="btn btn-primary w-100">생성</button>
        </div>
      </form>
    </div>
  </div>

  <?php if (!empty($blocks)): ?>
    <div class="row">
      <?php foreach ($blocks as $block): ?>
        <div class="col-12 mb-3">
          <div class="card">
            <div class="card-header">
              <strong><?=htmlspecialchars($block['label'])?></strong> (수량: <?=intval($block['surang'])?>)
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <table class="table table-bordered table-sm mb-0">
                  <tr class="table-light">
                    <th class="text-center" style="width:100px;">입력값</th>
                    <?php foreach ($block['inputValues'] as $v) echo "<td class='text-center'>$v</td>"; ?>
                  </tr>
                  <tr>
                    <th class="text-center table-light">연신율</th>
                    <?php foreach ($block['bendingRates'] as $v) echo "<td class='text-center'>$v</td>"; ?>
                  </tr>
                  <tr class="table-warning">
                    <th class="text-center">합계</th>
                    <?php foreach ($block['sums'] as $v) echo "<td class='text-center fw-bold'>$v</td>"; ?>
                  </tr>
                  <tr>
                    <th class="text-center table-light">음영</th>
                    <?php foreach ($block['colors'] as $v) echo "<td class='text-center'>".($v?'<span class="badge bg-dark">O</span>':'')."</td>"; ?>
                  </tr>
                  <tr>
                    <th class="text-center table-light">A각</th>
                    <?php foreach ($block['aAngles'] as $v) echo "<td class='text-center'>".($v?'<span class="badge bg-primary">A</span>':'')."</td>"; ?>
                  </tr>
                </table>
              </div>
            </div>
          </div>
        </div>
      <?php endforeach; ?>
    </div>
  <?php elseif ($_SERVER['REQUEST_METHOD'] === 'POST'): ?>
    <div class="alert alert-warning">
      <i class="bi bi-exclamation-triangle"></i> 생성된 데이터가 없습니다.
    </div>
  <?php endif; ?>
</div>

<script>  
$(document).ready(function(){        
    // 로더 숨기기
    var loader = document.getElementById('loadingOverlay');
    if(loader)
        loader.style.display = 'none';            
});

</script>
</body>
</html> 