@extends('layouts.app')

@section('title', __('stage_academy.offers.title'))

@push('styles')
<style>
    .sa-offer-thumb {
        width: 88px;
        height: 64px;
        border-radius: 12px;
        object-fit: cover;
        border: 1px solid rgba(0,0,0,.08);
    }

    body.dark-mode .sa-offer-thumb {
        border-color: rgba(255,255,255,.12);
    }
</style>
@endpush

@section('content')
@php
    $isAr = app()->getLocale() === 'ar';
@endphp

<div
    id="saOffersPage"
    class="container-fluid"
    data-offer-base-url="{{ url('/stage-academy/offers') }}"
    data-open-create="{{ session('open_offer_create_modal') ? '1' : '0' }}"
    data-open-edit-id="{{ session('open_offer_edit_id') }}"
>
    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white py-3">
            <div class="d-flex align-items-center justify-content-between gap-2 flex-wrap">
                <div>
                    <h5 class="mb-0 fw-bold">{{ __('stage_academy.offers.title') }}</h5>
                </div>
                <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createOfferModal">
                    <i class="bi bi-plus-lg"></i> {{ __('stage_academy.offers.add_button') }}
                </button>
            </div>
        </div>
        <div class="card-body">
            @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

            @if($offers->isEmpty())
                <div class="text-center py-5 text-muted">
                    <i class="bi bi-tag fs-1 d-block mb-3"></i>
                    <div>{{ __('stage_academy.offers.empty') }}</div>
                </div>
            @else
                <div class="row g-3">
                    @foreach($offers as $offer)
                        <div class="col-12">
                            <div class="card border-0 shadow-sm rounded-4">
                                <div class="card-body">
                                    <div class="d-flex align-items-start justify-content-between gap-3 flex-wrap">
                                        <div class="d-flex align-items-start gap-3">
                                            <img
                                                class="sa-offer-thumb"
                                                src="{{ asset('storage/'.$offer->image) }}"
                                                alt="{{ $isAr ? $offer->name_ar : $offer->name_en }}"
                                            >
                                            <div>
                                                <div class="fw-semibold">{{ $isAr ? $offer->name_ar : $offer->name_en }}</div>
                                                @php
                                                    $details = $isAr ? (string) $offer->details_ar : (string) $offer->details_en;
                                                    $details = trim($details);
                                                @endphp
                                                @if($details !== '')
                                                    <div class="text-secondary small mt-1">{{ $details }}</div>
                                                @endif
                                                @if($offer->link)
                                                    <div class="small mt-2">
                                                        <a href="{{ $offer->link }}" target="_blank" rel="noopener" class="text-decoration-none">
                                                            <i class="bi bi-link-45deg"></i> {{ $offer->link }}
                                                        </a>
                                                    </div>
                                                @endif
                                            </div>
                                        </div>

                                        <div class="d-flex align-items-center gap-2">
                                            <button
                                                type="button"
                                                class="btn btn-sm btn-outline-primary"
                                                data-bs-toggle="modal"
                                                data-bs-target="#editOfferModal"
                                                data-id="{{ $offer->id }}"
                                                data-name-ar="{{ $offer->name_ar }}"
                                                data-name-en="{{ $offer->name_en }}"
                                                data-details-ar="{{ $offer->details_ar }}"
                                                data-details-en="{{ $offer->details_en }}"
                                                data-image-url="{{ asset('storage/'.$offer->image) }}"
                                                data-link="{{ $offer->link }}"
                                            >
                                                <i class="bi bi-pencil"></i> {{ __('common.edit') }}
                                            </button>

                                            <button
                                                type="button"
                                                class="btn btn-sm btn-outline-danger"
                                                data-bs-toggle="modal"
                                                data-bs-target="#deleteOfferModal"
                                                data-id="{{ $offer->id }}"
                                                data-name="{{ $isAr ? $offer->name_ar : $offer->name_en }}"
                                            >
                                                <i class="bi bi-trash"></i> {{ __('common.delete') }}
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
            @endif
        </div>
    </div>
    <div class="mt-3 d-flex">
        <a href="{{ route('stage-academy') }}" class="btn btn-outline-secondary app-back-end">
            <i class="bi {{ app()->getLocale() == 'ar' ? 'bi-arrow-right' : 'bi-arrow-left' }}"></i> {{ __('common.back') }}
        </a>
    </div>
