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

$tablename = isset($_REQUEST['tablename']) ? $_REQUEST['tablename'] : '';  
$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';  

header("Content-Type: application/json");  // JSON 콘텐츠 유형 설정

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

include "_request.php";

// searchtag 생성
$searchtag = $lot_no . ' ' .
             $inspection_date . ' ' .
             $supplier . ' ' .
             $item_name . ' ' .
             $specification . ' ' .
             $unit . ' ' .
             $received_qty . ' ' .
             $material_no . ' ' .
             $manufacturer . ' ' .
             $purchase_price_excl_vat . ' ' .
             $weight_kg . ' ' .
             $remarks;

if ($mode == "update") {
    $update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . " " . $update_log . "&#10";
    try {
        $pdo->beginTransaction();
        // SQL 쿼리 생성 (업데이트)
        $sql = "UPDATE " . $DB . "." . $tablename . " SET ";
        $sql .= "lot_no = ?, inspection_date = ?, supplier = ?, item_name = ?, specification = ?, unit = ?, received_qty = ?, material_no = ?, manufacturer = ?, remarks = ?, purchase_price_excl_vat = ?, weight_kg = ?, searchtag = ?, update_log = ? ";
        $sql .= "WHERE num = ? LIMIT 1"; // num이 일치하는 하나의 레코드만 업데이트

        $stmh = $pdo->prepare($sql);

        // 변수 바인딩
        $stmh->bindValue(1, $lot_no, PDO::PARAM_STR);
        $stmh->bindValue(2, $inspection_date, PDO::PARAM_STR);
        $stmh->bindValue(3, $supplier, PDO::PARAM_STR);
        $stmh->bindValue(4, $item_name, PDO::PARAM_STR);
        $stmh->bindValue(5, $specification, PDO::PARAM_STR);
        $stmh->bindValue(6, $unit, PDO::PARAM_STR);
        $stmh->bindValue(7, str_replace(',', '', $received_qty), PDO::PARAM_STR);
        $stmh->bindValue(8, $material_no, PDO::PARAM_STR);
        $stmh->bindValue(9, $manufacturer, PDO::PARAM_STR);
        $stmh->bindValue(10, $remarks, PDO::PARAM_STR);
        $stmh->bindValue(11, str_replace(',', '', $purchase_price_excl_vat), PDO::PARAM_STR);
        $stmh->bindValue(12, str_replace(',', '', $weight_kg), PDO::PARAM_STR);
        $stmh->bindValue(13, $searchtag, PDO::PARAM_STR);
        $stmh->bindValue(14, $update_log, PDO::PARAM_STR);
        $stmh->bindValue(15, $num, PDO::PARAM_INT);

        // 실행
        $stmh->execute();
        $pdo->commit();
    } catch (PDOException $Exception) {
        $pdo->rollBack();
        print "오류: " . $Exception->getMessage();
    }
}

if ($mode == "insert" || $mode == '' || $mode == null) {
    $update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . " " . $update_log . "&#10";
    // 데이터 삽입
    try {
        $pdo->beginTransaction();

        // SQL 쿼리 생성 (삽입)
        $sql = "INSERT INTO " . $DB . "." . $tablename . " (";
        $sql .= "lot_no, inspection_date, supplier, item_name, specification, unit, received_qty, material_no, manufacturer, remarks, purchase_price_excl_vat, weight_kg, searchtag, update_log ";
        $sql .= ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        $stmh = $pdo->prepare($sql);

        // 변수 바인딩
        $stmh->bindValue(1, $lot_no, PDO::PARAM_STR);
        $stmh->bindValue(2, $inspection_date, PDO::PARAM_STR);
        $stmh->bindValue(3, $supplier, PDO::PARAM_STR);
        $stmh->bindValue(4, $item_name, PDO::PARAM_STR);
        $stmh->bindValue(5, $specification, PDO::PARAM_STR);
        $stmh->bindValue(6, $unit, PDO::PARAM_STR);
        $stmh->bindValue(7, str_replace(',', '', $received_qty), PDO::PARAM_STR);
        $stmh->bindValue(8, $material_no, PDO::PARAM_STR);
        $stmh->bindValue(9, $manufacturer, PDO::PARAM_STR);
        $stmh->bindValue(10, $remarks, PDO::PARAM_STR);
        $stmh->bindValue(11, str_replace(',', '', $purchase_price_excl_vat), PDO::PARAM_STR);
        $stmh->bindValue(12, str_replace(',', '', $weight_kg), PDO::PARAM_STR);
        $stmh->bindValue(13, $searchtag, PDO::PARAM_STR);
        $stmh->bindValue(14, $update_log, PDO::PARAM_STR);

        // 실행
        $stmh->execute();
        $pdo->commit();
    } catch (PDOException $Exception) {
        $pdo->rollBack();
        print "오류: " . $Exception->getMessage();
    }
}

if ($mode == "delete") { // 데이터 삭제
    try {
        $pdo->beginTransaction();
        $sql = "UPDATE " .  $DB . "." . $tablename . " SET is_deleted=1 WHERE num = ?";  
        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $num, PDO::PARAM_INT);      
        $stmh->execute();
        $pdo->commit();
    } catch (PDOException $ex) {
        $pdo->rollBack();
        print "오류: ".$ex->getMessage();
    }
}

$data = [   
 'num' => $num,
 'mode' => $mode
]; 

echo json_encode($data, JSON_UNESCAPED_UNICODE);

?>
