@extends('layouts.app')

@section('title', '일일 스크럼')

@section('content')
<!-- 페이지 헤더 -->
<div class="flex justify-between items-center mb-6">
    <h1 class="text-2xl font-bold text-gray-800">📅 일일 스크럼</h1>
    <div class="flex gap-2">
        <a href="{{ route('daily-logs.today') }}" class="bg-green-600 hover:bg-green-700 text-white px-4 py-2 rounded-lg transition">
            오늘 작성
        </a>
        <button type="button"
                onclick="openCreateModal()"
                class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg transition">
            + 새 로그
        </button>
    </div>
</div>

<!-- 주간 타임라인 -->
<div class="bg-white rounded-lg shadow-sm p-4 mb-6">
    <h2 class="text-sm font-medium text-gray-500 mb-3">최근 7일</h2>
    <div class="grid grid-cols-7 gap-2">
        @foreach($weeklyTimeline as $index => $day)
        <div class="relative group">
            <button type="button"
                    onclick="{{ $day['log'] ? 'scrollToCard(' . $day['log']['id'] . ')' : 'openCreateModalWithDate(\'' . $day['date'] . '\')' }}"
                    data-log-id="{{ $day['log']['id'] ?? '' }}"
                    data-date="{{ $day['date'] }}"
                    class="day-card w-full text-left p-3 rounded-lg border-2 transition-all hover:shadow-md
                           {{ $day['is_today'] ? 'border-blue-500 bg-blue-50' : 'border-gray-200' }}
                           {{ $day['is_weekend'] && !$day['is_today'] ? 'bg-gray-50' : '' }}
                           {{ $day['log'] ? 'cursor-pointer' : 'cursor-pointer hover:border-blue-300' }}">
                <!-- 요일 -->
                <div class="text-xs font-medium {{ $day['is_weekend'] ? 'text-red-500' : 'text-gray-500' }} {{ $day['is_today'] ? 'text-blue-600' : '' }}">
                    {{ $day['day_name'] }}
                </div>
                <!-- 날짜 -->
                <div class="text-lg font-bold {{ $day['is_today'] ? 'text-blue-600' : 'text-gray-800' }}">
                    {{ $day['day_number'] }}
                </div>

                @if($day['log'])
                <!-- 로그 있음: 진행률 표시 -->
                <div class="mt-2">
                    <div class="w-full bg-gray-200 rounded-full h-1.5">
                        <div class="bg-green-500 h-1.5 rounded-full transition-all" style="width: {{ $day['log']['completion_rate'] }}%"></div>
                    </div>
                    <div class="flex justify-between items-center mt-1">
                        <span class="text-xs text-gray-500">{{ $day['log']['entry_stats']['total'] }}건</span>
                        <span class="text-xs font-medium {{ $day['log']['completion_rate'] >= 100 ? 'text-green-600' : 'text-blue-600' }}">
                            {{ $day['log']['completion_rate'] }}%
                        </span>
                    </div>
                </div>
                @else
                <!-- 로그 없음 -->
                <div class="mt-2 text-center">
                    <span class="text-xs text-gray-400">미작성</span>
                </div>
                @endif

                <!-- 오늘 표시 -->
                @if($day['is_today'])
                <div class="absolute -top-1 -right-1 w-3 h-3 bg-blue-500 rounded-full animate-pulse"></div>
                @endif
            </button>

            <!-- 툴팁 (호버 시 표시) -->
            @if($day['log'] && $day['log']['entry_stats']['total'] > 0)
            <div class="hidden group-hover:block absolute z-10 left-1/2 -translate-x-1/2 top-full mt-2 w-36 bg-gray-800 text-white text-xs rounded-lg p-2 shadow-lg pointer-events-none">
                <div class="flex justify-between mb-1">
                    <span>예정</span>
                    <span>{{ $day['log']['entry_stats']['todo'] }}</span>
                </div>
                <div class="flex justify-between mb-1">
                    <span>진행중</span>
                    <span>{{ $day['log']['entry_stats']['in_progress'] }}</span>
                </div>
                <div class="flex justify-between">
                    <span>완료</span>
                    <span>{{ $day['log']['entry_stats']['done'] }}</span>
                </div>
                <div class="absolute -top-1 left-1/2 -translate-x-1/2 w-2 h-2 bg-gray-800 rotate-45"></div>
            </div>
            @endif
        </div>
        @endforeach
    </div>
</div>

<!-- 통계 카드 -->
<div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
    <div class="bg-white rounded-lg shadow-sm p-4">
        <div class="text-sm text-gray-500">전체 로그</div>
        <div class="text-2xl font-bold text-gray-800">{{ $stats['total_logs'] ?? 0 }}</div>
    </div>
    <div class="bg-white rounded-lg shadow-sm p-4">
        <div class="text-sm text-gray-500">최근 7일</div>
        <div class="text-2xl font-bold text-blue-600">{{ $stats['recent_logs'] ?? 0 }}</div>
    </div>
    <div class="bg-white rounded-lg shadow-sm p-4">
        <div class="text-sm text-gray-500">진행중 항목</div>
        <div class="text-2xl font-bold text-yellow-600">{{ $stats['entries']['in_progress'] ?? 0 }}</div>
    </div>
    <div class="bg-white rounded-lg shadow-sm p-4">
        <div class="text-sm text-gray-500">완료 항목</div>
        <div class="text-2xl font-bold text-green-600">{{ $stats['entries']['done'] ?? 0 }}</div>
    </div>
</div>

