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

$month = $_POST['month'];
$year = $_POST['year'];

$todo_data = array();
$leave_data = array();
$holiday_data = array();

// 출고일 outputdate 기준
try {
    $stmh = $pdo->query("SELECT num, orderdate, towhom, reply, deadline, work_status, title, first_writer, update_log, searchtag , itemsep 
                         FROM " . $DB . ".todos1 
                         WHERE is_deleted IS NULL 
                         AND MONTH(orderdate) = $month 
                         AND YEAR(orderdate) = $year");

    while($row = $stmh->fetch(PDO::FETCH_ASSOC)) {
        array_push($todo_data, $row);
    }

    $stmh = $pdo->query("SELECT author, al_askdatefrom, al_askdateto, al_item, al_content  
                         FROM " . $DB . ".eworks 
                         WHERE is_deleted IS NULL  AND  al_company='주일'  
                         AND ((MONTH(al_askdatefrom) = $month AND YEAR(al_askdatefrom) = $year) 
                         OR (MONTH(al_askdateto) = $month AND YEAR(al_askdateto) = $year))");

    while($row = $stmh->fetch(PDO::FETCH_ASSOC)) {
        array_push($leave_data, $row);
    }

    // holiday 데이터 가져오기
    $stmh = $pdo->query("SELECT num, startdate, enddate, comment 
                         FROM " . $DB . ".holiday 
                         WHERE is_deleted IS NULL 
                         AND ((MONTH(startdate) = $month AND YEAR(startdate) = $year) 
                         OR (MONTH(enddate) = $month AND YEAR(enddate) = $year))");

    while($row = $stmh->fetch(PDO::FETCH_ASSOC)) {
        array_push($holiday_data, $row);
    }
	


    // 응답 데이터 구성
    $response = array(
        "todo_data" => $todo_data,
        "leave_data" => $leave_data,
        "holiday_data" => $holiday_data,  // holiday 데이터 추가
    );

    echo(json_encode($response, JSON_UNESCAPED_UNICODE));

} catch (PDOException $Exception) {
    print "오류: ".$Exception->getMessage();
}
?>
