@extends('layouts.app')

@section('title', '영업파트너 등록')

@push('styles')
<style>
    .doc-drop-zone {
        border: 2px dashed #d1d5db;
        border-radius: 8px;
        padding: 16px;
        text-align: center;
        transition: all 0.2s;
        cursor: pointer;
        background: #f9fafb;
        min-height: 80px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }
    .doc-drop-zone:hover, .doc-drop-zone.dragover {
        border-color: #3b82f6;
        background: #eff6ff;
    }
    .doc-drop-zone.has-file {
        border-color: #10b981;
        background: #ecfdf5;
        border-style: solid;
    }
    .doc-drop-zone-icon {
        width: 24px;
        height: 24px;
        color: #9ca3af;
    }
    .doc-drop-zone.dragover .doc-drop-zone-icon,
    .doc-drop-zone:hover .doc-drop-zone-icon {
        color: #3b82f6;
    }
    .doc-drop-zone.has-file .doc-drop-zone-icon {
        color: #10b981;
    }
    .doc-preview {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 8px;
        background: white;
        border-radius: 6px;
        margin-top: 8px;
        width: 100%;
    }
    .doc-preview img {
        width: 40px;
        height: 40px;
        object-fit: cover;
        border-radius: 4px;
    }
    .doc-preview-info {
        flex: 1;
        min-width: 0;
        text-align: left;
    }
    .doc-preview-name {
        font-size: 12px;
        font-weight: 500;
        color: #374151;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .doc-preview-size {
        font-size: 11px;
        color: #9ca3af;
    }
    .doc-preview-remove {
        padding: 4px;
        color: #9ca3af;
        cursor: pointer;
        transition: color 0.2s;
    }
    .doc-preview-remove:hover {
        color: #ef4444;
    }
</style>
@endpush