<!-- 주의 필요 이슈/태스크 -->
@if($attentionIssues['items']->count() > 0 || $attentionTasks['items']->count() > 0)
<div class="bg-white rounded-lg shadow-sm p-4 mb-6">
    <div class="flex items-center justify-between mb-3">
        <h2 class="text-sm font-medium text-gray-500 flex items-center gap-2">
            <svg class="w-4 h-4 text-red-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
            </svg>
            주의 필요 항목
        </h2>
        <div class="flex items-center gap-2 text-xs">
            @if($attentionIssues['overdue_count'] + $attentionTasks['overdue_count'] > 0)
            <span class="px-2 py-1 bg-red-100 text-red-700 rounded-full font-medium">
                마감초과 {{ $attentionIssues['overdue_count'] + $attentionTasks['overdue_count'] }}
            </span>
            @endif
            @if($attentionIssues['due_soon_count'] + $attentionTasks['due_soon_count'] > 0)
            <span class="px-2 py-1 bg-orange-100 text-orange-700 rounded-full font-medium">
                이번주 {{ $attentionIssues['due_soon_count'] + $attentionTasks['due_soon_count'] }}
            </span>
            @endif
            @if($attentionIssues['urgent_count'] + $attentionTasks['urgent_count'] > 0)
            <span class="px-2 py-1 bg-purple-100 text-purple-700 rounded-full font-medium">
                긴급 {{ $attentionIssues['urgent_count'] + $attentionTasks['urgent_count'] }}
            </span>
            @endif
            <span class="text-gray-400">
                {{ $attentionIssues['items']->count() + $attentionTasks['items']->count() }}건
            </span>
        </div>
    </div>
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
        @php
            // 이슈를 팀별로 그룹핑 (team_display 사용)
            $issuesByTeam = $attentionIssues['items']->groupBy(fn($issue) => $issue->team_display ?? '팀 미지정');
            // 태스크를 담당자 기준 그룹핑 (프로젝트 팀 정보가 없으므로 담당자로)
            $tasksByAssignee = $attentionTasks['items']->groupBy(fn($task) => $task->assignee?->name ?? '담당자 미지정');

            // 팀 키 수집
            $allTeams = $issuesByTeam->keys();
        @endphp

        {{-- 이슈: 팀별 카드 --}}
        @foreach($allTeams as $teamName)
        @php
            $teamIssues = $issuesByTeam->get($teamName, collect());
        @endphp
        <div class="attention-project-card bg-white rounded-lg p-3 border border-red-200 hover:border-red-300 transition-all">
            <!-- 팀 헤더 -->
            <div class="flex items-center justify-between mb-2 pb-2 border-b border-gray-100">
                <div class="flex items-center gap-2">
                    <svg class="w-4 h-4 text-red-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"/>
                    </svg>
                    <span class="text-sm font-semibold text-gray-800">{{ Str::limit($teamName, 12) }}</span>
                    <span class="text-xs text-gray-400">({{ $teamIssues->count() }})</span>
                </div>
                <span class="px-1.5 py-0.5 text-[10px] font-medium bg-red-100 text-red-600 rounded">이슈</span>
            </div>

            <!-- 이슈 목록 -->
            <div class="space-y-1.5 max-h-48 overflow-y-auto">
                @foreach($teamIssues as $issue)
                <div class="group" data-issue-id="{{ $issue->id }}">
                    <div class="flex items-center gap-2">
                        <!-- D-day/상태 -->
                        <div class="flex items-center gap-1 shrink-0">
                            @if($issue->is_urgent)
                            <span class="w-1.5 h-1.5 rounded-full bg-purple-500 animate-pulse" title="긴급"></span>
                            @endif
                            @if($issue->due_status === 'overdue')
                            <span class="px-1 py-0.5 text-[9px] font-bold bg-red-100 text-red-700 rounded">D+{{ abs($issue->dday) }}</span>
                            @elseif($issue->due_status === 'due_soon')
                            <span class="px-1 py-0.5 text-[9px] font-bold bg-orange-100 text-orange-700 rounded">D{{ $issue->dday == 0 ? '-day' : $issue->dday }}</span>
                            @endif
                        </div>
                        <!-- 타입 아이콘 -->
                        <span class="shrink-0">
                            @if($issue->type === 'bug')
                            <svg class="w-3.5 h-3.5 text-red-500" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M6.56 1.14a.75.75 0 01.177 1.045 3.989 3.989 0 00-.464.86c.185.17.382.329.59.473A3.993 3.993 0 0110 2.75c1.237 0 2.368.56 3.137 1.473.208-.144.405-.303.59-.473a3.989 3.989 0 00-.464-.86.75.75 0 011.222-.869c.369.519.65 1.105.822 1.736a.75.75 0 01-.174.707 6.613 6.613 0 01-1.378 1.151 3.97 3.97 0 01-1.041 2.024l.537.134a2.24 2.24 0 012.187 2.712.75.75 0 01-1.472-.294.74.74 0 00-.725-.902l-3.444-.861a.75.75 0 01-.553-.535 2.468 2.468 0 00-1.394-1.572 2.46 2.46 0 00-1.024-.18.75.75 0 01-.553.536l-3.444.861a.74.74 0 00-.725.902.75.75 0 01-1.472.294 2.24 2.24 0 012.187-2.712l.537-.134a3.97 3.97 0 01-1.04-2.024 6.613 6.613 0 01-1.38-1.15.75.75 0 01-.174-.708c.173-.631.454-1.217.823-1.736a.75.75 0 011.046-.178z" clip-rule="evenodd"/></svg>
                            @elseif($issue->type === 'feature')
                            <svg class="w-3.5 h-3.5 text-blue-500" fill="currentColor" viewBox="0 0 20 20"><path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"/></svg>
                            @else
                            <svg class="w-3.5 h-3.5 text-green-500" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M12 7a1 1 0 110-2h5a1 1 0 011 1v5a1 1 0 11-2 0V8.414l-4.293 4.293a1 1 0 01-1.414 0L8 10.414l-4.293 4.293a1 1 0 01-1.414-1.414l5-5a1 1 0 011.414 0L11 10.586 14.586 7H12z" clip-rule="evenodd"/></svg>
                            @endif
                        </span>
                        <!-- 제목 -->
                        <span class="attention-item-text flex-1 text-sm text-gray-700 truncate cursor-pointer hover:text-red-600"
                              data-toggle-attention
                              title="{{ $issue->title }}">{{ $issue->title }}</span>
                        <!-- 상태변경 버튼 -->
                        <div class="flex gap-0.5 opacity-30 group-hover:opacity-100 transition-opacity shrink-0">
                            @if($issue->status === 'open')
                            <button onclick="event.stopPropagation(); updateIssueStatus({{ $issue->id }}, 'in_progress')"
                                    class="p-0.5 text-yellow-500 hover:bg-yellow-50 rounded" title="처리중">
                                <svg class="w-3.5 h-3.5" fill="currentColor" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9"/></svg>
                            </button>
                            @endif
                            <button onclick="event.stopPropagation(); updateIssueStatus({{ $issue->id }}, 'resolved')"
                                    class="p-0.5 text-green-500 hover:bg-green-50 rounded" title="해결">
                                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                                </svg>
                            </button>
                        </div>
                    </div>
                    <!-- 펼쳐지는 상세 정보: 회사/팀/담당자 -->
                    <div class="attention-item-full hidden mt-1.5 text-xs bg-red-50 rounded p-2">
                        <div class="grid grid-cols-2 gap-x-3 gap-y-1">
                            @if($issue->due_date)
                            <div class="flex items-center gap-1">
                                <span class="text-gray-400">마감:</span>
                                <span class="text-gray-700 font-medium">{{ $issue->due_date->format('m/d') }}</span>
                            </div>
                            @endif
                            <div class="flex items-center gap-1">
                                <span class="text-gray-400">상태:</span>
                                <span class="px-1.5 py-0.5 text-[10px] rounded {{ $issue->status === 'open' ? 'bg-red-100 text-red-700' : 'bg-yellow-100 text-yellow-700' }}">
                                    {{ $issue->status === 'open' ? '대기중' : '처리중' }}
                                </span>
                            </div>
                            @if($issue->client)
                            <div class="flex items-center gap-1">
                                <span class="text-gray-400">회사:</span>
                                <span class="text-gray-700">{{ $issue->client }}</span>
                            </div>
                            @endif
                            <div class="flex items-center gap-1">
                                <span class="text-gray-400">팀:</span>
                                <span class="text-gray-700">{{ $issue->team_display ?? '-' }}</span>
                            </div>
                            <div class="flex items-center gap-1">
                                <span class="text-gray-400">담당:</span>
                                <span class="text-gray-700">{{ $issue->assignee_display ?? '-' }}</span>
                            </div>
                            <div class="flex items-center gap-1 col-span-2">
                                <span class="text-gray-400">프로젝트:</span>
                                <span class="text-gray-700">{{ $issue->project?->name ?? '-' }}</span>
                            </div>
                        </div>
                    </div>
                </div>
                @endforeach
            </div>
        </div>
        @endforeach

        {{-- 태스크: 담당자별 카드 --}}
        @foreach($tasksByAssignee as $assigneeName => $assigneeTasks)
        <div class="attention-project-card bg-white rounded-lg p-3 border border-orange-200 hover:border-orange-300 transition-all">
            <!-- 담당자 헤더 -->
            <div class="flex items-center justify-between mb-2 pb-2 border-b border-gray-100">
                <div class="flex items-center gap-2">
                    <svg class="w-4 h-4 text-orange-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/>
                    </svg>
                    <span class="text-sm font-semibold text-gray-800">{{ Str::limit($assigneeName, 12) }}</span>
                    <span class="text-xs text-gray-400">({{ $assigneeTasks->count() }})</span>
                </div>
                <span class="px-1.5 py-0.5 text-[10px] font-medium bg-orange-100 text-orange-600 rounded">태스크</span>
            </div>

            <!-- 태스크 목록 -->
            <div class="space-y-1.5 max-h-48 overflow-y-auto">
                @foreach($assigneeTasks as $task)
                <div class="group" data-task-id="{{ $task->id }}">
                    <div class="flex items-center gap-2">
                        <!-- D-day/상태 -->
                        <div class="flex items-center gap-1 shrink-0">
                            @if($task->is_urgent)
                            <span class="w-1.5 h-1.5 rounded-full bg-purple-500 animate-pulse" title="긴급"></span>
                            @endif
                            @if($task->due_status === 'overdue')
                            <span class="px-1 py-0.5 text-[9px] font-bold bg-red-100 text-red-700 rounded">D+{{ abs($task->dday) }}</span>
                            @elseif($task->due_status === 'due_soon')
                            <span class="px-1 py-0.5 text-[9px] font-bold bg-orange-100 text-orange-700 rounded">D{{ $task->dday == 0 ? '-day' : $task->dday }}</span>
                            @endif
                        </div>
                        <!-- 우선순위 아이콘 -->
                        <span class="shrink-0">
                            @if($task->priority === 'high')
                            <svg class="w-3.5 h-3.5 text-red-500" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M5.293 9.707a1 1 0 010-1.414l4-4a1 1 0 011.414 0l4 4a1 1 0 01-1.414 1.414L11 7.414V15a1 1 0 11-2 0V7.414L6.707 9.707a1 1 0 01-1.414 0z" clip-rule="evenodd"/></svg>
                            @elseif($task->priority === 'medium')
                            <svg class="w-3.5 h-3.5 text-yellow-500" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z" clip-rule="evenodd"/></svg>
                            @else
                            <svg class="w-3.5 h-3.5 text-gray-400" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M14.707 10.293a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 111.414-1.414L9 12.586V5a1 1 0 012 0v7.586l2.293-2.293a1 1 0 011.414 0z" clip-rule="evenodd"/></svg>
                            @endif
                        </span>
                        <!-- 제목 -->
                        <span class="attention-item-text flex-1 text-sm text-gray-700 truncate cursor-pointer hover:text-orange-600"
                              data-toggle-attention
                              title="{{ $task->title }}">{{ $task->title }}</span>
                        <!-- 상태변경 버튼 -->
                        <div class="flex gap-0.5 opacity-30 group-hover:opacity-100 transition-opacity shrink-0">
                            @if($task->status === 'todo')
                            <button onclick="event.stopPropagation(); updateTaskStatus({{ $task->id }}, 'in_progress')"
                                    class="p-0.5 text-yellow-500 hover:bg-yellow-50 rounded" title="진행중">
                                <svg class="w-3.5 h-3.5" fill="currentColor" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9"/></svg>
                            </button>
                            @endif
                            <button onclick="event.stopPropagation(); updateTaskStatus({{ $task->id }}, 'done')"
                                    class="p-0.5 text-green-500 hover:bg-green-50 rounded" title="완료">
                                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                                </svg>
                            </button>
                        </div>
                    </div>
                    <!-- 펼쳐지는 상세 정보 -->
                    <div class="attention-item-full hidden mt-1.5 text-xs bg-orange-50 rounded p-2">
                        <div class="grid grid-cols-2 gap-x-3 gap-y-1">
                            @if($task->due_date)
                            <div class="flex items-center gap-1">
                                <span class="text-gray-400">마감:</span>
                                <span class="text-gray-700 font-medium">{{ $task->due_date->format('m/d') }}</span>
                            </div>
                            @endif
                            <div class="flex items-center gap-1">
                                <span class="text-gray-400">상태:</span>
                                <span class="px-1.5 py-0.5 text-[10px] rounded {{ $task->status === 'todo' ? 'bg-gray-100 text-gray-700' : 'bg-yellow-100 text-yellow-700' }}">
                                    {{ $task->status === 'todo' ? '예정' : '진행중' }}
                                </span>
                            </div>
                            <div class="flex items-center gap-1">
                                <span class="text-gray-400">담당:</span>
                                <span class="text-gray-700">{{ $task->assignee?->name ?? '-' }}</span>
                            </div>
                            <div class="flex items-center gap-1">
                                <span class="text-gray-400">우선순위:</span>
                                <span class="text-gray-700">{{ $task->priority === 'high' ? '높음' : ($task->priority === 'medium' ? '보통' : '낮음') }}</span>
                            </div>
                            <div class="flex items-center gap-1 col-span-2">
                                <span class="text-gray-400">프로젝트:</span>
                                <span class="text-gray-700">{{ $task->project?->name ?? '-' }}</span>
                            </div>
                        </div>
                    </div>
                </div>
                @endforeach
            </div>
        </div>
        @endforeach
    </div>
