@extends('layouts.app')

@section('title', '영업실적 등록')

@section('content')
<div class="max-w-2xl mx-auto">
    <!-- 페이지 헤더 -->
    <div class="mb-6">
        <a href="{{ route('sales.records.index') }}" class="text-gray-500 hover:text-gray-700 text-sm mb-2 inline-flex items-center">
            <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
            </svg>
            목록으로
        </a>
        <h1 class="text-2xl font-bold text-gray-800">영업실적 등록</h1>
    </div>

    <!-- 폼 -->
    <form action="{{ route('sales.records.store') }}" method="POST" class="bg-white rounded-lg shadow-sm p-6 space-y-6">
        @csrf

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">실적일 <span class="text-red-500">*</span></label>
                <input type="date" name="record_date" value="{{ old('record_date', date('Y-m-d')) }}" required
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 @error('record_date') border-red-500 @enderror">
                @error('record_date')
                <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                @enderror
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">실적 유형 <span class="text-red-500">*</span></label>
                <input type="text" name="record_type" value="{{ old('record_type') }}" required placeholder="예: 신규계약, 갱신, 추가매출"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 @error('record_type') border-red-500 @enderror">
                @error('record_type')
                <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                @enderror
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">담당자 <span class="text-red-500">*</span></label>
                <select name="manager_id" required
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 @error('manager_id') border-red-500 @enderror">
                    <option value="">선택하세요</option>
                    @foreach($managers as $manager)
                    <option value="{{ $manager->id }}" {{ old('manager_id') == $manager->id ? 'selected' : '' }}>
                        {{ $manager->name }} ({{ $manager->role_label }})
                    </option>
                    @endforeach
                </select>
                @error('manager_id')
                <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                @enderror
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">가망고객</label>
                <select name="prospect_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($prospects as $prospect)
                    <option value="{{ $prospect->id }}" {{ old('prospect_id') == $prospect->id ? 'selected' : '' }}>
                        {{ $prospect->company_name }}
                    </option>
                    @endforeach
                </select>
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">실적 금액 <span class="text-red-500">*</span></label>
                <input type="number" name="amount" value="{{ old('amount', 0) }}" required min="0"
                       class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 @error('amount') border-red-500 @enderror">
                @error('amount')
                <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                @enderror
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">수수료</label>
                <input type="number" name="commission" value="{{ old('commission', 0) }}" min="0"
                       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>

        <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">상태 <span class="text-red-500">*</span></label>
            <select name="status" required
                    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="pending" {{ old('status') === 'pending' ? 'selected' : '' }}>대기</option>
                <option value="approved" {{ old('status') === 'approved' ? 'selected' : '' }}>승인</option>
                <option value="rejected" {{ old('status') === 'rejected' ? 'selected' : '' }}>반려</option>
                <option value="paid" {{ old('status') === 'paid' ? 'selected' : '' }}>지급완료</option>
            </select>
        </div>

        <div>
            <label class="block text-sm font-medium text-gray-700 mb-2">설명</label>
            <textarea name="description" rows="3"
                      class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">{{ old('description') }}</textarea>
        </div>

        <div class="flex justify-end gap-3 pt-4 border-t">
            <a href="{{ route('sales.records.index') }}"
               class="px-6 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition">
                취소
            </a>
            <button type="submit"
                    class="px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition">
                등록
            </button>
        </div>
    </form>
</div>
@endsection
