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

$tablename = $_REQUEST['tablename'] ?? 'KDunitprice';
$mode = $_REQUEST['mode'] ?? '';

header("Content-Type: application/json");

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

$prodcode = $_POST['prodcode'] ?? '';
$item_name = $_POST['item_name'] ?? '';
$item_div = $_POST['item_div'] ?? '';
$spec = $_POST['spec'] ?? '';
$unit = $_POST['unit'] ?? '';
$unitprice = $_POST['unitprice'] ?? '';
$searchtag = $_POST['searchtag'] ?? '';
$update_log = $_POST['update_log'] ?? '';
$num = $_POST['num'] ?? '';

$searchtag = $prodcode . ' ' . $item_name . ' ' . $item_div . ' ' . $spec . ' ' . $unit . ' ' . $unitprice;

if ($mode === "copy") {
    $update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . " 복사 생성&#10;";
} else {
    $update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . " " . $update_log . "&#10;";
}

if ($mode === "update") {
    try {
        $pdo->beginTransaction();

        $sql = "UPDATE {$DB}.{$tablename} SET 
            prodcode = ?, 
            item_name = ?, 
            item_div = ?, 
            spec = ?, 
            unit = ?, 
            unitprice = ?, 
            searchtag = ?, 
            update_log = ? 
            WHERE num = ?";

        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $prodcode, PDO::PARAM_STR);
        $stmh->bindValue(2, $item_name, PDO::PARAM_STR);
        $stmh->bindValue(3, $item_div, PDO::PARAM_STR);
        $stmh->bindValue(4, $spec, PDO::PARAM_STR);
        $stmh->bindValue(5, $unit, PDO::PARAM_STR);
        $stmh->bindValue(6, str_replace(',', '', $unitprice), PDO::PARAM_STR);
        $stmh->bindValue(7, $searchtag, PDO::PARAM_STR);
        $stmh->bindValue(8, $update_log, PDO::PARAM_STR);
        $stmh->bindValue(9, $num, PDO::PARAM_INT);

        $stmh->execute();
        $pdo->commit();
    } catch (PDOException $e) {
        $pdo->rollBack();
        echo json_encode(['error' => $e->getMessage()]);
        exit;
    }
} elseif ($mode === "insert" || $mode === "copy") {
    try {
        $pdo->beginTransaction();

        $sql = "INSERT INTO {$DB}.{$tablename} (
            prodcode, item_name, item_div, spec, unit, unitprice, searchtag, update_log
        ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $prodcode, PDO::PARAM_STR);
        $stmh->bindValue(2, $item_name, PDO::PARAM_STR);
        $stmh->bindValue(3, $item_div, PDO::PARAM_STR);
        $stmh->bindValue(4, $spec, PDO::PARAM_STR);
        $stmh->bindValue(5, $unit, PDO::PARAM_STR);
        $stmh->bindValue(6, str_replace(',', '', $unitprice), PDO::PARAM_STR);
        $stmh->bindValue(7, $searchtag, PDO::PARAM_STR);
        $stmh->bindValue(8, $update_log, PDO::PARAM_STR);

        $stmh->execute();
        $pdo->commit();
    } catch (PDOException $e) {
        $pdo->rollBack();
        echo json_encode(['error' => $e->getMessage()]);
        exit;
    }
} elseif ($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 $e) {
        $pdo->rollBack();
        echo json_encode(['error' => $e->getMessage()]);
        exit;
    }
}

echo json_encode(["num" => $num, "mode" => $mode], JSON_UNESCAPED_UNICODE);
?>