false, 'message' => '알 수 없는 요청입니다.']; // 월별 스케줄 데이터 조회 (달력용) if ($action == 'get_monthly_schedule') { $year = (int) ($_GET['year'] ?? 0); $month = (int) ($_GET['month'] ?? 0); if ($year && $month) { $sql = "SELECT id, specific_date, start_time, is_available, temp_1, temp_2 FROM consultant_schedule WHERE YEAR(specific_date) = {$year} AND MONTH(specific_date) = {$month} ORDER BY specific_date, start_time"; $result = sql_query($sql); $schedule_data = []; while ($row = sql_fetch_array($result)) { $schedule_data[] = $row; } $response = ['success' => true, 'data' => $schedule_data]; } else { $response['message'] = '년도와 월 정보가 올바르지 않습니다.'; } } // 스케줄 슬롯 상태 변경 (블락/해제) if ($action == 'toggle_slot_status') { $id = (int) ($_POST['id'] ?? 0); if ($id) { $slot = sql_fetch("SELECT is_available, temp_1, temp_2 FROM consultant_schedule WHERE id = {$id}"); if ($slot) { $is_lunch_override = $_POST['is_lunch_override'] ?? '0'; $new_status = $slot['is_available'] ? 0 : 1; // 기본 토글 동작 $new_temp1 = $slot['temp_1']; $new_temp2 = $slot['temp_2']; $log_msg_action = ''; // 💡 [로직 개선] 휴게시간을 상담시간으로 변경하는 경우 if ($is_lunch_override === '1' && $slot['temp_1'] === 'lunch_time' && $new_status == 1) { $new_temp1 = 'manual_override'; // 휴게시간을 수동으로 변경했음을 명시 $new_temp2 = '관리자 긴급 설정'; $log_msg_action = "휴게시간을 상담 슬롯으로 변경"; } // 💡 [로직 개선] 긴급 설정된 상담시간을 다시 휴게시간으로 되돌리는 경우 else if ($slot['temp_1'] === 'manual_override' && $new_status == 0) { $new_temp1 = 'lunch_time'; $new_temp2 = '점심시간'; $log_msg_action = "긴급 슬롯을 다시 휴게시간으로 복원"; } // 일반 슬롯을 블락/해제하는 경우 else { $new_temp1 = $new_status ? 'auto_generated' : 'manual_block'; $new_temp2 = $new_status ? '' : '관리자 설정'; $log_msg_action = $new_status ? "슬롯 활성화" : "슬롯 비활성화"; } $sql = "UPDATE consultant_schedule SET is_available = '{$new_status}', temp_1 = '{$new_temp1}', temp_2 = '{$new_temp2}', updated_at = NOW() WHERE id = {$id}"; if (sql_query($sql)) { consultant_log("스케줄 수동 변경 (ID:{$id}): {$log_msg_action} (관리자: " . ($member['mb_id'] ?? 'unknown') . ")"); $response = ['success' => true, 'new_status' => $new_status]; } else { $response['message'] = '데이터베이스 업데이트에 실패했습니다.'; } } else { $response['message'] = '해당 스케줄을 찾을 수 없습니다.'; } } else { $response['message'] = 'ID가 제공되지 않았습니다.'; } } // 월별 스케줄 생성 if ($action == 'generate_schedule') { $year = (int) ($_POST['year'] ?? 0); $month = (int) ($_POST['month'] ?? 0); if ($year && $month) { try { $generator = new ScheduleGenerator(); if ($generator->generateMonth($year, $month)) { consultant_log("스케줄 생성/재생성 완료: {$year}년 {$month}월"); $response = ['success' => true, 'message' => "{$year}년 {$month}월 스케줄이 성공적으로 생성되었습니다."]; } else { $response['message'] = "{$year}년 {$month}월 스케줄 생성에 실패했습니다."; } } catch (Exception $e) { $response['message'] = '스케줄 생성 중 오류 발생: ' . $e->getMessage(); } } else { $response['message'] = '년도와 월 정보가 올바르지 않습니다.'; } } echo json_encode($response); exit; } // --- 페이지 로드 시 실행 --- // 월별 스케줄 상태 조회 함수 function get_schedule_generation_status($year, $month) { $year = (int)$year; $month = (int)$month; $sql = "SELECT COUNT(*) as total_slots, SUM(CASE WHEN temp_1 = 'auto_generated' AND is_available = 1 THEN 1 ELSE 0 END) as available_slots, SUM(CASE WHEN temp_1 = 'lunch_time' THEN 1 ELSE 0 END) as lunch_slots, SUM(CASE WHEN temp_1 = 'holiday' THEN 1 ELSE 0 END) as holiday_slots, SUM(CASE WHEN is_available = 0 AND temp_1 NOT IN ('lunch_time', 'holiday') THEN 1 ELSE 0 END) as blocked_slots FROM consultant_schedule WHERE YEAR(specific_date) = {$year} AND MONTH(specific_date) = {$month}"; return sql_fetch($sql); } // 다음 3개월 상태 조회 $next_months = []; for ($i = 0; $i < 3; $i++) { $target_date = mktime(0, 0, 0, date('n') + $i, 1, date('Y')); $year = date('Y', $target_date); $month = date('m', $target_date); $next_months[] = [ 'year' => $year, 'month' => $month, 'name' => date('Y년 n월', $target_date), 'status' => get_schedule_generation_status($year, $month) ]; } include_once(G5_ADMIN_PATH . '/admin.head.php'); ?>