</div>
@endif

<!-- 미완료 항목 (예정/진행중) - 담당자별 그룹핑 -->
@if(count($pendingEntries) > 0)
<div class="bg-white rounded-lg shadow-sm p-4 mb-6">
    <div class="flex items-center justify-between mb-3">
        <h2 class="text-sm font-medium text-gray-500 flex items-center gap-2">
            <svg class="w-4 h-4 text-indigo-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
            </svg>
            미완료 항목
        </h2>
        @php $totalPending = collect($pendingEntries)->sum('total_count'); @endphp
        <span class="text-xs text-gray-400">{{ count($pendingEntries) }}명 / {{ $totalPending }}건</span>
    </div>
    <div class="flex flex-wrap gap-4" id="pendingEntriesGrid">
        @foreach($pendingEntries as $group)
        @php
            $entriesJson = collect($group['entries'])->map(fn($e) => [
                'id' => $e['id'],
                'content' => $e['content'],
                'status' => $e['status'],
                'daily_log_id' => $e['daily_log_id'] ?? null,
            ])->toJson();
        @endphp
        <div class="pending-assignee-card bg-white rounded-lg p-3 border border-gray-200 hover:border-blue-300 transition-all shrink-0"
             style="width: 300px;"
             data-assignee="{{ $group['assignee_name'] }}">
            <!-- 담당자 이름 + 수정 버튼 -->
            <div class="flex items-center justify-between mb-2 pb-2 border-b border-gray-100">
                <div class="flex items-center gap-2">
                    <span class="text-sm font-semibold text-gray-900">{{ $group['assignee_name'] }}</span>
                    <span class="text-xs text-gray-400">({{ $group['total_count'] }})</span>
                </div>
                <button onclick='openPendingEditModal({!! htmlspecialchars($entriesJson, ENT_QUOTES) !!}, "{{ addslashes($group['assignee_name']) }}")'
                        class="p-1 text-gray-400 hover:text-indigo-600 hover:bg-indigo-50 rounded transition-colors" title="수정">
                    <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                    </svg>
                </button>
            </div>
            <!-- 항목 목록 -->
            <div class="space-y-1.5">
                @foreach($group['entries'] as $entry)
                <div class="group" data-entry-id="{{ $entry['id'] }}">
                    <div class="flex items-center gap-2">
                        <!-- 상태 + 날짜 -->
                        <span class="px-1.5 py-0.5 text-[10px] rounded shrink-0 {{ $entry['status'] === 'in_progress' ? 'bg-yellow-100 text-yellow-700' : 'bg-gray-100 text-gray-600' }}">
                            {{ $entry['status'] === 'in_progress' ? '진행' : '예정' }}
                        </span>
                        <span class="text-[10px] text-gray-400 shrink-0">{{ \Carbon\Carbon::parse($entry['log_date'])->format('m/d') }}</span>
                        <!-- 내용 (클릭하면 펼침) -->
                        <span class="pending-entry-text flex-1 text-sm text-gray-700 truncate cursor-pointer hover:text-blue-600"
                              onclick="togglePendingEntry(this)"
                              title="{{ $entry['content'] }}">{{ $entry['content'] }}</span>
                        <!-- 상태변경 버튼 -->
                        <div class="flex gap-0.5 opacity-30 group-hover:opacity-100 transition-opacity shrink-0">
                            @if($entry['status'] !== 'todo')
                            <button onclick="event.stopPropagation(); updatePendingStatus({{ $entry['id'] }}, 'todo')"
                                    class="p-0.5 text-gray-400 hover:bg-gray-100 rounded" title="예정">
                                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9" stroke-width="2"/></svg>
                            </button>
                            @endif
                            @if($entry['status'] !== 'in_progress')
                            <button onclick="event.stopPropagation(); updatePendingStatus({{ $entry['id'] }}, 'in_progress')"
                                    class="p-0.5 text-yellow-500 hover:bg-yellow-50 rounded" title="진행중">
                                <svg class="w-3.5 h-3.5" fill="currentColor" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9"/></svg>
                            </button>
                            @endif
                            <button onclick="event.stopPropagation(); updatePendingStatus({{ $entry['id'] }}, 'done')"
                                    class="p-0.5 text-green-500 hover:bg-green-50 rounded" title="완료">
                                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                                </svg>
                            </button>
                        </div>
                    </div>
                    <!-- 펼쳐지는 전체 내용 (전체 너비) -->
                    <div class="pending-entry-full hidden mt-1.5 text-sm text-gray-600 bg-gray-50 rounded p-2 whitespace-pre-wrap">{{ $entry['content'] }}</div>
                </div>
                @endforeach
            </div>
        </div>
        @endforeach
    </div>
</div>
@endif

