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

header("Content-Type: application/json");  // Set content type for JSON response

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

$tablename = 'bendingfee';

require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
$pdo = db_connect();
 
include $_SERVER['DOCUMENT_ROOT'] . "/bendingfee/_request.php"; // Ensure this file properly sets all needed variables

$searchtag = $basicdate . ' ' . $firstitem . ' ' . $seconditem . ' ' . $prodcode . ' ' . $proditem . ' ' . $material . ' ' . $itemcode . ' ' . $unit . ' ' . $su . ' ' . $unitprice . ' ' . $update_log . ' ' . $unitList . ' ' . $memo;
$searchtag = trim($searchtag); // 양쪽 공백 제거


if ($mode == "modify") {
    $num = isset($_POST['num']) ? $_POST['num'] : 0;
    $update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . "&#10" . (isset($_POST['update_log']) ? $_POST['update_log'] : '');

    try {
        $pdo->beginTransaction();

        $sql = "UPDATE " . $DB . "." . $tablename . " 
                SET basicdate = ?, firstitem = ?, seconditem = ?, prodcode = ?, proditem = ?, material = ?, 
                    itemcode = ?, unit = ?, su = ?, unitprice = ?, memo = ?, update_log = ?, searchtag = ?, unitList = ? 
                WHERE num = ?";

        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $basicdate, PDO::PARAM_STR);
        $stmh->bindValue(2, $firstitem, PDO::PARAM_STR);
        $stmh->bindValue(3, $seconditem, PDO::PARAM_STR);
        $stmh->bindValue(4, $prodcode, PDO::PARAM_STR);
        $stmh->bindValue(5, $proditem, PDO::PARAM_STR);
        $stmh->bindValue(6, $material, PDO::PARAM_STR);
        $stmh->bindValue(7, $itemcode, PDO::PARAM_STR);
        $stmh->bindValue(8, $unit, PDO::PARAM_STR);
        $stmh->bindValue(9, $su, PDO::PARAM_INT);
        $stmh->bindValue(10, $unitprice, PDO::PARAM_STR);
        $stmh->bindValue(11, $memo, PDO::PARAM_STR);
        $stmh->bindValue(12, $update_log, PDO::PARAM_STR);
        $stmh->bindValue(13, $searchtag, PDO::PARAM_STR);
        $stmh->bindValue(14, $unitList, PDO::PARAM_STR);
        $stmh->bindValue(15, $num, PDO::PARAM_INT);

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

} elseif ($mode == "delete") {
    $num = isset($_POST['num']) ? $_POST['num'] : 0;
    $update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . "&#10" . (isset($_POST['update_log']) ? $_POST['update_log'] : '');

    try {
        $pdo->beginTransaction();

        $sql = "UPDATE " . $DB . "." . $tablename . " SET update_log = ?, is_deleted = ? WHERE num = ? LIMIT 1";
        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $update_log, PDO::PARAM_STR);
        $stmh->bindValue(2, 1, PDO::PARAM_INT);  // 1 means deleted
        $stmh->bindValue(3, $num, PDO::PARAM_INT);

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

} else {
    $update_log = date("Y-m-d H:i:s") . " - " . $_SESSION["name"] . "&#10";

    try {
        $pdo->beginTransaction();

        $sql = "INSERT INTO " . $DB . "." . $tablename . " (basicdate, firstitem, seconditem, prodcode, proditem, 
                material, itemcode, unit, su, unitprice, memo, update_log, searchtag, unitList) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $basicdate, PDO::PARAM_STR);
        $stmh->bindValue(2, $firstitem, PDO::PARAM_STR);
        $stmh->bindValue(3, $seconditem, PDO::PARAM_STR);
        $stmh->bindValue(4, $prodcode, PDO::PARAM_STR);
        $stmh->bindValue(5, $proditem, PDO::PARAM_STR);
        $stmh->bindValue(6, $material, PDO::PARAM_STR);
        $stmh->bindValue(7, $itemcode, PDO::PARAM_STR);
        $stmh->bindValue(8, $unit, PDO::PARAM_STR);
        $stmh->bindValue(9, $su, PDO::PARAM_INT);
        $stmh->bindValue(10, $unitprice, PDO::PARAM_STR);
        $stmh->bindValue(11, $memo, PDO::PARAM_STR);
        $stmh->bindValue(12, $update_log, PDO::PARAM_STR);
        $stmh->bindValue(13, $searchtag, PDO::PARAM_STR);
        $stmh->bindValue(14, $unitList, PDO::PARAM_STR);

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

$data = [
    'status' => 'success',
    'mode' => $mode,
    'basicdate' => $basicdate,
    'firstitem' => $firstitem,
    'seconditem' => $seconditem,
    'prodcode' => $prodcode,
    'proditem' => $proditem,
    'material' => $material,
    'itemcode' => $itemcode,
    'unit' => $unit,
    'su' => $su,
    'unitprice' => $unitprice,
    'memo' => $memo,
    'unitList' => $unitList
];

echo json_encode($data, JSON_UNESCAPED_UNICODE);
?>
