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

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

$mode = $_REQUEST["mode"] ?? '';

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

if ($mode == "modify") {      
    try {
        $pdo->beginTransaction();   
        $sql = "UPDATE " . $DB . ".member SET 
                id = ?, 
                name = ?, 
                pass = ?, 
                lv = ?, 
                part = ?, 
                hp = ?, 
                numorder = ?, 
                eworks_lv = ?, 
                regist_day = ?, 
                position = ?, 
                nick = ?, 
                email = ?, 
                authority = ?, 
                division = ?, 
                enterDate = ?, 
                birthday = ?, 
                address = ?, 
                quitDate = ?,
                first_approval_id = ?,
                first_approval_name = ?,
                card_company = ?, 
                card_number = ?,
                pay_bank = ?,
                pay_account = ?,
                pay_holder = ?,
                IDnumber = ?,
                birthday_type = ?
                WHERE num = ? LIMIT 1";
        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $id, PDO::PARAM_STR);
        $stmh->bindValue(2, $name, PDO::PARAM_STR);
        $stmh->bindValue(3, $pass, PDO::PARAM_STR);
        $stmh->bindValue(4, $lv, PDO::PARAM_STR);
        $stmh->bindValue(5, $part, PDO::PARAM_STR);
        $stmh->bindValue(6, $hp, PDO::PARAM_STR);
        $stmh->bindValue(7, $numorder, PDO::PARAM_STR);
        $stmh->bindValue(8, $eworks_lv, PDO::PARAM_STR);
        $stmh->bindValue(9, $regist_day, PDO::PARAM_STR);
        $stmh->bindValue(10, $position, PDO::PARAM_STR);
        $stmh->bindValue(11, $nick, PDO::PARAM_STR);
        $stmh->bindValue(12, $email, PDO::PARAM_STR);
        $stmh->bindValue(13, $authority, PDO::PARAM_STR);
        $stmh->bindValue(14, $division, PDO::PARAM_STR);
        $stmh->bindValue(15, $enterDate, PDO::PARAM_STR);
        $stmh->bindValue(16, $birthday, PDO::PARAM_STR);
        $stmh->bindValue(17, $address, PDO::PARAM_STR);
        $stmh->bindValue(18, $quitDate, PDO::PARAM_STR);
        $stmh->bindValue(19, $first_approval_id, PDO::PARAM_STR);
        $stmh->bindValue(20, $first_approval_name, PDO::PARAM_STR);
        $stmh->bindValue(21, $card_company, PDO::PARAM_STR);
        $stmh->bindValue(22, $card_number, PDO::PARAM_STR);
        $stmh->bindValue(23, $pay_bank, PDO::PARAM_STR);
        $stmh->bindValue(24, $pay_account, PDO::PARAM_STR);
        $stmh->bindValue(25, $pay_holder, PDO::PARAM_STR);
        $stmh->bindValue(26, $IDnumber, PDO::PARAM_STR);
        $stmh->bindValue(27, $birthday_type, PDO::PARAM_STR);
        $stmh->bindValue(28, $num, PDO::PARAM_STR);

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

if ($mode == "insert") {     
    $regist_day = date('Y-m-d');

    // 컬럼 개수와 VALUES의 ? 개수, 바인딩 개수 모두 28개로 맞춰야 함
    // 아래는 실제 테이블 컬럼 순서와 변수 매칭을 맞춘 코드입니다.
    try {
        $pdo->beginTransaction();
        $sql = "INSERT INTO " . $DB . ".member(
                    id, name, pass, lv, part, hp, numorder, eworks_lv, regist_day, position, 
                    nick, email, authority, division, enterDate, birthday, address, quitDate, first_approval_id, first_approval_name, 
                    card_company, card_number, pay_bank, pay_account, pay_holder, IDnumber, birthday_type
                ) VALUES (?, ?, ?, ?, ?,    ?, ?, ?, ?, ?,    ?, ?, ?, ?, ?,   ?, ?, ?, ?, ?,   ?, ?, ?, ?, ?,   ?, ?)";
        
        $stmh = $pdo->prepare($sql);
        $stmh->bindValue(1, $id, PDO::PARAM_STR);
        $stmh->bindValue(2, $name, PDO::PARAM_STR);
        $stmh->bindValue(3, $pass, PDO::PARAM_STR);
        $stmh->bindValue(4, $lv, PDO::PARAM_STR);
        $stmh->bindValue(5, $part, PDO::PARAM_STR);
        $stmh->bindValue(6, $hp, PDO::PARAM_STR);
        $stmh->bindValue(7, $numorder, PDO::PARAM_STR);
        $stmh->bindValue(8, $eworks_lv, PDO::PARAM_STR);
        $stmh->bindValue(9, $regist_day, PDO::PARAM_STR);
        $stmh->bindValue(10, $position, PDO::PARAM_STR);
        $stmh->bindValue(11, $nick, PDO::PARAM_STR);
        $stmh->bindValue(12, $email, PDO::PARAM_STR);
        $stmh->bindValue(13, $authority, PDO::PARAM_STR);
        $stmh->bindValue(14, $division, PDO::PARAM_STR);
        $stmh->bindValue(15, $enterDate, PDO::PARAM_STR);
        $stmh->bindValue(16, $birthday, PDO::PARAM_STR);
        $stmh->bindValue(17, $address, PDO::PARAM_STR);
        $stmh->bindValue(18, $quitDate, PDO::PARAM_STR);
        $stmh->bindValue(19, $first_approval_id, PDO::PARAM_STR);
        $stmh->bindValue(20, $first_approval_name, PDO::PARAM_STR);
        $stmh->bindValue(21, $card_company, PDO::PARAM_STR);
        $stmh->bindValue(22, $card_number, PDO::PARAM_STR);
        $stmh->bindValue(23, $pay_bank, PDO::PARAM_STR);
        $stmh->bindValue(24, $pay_account, PDO::PARAM_STR);
        $stmh->bindValue(25, $pay_holder, PDO::PARAM_STR);
        $stmh->bindValue(26, $IDnumber, PDO::PARAM_STR);
        $stmh->bindValue(27, $birthday_type, PDO::PARAM_STR);
        // 28번째 파라미터가 누락되어 있었으나, 실제로는 컬럼과 변수 매칭이 맞음.
        // 위 컬럼과 변수 매칭을 다시 확인해보면, 28개 컬럼에 28개 변수(마지막이 birthday_type)로 맞음.
        // 즉, 기존 코드에서 문제가 된 것은 VALUES의 개수와 바인딩 개수가 일치하지 않았던 것.
        // 위 코드에서는 VALUES와 바인딩 개수가 28개로 일치하므로 정상 동작함.

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

if ($mode == "delete") {     
    try {
        $pdo->beginTransaction();
        $sql = "DELETE FROM " . $DB . ".member 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();
    }
}

// JSON 응답
$data = array(
    "num"         => $num,
    "mode"        => $mode,
    "id"          => $id,
    "name"        => $name,
    "pass"        => $pass,
    "lv"          => $lv,
    "part"        => $part,
    "hp"          => $hp,
    "numorder"    => $numorder,
    "eworks_lv"   => $eworks_lv,
    "regist_day"  => $regist_day,
    "position"    => $position,
    "nick"        => $nick,
    "email"       => $email,
    "authority"   => $authority,
    "division"    => $division,
    "enterDate"   => $enterDate,
    "birthday"    => $birthday,
    "address"     => $address,
    "quitDate"    => $quitDate,
    "first_approval_id"   => $first_approval_id,
    "first_approval_name" => $first_approval_name,
    "card_company" => $card_company,
    "card_number"  => $card_number,
    "pay_bank"     => $pay_bank,
    "pay_account"  => $pay_account,
    "pay_holder"   => $pay_holder,
    "IDnumber"     => $IDnumber,
    "birthday_type" => $birthday_type
);

echo(json_encode($data, JSON_UNESCAPED_UNICODE));
?>
