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

header("Content-Type: application/json");  // JSON을 사용하기 위해 필요한 구문  

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

include $_SERVER['DOCUMENT_ROOT'] . "/almember/_request.php";

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

if ($mode == "modify") {      
    try {
        $sql = "SELECT * FROM " . $DB . ".almember WHERE num=?";  // get target record
        $stmh = $pdo->prepare($sql); 
        $stmh->bindValue(1, $num, PDO::PARAM_STR); 
        $stmh->execute(); 
        $row = $stmh->fetch(PDO::FETCH_ASSOC);
    } catch (PDOException $Exception) {
        $pdo->rollBack();
        print "오류: ".$Exception->getMessage();
    } 
                  
    try {
        $pdo->beginTransaction();   
        $sql = "UPDATE " . $DB . ".almember 
                SET name=?, part=?, dateofentry=?, referencedate=?, availableday=?, comment=?, 
                    years_of_service=?, initial_less_than_one_year=?, service_based=?, previous_year_usage=?
                WHERE num=? LIMIT 1";

        $stmh = $pdo->prepare($sql); 
        $stmh->bindValue(1, $name, PDO::PARAM_STR);  
        $stmh->bindValue(2, $part, PDO::PARAM_STR);  
        $stmh->bindValue(3, $dateofentry, PDO::PARAM_STR);  
        $stmh->bindValue(4, $referencedate, PDO::PARAM_STR);  
        $stmh->bindValue(5, $availableday, PDO::PARAM_STR);  
        $stmh->bindValue(6, $comment, PDO::PARAM_STR);  
        $stmh->bindValue(7, $years_of_service, PDO::PARAM_STR);
        $stmh->bindValue(8, $initial_less_than_one_year, PDO::PARAM_STR);
        $stmh->bindValue(9, $service_based, PDO::PARAM_STR);
        $stmh->bindValue(10, $previous_year_usage, PDO::PARAM_STR);
        $stmh->bindValue(11, $num, PDO::PARAM_STR);  

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

if ($mode == "insert" || $mode == "copy") {	 	 
    try {
        $pdo->beginTransaction();
        
        $sql = "INSERT INTO " . $DB . ".almember (name, part, dateofentry, referencedate, availableday, comment, 
                                                  years_of_service, initial_less_than_one_year, service_based, previous_year_usage) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        $stmh = $pdo->prepare($sql); 
        $stmh->bindValue(1, $name, PDO::PARAM_STR);  
        $stmh->bindValue(2, $part, PDO::PARAM_STR);  
        $stmh->bindValue(3, $dateofentry, PDO::PARAM_STR);  
        $stmh->bindValue(4, $referencedate, PDO::PARAM_STR);  
        $stmh->bindValue(5, $availableday, PDO::PARAM_STR); 
        $stmh->bindValue(6, $comment, PDO::PARAM_STR); 
        $stmh->bindValue(7, $years_of_service, PDO::PARAM_STR);
        $stmh->bindValue(8, $initial_less_than_one_year, PDO::PARAM_STR);
        $stmh->bindValue(9, $service_based, PDO::PARAM_STR);
        $stmh->bindValue(10, $previous_year_usage, PDO::PARAM_STR);
    
        $stmh->execute();
        $pdo->commit(); 
    } catch (PDOException $Exception) {
        $pdo->rollBack();
        print "오류: ".$Exception->getMessage();
    }   
}

if ($mode == "delete") {	 	 
    try {
        $pdo->beginTransaction();
        
        $sql = "DELETE FROM " . $DB . ".almember WHERE num = ?";  
        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $num, PDO::PARAM_STR);      
        $stmh->execute();   
        $pdo->commit();	 
    } catch (PDOException $Exception) {
        $pdo->rollBack();
        print "오류: ".$Exception->getMessage();
    }   
}

// 각각의 정보를 하나의 배열 변수에 넣어준다.
$data = array(
    "mode" => $mode,
    "dateofentry" =>  $dateofentry,
    "years_of_service" => $years_of_service,
    "initial_less_than_one_year" => $initial_less_than_one_year,
    "service_based" => $service_based,
    "previous_year_usage" => $previous_year_usage
);

// JSON 출력
echo json_encode($data, JSON_UNESCAPED_UNICODE);   
?>