@section('content')
<div class="max-w-3xl mx-auto">
    <!-- 페이지 헤더 -->
    <div class="mb-6">
        <a href="{{ route('sales.managers.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>
        <div class="flex items-center justify-between">
            <div>
                <h1 class="text-2xl font-bold text-gray-800">영업파트너 등록</h1>
                <p class="text-sm text-gray-500 mt-1">등록 후 본사 승인이 필요합니다.</p>
            </div>
            @if(app()->environment('local', 'development'))
            <button type="button" onclick="fillRandomData()"
                    class="p-2 bg-yellow-400 hover:bg-yellow-500 text-yellow-900 rounded-lg transition shadow-sm"
                    title="테스트 데이터 자동 입력">
                <svg class="w-6 h-6" fill="currentColor" viewBox="0 0 24 24">
                    <path d="M13 10V3L4 14h7v7l9-11h-7z"/>
                </svg>
            </button>
            @endif
        </div>
    </div>

    <!-- 폼 -->
    <form action="{{ route('sales.managers.store') }}" method="POST" enctype="multipart/form-data" class="space-y-6">
        @csrf

        <!-- 기본 정보 -->
        <div class="bg-white rounded-lg shadow-sm p-6">
            <h2 class="text-lg font-semibold text-gray-800 mb-4">기본 정보</h2>

            <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">로그인 ID</label>
                    <input type="text" name="user_id" value="{{ old('user_id') }}"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 @error('user_id') border-red-500 @enderror"
                           placeholder="미입력 시 이메일 사용">
                    @error('user_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">이름 <span class="text-red-500">*</span></label>
                    <input type="text" name="name" value="{{ old('name') }}" 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('name') border-red-500 @enderror">
                    @error('name')
                    <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="email" name="email" value="{{ old('email') }}" 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('email') border-red-500 @enderror">
                    @error('email')
                    <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="text" name="phone" value="{{ old('phone') }}"
                           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>
                    <label class="block text-sm font-medium text-gray-700 mb-2">비밀번호 <span class="text-red-500">*</span></label>
                    <input type="password" name="password" 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('password') border-red-500 @enderror">
                    @error('password')
                    <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="password" name="password_confirmation" required
                           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>

        <!-- 사업자 정보 (전자서명용) -->
        <div class="bg-white rounded-lg shadow-sm p-6">
            <h2 class="text-lg font-semibold text-gray-800 mb-1">사업자 정보</h2>
            <p class="text-sm text-gray-400 mb-4">전자서명 계약 시 자동으로 채워집니다 (선택사항)</p>

            <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">상호</label>
                    <input type="text" name="company_name" value="{{ old('company_name') }}"
                           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>
                    <label class="block text-sm font-medium text-gray-700 mb-2">사업자등록번호</label>
                    <input type="text" name="biz_no" value="{{ old('biz_no') }}"
                           placeholder="000-00-00000"
                           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="md:col-span-2">
                    <label class="block text-sm font-medium text-gray-700 mb-2">주소</label>
                    <input type="text" name="address" value="{{ old('address') }}"
                           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>
        </div>

        <!-- 파트너 유형 -->
        <div class="bg-white rounded-lg shadow-sm p-6" x-data="{ partnerType: '{{ old('partner_type', 'individual') }}' }">
            <h2 class="text-lg font-semibold text-gray-800 mb-4">파트너 유형</h2>

            <div class="space-y-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">유형 선택 <span class="text-red-500">*</span></label>
                    <div class="flex gap-4">
                        <label class="flex items-center">
                            <input type="radio" name="partner_type" value="individual" x-model="partnerType"
                                   class="w-4 h-4 text-blue-600 border-gray-300 focus:ring-blue-500">
                            <span class="ml-2 text-sm text-gray-700">개인 (영업파트너)</span>
                        </label>
                        <label class="flex items-center">
                            <input type="radio" name="partner_type" value="corporate" x-model="partnerType"
                                   class="w-4 h-4 text-blue-600 border-gray-300 focus:ring-blue-500">
                            <span class="ml-2 text-sm text-gray-700">단체 (그룹)</span>
                            <span class="ml-1 text-xs text-gray-400">단체 30%, 유치 3%</span>
                        </label>
                    </div>
                </div>

                <div x-show="partnerType === 'corporate'" x-transition class="mt-4">
                    <label class="block text-sm font-medium text-gray-700 mb-2">유치 영업파트너</label>
                    <div class="flex items-center gap-2 px-4 py-2 bg-gray-50 border border-gray-200 rounded-lg">
                        <svg class="w-4 h-4 text-blue-500" 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 text-gray-700 font-medium">{{ auth()->user()->name }}</span>
                        <span class="text-xs text-gray-400">(등록자가 유치 파트너로 자동 지정됩니다)</span>
                    </div>
                    <p class="mt-1 text-xs text-gray-400">단체 가입 시: 단체 30%, 유치 파트너 3%, 매니저 0%</p>
                </div>
            </div>
        </div>

        <!-- 역할 및 조직 -->
        <div class="bg-white rounded-lg shadow-sm p-6">
            <h2 class="text-lg font-semibold text-gray-800 mb-4">역할 및 조직</h2>

            <div class="space-y-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">역할 <span class="text-red-500">*</span></label>
                    <div class="space-y-2">
                        @foreach($roles as $role)
                        <label class="flex items-center">
                            <input type="checkbox" name="role_ids[]" value="{{ $role->id }}"
                                   {{ in_array($role->id, old('role_ids', [])) ? 'checked' : '' }}
                                   class="w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500">
                            <span class="ml-2 text-sm text-gray-700">
                                {{ $role->description ?? $role->name }}
                                <span class="text-gray-400">({{ $role->name }})</span>
                            </span>
                        </label>
                        @endforeach
                    </div>
                    @error('role_ids')
                    <p class="mt-1 text-sm text-red-500">{{ $message }}</p>
                    @enderror
                </div>

            </div>
        </div>

        <!-- 첨부 서류 (멀티파일) -->
        <div class="bg-white rounded-lg shadow-sm p-6">
            <h2 class="text-lg font-semibold text-gray-800 mb-4">첨부 서류</h2>
            <p class="text-sm text-gray-500 mb-4">승인에 필요한 서류를 첨부해주세요. (등본사본, 통장사본)</p>

            <div id="document-list" class="space-y-4">
                <!-- 초기 문서 입력 필드 -->
                <div class="document-item border border-gray-200 rounded-lg p-4" data-index="0">
                    <div class="flex items-start gap-4">
                        <div class="w-40 flex-shrink-0">
                            <label class="block text-sm font-medium text-gray-700 mb-1">문서 유형</label>
                            <select name="documents[0][document_type]"
                                    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">
                                @foreach($documentTypes as $value => $label)
                                <option value="{{ $value }}">{{ $label }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="flex-1">
                            <label class="block text-sm font-medium text-gray-700 mb-1">파일</label>
                            <div class="doc-drop-zone" data-index="0">
                                <input type="file" name="documents[0][file]" class="hidden doc-file-input" accept="image/*,.pdf,.doc,.docx">
                                <input type="file" class="hidden doc-camera-input" accept="image/*" capture="environment">
                                <svg class="doc-drop-zone-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12" />
                                </svg>
                                <p class="text-xs text-gray-500 mt-1">클릭 또는 드래그하여 업로드</p>
                                <div class="doc-buttons flex gap-2 mt-2">
                                    <button type="button" class="doc-file-btn inline-flex items-center px-3 py-1.5 text-xs font-medium text-gray-700 bg-white border border-gray-300 rounded-md hover:bg-gray-50">
                                        <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="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                                        </svg>
                                        파일 선택
                                    </button>
                                    <button type="button" class="doc-camera-btn inline-flex items-center px-3 py-1.5 text-xs font-medium text-white bg-blue-600 border border-blue-600 rounded-md hover:bg-blue-700">
                                        <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="M3 9a2 2 0 012-2h.93a2 2 0 001.664-.89l.812-1.22A2 2 0 0110.07 4h3.86a2 2 0 011.664.89l.812 1.22A2 2 0 0018.07 7H19a2 2 0 012 2v9a2 2 0 01-2 2H5a2 2 0 01-2-2V9z" />
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 13a3 3 0 11-6 0 3 3 0 016 0z" />
                                        </svg>
                                        카메라 촬영
                                    </button>
                                </div>
                            </div>
                        </div>
                        <div class="w-40 flex-shrink-0">
                            <label class="block text-sm font-medium text-gray-700 mb-1">설명</label>
                            <input type="text" name="documents[0][description]"
                                   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>
            </div>

            <button type="button" id="add-document-btn"
                    class="mt-4 inline-flex items-center px-4 py-2 border border-gray-300 rounded-lg text-sm font-medium text-gray-700 bg-white hover:bg-gray-50">
                <svg class="w-4 h-4 mr-2" 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 class="flex justify-end gap-3">
            <a href="{{ route('sales.managers.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

@push('scripts')
<script>
let documentIndex = 1;

// 드래그 앤 드롭 초기화
function initDropZone(dropZone) {
    const fileInput = dropZone.querySelector('.doc-file-input');
    const cameraInput = dropZone.querySelector('.doc-camera-input');
    const fileBtn = dropZone.querySelector('.doc-file-btn');
    const cameraBtn = dropZone.querySelector('.doc-camera-btn');

    // 파일 선택 버튼 클릭
    if (fileBtn) {
        fileBtn.addEventListener('click', (e) => {
            e.stopPropagation();
            fileInput.click();
        });
    }

    // 카메라 촬영 버튼 클릭
    if (cameraBtn) {
        cameraBtn.addEventListener('click', (e) => {
            e.stopPropagation();
            cameraInput.click();
        });
    }

    // 드롭존 영역 클릭 (버튼 외 영역)
    dropZone.addEventListener('click', (e) => {
        if (!e.target.closest('.doc-preview-remove') &&
            !e.target.closest('.doc-file-btn') &&
            !e.target.closest('.doc-camera-btn')) {
            fileInput.click();
        }
    });

    dropZone.addEventListener('dragover', (e) => {
        e.preventDefault();
        dropZone.classList.add('dragover');
    });

    dropZone.addEventListener('dragleave', () => {
        dropZone.classList.remove('dragover');
    });

    dropZone.addEventListener('drop', (e) => {
        e.preventDefault();
        dropZone.classList.remove('dragover');
        if (e.dataTransfer.files.length) {
            handleFile(dropZone, e.dataTransfer.files[0]);
        }
    });

    // 파일 input change 이벤트
    fileInput.addEventListener('change', (e) => {
        if (e.target.files.length) {
            handleFile(dropZone, e.target.files[0]);
        }
    });

    // 카메라 input change 이벤트 (카메라로 촬영한 이미지 처리)
    if (cameraInput) {
        cameraInput.addEventListener('change', (e) => {
            if (e.target.files.length) {
                handleFile(dropZone, e.target.files[0]);
                // 카메라 input 초기화 (같은 이미지 다시 촬영 가능하도록)
                cameraInput.value = '';
            }
        });
    }
}

function handleFile(dropZone, file) {
    const fileInput = dropZone.querySelector('.doc-file-input');

    // DataTransfer로 파일 설정 (메인 file input에 저장)
    const dt = new DataTransfer();
    dt.items.add(file);
    fileInput.files = dt.files;

    // UI 업데이트
    dropZone.classList.add('has-file');

    // 기존 미리보기 제거
    const existingPreview = dropZone.querySelector('.doc-preview');
    if (existingPreview) existingPreview.remove();

    // 아이콘, 텍스트, 버튼 숨기기
    const icon = dropZone.querySelector('.doc-drop-zone-icon');
    const text = dropZone.querySelector('p');
    const buttons = dropZone.querySelector('.doc-buttons');
    if (icon) icon.style.display = 'none';
    if (text) text.style.display = 'none';
    if (buttons) buttons.style.display = 'none';

    // 미리보기 생성
    const preview = document.createElement('div');
    preview.className = 'doc-preview';

    const isImage = file.type.startsWith('image/');
    const fileSize = formatFileSize(file.size);

    if (isImage) {
        const reader = new FileReader();
        reader.onload = (e) => {
            preview.innerHTML = `
                <img src="${e.target.result}" alt="Preview">
                <div class="doc-preview-info">
                    <div class="doc-preview-name">${file.name}</div>
                    <div class="doc-preview-size">${fileSize}</div>
                </div>
                <button type="button" class="doc-preview-remove" onclick="removeFile(this)">
                    <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>
            `;
        };
        reader.readAsDataURL(file);
    } else {
        const ext = file.name.split('.').pop().toUpperCase();
        preview.innerHTML = `
            <div class="w-10 h-10 bg-gray-100 rounded flex items-center justify-center text-xs font-medium text-gray-500">${ext}</div>
            <div class="doc-preview-info">
                <div class="doc-preview-name">${file.name}</div>
                <div class="doc-preview-size">${fileSize}</div>
            </div>
            <button type="button" class="doc-preview-remove" onclick="removeFile(this)">
                <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>
        `;
    }

    dropZone.appendChild(preview);
}

function removeFile(btn) {
    const dropZone = btn.closest('.doc-drop-zone');
    const fileInput = dropZone.querySelector('.doc-file-input');
    const preview = dropZone.querySelector('.doc-preview');
    const icon = dropZone.querySelector('.doc-drop-zone-icon');
    const text = dropZone.querySelector('p');
    const buttons = dropZone.querySelector('.doc-buttons');

    // 파일 input 초기화
    fileInput.value = '';

    // 미리보기 제거
    if (preview) preview.remove();

    // 아이콘, 텍스트, 버튼 다시 표시
    if (icon) icon.style.display = '';
    if (text) text.style.display = '';
    if (buttons) buttons.style.display = '';

    dropZone.classList.remove('has-file');
}

function formatFileSize(bytes) {
    if (bytes >= 1048576) return (bytes / 1048576).toFixed(1) + ' MB';
    if (bytes >= 1024) return (bytes / 1024).toFixed(1) + ' KB';
    return bytes + ' bytes';
}

// 문서 추가 버튼
document.getElementById('add-document-btn').addEventListener('click', function() {
    const container = document.getElementById('document-list');
    const template = `
        <div class="document-item border border-gray-200 rounded-lg p-4 relative" data-index="${documentIndex}">
            <button type="button" onclick="this.parentElement.remove()"
                    class="absolute top-2 right-2 text-gray-400 hover:text-red-500 z-10">
                <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 class="flex items-start gap-4">
                <div class="w-40 flex-shrink-0">
                    <label class="block text-sm font-medium text-gray-700 mb-1">문서 유형</label>
                    <select name="documents[${documentIndex}][document_type]"
                            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">
                        @foreach($documentTypes as $value => $label)
                        <option value="{{ $value }}">{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="flex-1">
                    <label class="block text-sm font-medium text-gray-700 mb-1">파일</label>
                    <div class="doc-drop-zone" data-index="${documentIndex}">
                        <input type="file" name="documents[${documentIndex}][file]" class="hidden doc-file-input" accept="image/*,.pdf,.doc,.docx">
                        <input type="file" class="hidden doc-camera-input" accept="image/*" capture="environment">
                        <svg class="doc-drop-zone-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12" />
                        </svg>
                        <p class="text-xs text-gray-500 mt-1">클릭 또는 드래그하여 업로드</p>
                        <div class="doc-buttons flex gap-2 mt-2">
                            <button type="button" class="doc-file-btn inline-flex items-center px-3 py-1.5 text-xs font-medium text-gray-700 bg-white border border-gray-300 rounded-md hover:bg-gray-50">
                                <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="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                                </svg>
                                파일 선택
                            </button>
                            <button type="button" class="doc-camera-btn inline-flex items-center px-3 py-1.5 text-xs font-medium text-white bg-blue-600 border border-blue-600 rounded-md hover:bg-blue-700">
                                <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="M3 9a2 2 0 012-2h.93a2 2 0 001.664-.89l.812-1.22A2 2 0 0110.07 4h3.86a2 2 0 011.664.89l.812 1.22A2 2 0 0018.07 7H19a2 2 0 012 2v9a2 2 0 01-2 2H5a2 2 0 01-2-2V9z" />
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 13a3 3 0 11-6 0 3 3 0 016 0z" />
                                </svg>
                                카메라 촬영
                            </button>
                        </div>
                    </div>
                </div>
                <div class="w-40 flex-shrink-0">
                    <label class="block text-sm font-medium text-gray-700 mb-1">설명</label>
                    <input type="text" name="documents[${documentIndex}][description]"
                           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>
    `;
    container.insertAdjacentHTML('beforeend', template);

    // 새로 추가된 드롭존 초기화
    const newItem = container.lastElementChild;
    const newDropZone = newItem.querySelector('.doc-drop-zone');
    initDropZone(newDropZone);

    documentIndex++;
});

// 초기 드롭존 초기화
document.querySelectorAll('.doc-drop-zone').forEach(initDropZone);

// 테스트 데이터 자동 입력 (번개 버튼)
function fillRandomData() {
    // 한글 성씨와 이름 목록
    const lastNames = ['김', '이', '박', '최', '정', '강', '조', '윤', '장', '임'];
    const firstNames = ['민준', '서연', '예준', '서윤', '도윤', '지우', '시우', '하윤', '주원', '지호', '수빈', '유진', '지민', '현우', '승현'];

    // 랜덤 문자열 생성
    const randomStr = () => Math.random().toString(36).substring(2, 8);
    const randomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;

    // 랜덤 이름 (3자)
    const name = lastNames[randomNum(0, lastNames.length - 1)] +
                 firstNames[randomNum(0, firstNames.length - 1)].substring(0, 2);

    // 랜덤 ID와 이메일
    const userId = 'test_' + randomStr();
    const email = userId + '@example.com';

    // 랜덤 전화번호
    const phone = '010-' + randomNum(1000, 9999) + '-' + randomNum(1000, 9999);

    // 고정 비밀번호 (테스트용)
    const password = '12341234';

    // 폼 필드 채우기
    document.querySelector('input[name="user_id"]').value = userId;
    document.querySelector('input[name="name"]').value = name;
    document.querySelector('input[name="email"]').value = email;
    document.querySelector('input[name="phone"]').value = phone;
    document.querySelector('input[name="password"]').value = password;
    document.querySelector('input[name="password_confirmation"]').value = password;

    // 사업자 정보
    const companyInput = document.querySelector('input[name="company_name"]');
    const bizNoInput = document.querySelector('input[name="biz_no"]');
    const addressInput = document.querySelector('input[name="address"]');
    if (companyInput) companyInput.value = name + ' 개인사업자';
    if (bizNoInput) bizNoInput.value = randomNum(100, 999) + '-' + randomNum(10, 99) + '-' + randomNum(10000, 99999);
    if (addressInput) addressInput.value = '서울특별시 강남구 테헤란로 ' + randomNum(1, 500) + '번길 ' + randomNum(1, 30);

    // 역할 체크박스 모두 체크
    document.querySelectorAll('input[name="role_ids[]"]').forEach(cb => {
        cb.checked = true;
    });

    // 콘솔에 비밀번호 출력 (테스트용)
    console.log('=== 테스트 계정 정보 ===');
    console.log('ID:', userId);
    console.log('이메일:', email);
    console.log('비밀번호:', password);
    console.log('========================');

    // 알림
    alert('테스트 데이터가 입력되었습니다.\n\n비밀번호: 12341234');
}
</script>
@endpush
