@extends('layouts.app')
@section('title', __('Translation Manager'))
@section('content')
@php($completion = $totalKeys ? round(($savedCount / $totalKeys) * 100) : 100)
<div class="row g-3">
    <div class="col-lg-4">
        <div class="card mb-3">
            <div class="card-body p-4">
                <h6>{{ __('Localization Progress') }}</h6>
                <div class="d-flex justify-content-between mb-1"><small>{{ strtoupper($locale) }}</small><small>{{ $completion }}%</small></div>
                <div class="progress mb-3" style="height:8px"><div class="progress-bar bg-success" style="width:{{ min(100, $completion) }}%"></div></div>
                <div class="d-flex gap-2 flex-wrap">
                    @foreach(['en' => 'English', 'fa' => 'دری', 'ps' => 'پښتو'] as $code => $name)
                        <a href="{{ route('translations.index', ['locale' => $code]) }}" class="btn btn-sm {{ $locale === $code ? 'btn-success' : 'btn-light' }}">{{ $name }}</a>
                    @endforeach
                </div>
            </div>
        </div>
        <div class="card">
            <div class="card-body p-4">
                <h6>{{ __('Add or Update Translation') }}</h6>
                <form method="POST" action="{{ route('translations.store') }}">@csrf
                    <div class="mb-3"><label class="form-label">{{ __('Language') }}</label><select name="locale" class="form-select"><option value="en" @selected($locale === 'en')>English</option><option value="fa" @selected($locale === 'fa')>دری</option><option value="ps" @selected($locale === 'ps')>پښتو</option></select></div>
                    <div class="mb-3"><label class="form-label">{{ __('English Key') }}</label><input name="key" class="form-control" value="{{ request('key') }}" placeholder="{{ __('Use the exact English screen text') }}" required></div>
                    <div class="mb-3"><label class="form-label">{{ __('Translation') }}</label><textarea name="value" class="form-control" rows="4" required></textarea></div>
                    <button class="btn btn-success w-100">{{ __('Save Translation') }}</button>
                </form>
            </div>
        </div>
    </div>
    <div class="col-lg-8">
        <div class="card mb-3">
            <div class="card-body p-4">
                <div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">
                    <h6 class="mb-0">{{ __('Missing Translation Keys') }}</h6>
                    <form class="d-flex gap-2">
                        <input type="hidden" name="locale" value="{{ $locale }}">
                        <input name="search" class="form-control form-control-sm" value="{{ $search }}" placeholder="{{ __('Search key') }}">
                        <button class="btn btn-light btn-sm"><i class="fa-solid fa-magnifying-glass"></i></button>
                    </form>
                </div>
                @forelse($missingKeys as $key)
                    <div class="d-flex justify-content-between align-items-center border-top py-2 gap-2">
                        <span class="small">{{ $key }}</span>
                        <a class="btn btn-light btn-sm" href="{{ route('translations.index', ['locale' => $locale, 'key' => $key]) }}"><i class="fa-solid fa-plus me-1"></i>{{ __('Translate') }}</a>
                    </div>
                @empty
                    <p class="text-muted mb-0 text-center py-3">{{ __('No missing translation keys found for this filter.') }}</p>
                @endforelse
            </div>
        </div>
        <div class="card">
            <div class="card-body p-4">
                <h6 class="mb-3">{{ __('Saved Translations') }}</h6>
                <div class="table-responsive">
                    <table class="table align-middle">
                        <thead><tr><th>{{ __('Key') }}</th><th>{{ __('Translation') }}</th><th></th></tr></thead>
                        <tbody>
                            @forelse($translations as $translation)
                                <tr>
                                    <td>{{ $translation->key }}</td>
                                    <td>{{ $translation->value }}</td>
                                    <td class="text-end">
                                        <a class="btn btn-light btn-sm" href="{{ route('translations.index', ['locale' => $locale, 'key' => $translation->key]) }}"><i class="fa-solid fa-pen"></i></a>
                                        <form class="d-inline" method="POST" action="{{ route('translations.destroy', $translation) }}" onsubmit="return confirm('{{ __('Remove this translation?') }}')">@csrf @method('DELETE')<button class="btn btn-light text-danger btn-sm"><i class="fa-solid fa-trash"></i></button></form>
                                    </td>
                                </tr>
                            @empty
                                <tr><td colspan="3" class="text-center text-muted">{{ __('No translations saved for this language.') }}</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                {{ $translations->appends(['locale' => $locale, 'search' => $search])->links('pagination::bootstrap-5') }}
            </div>
        </div>
    </div>
</div>
@endsection
