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

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

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

$title_message = ($mode === 'update') ? '소모품 단가 수정' : (($mode === 'copy') ? '소모품 단가 복사' : '소모품 단가 신규 등록');

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 if ($mode === 'copy' && $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';
		$mode = 'copy';    
		$num = null;
    } catch (PDOException $Exception) {
        echo "오류: ".$Exception->getMessage();
        exit;
    }
}
else {
    include '_request.php';
    $mode = 'insert';    
    $registedate = date('Y-m-d');
    $inoutdate = date('Y-m-d');
    $secondord = '';
}

?>

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

<div class="container">
    <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" >등록일자</td>
                                    <td class="text-center" >
                                        <input type="date" class="form-control fs-6  noborder-input" id="registedate" name="registedate" style="width:130px;" value="<?=$registedate?>">
                                    </td>
                                    <td class="text-center fs-6 fw-bold" >품목명</td>
                                    <td class="text-center" style="width:100px;">
                                        <div class="specialinputWrap">
                                        <input type="text" class="form-control inputitemname fs-6 noborder-input" id="item_name" name="item_name" value="<?=$item_name?>" autocomplete="off">
                                        <button class="specialbtnClear"></button>
                                        </div>
                                    </td>									                                    
                                </tr>                
                                <tr>
                                    <td class="text-center fs-6 fw-bold" >규격</td>
                                    <td class="text-center">
                                        <div class="specialinputWrap">
                                            <input type="text" class="form-control inputcode fs-6 noborder-input" id="spec" name="spec" value="<?=$spec?>" autocomplete="off">
                                            <button class="specialbtnClear"></button>
                                        </div>
                                    </td>
									<td class="text-center fs-6 fw-bold" >단위</td>
                                    <td class="text-center" style="width:500px;">
                                        <input type="text" class="form-control fs-6 noborder-input" id="unit" name="unit"  value="<?=$unit?>">
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-center fs-6 fw-bold" >매입처</td>
                                    <td class="text-center" >
                                       <div class="d-flex">
                                        <input type="text" class="form-control fs-6 noborder-input" id="secondord" name="secondord" value="<?=$secondord?>"  autocomplete="off" onkeydown="if(event.keyCode == 13) { phonebookBtn('secondord'); }"  > &nbsp;                
                                        <button type="button" class="btn btn-dark-outline btn-sm " onclick="phonebookBtn('secondord');">  <i class="bi bi-gear"></i> </button>
                                    </div>
                                        
                                    </td>
                                    <td class="text-center fs-6 fw-bold" >입고 단가</td>
                                    <td class="text-center">                                        
                                        <input type="text" class="form-control fs-6 noborder-input" id="unitprice" name="unitprice" value="<?=$unitprice ?>"  autocomplete="off" onkeyup="inputNumberFormat(this)" >
                                    </td>
                                </tr>
                                <tr>
                                    <td class="text-center fs-6 fw-bold" >수량</td>
                                    <td class="text-center">
                                        <input type="text" class="form-control fs-6 noborder-input" id="surang" name="surang" value="<?=$surang?>"  autocomplete="off" onkeyup="inputNumberFormat(this)"  >
                                    </td>
                                <td class="text-center fs-6 fw-bold" >비고</td>
                                    <td class="text-center" colspan="3">
                                        <input type="text" class="form-control fs-6 noborder-input" id="comment" name="comment" value="<?=$comment?>" 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">
                        <i class="bi bi-floppy-fill"></i> 저장
                    </button>
                    <?php if($mode != 'insert' && $mode != 'copy') { ?>
                    <button type="button" id="copyBtn" class="btn btn-primary btn-sm me-3">
                        <i class="bi bi-copy"></i> 복사
                    </button>					
                    <button type="button" id="deleteBtn" class="btn btn-danger btn-sm me-3">
                        <i class="bi bi-trash"></i>  삭제 
                    </button>
                    <?php } ?>
                    <button type="button" id="closeBtn" class="btn btn-outline-dark btn-sm me-2">
                        &times; 닫기
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