<!-- 필터 영역 -->
<div class="bg-white rounded-lg shadow-sm p-4 mb-6">
    <form id="filterForm" class="flex gap-4 flex-wrap">
        <!-- 검색 -->
        <div class="flex-1 min-w-[200px]">
            <input type="text"
                   name="search"
                   placeholder="요약, 담당자, 내용으로 검색..."
                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
        </div>

        <!-- 프로젝트 필터 -->
        <div class="w-48">
            <select name="project_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                <option value="">전체 프로젝트</option>
                @foreach($projects as $project)
                    <option value="{{ $project->id }}">{{ $project->name }}</option>
                @endforeach
            </select>
        </div>

        <!-- 날짜 범위 -->
        <div class="w-40">
            <input type="date"
                   name="start_date"
                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
        </div>
        <span class="self-center text-gray-500">~</span>
        <div class="w-40">
            <input type="date"
                   name="end_date"
                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
        </div>

        <!-- 삭제된 항목 포함 -->
        <div class="w-36">
            <select name="trashed" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                <option value="">활성만</option>
                <option value="with">삭제 포함</option>
                <option value="only">삭제만</option>
            </select>
        </div>

        <!-- 검색 버튼 -->
        <button type="submit" class="bg-gray-600 hover:bg-gray-700 text-white px-6 py-2 rounded-lg transition">
            검색
        </button>
    </form>
</div>

<!-- 테이블 영역 (HTMX로 로드) -->
<div id="log-table"
     hx-get="/api/admin/daily-logs"
     hx-trigger="load, filterSubmit from:body"
     hx-include="#filterForm"
     hx-headers='{"X-CSRF-TOKEN": "{{ csrf_token() }}"}'
     class="bg-white rounded-lg shadow-sm overflow-hidden">
    <!-- 로딩 스피너 -->
    <div class="flex justify-center items-center p-12">
        <div class="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600"></div>
    </div>
</div>

<!-- 생성/수정 모달 -->
@include('daily-logs.partials.modal-form', [
    'projects' => $projects,
    'assigneeTypes' => $assigneeTypes,
    'entryStatuses' => $entryStatuses,
    'assignees' => $assignees
])

