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

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

header("Content-Type: application/json");  // JSON 응답 설정

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

$quantityJson = isset($_POST['quantityJson']) ? $_POST['quantityJson'] : '[]';
$participantJson = isset($_POST['participantJson']) ? $_POST['participantJson'] : '[]';
 
$searchtag = $registedate . ' ' .
              $receiver . ' ' . 
              $receiver_tel . ' ' .
              $address . ' ' .
              $siteDate . ' ' .
              $siteAttendance . ' ' .
              $siteStatus . ' ' .
              $quantityJson . ' ' .
              $fee . ' ' .
              $siteresult . ' ' .
              $workplacename . ' ' .
              $estimatedbyPerson . ' ' .
              $memo;

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

if ($mode == "modify") {
    try {
        $pdo->beginTransaction();        
        $sql = "update " . $DB . "." . $tablename . " SET 
                registedate = ?, receiver = ?, receiver_tel = ?, address = ?, siteDate = ?, 
                siteAttendance = ?, siteStatus = ?, quantityJson = ?, fee = ?, siteresult = ?, memo = ?, 
                update_log = ?, searchtag = ?, secondord=?, secondordnum=?, workplacename=?, estimatedbyPerson=? , bidDate=?, totalitem=?, participantJson=?   
                WHERE num = ? LIMIT 1"; 
				
        $stmh = $pdo->prepare($sql);

        // 바인딩
        $stmh->bindValue(1, $registedate, PDO::PARAM_STR);
        $stmh->bindValue(2, $receiver, PDO::PARAM_STR);
        $stmh->bindValue(3, $receiver_tel, PDO::PARAM_STR);
        $stmh->bindValue(4, $address, PDO::PARAM_STR);
        $stmh->bindValue(5, $siteDate, PDO::PARAM_STR);
        $stmh->bindValue(6, $siteAttendance, PDO::PARAM_STR);
        $stmh->bindValue(7, $siteStatus, PDO::PARAM_STR);        
        $stmh->bindValue(8, $quantityJson, PDO::PARAM_STR);
        $stmh->bindValue(9, str_replace(',', '', $fee), PDO::PARAM_STR);
        $stmh->bindValue(10, $siteresult, 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, $secondord, PDO::PARAM_STR);
        $stmh->bindValue(15, $secondordnum, PDO::PARAM_STR);
        $stmh->bindValue(16, $workplacename, PDO::PARAM_STR);
        $stmh->bindValue(17, $estimatedbyPerson, PDO::PARAM_STR);
        $stmh->bindValue(18, $bidDate, PDO::PARAM_STR);
        $stmh->bindValue(19, $totalitem, PDO::PARAM_STR);
        $stmh->bindValue(20, $participantJson, PDO::PARAM_STR);
        $stmh->bindValue(21, $num, PDO::PARAM_INT);

        $stmh->execute();
        $pdo->commit();
    } catch (PDOException $Exception) {
        $pdo->rollBack();
        echo json_encode(["error" => $Exception->getMessage()], JSON_UNESCAPED_UNICODE);
        exit;
    }
}

if ($mode == "insert" || $mode == "copy" || $mode == '' || $mode == null) {
    try {
        $pdo->beginTransaction();

        $sql = "INSERT INTO " . $DB . "." . $tablename . " (
                registedate, receiver, receiver_tel, address, siteDate, 
                siteAttendance, siteStatus, quantityJson, fee, siteresult,
                 memo, update_log, searchtag, secondord, secondordnum, 
                 workplacename, estimatedbyPerson, bidDate, totalitem, participantJson) 
                VALUES (?, ?, ?, ?, ?,     ?, ?, ?, ?, ?,       ?, ?, ?, ?, ?,    ?, ?, ?, ?, ?)" ;

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

        $stmh->bindValue(1, $registedate, PDO::PARAM_STR);
        $stmh->bindValue(2, $receiver, PDO::PARAM_STR);
        $stmh->bindValue(3, $receiver_tel, PDO::PARAM_STR);
        $stmh->bindValue(4, $address, PDO::PARAM_STR);
        $stmh->bindValue(5, $siteDate, PDO::PARAM_STR);
        $stmh->bindValue(6, $siteAttendance, PDO::PARAM_STR);
        $stmh->bindValue(7, $siteStatus, PDO::PARAM_STR);        
        $stmh->bindValue(8, $quantityJson, PDO::PARAM_STR);
        $stmh->bindValue(9, str_replace(',', '', $fee), PDO::PARAM_STR);
        $stmh->bindValue(10, $siteresult, 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, $secondord, PDO::PARAM_STR);
        $stmh->bindValue(15, $secondordnum, PDO::PARAM_STR);
        $stmh->bindValue(16, $workplacename, PDO::PARAM_STR);
        $stmh->bindValue(17, $estimatedbyPerson, PDO::PARAM_STR);
        $stmh->bindValue(18, $bidDate, PDO::PARAM_STR);
        $stmh->bindValue(19, $totalitem, PDO::PARAM_STR);
        $stmh->bindValue(20, $participantJson, PDO::PARAM_STR); 
        $stmh->execute();
        $pdo->commit();
    } catch (PDOException $Exception) {
        $pdo->rollBack();
        echo json_encode(["error" => $Exception->getMessage()], JSON_UNESCAPED_UNICODE);
        exit;
    }
}

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();
        echo json_encode(["error" => $ex->getMessage()], JSON_UNESCAPED_UNICODE);
        exit;
    }
}

// JSON 응답
$data = [   
 'num' => $num,
 'mode' => $mode,
 'receiver' => $receiver,
 'receiver_tel' => $receiver_tel,
 'address' => $address,
 'siteDate' => $siteDate,
 'siteAttendance' => $siteAttendance,
 'siteStatus' => $siteStatus,
 'quantityJson' => $quantityJson,
 'bidDate' => $bidDate,
 'fee' => $fee,
 'siteresult' => $siteresult,
 'totalitem' => $totalitem,
 'memo' => $memo,
 'participantJson' => $participantJson
]; 

echo json_encode($data, JSON_UNESCAPED_UNICODE);
?>