</div>

<div class="modal fade" id="createOfferModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">{{ __('stage_academy.offers.add_button') }}</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form method="POST" action="{{ route('stage-academy.offers.store') }}" enctype="multipart/form-data">
                @csrf
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-12 col-md-6">
                            <label class="form-label">{{ __('stage_academy.offers.name_ar') }}</label>
                            <input type="text" name="name_ar" class="form-control @error('name_ar') is-invalid @enderror" value="{{ old('name_ar') }}" required>
                            @error('name_ar')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                        <div class="col-12 col-md-6">
                            <label class="form-label">{{ __('stage_academy.offers.name_en') }}</label>
                            <input type="text" name="name_en" class="form-control @error('name_en') is-invalid @enderror" value="{{ old('name_en') }}" required dir="ltr">
                            @error('name_en')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                        <div class="col-12 col-md-6">
                            <label class="form-label">{{ __('stage_academy.offers.details_ar') }}</label>
                            <textarea name="details_ar" rows="4" class="form-control @error('details_ar') is-invalid @enderror">{{ old('details_ar') }}</textarea>
                            @error('details_ar')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                        <div class="col-12 col-md-6">
                            <label class="form-label">{{ __('stage_academy.offers.details_en') }}</label>
                            <textarea name="details_en" rows="4" class="form-control @error('details_en') is-invalid @enderror" dir="ltr">{{ old('details_en') }}</textarea>
                            @error('details_en')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                        <div class="col-12 col-md-6">
                            <label class="form-label">{{ __('stage_academy.offers.image') }}</label>
                            <input type="file" name="image" class="form-control @error('image') is-invalid @enderror" accept=".jpg,.jpeg,.png,.webp" required>
                            @error('image')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                        <div class="col-12 col-md-6">
                            <label class="form-label">{{ __('stage_academy.offers.link') }}</label>
                            <input type="url" name="link" class="form-control @error('link') is-invalid @enderror" value="{{ old('link') }}" placeholder="{{ __('stage_academy.offers.link_placeholder') }}" dir="ltr">
                            @error('link')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">{{ __('common.cancel') }}</button>
                    <button type="submit" class="btn btn-primary">{{ __('common.save') }}</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="editOfferModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">{{ __('common.edit') }}</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="editOfferForm" method="POST" action="#" enctype="multipart/form-data">
                @csrf
                @method('PUT')
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-12 col-md-6">
                            <label class="form-label">{{ __('stage_academy.offers.name_ar') }}</label>
                            <input type="text" name="name_ar" id="edit_offer_name_ar" class="form-control @error('name_ar') is-invalid @enderror" value="{{ old('name_ar') }}" required>
                            @error('name_ar')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                        <div class="col-12 col-md-6">
                            <label class="form-label">{{ __('stage_academy.offers.name_en') }}</label>
                            <input type="text" name="name_en" id="edit_offer_name_en" class="form-control @error('name_en') is-invalid @enderror" value="{{ old('name_en') }}" required dir="ltr">
                            @error('name_en')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                        <div class="col-12 col-md-6">
                            <label class="form-label">{{ __('stage_academy.offers.details_ar') }}</label>
                            <textarea name="details_ar" id="edit_offer_details_ar" rows="4" class="form-control @error('details_ar') is-invalid @enderror">{{ old('details_ar') }}</textarea>
                            @error('details_ar')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                        <div class="col-12 col-md-6">
                            <label class="form-label">{{ __('stage_academy.offers.details_en') }}</label>
                            <textarea name="details_en" id="edit_offer_details_en" rows="4" class="form-control @error('details_en') is-invalid @enderror" dir="ltr">{{ old('details_en') }}</textarea>
                            @error('details_en')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                        <div class="col-12 col-md-6">
                            <label class="form-label">{{ __('stage_academy.offers.image') }}</label>
                            <input type="file" name="image" class="form-control @error('image') is-invalid @enderror" accept=".jpg,.jpeg,.png,.webp">
                            @error('image')<div class="invalid-feedback">{{ $message }}</div>@enderror
                            <div class="mt-2">
                                <img id="edit_offer_image_preview" class="sa-offer-thumb" src="" alt="">
                            </div>
                        </div>
                        <div class="col-12 col-md-6">
                            <label class="form-label">{{ __('stage_academy.offers.link') }}</label>
                            <input type="url" name="link" id="edit_offer_link" class="form-control @error('link') is-invalid @enderror" value="{{ old('link') }}" placeholder="{{ __('stage_academy.offers.link_placeholder') }}" dir="ltr">
                            @error('link')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">{{ __('common.cancel') }}</button>
                    <button type="submit" class="btn btn-primary">{{ __('common.save') }}</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="deleteOfferModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">{{ __('stage_academy.offers.delete_confirm_title') }}</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="text-secondary">
                    {{ __('stage_academy.offers.delete_confirm_body') }}
                </div>
                <div class="mt-2 fw-semibold" id="deleteOfferName"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">{{ __('common.cancel') }}</button>
                <form id="deleteOfferForm" method="POST" action="#">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-danger">{{ __('common.delete') }}</button>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
    (function () {
        const editModal = document.getElementById('editOfferModal');
        const deleteModal = document.getElementById('deleteOfferModal');

        if (editModal) {
            editModal.addEventListener('show.bs.modal', function (event) {
                const button = event.relatedTarget;
                if (!button) return;

                const id = button.getAttribute('data-id');
                const nameAr = button.getAttribute('data-name-ar') || '';
                const nameEn = button.getAttribute('data-name-en') || '';
                const detailsAr = button.getAttribute('data-details-ar') || '';
                const detailsEn = button.getAttribute('data-details-en') || '';
                const imageUrl = button.getAttribute('data-image-url') || '';
                const link = button.getAttribute('data-link') || '';

                const form = document.getElementById('editOfferForm');
                const nameArInput = document.getElementById('edit_offer_name_ar');
                const nameEnInput = document.getElementById('edit_offer_name_en');
                const detailsArInput = document.getElementById('edit_offer_details_ar');
                const detailsEnInput = document.getElementById('edit_offer_details_en');
                const imagePreview = document.getElementById('edit_offer_image_preview');
                const linkInput = document.getElementById('edit_offer_link');

                const root = document.getElementById('saOffersPage');
                const baseUrl = root ? root.getAttribute('data-offer-base-url') : '';

                if (form) {
                    form.action = baseUrl ? (baseUrl + "/" + id) : ("/stage-academy/offers/" + id);
                }
                if (nameArInput) nameArInput.value = nameAr;
                if (nameEnInput) nameEnInput.value = nameEn;
                if (detailsArInput) detailsArInput.value = detailsAr;
                if (detailsEnInput) detailsEnInput.value = detailsEn;
                if (imagePreview) imagePreview.src = imageUrl;
                if (linkInput) linkInput.value = link;
            });
        }

        if (deleteModal) {
            deleteModal.addEventListener('show.bs.modal', function (event) {
                const button = event.relatedTarget;
                if (!button) return;

                const id = button.getAttribute('data-id');
                const name = button.getAttribute('data-name') || '';

                const form = document.getElementById('deleteOfferForm');
                const nameEl = document.getElementById('deleteOfferName');
                const root = document.getElementById('saOffersPage');
                const baseUrl = root ? root.getAttribute('data-offer-base-url') : '';

                if (form) {
                    form.action = baseUrl ? (baseUrl + "/" + id) : ("/stage-academy/offers/" + id);
                }
                if (nameEl) nameEl.textContent = name;
            });
        }

        const root = document.getElementById('saOffersPage');
        const openCreate = root ? root.getAttribute('data-open-create') === '1' : false;
        const openEditId = root ? root.getAttribute('data-open-edit-id') : '';

        if (openCreate) {
            const modal = new bootstrap.Modal(document.getElementById('createOfferModal'));
            modal.show();
        } else if (openEditId) {
            const trigger = document.querySelector('[data-bs-target="#editOfferModal"][data-id="' + openEditId + '"]');
            if (trigger) {
                trigger.click();
            }
        }
    })();
</script>
@endpush
