@extends('layouts.admin')

@section('title', 'New Announcement')
@section('page-title', 'Create Announcement')

@section('content')
<div class="max-w-2xl mx-auto bg-white rounded-xl shadow-sm p-6">
    <form action="{{ route('admin.announcements.store') }}" method="POST" class="space-y-6">
        @csrf

        <div>
            <label class="block text-sm font-medium text-gray-700 mb-1">Title *</label>
            <input type="text" name="title" value="{{ old('title') }}" required
                class="w-full border border-gray-300 rounded-lg px-3 py-2 @error('title') border-red-500 @enderror">
            @error('title')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
        </div>

        <div>
            <label class="block text-sm font-medium text-gray-700 mb-1">Message *</label>
            <textarea name="message" rows="5" required
                class="w-full border border-gray-300 rounded-lg px-3 py-2 @error('message') border-red-500 @enderror">{{ old('message') }}</textarea>
            @error('message')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
        </div>

        <div class="grid grid-cols-2 gap-4">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Type *</label>
                <select name="type" required class="w-full border border-gray-300 rounded-lg px-3 py-2">
                    <option value="info" {{ old('type') == 'info' ? 'selected' : '' }}>Info (Blue)</option>
                    <option value="success" {{ old('type') == 'success' ? 'selected' : '' }}>Success (Green)</option>
                    <option value="warning" {{ old('type') == 'warning' ? 'selected' : '' }}>Warning (Yellow)</option>
                    <option value="danger" {{ old('type') == 'danger' ? 'selected' : '' }}>Danger (Red)</option>
                </select>
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Target *</label>
                <select name="target" id="target" required class="w-full border border-gray-300 rounded-lg px-3 py-2" onchange="toggleVendorSelect()">
                    <option value="all_vendors">All Vendors</option>
                    <option value="specific_vendors">Specific Vendors</option>
                </select>
            </div>
        </div>

        <div id="vendor-select" class="hidden">
            <label class="block text-sm font-medium text-gray-700 mb-1">Select Vendors</label>
            <div class="border rounded-lg p-3 max-h-48 overflow-y-auto space-y-2">
                @foreach($vendors as $vendor)
                    <label class="flex items-center">
                        <input type="checkbox" name="vendor_ids[]" value="{{ $vendor->id }}" class="rounded mr-2">
                        <span class="text-sm">{{ $vendor->store_name ?? $vendor->shop_name }}</span>
                    </label>
                @endforeach
            </div>
        </div>

        <div class="grid grid-cols-2 gap-4">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Expires At</label>
                <input type="date" name="expires_at" value="{{ old('expires_at') }}"
                    class="w-full border border-gray-300 rounded-lg px-3 py-2">
            </div>

            <div class="flex items-end pb-1">
                <label class="flex items-center">
                    <input type="checkbox" name="is_pinned" value="1" {{ old('is_pinned') ? 'checked' : '' }} class="rounded mr-2">
                    <span class="text-sm text-gray-700">Pin to top of dashboard</span>
                </label>
            </div>
        </div>

        <div class="flex gap-4">
            <button type="submit" class="bg-blue-600 text-white px-6 py-2 rounded-lg hover:bg-blue-700">Publish Announcement</button>
            <a href="{{ route('admin.announcements.index') }}" class="bg-gray-100 text-gray-700 px-6 py-2 rounded-lg hover:bg-gray-200">Cancel</a>
        </div>
    </form>
</div>

@push('scripts')
<script>
function toggleVendorSelect() {
    const target = document.getElementById('target').value;
    document.getElementById('vendor-select').classList.toggle('hidden', target !== 'specific_vendors');
}
</script>
@endpush
@endsection
