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

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

$tablename = 'instock';
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';    
    $inspection_date = date('Y-m-d');
}

$title_message = ($mode === 'update') ? '수입검사 수정' : '수입검사 신규 등록';
?>

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

<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:150px;">로트번호</td>
                                    <td class="text-center" style="width:200px;">
                                        <input type="text" class="form-control fs-6" id="lot_no" name="lot_no" value="<?=$lot_no?>" autocomplete="off">
                                    </td>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">검사일</td>
                                    <td class="text-center" style="width:200px;">
                                        <input type="date" class="form-control fs-6" id="inspection_date" name="inspection_date" value="<?=$inspection_date?>">
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">납품업체</td>
                                    <td class="text-center" style="width:200px;">
                                       <div class="d-flex">
                                            <input class="form-control fs-6" id="supplier" name="supplier" value="<?=$supplier?>" autocomplete="off">
                                        </div>
                                    </td>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">품명</td>
                                    <td class="text-center" style="width:200px;">
                                        <input type="text" class="form-control fs-6" id="item_name" name="item_name" value="<?=$item_name?>" autocomplete="off">
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">규격</td>
                                    <td class="text-center" style="width:200px;">
                                        <input type="text" class="form-control fs-6" id="specification" name="specification" value="<?=$specification?>" autocomplete="off">
                                    </td>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">단위</td>
                                    <td class="text-center" style="width:200px;">
                                        <input type="text" class="form-control fs-6" id="unit" name="unit" value="<?=$unit?>" autocomplete="off">
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">입고량</td>
                                    <td class="text-center" style="width:200px;">
                                        <input type="text" class="form-control fs-6" id="received_qty" name="received_qty" value="<?=$received_qty?>" autocomplete="off" onkeyup="inputNumberFormat(this)">
                                    </td>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">자재번호</td>
                                    <td class="text-center" style="width:200px;">
                                        <input type="text" class="form-control fs-6" id="material_no" name="material_no" value="<?=$material_no?>" autocomplete="off">
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">제조사</td>
                                    <td class="text-center" style="width:200px;">
                                        <input type="text" class="form-control fs-6" id="manufacturer" name="manufacturer" value="<?=$manufacturer?>" autocomplete="off">
                                    <td colspan="2" class="text-center" > </td>
                                </tr>
                                <tr>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">매입가(VAT 별도)</td>
                                    <td class="text-center" style="width:200px;">
                                        <input type="text" class="form-control fs-6" id="purchase_price_excl_vat" name="purchase_price_excl_vat" value="<?=$purchase_price_excl_vat?>" autocomplete="off" onkeyup="inputNumberFormat(this)">
                                    </td>
                                    <td class="text-center fs-6 fw-bold" style="width:150px;">중량(kg)</td>
                                    <td class="text-center" style="width:200px;">
                                        <input type="text" class="form-control fs-6" id="weight_kg" name="weight_kg" value="<?=$weight_kg?>" autocomplete="off" onkeyup="inputNumberFormat(this)">
                                    </td>
                                </tr>
                                <tr>
                                    </td>
                                    <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" id="remarks" name="remarks" value="<?=$remarks?>" 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> Close
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
