@extends('layouts.app')

@section('title', '영업실적 관리')

@section('content')
<div class="flex flex-col h-full">
    <!-- 페이지 헤더 -->
    <div class="flex flex-col sm:flex-row sm:justify-between sm:items-center gap-4 mb-6 flex-shrink-0">
        <div>
            <h1 class="text-2xl font-bold text-gray-800">영업실적 관리</h1>
            <p class="text-sm text-gray-500 mt-1">영업 실적을 관리합니다</p>
        </div>
        <a href="{{ route('sales.records.create') }}"
           class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg transition text-center w-full sm:w-auto flex items-center justify-center gap-2">
            <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="M12 4v16m8-8H4" />
            </svg>
            실적 등록
        </a>
    </div>

    <!-- 통계 카드 -->
    <div class="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-7 gap-4 mb-6 flex-shrink-0">
        <div class="bg-white rounded-lg shadow-sm p-4">
            <div class="text-sm text-gray-500">전체 건수</div>
            <div class="text-xl font-bold text-gray-800">{{ number_format($stats['total_count']) }}</div>
        </div>
        <div class="bg-yellow-50 rounded-lg shadow-sm p-4">
            <div class="text-sm text-yellow-600">대기</div>
            <div class="text-xl font-bold text-yellow-800">{{ number_format($stats['pending_count']) }}</div>
        </div>
        <div class="bg-blue-50 rounded-lg shadow-sm p-4">
            <div class="text-sm text-blue-600">승인</div>
            <div class="text-xl font-bold text-blue-800">{{ number_format($stats['approved_count']) }}</div>
        </div>
        <div class="bg-gray-50 rounded-lg shadow-sm p-4">
            <div class="text-sm text-gray-600">총 실적</div>
            <div class="text-xl font-bold text-gray-800">{{ number_format($stats['total_amount']) }}</div>
        </div>
        <div class="bg-purple-50 rounded-lg shadow-sm p-4">
            <div class="text-sm text-purple-600">총 수수료</div>
            <div class="text-xl font-bold text-purple-800">{{ number_format($stats['total_commission']) }}</div>
        </div>
        <div class="bg-orange-50 rounded-lg shadow-sm p-4">
            <div class="text-sm text-orange-600">대기 금액</div>
            <div class="text-xl font-bold text-orange-800">{{ number_format($stats['pending_amount']) }}</div>
        </div>
        <div class="bg-green-50 rounded-lg shadow-sm p-4">
            <div class="text-sm text-green-600">승인 금액</div>
            <div class="text-xl font-bold text-green-800">{{ number_format($stats['approved_amount']) }}</div>
        </div>
    </div>

    <!-- 필터 영역 -->
    <div class="flex-shrink-0 mb-4">
        <form method="GET" class="flex flex-wrap gap-2 sm:gap-4 items-center bg-white p-4 rounded-lg shadow-sm">
            <div class="flex-1 min-w-0 w-full sm:w-auto">
                <input type="text"
                       name="search"
                       value="{{ request('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-full sm:w-36">
                <select name="status" 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="pending" {{ request('status') === 'pending' ? 'selected' : '' }}>대기</option>
                    <option value="approved" {{ request('status') === 'approved' ? 'selected' : '' }}>승인</option>
                    <option value="rejected" {{ request('status') === 'rejected' ? 'selected' : '' }}>반려</option>
                    <option value="paid" {{ request('status') === 'paid' ? 'selected' : '' }}>지급완료</option>
                </select>
            </div>
            <div class="w-full sm:w-40">
                <select name="manager_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($managers as $manager)
                    <option value="{{ $manager->id }}" {{ request('manager_id') == $manager->id ? 'selected' : '' }}>
                        {{ $manager->name }}
                    </option>
                    @endforeach
                </select>
            </div>
            <div class="w-full sm:w-auto flex gap-2">
                <input type="date" name="start_date" value="{{ request('start_date') }}"
                       class="px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                <span class="self-center text-gray-500">~</span>
                <input type="date" name="end_date" value="{{ request('end_date') }}"
                       class="px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
            </div>
            <button type="submit" class="bg-gray-600 hover:bg-gray-700 text-white px-6 py-2 rounded-lg transition w-full sm:w-auto">
                검색
            </button>
        </form>
    </div>

    <!-- 테이블 -->
    <div class="bg-white rounded-lg shadow-sm overflow-hidden flex-1 flex flex-col min-h-0">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">실적일</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">담당자</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">유형</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">가망고객</th>
                        <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">금액</th>
                        <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">수수료</th>
                        <th class="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider">상태</th>
                        <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">관리</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($records as $record)
                    <tr class="hover:bg-gray-50">
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            {{ $record->record_date->format('Y-m-d') }}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <div class="text-sm text-gray-900">{{ $record->manager?->name ?? '-' }}</div>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            {{ $record->record_type }}
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm">
                            @if($record->prospect)
                            <a href="{{ route('sales.prospects.show', $record->prospect->id) }}" class="text-blue-600 hover:underline">
                                {{ $record->prospect->company_name }}
                            </a>
                            @else
                            <span class="text-gray-400">-</span>
                            @endif
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium text-gray-900">
                            {{ number_format($record->amount) }}원
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-right text-sm text-gray-900">
                            {{ number_format($record->commission) }}원
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-center">
                            <span class="px-2 py-1 text-xs font-medium rounded-full {{ $record->status_color }}">
                                {{ $record->status_label }}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                            <a href="{{ route('sales.records.show', $record->id) }}" class="text-blue-600 hover:text-blue-900 mr-3">상세</a>
                            <a href="{{ route('sales.records.edit', $record->id) }}" class="text-indigo-600 hover:text-indigo-900 mr-3">수정</a>
                            <form action="{{ route('sales.records.destroy', $record->id) }}" method="POST" class="inline"
                                  onsubmit="return confirm('정말 삭제하시겠습니까?')">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="text-red-600 hover:text-red-900">삭제</button>
                            </form>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="8" class="px-6 py-12 text-center text-gray-500">
                            등록된 실적이 없습니다.
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <!-- 페이지네이션 -->
        @if($records->hasPages())
        <div class="px-6 py-4 border-t border-gray-200">
            {{ $records->withQueryString()->links() }}
        </div>
        @endif
    </div>
</div>
@endsection
