@extends('layouts.app')

@section('title', '명함신청')

@section('content')
<div class="max-w-4xl mx-auto space-y-6">
    <!-- 페이지 헤더 -->
    <div>
        <h1 class="text-2xl font-bold text-gray-800">명함신청</h1>
        <p class="text-sm text-gray-500 mt-1">명함 인쇄를 신청합니다. 신청 후 본사에서 처리합니다.</p>
    </div>

    <!-- 회사 정보 안내 -->
    <div class="bg-blue-50 border border-blue-200 rounded-lg p-4">
        <h3 class="text-sm font-semibold text-blue-800 mb-2">명함에 인쇄되는 회사 정보</h3>
        <div class="grid grid-cols-1 sm:grid-cols-2 gap-2 text-sm text-blue-700">
            <div><span class="font-medium">회사명:</span> (주)코드브릿지엑스</div>
            <div><span class="font-medium">서비스:</span> SAM (Smart Automation Management)</div>
        </div>
        <p class="text-xs text-blue-600 mt-2">* 아래 입력하신 정보가 명함에 인쇄됩니다.</p>
    </div>

    <!-- 신청 폼 -->
    <div class="bg-white rounded-lg shadow-sm p-6">
        <h2 class="text-lg font-semibold text-gray-800 mb-4">명함 신청</h2>

        @if(session('success'))
        <div class="mb-4 p-3 bg-emerald-50 border border-emerald-200 text-emerald-700 rounded-lg text-sm">
            {{ session('success') }}
        </div>
        @endif

        @if($errors->any())
        <div class="mb-4 p-3 bg-red-50 border border-red-200 text-red-700 rounded-lg text-sm">
            <ul class="list-disc list-inside">
                @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
        @endif

        <form method="POST" action="{{ route('sales.business-cards.store') }}">
            @csrf
            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">성함 <span class="text-red-500">*</span></label>
                    <input type="text" name="name" value="{{ old('name', $user->name) }}" required
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm"
                           placeholder="명함에 인쇄될 이름">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">직함</label>
                    <input type="text" name="title" value="{{ old('title') }}"
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm"
                           placeholder="예: 영업팀장, 대리">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">전화번호 <span class="text-red-500">*</span></label>
                    <input type="text" name="phone" value="{{ old('phone', $user->phone) }}" required
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm"
                           placeholder="010-0000-0000">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">이메일</label>
                    <input type="email" name="email" value="{{ old('email', $user->email) }}"
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm"
                           placeholder="example@email.com">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">수량</label>
                    <input type="number" name="quantity" value="{{ old('quantity', 500) }}" min="1" max="9999"
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm"
                           placeholder="500">
                    <p class="text-xs text-gray-400 mt-1">기본 500매</p>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">메모</label>
                    <input type="text" name="memo" value="{{ old('memo') }}"
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 text-sm"
                           placeholder="특이사항이 있으면 입력하세요">
                </div>
            </div>
            <div class="mt-6">
                <button type="submit"
                        class="w-full sm:w-auto px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm">
                    명함 신청하기
                </button>
            </div>
        </form>
    </div>

    <!-- 내 신청 이력 -->
    <div class="bg-white rounded-lg shadow-sm overflow-hidden">
        <div class="px-6 py-4 border-b border-gray-200">
            <h2 class="text-lg font-semibold text-gray-800">내 신청 이력</h2>
        </div>
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">신청일</th>
                        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">성함</th>
                        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">직함</th>
                        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">전화번호</th>
                        <th class="px-4 py-2 text-center text-xs font-medium text-gray-500 uppercase">수량</th>
                        <th class="px-4 py-2 text-center text-xs font-medium text-gray-500 uppercase">상태</th>
                        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">처리일</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($myRequests as $req)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 text-sm text-gray-700">{{ $req->created_at->format('Y-m-d') }}</td>
                        <td class="px-4 py-3 text-sm font-medium text-gray-900">{{ $req->name }}</td>
                        <td class="px-4 py-3 text-sm text-gray-600">{{ $req->title ?: '-' }}</td>
                        <td class="px-4 py-3 text-sm text-gray-600">{{ $req->phone }}</td>
                        <td class="px-4 py-3 text-sm text-gray-700 text-center">{{ number_format($req->quantity) }}매</td>
                        <td class="px-4 py-3 text-center">
                            @if($req->status === 'pending')
                                <span class="px-2 py-0.5 text-xs font-medium rounded-full bg-yellow-100 text-yellow-800">요청</span>
                            @elseif($req->status === 'ordered')
                                <span class="px-2 py-0.5 text-xs font-medium rounded-full bg-blue-100 text-blue-800">제작중</span>
                            @else
                                <span class="px-2 py-0.5 text-xs font-medium rounded-full bg-emerald-100 text-emerald-800">처리완료</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-500">
                            {{ $req->processed_at?->format('Y-m-d') ?? '-' }}
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="px-4 py-8 text-center text-gray-500 text-sm">
                            신청 이력이 없습니다.
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($myRequests->hasPages())
        <div class="px-4 py-2 border-t border-gray-200 bg-gray-50">
            {{ $myRequests->links() }}
        </div>
        @endif
    </div>
</div>
@endsection
