<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/load_GoogleDrive.php';

if (!isset($_SESSION["level"]) || $_SESSION["level"] > 5) {
    header("Content-Type: application/json");
    echo json_encode(['error' => '권한이 없습니다.']);
    exit;
}

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

try {
    // 배서일자가 없는 전자어음 데이터 조회
    $sql = "SELECT num, registDate, content, contentSub, content_detail, amount, dueDate  
            FROM account 
            WHERE bankbook = '전자어음' 
            AND (endorsementDate IS NULL OR endorsementDate = '' OR endorsementDate = '0000-00-00') 
            AND (parentEBNum IS NULL OR parentEBNum = '' ) 
            AND (is_deleted = 0 OR is_deleted IS NULL) 
            ORDER BY registDate DESC, num DESC";
    
    $stmh = $pdo->prepare($sql);
    $stmh->execute();
    $results = $stmh->fetchAll(PDO::FETCH_ASSOC);
    
    header("Content-Type: application/json; charset=utf-8");
    echo json_encode(['success' => true, 'data' => $results], JSON_UNESCAPED_UNICODE);
    
} catch (PDOException $e) {
    header("Content-Type: application/json; charset=utf-8");
    echo json_encode(['error' => '데이터 조회 중 오류가 발생했습니다: ' . $e->getMessage()], JSON_UNESCAPED_UNICODE);
}
?> 