<!-- 미완료 항목 수정 모달 -->
<div id="pendingEditModal" class="fixed inset-0 z-50 hidden overflow-y-auto" aria-labelledby="pending-edit-modal-title" role="dialog" aria-modal="true">
    <div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
        <div class="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" onclick="closePendingEditModal()"></div>
        <span class="hidden sm:inline-block sm:align-middle sm:h-screen">&#8203;</span>
        <div class="inline-block align-bottom bg-white rounded-lg px-4 pt-5 pb-4 text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-2xl sm:w-full sm:p-6">
            <div>
                <div class="flex items-center justify-between mb-4">
                    <h3 class="text-lg font-medium text-gray-900 flex items-center gap-2" id="pending-edit-modal-title">
                        <svg class="w-5 h-5 text-indigo-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                        </svg>
                        <span>미완료 항목 수정</span>
                    </h3>
                    <button type="button" onclick="closePendingEditModal()" class="text-gray-400 hover:text-gray-500">
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                        </svg>
                    </button>
                </div>

                <div class="mb-4 p-3 bg-gray-50 rounded-lg">
                    <label class="text-xs text-gray-500 block mb-1">담당자</label>
                    <!-- 기존 항목 수정 시: 텍스트 표시 -->
                    <p id="pendingEditAssigneeNameText" class="font-medium text-gray-900 hidden"></p>
                    <!-- 새 항목 추가 시: 입력 필드 (datalist로 자동완성) -->
                    <input type="text" id="pendingEditAssigneeNameInput"
                           list="assigneeDatalist"
                           class="w-full px-3 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 hidden"
                           placeholder="담당자 이름 입력 또는 선택" autocomplete="off" required>
                    <datalist id="assigneeDatalist">
                        @foreach($assignees['teams'] ?? [] as $team)
                        <option value="{{ $team['name'] }}"></option>
                        @endforeach
                        @foreach($assignees['users'] ?? [] as $user)
                        <option value="{{ $user['name'] }}"></option>
                        @endforeach
                    </datalist>
                </div>

                <form id="pendingEditForm" onsubmit="submitPendingEditEntries(event)">
                    <!-- 업무 항목 목록 -->
                    <div class="mb-4">
                        <div class="flex items-center justify-between mb-2">
                            <label class="block text-sm font-medium text-gray-700">업무 내용</label>
                            <button type="button" onclick="addPendingEntryRow()" class="text-xs text-indigo-600 hover:text-indigo-800 flex items-center gap-1">
                                <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                                </svg>
                                항목 추가
                            </button>
                        </div>
                        <div id="pendingEditEntriesContainer" class="space-y-2">
                            <!-- 동적으로 항목 행들이 추가됨 -->
                        </div>
                    </div>

                    <div class="flex gap-3 pt-4 border-t border-gray-200">
                        <button type="button" onclick="closePendingEditModal()"
                                class="flex-1 px-4 py-2 border border-gray-300 rounded-lg text-gray-700 hover:bg-gray-50">
                            취소
                        </button>
                        <button type="submit" id="pendingEditSubmitBtn"
                                class="flex-1 px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 disabled:opacity-50">
                            저장
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
    // 담당자 데이터
    const assignees = @json($assignees);

    // HTML 이스케이프 및 줄바꿈 처리 헬퍼
    function escapeHtml(text) {
        if (!text) return '';
        const div = document.createElement('div');
        div.textContent = text;
        return div.innerHTML;
    }

    function nl2br(text) {
        if (!text) return '';
        return escapeHtml(text).replace(/\n/g, '<br>');
    }

    // ========================================
    // 이슈/태스크 상태 변경 기능
    // ========================================
    function updateIssueStatus(issueId, status) {
        fetch(`/api/admin/pm/issues/${issueId}/status`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content || ''
            },
            body: JSON.stringify({ status })
        })
        .then(res => res.json())
        .then(result => {
            if (result.success) {
                // 해당 이슈 행 제거 (fade out)
                const issueRow = document.querySelector(`[data-issue-id="${issueId}"]`);
                if (issueRow) {
                    issueRow.style.transition = 'opacity 0.3s, transform 0.3s';
                    issueRow.style.opacity = '0';
                    issueRow.style.transform = 'translateX(20px)';
                    setTimeout(() => {
                        issueRow.remove();
                        checkEmptyAttentionSection();
                    }, 300);
                }
                showToast(`이슈가 "${status === 'resolved' ? '해결됨' : '처리중'}"으로 변경되었습니다.`, 'success');
            } else {
                showToast(result.message || '상태 변경에 실패했습니다.', 'error');
            }
        })
        .catch(err => {
            console.error(err);
            showToast('상태 변경 중 오류가 발생했습니다.', 'error');
        });
    }

    function updateTaskStatus(taskId, status) {
        fetch(`/api/admin/pm/tasks/${taskId}/status`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content || ''
            },
            body: JSON.stringify({ status })
        })
        .then(res => res.json())
        .then(result => {
            if (result.success) {
                // 해당 태스크 행 제거 (fade out)
                const taskRow = document.querySelector(`[data-task-id="${taskId}"]`);
                if (taskRow) {
                    taskRow.style.transition = 'opacity 0.3s, transform 0.3s';
                    taskRow.style.opacity = '0';
                    taskRow.style.transform = 'translateX(20px)';
                    setTimeout(() => {
                        taskRow.remove();
                        checkEmptyAttentionSection();
                    }, 300);
                }
                showToast(`태스크가 "${status === 'done' ? '완료' : '진행중'}"으로 변경되었습니다.`, 'success');
            } else {
                showToast(result.message || '상태 변경에 실패했습니다.', 'error');
            }
        })
        .catch(err => {
            console.error(err);
            showToast('상태 변경 중 오류가 발생했습니다.', 'error');
        });
    }

    // 주의 필요 섹션이 비어있으면 전체 섹션 숨기기
    function checkEmptyAttentionSection() {
        // 프로젝트 카드 내 이슈/태스크 체크
        const projectCards = document.querySelectorAll('.attention-project-card');

        projectCards.forEach(card => {
            const issueItems = card.querySelectorAll('[data-issue-id]');
            const taskItems = card.querySelectorAll('[data-task-id]');

            // 카드 내 모든 항목이 제거되면 카드도 제거
            if (issueItems.length === 0 && taskItems.length === 0) {
                card.style.transition = 'opacity 0.3s, transform 0.3s';
                card.style.opacity = '0';
                card.style.transform = 'scale(0.95)';
                setTimeout(() => card.remove(), 300);
            }
        });

        // 모든 카드가 제거되면 전체 섹션 숨기기
        setTimeout(() => {
            const remainingCards = document.querySelectorAll('.attention-project-card');
            if (remainingCards.length === 0) {
                const attentionSection = document.querySelector('.attention-project-card')?.closest('.bg-white.rounded-lg.shadow-sm');
                if (!attentionSection) {
                    // 섹션 자체를 찾아서 제거
                    const allSections = document.querySelectorAll('.bg-white.rounded-lg.shadow-sm.p-4.mb-6');
                    allSections.forEach(section => {
                        if (section.querySelector('h2')?.textContent?.includes('주의 필요')) {
                            section.style.transition = 'opacity 0.3s';
                            section.style.opacity = '0';
                            setTimeout(() => section.remove(), 300);
                        }
                    });
                }
            }
        }, 350);
    }

    // ========================================
    // 카드 아코디언 기능 (table.blade.php에서 이동)
    // ========================================
    let cardOpenAccordionId = null;

    function toggleCardAccordion(logId, event) {
        // 클릭한 요소가 버튼이면 무시
        if (event.target.closest('button') || event.target.closest('a')) {
            return;
        }

        const card = document.querySelector(`.log-card[data-log-id="${logId}"]`);
        if (!card) return;

        const accordion = card.querySelector('.card-accordion');
        const chevron = card.querySelector('.accordion-chevron');

        // 같은 카드를 다시 클릭하면 닫기
        if (cardOpenAccordionId === logId) {
            accordion.classList.add('hidden');
            chevron.classList.remove('rotate-90');
            card.classList.remove('ring-2', 'ring-blue-500');
            cardOpenAccordionId = null;
            return;
        }

        // 다른 열린 아코디언 닫기
        if (cardOpenAccordionId !== null) {
            const prevCard = document.querySelector(`.log-card[data-log-id="${cardOpenAccordionId}"]`);
            if (prevCard) {
                prevCard.querySelector('.card-accordion')?.classList.add('hidden');
                prevCard.querySelector('.accordion-chevron')?.classList.remove('rotate-90');
                prevCard.classList.remove('ring-2', 'ring-blue-500');
            }
        }

        // 현재 아코디언 열기
        accordion.classList.remove('hidden');
        chevron.classList.add('rotate-90');
        card.classList.add('ring-2', 'ring-blue-500');
        cardOpenAccordionId = logId;

        // 데이터 로드
        loadCardAccordionContent(logId);
    }

    function loadCardAccordionContent(logId) {
        const contentDiv = document.getElementById(`card-accordion-content-${logId}`);
        if (!contentDiv) return;

        contentDiv.innerHTML = '<div class="text-center py-4 text-gray-500">로딩 중...</div>';

        fetch(`/api/admin/daily-logs/${logId}`, {
            headers: {
                'Accept': 'application/json',
                'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content || ''
            }
        })
        .then(res => res.json())
        .then(data => {
            if (data.success) {
                renderCardAccordionContent(logId, data.data);
            } else {
                contentDiv.innerHTML = '<div class="text-center py-4 text-red-500">데이터 로드 실패</div>';
            }
        })
        .catch(err => {
            contentDiv.innerHTML = '<div class="text-center py-4 text-red-500">데이터 로드 실패</div>';
        });
    }

    function renderCardAccordionContent(logId, log) {
        const contentDiv = document.getElementById(`card-accordion-content-${logId}`);
        if (!contentDiv) return;

        const statusColors = {
            'todo': 'bg-gray-100 text-gray-700',
            'in_progress': 'bg-yellow-100 text-yellow-700',
            'done': 'bg-green-100 text-green-700'
        };
        const statusLabels = {
            'todo': '예정',
            'in_progress': '진행중',
            'done': '완료'
        };

        let entriesHtml = '';
        if (log.entries && log.entries.length > 0) {
            // 담당자별로 그룹핑
            const grouped = {};
            log.entries.forEach(entry => {
                const name = entry.assignee_name || '미지정';
                if (!grouped[name]) {
                    grouped[name] = [];
                }
                grouped[name].push(entry);
            });

            // 담당자별 카드 생성
            entriesHtml = Object.entries(grouped).map(([assigneeName, entries]) => `
                <div class="bg-white rounded-lg border border-gray-200 overflow-hidden">
                    <div class="px-3 py-2 bg-gray-100 border-b border-gray-200 flex items-center justify-between">
                        <span class="text-sm font-semibold text-gray-900">${escapeHtml(assigneeName)}</span>
                        <span class="text-xs text-gray-500">${entries.length}건</span>
                    </div>
                    <div class="divide-y divide-gray-100">
                        ${entries.map(entry => `
                            <div class="p-3 hover:bg-gray-50" data-entry-id="${entry.id}">
                                <div class="flex items-start gap-2">
                                    <span class="px-1.5 py-0.5 text-[10px] rounded shrink-0 ${statusColors[entry.status]}">${statusLabels[entry.status]}</span>
                                    <div class="flex-1 min-w-0">
                                        <p class="text-sm text-gray-700">${nl2br(entry.content)}</p>
                                    </div>
                                    <div class="flex items-center gap-0.5 shrink-0">
                                        ${entry.status !== 'todo' ? `
                                        <button onclick="updateCardEntryStatus(${logId}, ${entry.id}, 'todo')" class="p-1 text-gray-400 hover:bg-gray-100 rounded" title="예정">
                                            <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9" stroke-width="2"/></svg>
                                        </button>` : ''}
                                        ${entry.status !== 'in_progress' ? `
                                        <button onclick="updateCardEntryStatus(${logId}, ${entry.id}, 'in_progress')" class="p-1 text-yellow-500 hover:bg-yellow-50 rounded" title="진행중">
                                            <svg class="w-3.5 h-3.5" fill="currentColor" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9"/></svg>
                                        </button>` : ''}
                                        ${entry.status !== 'done' ? `
                                        <button onclick="updateCardEntryStatus(${logId}, ${entry.id}, 'done')" class="p-1 text-green-500 hover:bg-green-50 rounded" title="완료">
                                            <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
                                        </button>` : ''}
                                        <button onclick="deleteCardEntry(${logId}, ${entry.id})" class="p-1 text-red-400 hover:bg-red-50 rounded" title="삭제">
                                            <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        `).join('')}
                    </div>
                </div>
            `).join('');
        } else {
            entriesHtml = '<div class="text-center py-4 text-gray-400">등록된 항목이 없습니다.</div>';
        }

        const summaryHtml = log.summary ? `
            <div class="mb-4 p-3 bg-white rounded-lg border border-gray-200">
                <div class="text-xs font-medium text-gray-500 mb-1">요약</div>
                <div class="text-sm text-gray-700">${nl2br(log.summary)}</div>
            </div>
        ` : '';

        contentDiv.innerHTML = `
            <div class="space-y-3">
                ${summaryHtml}
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3">
                    ${entriesHtml}
                </div>
                <div class="pt-3 border-t border-gray-200 flex justify-between items-center">
                    <button onclick="openQuickAddCardEntry(${logId})" class="text-sm text-blue-600 hover:text-blue-800 font-medium">
                        + 항목 추가
                    </button>
                    <button onclick="editLog(${logId})" class="text-sm text-indigo-600 hover:text-indigo-800 font-medium">
                        전체 수정
                    </button>
                </div>
            </div>
        `;
    }

    function updateCardEntryStatus(logId, entryId, status) {
        fetch(`/api/admin/daily-logs/entries/${entryId}/status`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content || ''
            },
            body: JSON.stringify({ status })
        })
        .then(res => res.json())
        .then(result => {
            if (result.success) {
                loadCardAccordionContent(logId);
            }
        });
    }

    function deleteCardEntry(logId, entryId) {
        showConfirm('이 항목을 삭제하시겠습니까?', () => {
            fetch(`/api/admin/daily-logs/entries/${entryId}`, {
                method: 'DELETE',
                headers: {
                    'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content || ''
                }
            })
            .then(res => res.json())
            .then(result => {
                if (result.success) {
                    loadCardAccordionContent(logId);
                }
            });
        }, { title: '항목 삭제', icon: 'warning' });
    }

    function openQuickAddCardEntry(logId) {
        if (typeof openQuickAddModal === 'function') {
            openQuickAddModal(logId);
        } else {
            showToast('모달을 열 수 없습니다. 페이지를 새로고침해주세요.', 'warning');
        }
    }

    // 주간 타임라인에서 카드로 스크롤
    function scrollToCard(logId) {
        const card = document.querySelector(`.log-card[data-log-id="${logId}"]`);
        if (card) {
            card.scrollIntoView({ behavior: 'smooth', block: 'center' });
            setTimeout(() => {
                const fakeEvent = { target: card };
                toggleCardAccordion(logId, fakeEvent);
            }, 300);
        }
    }

    // HTMX 로드 완료 후 카드 아코디언 상태 리셋
    document.body.addEventListener('htmx:afterSwap', function(event) {
        if (event.detail.target.id === 'log-table') {
            cardOpenAccordionId = null;
        }
    });

    // ISO 날짜를 YYYY-MM-DD 형식으로 변환
    function formatDateForInput(dateString) {
        if (!dateString) return '';
        // ISO 형식 또는 일반 날짜 문자열에서 YYYY-MM-DD 추출
        return dateString.substring(0, 10);
    }

    // 폼 제출 시 HTMX 이벤트 트리거
    document.getElementById('filterForm').addEventListener('submit', function(e) {
        e.preventDefault();
        htmx.trigger('#log-table', 'filterSubmit');
    });

    // 모달 열기
    function openCreateModal() {
        document.getElementById('modalTitle').textContent = '새 일일 로그';
        document.getElementById('logForm').reset();
        document.getElementById('logId').value = '';
        document.getElementById('logDate').value = new Date().toISOString().split('T')[0];
        clearEntries();
        document.getElementById('logModal').classList.remove('hidden');
    }

    // 특정 날짜로 모달 열기 (주간 타임라인에서 사용)
    function openCreateModalWithDate(date) {
        document.getElementById('modalTitle').textContent = '새 일일 로그';
        document.getElementById('logForm').reset();
        document.getElementById('logId').value = '';
        document.getElementById('logDate').value = date;
        clearEntries();
        document.getElementById('logModal').classList.remove('hidden');
    }

    // 모달 닫기
    function closeModal() {
        document.getElementById('logModal').classList.add('hidden');
    }

    // 로그 수정 모달 열기
    function editLog(id) {
        fetch(`/api/admin/daily-logs/${id}`, {
            headers: {
                'Accept': 'application/json',
                'X-CSRF-TOKEN': '{{ csrf_token() }}'
            }
        })
        .then(res => res.json())
        .then(data => {
            if (data.success) {
                document.getElementById('modalTitle').textContent = '일일 로그 수정';
                document.getElementById('logId').value = data.data.id;
                document.getElementById('logDate').value = formatDateForInput(data.data.log_date);
                document.getElementById('projectId').value = data.data.project_id || '';
                document.getElementById('summary').value = data.data.summary || '';

                // 항목들 로드
                clearEntries();
                if (data.data.entries && data.data.entries.length > 0) {
                    data.data.entries.forEach(entry => addEntry(entry));
                }

                document.getElementById('logModal').classList.remove('hidden');
            }
        });
    }

    // 항목 비우기
    function clearEntries() {
        document.getElementById('entriesContainer').innerHTML = '';
    }

    // 항목 추가
    function addEntry(entry = null) {
        const container = document.getElementById('entriesContainer');
        const index = container.children.length;

        const html = `
            <div class="entry-item border rounded-lg p-4 bg-gray-50" data-index="${index}">
                <div class="flex gap-4 mb-2">
                    <input type="hidden" name="entries[${index}][id]" value="${entry?.id || ''}">
                    <div class="w-24">
                        <select name="entries[${index}][assignee_type]" class="w-full px-2 py-1 border rounded text-sm" onchange="updateAssigneeOptions(this, ${index})">
                            <option value="user" ${(!entry || entry.assignee_type === 'user') ? 'selected' : ''}>개인</option>
                            <option value="team" ${entry?.assignee_type === 'team' ? 'selected' : ''}>팀</option>
                        </select>
                    </div>
                    <div class="flex-1">
                        <input type="text"
                               name="entries[${index}][assignee_name]"
                               value="${entry?.assignee_name || ''}"
                               placeholder="담당자 (직접입력 또는 선택)"
                               list="assigneeList${index}"
                               class="w-full px-2 py-1 border rounded text-sm"
                               required>
                        <datalist id="assigneeList${index}">
                            ${getAssigneeOptions(entry?.assignee_type || 'user')}
                        </datalist>
                        <input type="hidden" name="entries[${index}][assignee_id]" value="${entry?.assignee_id || ''}">
                    </div>
                    <div class="w-24">
                        <select name="entries[${index}][status]" class="w-full px-2 py-1 border rounded text-sm">
                            <option value="todo" ${(!entry || entry.status === 'todo') ? 'selected' : ''}>예정</option>
                            <option value="in_progress" ${entry?.status === 'in_progress' ? 'selected' : ''}>진행중</option>
                            <option value="done" ${entry?.status === 'done' ? 'selected' : ''}>완료</option>
                        </select>
                    </div>
                    <button type="button" onclick="removeEntry(this)" class="text-red-500 hover:text-red-700">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                        </svg>
                    </button>
                </div>
                <textarea name="entries[${index}][content]"
                          placeholder="업무 내용"
                          rows="2"
                          class="w-full px-2 py-1 border rounded text-sm"
                          required>${entry?.content || ''}</textarea>
            </div>
        `;

        container.insertAdjacentHTML('beforeend', html);
    }

    // 담당자 옵션 생성
    function getAssigneeOptions(type) {
        const list = type === 'team' ? assignees.teams : assignees.users;
        return list.map(item => `<option value="${item.name}" data-id="${item.id}">`).join('');
    }

    // 담당자 타입 변경 시 옵션 업데이트
    function updateAssigneeOptions(select, index) {
        const datalist = document.getElementById(`assigneeList${index}`);
        datalist.innerHTML = getAssigneeOptions(select.value);
    }

    // 항목 삭제
    function removeEntry(btn) {
        btn.closest('.entry-item').remove();
        reindexEntries();
    }

    // 항목 인덱스 재정렬
    function reindexEntries() {
        const entries = document.querySelectorAll('.entry-item');
        entries.forEach((entry, index) => {
            entry.dataset.index = index;
            entry.querySelectorAll('[name^="entries["]').forEach(input => {
                input.name = input.name.replace(/entries\[\d+\]/, `entries[${index}]`);
            });
            const datalist = entry.querySelector('datalist');
            if (datalist) {
                datalist.id = `assigneeList${index}`;
            }
            const nameInput = entry.querySelector('input[list]');
            if (nameInput) {
                nameInput.setAttribute('list', `assigneeList${index}`);
            }
        });
    }

    // 폼 제출
    document.getElementById('logForm').addEventListener('submit', function(e) {
        e.preventDefault();

        const logId = document.getElementById('logId').value;
        const url = logId ? `/api/admin/daily-logs/${logId}` : '/api/admin/daily-logs';
        const method = logId ? 'PUT' : 'POST';

        const formData = new FormData(this);
        const data = {};

        // FormData를 객체로 변환
        for (let [key, value] of formData.entries()) {
            const match = key.match(/^entries\[(\d+)\]\[(.+)\]$/);
            if (match) {
                const idx = match[1];
                const field = match[2];
                if (!data.entries) data.entries = [];
                if (!data.entries[idx]) data.entries[idx] = {};
                data.entries[idx][field] = value;
            } else {
                data[key] = value;
            }
        }

        // 빈 항목 제거
        if (data.entries) {
            data.entries = data.entries.filter(e => e && e.assignee_name && e.content);
        }

        fetch(url, {
            method: method,
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'X-CSRF-TOKEN': '{{ csrf_token() }}'
            },
            body: JSON.stringify(data)
        })
        .then(res => res.json())
        .then(result => {
            if (result.success) {
                closeModal();
                htmx.trigger('#log-table', 'filterSubmit');
            } else {
                showToast(result.message || '오류가 발생했습니다.', 'error');
            }
        })
        .catch(err => {
            console.error(err);
            showToast('오류가 발생했습니다.', 'error');
        });
    });

    // 삭제 확인
    function confirmDelete(id, date) {
        showDeleteConfirm(`"${date}" 일일 로그`, () => {
            fetch(`/api/admin/daily-logs/${id}`, {
                method: 'DELETE',
                headers: {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}'
                }
            })
            .then(res => res.json())
            .then(result => {
                if (result.success) {
                    htmx.trigger('#log-table', 'filterSubmit');
                }
            });
        });
    }

    // 복원 확인
    function confirmRestore(id, date) {
        showConfirm(`"${date}" 일일 로그를 복원하시겠습니까?`, () => {
            fetch(`/api/admin/daily-logs/${id}/restore`, {
                method: 'POST',
                headers: {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}'
                }
            })
            .then(res => res.json())
            .then(result => {
                if (result.success) {
                    htmx.trigger('#log-table', 'filterSubmit');
                }
            });
        }, { title: '복원 확인', icon: 'question' });
    }

    // 영구삭제 확인
    function confirmForceDelete(id, date) {
        showPermanentDeleteConfirm(`"${date}" 일일 로그`, () => {
            fetch(`/api/admin/daily-logs/${id}/force`, {
                method: 'DELETE',
                headers: {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}'
                }
            })
            .then(res => res.json())
            .then(result => {
                if (result.success) {
                    htmx.trigger('#log-table', 'filterSubmit');
                }
            });
        });
    }

    // ========================================
    // 주의 필요 항목 펼치기/접기 (이벤트 위임)
    // ========================================
    document.addEventListener('click', function(e) {
        const textEl = e.target.closest('[data-toggle-attention]');
        if (!textEl) return;

        const itemDiv = textEl.closest('[data-issue-id], [data-task-id]');
        if (!itemDiv) return;

        const fullDiv = itemDiv.querySelector('.attention-item-full');
        if (!fullDiv) return;

        if (fullDiv.classList.contains('hidden')) {
            // 펼치기: 상세 정보 표시 + 제목 전체 표시
            fullDiv.classList.remove('hidden');
            textEl.classList.remove('truncate');
            textEl.classList.add('text-red-600');
        } else {
            // 접기: 상세 정보 숨김 + 제목 한 줄로
            fullDiv.classList.add('hidden');
            textEl.classList.add('truncate');
            textEl.classList.remove('text-red-600');
        }
    });

    // ========================================
    // 미완료 항목 펼치기/접기
    // ========================================
    function togglePendingEntry(textEl) {
        const entryDiv = textEl.closest('[data-entry-id]');
        const fullDiv = entryDiv.querySelector('.pending-entry-full');

        if (fullDiv.classList.contains('hidden')) {
            // 펼치기 - 타이틀은 truncate 유지, 전체 내용 표시
            fullDiv.classList.remove('hidden');
            textEl.classList.add('text-blue-600');
        } else {
            // 접기
            fullDiv.classList.add('hidden');
            textEl.classList.remove('text-blue-600');
        }
    }

    // ========================================
    // 미완료 항목 상태 업데이트
    // ========================================
    function updatePendingStatus(entryId, status) {
        fetch(`/api/admin/daily-logs/entries/${entryId}/status`, {
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': '{{ csrf_token() }}'
            },
            body: JSON.stringify({ status })
        })
        .then(res => res.json())
        .then(result => {
            if (result.success) {
                const item = document.querySelector(`[data-entry-id="${entryId}"]`);
                if (item) {
                    if (status === 'done') {
                        // 완료 처리: 항목 제거 애니메이션
                        const parentCard = item.closest('.pending-assignee-card');
                        item.style.transition = 'opacity 0.3s, transform 0.3s';
                        item.style.opacity = '0';
                        item.style.transform = 'scale(0.95)';
                        setTimeout(() => {
                            item.remove();
                            // 담당자 카드에 항목이 없으면 카드도 제거
                            if (parentCard && parentCard.querySelectorAll('[data-entry-id]').length === 0) {
                                parentCard.style.transition = 'opacity 0.3s, transform 0.3s';
                                parentCard.style.opacity = '0';
                                parentCard.style.transform = 'scale(0.95)';
                                setTimeout(() => {
                                    parentCard.remove();
                                    // 전체 섹션 체크
                                    const grid = document.getElementById('pendingEntriesGrid');
                                    if (grid && grid.children.length === 0) {
                                        grid.closest('.bg-white')?.remove();
                                    }
                                }, 300);
                            }
                        }, 300);
                    } else {
                        // 예정/진행중 변경: 상태 뱃지만 업데이트
                        const badge = item.querySelector('span.px-1\\.5');
                        if (badge) {
                            if (status === 'in_progress') {
                                badge.className = 'px-1.5 py-0.5 text-[10px] rounded shrink-0 bg-yellow-100 text-yellow-700';
                                badge.textContent = '진행';
                            } else {
                                badge.className = 'px-1.5 py-0.5 text-[10px] rounded shrink-0 bg-gray-100 text-gray-600';
                                badge.textContent = '예정';
                            }
                        }
                        // 상태변경 버튼들도 업데이트
                        updatePendingEntryButtons(item, entryId, status);
                    }
                }
                // 로그 리스트도 새로고침
                htmx.trigger('#log-table', 'filterSubmit');
            }
        });
    }

    // 미완료 항목 버튼 업데이트
    function updatePendingEntryButtons(item, entryId, currentStatus) {
        const buttonsDiv = item.querySelector('.flex.gap-0\\.5');
        if (!buttonsDiv) return;

        let buttonsHtml = '';

        if (currentStatus !== 'todo') {
            buttonsHtml += `
                <button onclick="event.stopPropagation(); updatePendingStatus(${entryId}, 'todo')"
                        class="p-0.5 text-gray-400 hover:bg-gray-100 rounded" title="예정">
                    <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9" stroke-width="2"/></svg>
                </button>
            `;
        }
        if (currentStatus !== 'in_progress') {
            buttonsHtml += `
                <button onclick="event.stopPropagation(); updatePendingStatus(${entryId}, 'in_progress')"
                        class="p-0.5 text-yellow-500 hover:bg-yellow-50 rounded" title="진행중">
                    <svg class="w-3.5 h-3.5" fill="currentColor" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9"/></svg>
                </button>
            `;
        }
        buttonsHtml += `
            <button onclick="event.stopPropagation(); updatePendingStatus(${entryId}, 'done')"
                    class="p-0.5 text-green-500 hover:bg-green-50 rounded" title="완료">
                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                </svg>
            </button>
        `;

        buttonsDiv.innerHTML = buttonsHtml;
    }

    // ========================================
    // 미완료 항목 수정 모달 관련 함수
    // ========================================
    let pendingEditEntries = [];
    let pendingEditAssigneeName = '';
    let pendingEditFromAccordion = null; // 아코디언에서 열렸으면 logId 저장

    // 모달 열기 (기존 항목 수정)
    function openPendingEditModal(entriesJson, assigneeName) {
        pendingEditEntries = entriesJson;
        pendingEditAssigneeName = assigneeName;
        pendingEditFromAccordion = null; // 기존 항목 수정 모드

        // 담당자 텍스트 표시, 입력 필드 숨김
        document.getElementById('pendingEditAssigneeNameText').textContent = assigneeName;
        document.getElementById('pendingEditAssigneeNameText').classList.remove('hidden');
        document.getElementById('pendingEditAssigneeNameInput').classList.add('hidden');

        // 컨테이너 초기화 및 항목 추가
        const container = document.getElementById('pendingEditEntriesContainer');
        container.innerHTML = '';

        entriesJson.forEach((entry, index) => {
            addPendingEntryRow(entry, index);
        });

        document.getElementById('pendingEditModal').classList.remove('hidden');
    }

    // 모달 열기 (새 항목 추가 - 아코디언에서)
    function openQuickAddModal(logId) {
        pendingEditEntries = [{ id: '', content: '', status: 'todo', daily_log_id: logId }];
        pendingEditAssigneeName = '';
        pendingEditFromAccordion = logId;

        // 담당자 입력 필드 표시, 텍스트 숨김
        document.getElementById('pendingEditAssigneeNameText').classList.add('hidden');
        const assigneeInput = document.getElementById('pendingEditAssigneeNameInput');
        assigneeInput.value = '';
        assigneeInput.classList.remove('hidden');

        // 컨테이너 초기화 및 빈 항목 추가
        const container = document.getElementById('pendingEditEntriesContainer');
        container.innerHTML = '';
        addPendingEntryRow({ id: '', content: '', status: 'todo', daily_log_id: logId }, 0);

        document.getElementById('pendingEditModal').classList.remove('hidden');

        // 담당자 입력 필드에 포커스
        setTimeout(() => {
            assigneeInput.focus();
        }, 100);
    }

    // 모달 닫기
    function closePendingEditModal() {
        document.getElementById('pendingEditModal').classList.add('hidden');
        pendingEditEntries = [];
        pendingEditAssigneeName = '';
        pendingEditFromAccordion = null;
    }

    // 항목 행 추가
    function addPendingEntryRow(entry = null, index = null) {
        const container = document.getElementById('pendingEditEntriesContainer');
        if (index === null) {
            index = container.children.length;
        }

        const currentStatus = entry?.status || 'todo';

        // 새 항목인 경우 기존 항목의 daily_log_id 사용
        let dailyLogId = entry?.daily_log_id || '';
        if (!dailyLogId && pendingEditEntries.length > 0) {
            dailyLogId = pendingEditEntries[0].daily_log_id || '';
        }

        const html = `
            <div class="pending-edit-row flex gap-2 items-start p-2 bg-gray-50 rounded-lg" data-entry-id="${entry?.id || ''}" data-index="${index}">
                <!-- 상태 버튼 그룹 -->
                <div class="flex gap-1 shrink-0 pt-1">
                    <button type="button" onclick="togglePendingStatus(this, 'todo')"
                            class="status-btn p-1.5 rounded transition-all ${currentStatus === 'todo' ? 'bg-gray-200 ring-2 ring-gray-400' : 'hover:bg-gray-100'}"
                            data-status="todo" title="예정">
                        <svg class="w-4 h-4 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9" stroke-width="2"/></svg>
                    </button>
                    <button type="button" onclick="togglePendingStatus(this, 'in_progress')"
                            class="status-btn p-1.5 rounded transition-all ${currentStatus === 'in_progress' ? 'bg-yellow-100 ring-2 ring-yellow-400' : 'hover:bg-yellow-50'}"
                            data-status="in_progress" title="진행중">
                        <svg class="w-4 h-4 text-yellow-500" fill="currentColor" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9"/></svg>
                    </button>
                    <button type="button" onclick="togglePendingStatus(this, 'done')"
                            class="status-btn p-1.5 rounded transition-all ${currentStatus === 'done' ? 'bg-green-100 ring-2 ring-green-400' : 'hover:bg-green-50'}"
                            data-status="done" title="완료">
                        <svg class="w-4 h-4 text-green-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
                    </button>
                </div>
                <input type="hidden" name="pending_entries[${index}][status]" value="${currentStatus}">
                <div class="flex-1">
                    <textarea name="pending_entries[${index}][content]" rows="2" required
                              class="w-full px-3 py-1.5 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 resize-none"
                              placeholder="업무 내용">${escapeHtml(entry?.content || '')}</textarea>
                </div>
                <input type="hidden" name="pending_entries[${index}][id]" value="${entry?.id || ''}">
                <input type="hidden" name="pending_entries[${index}][daily_log_id]" value="${dailyLogId}">
                <button type="button" onclick="removePendingEntryRow(this)" class="p-1.5 text-red-500 hover:bg-red-50 rounded shrink-0 pt-1" title="삭제">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                    </svg>
                </button>
            </div>
        `;

        container.insertAdjacentHTML('beforeend', html);
    }

    // 상태 버튼 토글
    function togglePendingStatus(btn, status) {
        const row = btn.closest('.pending-edit-row');
        const statusInput = row.querySelector('input[name*="[status]"]');
        const buttons = row.querySelectorAll('.status-btn');

        // 모든 버튼 스타일 초기화
        buttons.forEach(b => {
            b.classList.remove('bg-gray-200', 'ring-2', 'ring-gray-400', 'bg-yellow-100', 'ring-yellow-400', 'bg-green-100', 'ring-green-400');
        });

        // 선택된 버튼 스타일 적용
        if (status === 'todo') {
            btn.classList.add('bg-gray-200', 'ring-2', 'ring-gray-400');
        } else if (status === 'in_progress') {
            btn.classList.add('bg-yellow-100', 'ring-2', 'ring-yellow-400');
        } else if (status === 'done') {
            btn.classList.add('bg-green-100', 'ring-2', 'ring-green-400');
        }

        // hidden input 값 업데이트
        statusInput.value = status;
    }

    // 항목 행 삭제
    function removePendingEntryRow(btn) {
        const row = btn.closest('.pending-edit-row');
        row.remove();
        reindexPendingEntryRows();
    }

    // 인덱스 재정렬
    function reindexPendingEntryRows() {
        const rows = document.querySelectorAll('.pending-edit-row');
        rows.forEach((row, index) => {
            row.dataset.index = index;
            row.querySelectorAll('[name^="pending_entries["]').forEach(input => {
                input.name = input.name.replace(/pending_entries\[\d+\]/, `pending_entries[${index}]`);
            });
        });
    }

    // 폼 제출
    function submitPendingEditEntries(event) {
        event.preventDefault();

        // 새 항목 추가 모드일 때 입력 필드에서 담당자 이름 가져오기
        let assigneeName = pendingEditAssigneeName;
        if (pendingEditFromAccordion) {
            const inputField = document.getElementById('pendingEditAssigneeNameInput');
            assigneeName = inputField.value.trim();
            if (!assigneeName) {
                showToast('담당자 이름을 입력해주세요.', 'warning');
                inputField.focus();
                return;
            }
        }

        const submitBtn = document.getElementById('pendingEditSubmitBtn');
        submitBtn.disabled = true;
        submitBtn.textContent = '저장 중...';

        const rows = document.querySelectorAll('.pending-edit-row');
        const updatePromises = [];
        const deletePromises = [];

        // 기존 항목 ID 수집
        const currentIds = Array.from(rows).map(row => row.dataset.entryId).filter(id => id);
        const originalIds = pendingEditEntries.map(e => String(e.id));

        // 삭제할 항목
        const deletedIds = originalIds.filter(id => !currentIds.includes(id));
        deletedIds.forEach(id => {
            deletePromises.push(
                fetch(`/api/admin/daily-logs/entries/${id}`, {
                    method: 'DELETE',
                    headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' }
                })
            );
        });

        // 수정/추가할 항목
        rows.forEach(row => {
            const entryId = row.dataset.entryId;
            const status = row.querySelector('input[name*="[status]"]').value;
            const content = row.querySelector('textarea').value;
            const dailyLogId = row.querySelector('input[name*="[daily_log_id]"]').value;

            if (!content.trim()) return;

            if (entryId) {
                // 기존 항목 수정
                updatePromises.push(
                    fetch(`/api/admin/daily-logs/entries/${entryId}`, {
                        method: 'PUT',
                        headers: {
                            'Content-Type': 'application/json',
                            'X-CSRF-TOKEN': '{{ csrf_token() }}'
                        },
                        body: JSON.stringify({ content, status })
                    })
                );
            } else if (dailyLogId) {
                // 새 항목 추가
                updatePromises.push(
                    fetch(`/api/admin/daily-logs/${dailyLogId}/entries`, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                            'X-CSRF-TOKEN': '{{ csrf_token() }}'
                        },
                        body: JSON.stringify({
                            assignee_type: 'user',
                            assignee_name: assigneeName,
                            content,
                            status
                        })
                    })
                );
            }
        });

        Promise.all([...deletePromises, ...updatePromises])
            .then(() => {
                closePendingEditModal();
                // 페이지 새로고침으로 미완료 항목 갱신
                location.reload();
            })
            .catch(err => {
                console.error(err);
                showToast('저장 중 오류가 발생했습니다.', 'error');
            })
            .finally(() => {
                submitBtn.disabled = false;
                submitBtn.textContent = '저장';
            });
    }

    // ESC 키로 모달 닫기
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            if (!document.getElementById('pendingEditModal').classList.contains('hidden')) {
                closePendingEditModal();
            }
        }
    });
</script>
@endpush