@extends('layouts.app')

@section('content')
<div class="container">
    <h1 class="mb-4">Edit Exhibit</h1>

    <a href="{{ route('exhibits.index') }}" class="btn btn-secondary mb-3">Back to List</a>

    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <form action="{{ route('exhibits.update', $exhibit->id) }}" method="POST" enctype="multipart/form-data">
        @csrf
        @method('PUT')

        <div class="mb-3">
            <label for="gallery_id" class="form-label">Select Gallery</label>
            <select name="gallery_id" class="form-control" required>
                <option value="">-- Select Gallery --</option>
                @foreach ($galleries as $gallery)
                    <option value="{{ $gallery->id }}" {{ $exhibit->gallery_id == $gallery->id ? 'selected' : '' }}>
                        {{ $gallery->gallery_name }}
                    </option>
                @endforeach
            </select>
        </div>

        <div class="mb-3">
            <label for="exhibit_name" class="form-label">Exhibit Name</label>
            <input type="text" name="exhibit_name" class="form-control" value="{{ old('exhibit_name', $exhibit->exhibit_name) }}" required>
        </div>

        <div class="mb-3">
            <label for="language" class="form-label">Language</label>
            <select name="language" class="form-control" required>
                <option value="English" {{ $exhibit->language == 'English' ? 'selected' : '' }}>English</option>
                <option value="Malayalam" {{ $exhibit->language == 'Malayalam' ? 'selected' : '' }}>Malayalam</option>
                <option value="Hindi" {{ $exhibit->language == 'Hindi' ? 'selected' : '' }}>Hindi</option>
                <option value="Tamil" {{ $exhibit->language == 'Tamil' ? 'selected' : '' }}>Tamil</option>
            </select>
        </div>

        <div class="mb-3">
            <label for="short_description" class="form-label">Short Description</label>
            <textarea name="short_description" class="form-control" rows="3">{{ old('short_description', $exhibit->short_description) }}</textarea>
        </div>

        <div class="mb-3">
            <label for="auto_play" class="form-label">Auto Play</label>
            <select name="auto_play" class="form-control" required>
                <option value="Yes" {{ $exhibit->auto_play == 'Yes' ? 'selected' : '' }}>Yes</option>
                <option value="No" {{ $exhibit->auto_play == 'No' ? 'selected' : '' }}>No</option>
            </select>
        </div>

        <!-- New Field: Close after Autoplay -->
        <div class="form-group">
          <label for="close_after_autoplay">Close After Autoplay</label>
          <select name="close_after_autoplay" id="close_after_autoplay" class="form-control">
            <option value="Yes" {{ $exhibit->close_after_autoplay == 'Yes' ? 'selected' : '' }}>Yes</option>
            <option value="No" {{ $exhibit->close_after_autoplay == 'No' ? 'selected' : '' }}>No</option>
          </select>
        </div>

        <!-- New Field: Show in App Screen as Icons -->
        <div class="form-group">
          <label for="show_in_app_icon">Show in App Screen as Icons</label>
          <select name="show_in_app_icon" id="show_in_app_icon" class="form-control">
            <option value="Yes" {{ $exhibit->show_in_app_icon == 'Yes' ? 'selected' : '' }}>Yes</option>
            <option value="No" {{ $exhibit->show_in_app_icon == 'No' ? 'selected' : '' }}>No</option>
          </select>
        </div>


        <div class="mb-3">
            <label for="order" class="form-label">Order</label>
            <input type="number" name="order" class="form-control" value="{{ old('order', $exhibit->order) }}" required>
        </div>

        <!--<div class="mb-3">
            <label for="exhibit_icon_path" class="form-label">Exhibit Icon</label>
            <input type="file" name="exhibit_icon_path" class="form-control">
            @if ($exhibit->exhibit_icon_path)
                <div class="mt-2">
                    <img src="{{ asset('storage/' . $exhibit->exhibit_icon_path) }}" width="100" alt="Exhibit Icon">
                </div>
            @endif
        </div>

        <div class="mb-3">
            <label for="video_path" class="form-label">Exhibit Video</label>
            <input type="file" name="video_path" class="form-control">
            @if ($exhibit->video_path)
                <div class="mt-2">
                    <video width="200" controls>
                        <source src="{{ asset('storage/' . $exhibit->video_path) }}" type="video/mp4">
                        Your browser does not support the video tag.
                    </video>
                </div>
            @endif
        </div>-->

        <div class="mb-3">
            <label for="exhibit_icon_path" class="form-label">Exhibit Icon</label>
            <input type="text" name="exhibit_icon_path" class="form-control" value="{{ old('exhibit_icon_path', $exhibit->exhibit_icon_path) }}">
            
        </div>

        <div class="mb-3">
            <label for="video_path" class="form-label">Exhibit Video</label>
            <input type="text" name="video_path" class="form-control" value="{{ old('video_path', $exhibit->video_path) }}">
            
        </div>

        <div class="mb-3">
            <label for="show_description_flag" class="form-label">Show Description</label>
            <select name="show_description_flag" class="form-control" required>
                <option value="Yes" {{ $exhibit->show_description_flag == 'Yes' ? 'selected' : '' }}>Yes</option>
                <option value="No" {{ $exhibit->show_description_flag == 'No' ? 'selected' : '' }}>No</option>
            </select>
        </div>

        <!--<div class="mb-3">
            <label for="next_exhibit_ids" class="form-label">Next Exhibits</label>
            <select name="next_exhibit_ids[]" class="form-control" multiple>
                @foreach ($exhibits as $e)
                    <option value="{{ $e->id }}" 
                        {{ is_string($exhibit->next_exhibit_ids) && in_array($e->id, explode(',', $exhibit->next_exhibit_ids)) ? 'selected' : '' }}>
                        {{ $e->exhibit_name }}
                    </option>
                @endforeach
            </select>
        </div>-->

        <!-- Next Exhibit as Single Combo Box -->
        <div class="form-group">
          <label for="next_exhibit_id">Next Exhibit</label>
          <select name="next_exhibit_id" id="next_exhibit_id" class="form-control">
            <option value="">None</option>
            @foreach($exhibits as $ex)
              <option value="{{ $ex->id }}" {{ $ex->id == $exhibit->next_exhibit_id ? 'selected' : '' }}>
                {{ $ex->exhibit_name }}
              </option>
            @endforeach
          </select>
        </div>

        <!-- Previous Exhibit as Single Combo Box -->
        <div class="form-group">
          <label for="previous_exhibit_id">Previous Exhibit</label>
          <select name="previous_exhibit_id" id="previous_exhibit_id" class="form-control">
            <option value="">None</option>
            @foreach($exhibits as $ex)
              <option value="{{ $ex->id }}" {{ $ex->id == $exhibit->previous_exhibit_id ? 'selected' : '' }}>
                {{ $ex->exhibit_name }}
              </option>
            @endforeach
          </select>
        </div>

        <button type="submit" class="btn btn-success">Update Exhibit</button>
    </form>
</div>
@endsection
