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

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

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

if ($mode === 'update' && $num) {
    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;
    }
} else {
    include '_request.php';
    $mode = 'insert';
    $reg_date = date('Y-m-d');
    $author = $user_name;
    $num = '';
    $frontbottom = 50;  // 기본값 설정
    $railwidth = 70;    // 기본값 설정
}

$title_message = ($mode === 'update') ? '매핑 정보 수정' : '매핑 정보 신규 등록';
?>

<input type="hidden" id="update_log" name="update_log" value="<?=$update_log?>">
<input type="hidden" id="reg_date" name="reg_date" value="<?=$reg_date?>">
<input type="hidden" id="user_name" name="user_name" value="<?=$user_name?>">
<input type="hidden" id="author" name="author" value="<?=$author?>">

<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 fs-6 fw-bold" style="width:220px;">제품코드</td>
                                    <td colspan="3" class="text-center">
                                        <select class="form-control fs-6" id="prodcode" name="prodcode">
                                            <option value="SL60" <?= $prodcode === 'SL60' ? 'selected' : '' ?>>SL60</option>
                                            <option value="KSS01" <?= $prodcode === 'KSS01' ? 'selected' : '' ?>>KSS01</option>
                                            <option value="KSE01" <?= $prodcode === 'KSE01' ? 'selected' : '' ?>>KSE01</option>
                                            <option value="KWE01" <?= $prodcode === 'KWE01' ? 'selected' : '' ?>>KWE01</option>
                                            <option value="KTE01" <?= $prodcode === 'KTE01' ? 'selected' : '' ?>>KTE01</option>
                                            <option value="KQTS" <?= $prodcode === 'KQTS' ? 'selected' : '' ?>>KQTS</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">레일형태</td>
                                    <td colspan="3" class="text-center">
                                        <select class="form-control fs-6" id="railtype" name="railtype">
                                            <option value="벽면형120*70" <?= $railtype === '벽면형120*70' ? 'selected' : '' ?>>벽면형120*70</option>
                                            <option value="측면형120*120" <?= $railtype === '측면형120*120' ? 'selected' : '' ?>>측면형120*120</option>
                                            <option value="혼합형" <?= $railtype === '혼합형' ? 'selected' : '' ?>>혼합형</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">박스크기</td>
                                    <td colspan="3" class="text-center">
                                        <select class="form-control fs-6" id="boxsize" name="boxsize">
                                            <option value="500*350" <?= $boxsize === '500*350' ? 'selected' : '' ?>>500*350</option>
                                            <option value="500*380" <?= $boxsize === '500*380' ? 'selected' : '' ?>>500*380</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">박스점검구</td>
                                    <td colspan="3" class="text-center">
                                        <select class="form-control fs-6" id="boxexit" name="boxexit">
                                            <option value="양쪽 점검구" <?= $boxexit === '양쪽 점검구' ? 'selected' : '' ?>>양쪽 점검구</option>
                                            <option value="밑면 점검구" <?= $boxexit === '밑면 점검구' ? 'selected' : '' ?>>밑면 점검구</option>
                                            <option value="후면 점검구" <?= $boxexit === '후면 점검구' ? 'selected' : '' ?>>후면 점검구</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">셔터박스 전면부 밑면치수</td>
                                    <td colspan="3" class="text-center">
                                        <input type="number" class="form-control fs-6 text-center noborder-input w60px" id="frontbottom" name="frontbottom" value="<?=$frontbottom?>" autocomplete="off">
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">셔터박스 레일(폭)</td>
                                    <td colspan="3" class="text-center">
                                        <input type="number" class="form-control fs-6 text-center noborder-input w60px" id="railwidth" name="railwidth" value="<?=$railwidth?>" autocomplete="off">
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">비고</td>
                                    <td colspan="3" class="text-center">
                                        <input type="text" class="form-control fs-6 text-start noborder-input" id="remark" name="remark" value="<?=$remark?>" autocomplete="off">
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="d-flex justify-content-center">
                    <button type="button" id="saveBtn" class="btn btn-dark btn-sm me-3">
                        <ion-icon name="save-outline"></ion-icon> 저장 
                    </button>
                    <?php if ($mode != 'insert') { ?>
                    <button type="button" id="deleteBtn" class="btn btn-danger btn-sm me-3">
                        <ion-icon name="trash-outline"></ion-icon> 삭제
                    </button>
                    <?php } ?>
                    <button type="button" id="closeBtn" class="btn btn-outline-dark btn-sm me-2">
                        <ion-icon name="close-circle-outline"></ion-icon> 닫기
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
