<?php
header('Content-Type: application/json; charset=utf-8');
require_once($_SERVER['DOCUMENT_ROOT'].'/session.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/mydb.php');
$pdo = db_connect();

// JS에서 POST된 JSON 파싱
$data     = json_decode(file_get_contents('php://input'), true);

$level    = intval($data['level']     ?? 0);
$id       = intval($data['id']        ?? 0);      // 0이면 Insert, >0이면 Update
$parentId = intval($data['parentId']  ?? 0);      // 상위 id (Insert 시 사용)
$name     = trim($data['name']        ?? '');

// 예외 처리
if ($level < 1 || $level > 4) {
    echo json_encode(["result"=>"error","msg"=>"invalid level"]);
    exit;
}
if ($name === '') {
    echo json_encode(["result"=>"error","msg"=>"name is empty"]);
    exit;
}

try {
    switch($level) {
        case 1: // category_l1
            if($id === 0) {
                // INSERT
                $sql = "INSERT INTO {$DB}.category_l1 (name) VALUES (:name)";
                $st  = $pdo->prepare($sql);
                $st->bindValue(':name', $name);
                $st->execute();
            } else {
                // UPDATE
                $sql = "UPDATE {$DB}.category_l1 SET name=:name WHERE id=:id";
                $st  = $pdo->prepare($sql);
                $st->bindValue(':name', $name);
                $st->bindValue(':id',   $id, PDO::PARAM_INT);
                $st->execute();
            }
            break;

        case 2: // category_l2
            if($id === 0) {
                // 새로 추가 → parentId가 category_l1의 id
                $sql = "INSERT INTO {$DB}.category_l2 (name, parent_id) VALUES (:name, :p)";
                $st  = $pdo->prepare($sql);
                $st->bindValue(':name', $name);
                $st->bindValue(':p',    $parentId, PDO::PARAM_INT);
                $st->execute();
            } else {
                // 수정
                $sql = "UPDATE {$DB}.category_l2 SET name=:name WHERE id=:id";
                $st  = $pdo->prepare($sql);
                $st->bindValue(':name', $name);
                $st->bindValue(':id',   $id, PDO::PARAM_INT);
                $st->execute();
            }
            break;

        case 3: // category_l3
            if($id === 0) {
                // parentId는 category_l2의 id
                $sql = "INSERT INTO {$DB}.category_l3 (name, parent_id) VALUES (:name, :p)";
                $st  = $pdo->prepare($sql);
                $st->bindValue(':name', $name);
                $st->bindValue(':p',    $parentId, PDO::PARAM_INT);
                $st->execute();
            } else {
                $sql = "UPDATE {$DB}.category_l3 SET name=:name WHERE id=:id";
                $st  = $pdo->prepare($sql);
                $st->bindValue(':name', $name);
                $st->bindValue(':id',   $id, PDO::PARAM_INT);
                $st->execute();
            }
            break;

        case 4: // category_l4
            if($id === 0) {
                // parentId는 category_l3의 id
                $sql = "INSERT INTO {$DB}.category_l4 (name, parent_id) VALUES (:name, :p)";
                $st  = $pdo->prepare($sql);
                $st->bindValue(':name', $name);
                $st->bindValue(':p',    $parentId, PDO::PARAM_INT);
                $st->execute();
            } else {
                $sql = "UPDATE {$DB}.category_l4 SET name=:name WHERE id=:id";
                $st  = $pdo->prepare($sql);
                $st->bindValue(':name', $name);
                $st->bindValue(':id',   $id, PDO::PARAM_INT);
                $st->execute();
            }
            break;
    }

    echo json_encode(["result" => "ok"]);
} catch (Exception $e) {
    echo json_encode([
        "result" => "error",
        "msg" => $e->getMessage()
    ]);
}
