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

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

// 기본값 세팅
$model_name = $_POST['model_name'] ?? 'KSS01';
$finishing_type = $_POST['finishing_type'] ?? 'EGI마감';
$check_type = $_POST['check_type'] ?? '벽면형';

// 모델과 레일형태에 따른 기본값 설정
function getDefaultRailDimensions($model_name, $check_type) {
    $screen_models = ['KSS01', 'KDSS01', 'KSS02', 'KWE01', 'KSE01'];
    $steel_models = ['KTE01', 'KQTS01'];
    
    if (in_array($model_name, $screen_models)) {
        if ($check_type === '벽면형') {
            return [120, 70];
        } else { // 측면형
            return [120, 120];
        }
    } elseif (in_array($model_name, $steel_models)) {
        if ($check_type === '벽면형') {
            return [120, 75];
        } else { // 측면형
            return [120, 125];
        }
    }
    
    // 기본값
    return [120, 70];
}

// POST 데이터가 있으면 사용, 없으면 기본값 계산
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $rail_length = isset($_POST['rail_length']) ? floatval($_POST['rail_length']) : getDefaultRailDimensions($model_name, $check_type)[0];
    $rail_width = isset($_POST['rail_width']) ? floatval($_POST['rail_width']) : getDefaultRailDimensions($model_name, $check_type)[1];
} else {
    list($rail_length, $rail_width) = getDefaultRailDimensions($model_name, $check_type);
}

$blocks = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $blocks = getProductData($model_name, $finishing_type, $rail_length, $rail_width, $check_type);
}
?>
<!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-2">
          <label class="form-label">모델명</label>
          <select name="model_name" class="form-select" style="height: calc(1.5em + 0.75rem + 2px);">
            <option value="KSS01"<?= $model_name==='KSS01'?' selected':''?>>KSS01</option>
            <option value="KDSS01"<?= $model_name==='KDSS01'?' selected':''?>>KDSS01</option>
            <option value="KSS02"<?= $model_name==='KSS02'?' selected':''?>>KSS02</option>
            <option value="KWE01"<?= $model_name==='KWE01'?' selected':''?>>KWE01</option>
            <option value="KSE01"<?= $model_name==='KSE01'?' selected':''?>>KSE01</option>
            <option value="KTE01"<?= $model_name==='KTE01'?' selected':''?>>KTE01</option>
            <option value="KQTS01"<?= $model_name==='KQTS01'?' selected':''?>>KQTS01</option>
          </select>
        </div>
        <div class="col-md-3">
          <label class="form-label">마감형태</label>
          <select name="finishing_type" class="form-select" style="height: calc(1.5em + 0.75rem + 2px);">
            <option value="EGI마감"<?= $finishing_type==='EGI마감'?' selected':''?>>EGI마감</option>
            <option value="SUS마감"<?= $finishing_type==='SUS마감'?' selected':''?>>SUS마감</option>
          </select>
        </div>
        <div class="col-md-2">
          <label class="form-label">레일형태</label>
          <select name="check_type" class="form-select" style="height: calc(1.5em + 0.75rem + 2px);">
            <option value="벽면형"<?= $check_type==='벽면형'?' selected':''?>>벽면형</option>
            <option value="측면형"<?= $check_type==='측면형'?' selected':''?>>측면형</option>
          </select>
        </div>
        <div class="col-md-2">
          <label class="form-label">레일너비</label>
          <input type="number" name="rail_length" value="<?=htmlspecialchars($rail_length)?>" 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 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'])?>) - <?=htmlspecialchars($block['material'])?>
            </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';
    
    // 모델과 레일형태에 따른 기본값 설정 함수
    function updateDefaultDimensions() {
        var modelName = $('select[name="model_name"]').val();
        var checkType = $('select[name="check_type"]').val();
        
        var screenModels = ['KSS01', 'KDSS01', 'KSS02', 'KWE01', 'KSE01'];
        var steelModels = ['KTE01', 'KQTS01'];
        
        var railLength, railWidth;
        
        if (screenModels.includes(modelName)) {
            if (checkType === '벽면형') {
                railLength = 120;
                railWidth = 70;
            } else { // 측면형
                railLength = 120;
                railWidth = 120;
            }
        } else if (steelModels.includes(modelName)) {
            if (checkType === '벽면형') {
                railLength = 120;
                railWidth = 75;
            } else { // 측면형
                railLength = 120;
                railWidth = 125;
            }
        } else {
            railLength = 120;
            railWidth = 70;
        }
        
        $('input[name="rail_length"]').val(railLength);
        $('input[name="rail_width"]').val(railWidth);
    }
    
    // 모델명 변경 시 기본값 업데이트
    $('select[name="model_name"]').on('change', updateDefaultDimensions);
    
    // 레일형태 변경 시 기본값 업데이트
    $('select[name="check_type"]').on('change', updateDefaultDimensions);
});

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