@extends('layouts.app')

@section('page_title', 'Edit Promotion')

@section('content')
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0 fw-bold text-dark">Edit Promotion</h5>
        </div>
        <div class="card-body">
            @if (session('error'))
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    {{ session('error') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif
            @if (session('success'))
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    {{ session('success') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif
            <form action="{{ route('promotions.update', $promotion['id']) }}" method="POST" enctype="multipart/form-data">
                @csrf
                @method('PUT')
                <div class="mb-3">
                    <label for="title" class="form-label">Title</label>
                    <input type="text" class="form-control" id="title" name="title" value="{{ $promotion['title'] }}" required maxlength="100">
                    @error('title')
                        <div class="text-danger">{{ $message }}</div>
                    @enderror
                </div>
                <div class="mb-3">
                    <label for="type" class="form-label">Type</label>
                    <select class="form-select" id="type" name="type" required>
                        @foreach ($promoTypes as $key => $type)
                            <option value="{{ $type }}" data-id="{{ $key }}" {{ $promotion['type'] == $type ? 'selected' : '' }}>{{ $type }}</option>
                        @endforeach
                    </select>
                    <input type="hidden" name="promo_type" id="promo_type" value="{{ $promotion['promo_type'] }}">
                    @error('type')
                        <div class="text-danger">{{ $message }}</div>
                    @enderror
                    @error('promo_type')
                        <div class="text-danger">{{ $message }}</div>
                    @enderror
                </div>
                <div class="mb-3">
                    <label for="description" class="form-label">Description</label>
                    <textarea class="form-control" id="description" name="description" required>{{ $promotion['description'] }}</textarea>
                    @error('description')
                        <div class="text-danger">{{ $message }}</div>
                    @enderror
                </div>
                <div class="mb-3">
                    <label for="image" class="form-label">Image</label>
                    @if ($promotion['image'])
                        <div class="mb-2">
                            <p>Current Image: <a href="{{ $promotion['image'] }}" target="_blank">View Image</a></p>
                        </div>
                    @endif
                    <input type="file" class="form-control" id="image" name="image" accept="image/*">
                    <small class="form-text text-muted">Leave blank to keep the current image.</small>
                    @error('image')
                        <div class="text-danger">{{ $message }}</div>
                    @enderror
                </div>
                <div class="mb-3">
                    <label for="start_date" class="form-label">Start Date</label>
                    <input type="date" class="form-control" id="start_date" name="start_date" value="{{ $promotion['startDate'] }}" required>
                    @error('start_date')
                        <div class="text-danger">{{ $message }}</div>
                    @enderror
                </div>
                <div class="mb-3">
                    <label for="end_date" class="form-label">End Date</label>
                    <input type="date" class="form-control" id="end_date" name="end_date" value="{{ $promotion['endDate'] }}" required>
                    @error('end_date')
                        <div class="text-danger">{{ $message }}</div>
                    @enderror
                </div>
                <div class="mb-3">
                    <label for="status" class="form-label">Status</label>
                    <select class="form-select" id="status" name="status" required>
                        <option value="On Going" {{ $promotion['status'] == 'On Going' ? 'selected' : '' }}>On Going</option>
                        <option value="Done" {{ $promotion['status'] == 'Done' ? 'selected' : '' }}>Done</option>
                    </select>
                    @error('status')
                        <div class="text-danger">{{ $message }}</div>
                    @enderror
                </div>
                <div class="mb-3 form-check">
                    <input type="checkbox" class="form-check-input" id="is_toppromotion" name="is_toppromotion" value="1" {{ $promotion['is_toppromotion'] ? 'checked' : '' }}>
                    <label class="form-check-label" for="is_toppromotion">Is Top Promotion</label>
                    @error('is_toppromotion')
                        <div class="text-danger">{{ $message }}</div>
                    @enderror
                </div>
                <div class="mb-3 form-check">
                    <input type="checkbox" class="form-check-input" id="is_gps" name="is_gps" value="1" {{ $promotion['is_gps'] ? 'checked' : '' }}>
                    <label class="form-check-label" for="is_gps">Is GPS-Based</label>
                    @error('is_gps')
                        <div class="text-danger">{{ $message }}</div>
                    @enderror
                </div>
                <input type="hidden" name="station_uuid" value="[]">
                <button type="submit" class="btn btn-primary">Update Promotion</button>
                <a href="{{ route('promotions') }}" class="btn btn-secondary ms-2">Cancel</a>
            </form>
        </div>
    </div>

    <script>
        document.getElementById('type').addEventListener('change', function() {
            const selectedOption = this.options[this.selectedIndex];
            const promoTypeId = selectedOption.getAttribute('data-id');
            document.getElementById('promo_type').value = promoTypeId;
        });
    </script>
@endsection