@extends('layouts.admin.theme')
@section('title', env('APP_NAME') . ' :: ' . __('Nova categoria da Carta'))

@section('content')
    @include('errors.messageFlash')

        <div class="card shadow-sm">
        @php $headLine = "<i class='fa-solid fa-folder-plus'></i> " . __('Nova categoria da Carta'); @endphp
                    @include('components.buttons._headLineButtonsBackAdd', [
                        'headLine' => __($headLine),
                        'routeBack' => 'admin.cartaServicos.index',
                        'routeBackParams' => request()->only(['q', 'tipo_id', 'page']),
                        'nameBack' => __('Voltar'),
                    ])

            {{-- Google Fonts --}}
            <link rel="preconnect" href="https://fonts.googleapis.com">
            <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
            <link href="https://fonts.googleapis.com/css2?family=DM+Serif+Display:ital@0;1&family=DM+Sans:opsz,wght@9..40,300;9..40,400;9..40,500;9..40,600&display=swap" rel="stylesheet">

            <style>
                :root {
                    --c-bg:        #F7F5F2;
                    --c-surface:   #FFFFFF;
                    --c-border:    #E2DDD8;
                    --c-border-focus: #2C2C2C;
                    --c-text:      #1A1A1A;
                    --c-muted:     #6B6560;
                    --c-accent:    #C9622F;
                    --c-accent-lt: #F5EAE3;
                    --c-dark:      #1E1C1A;
                    --c-chip-bg:   #F0EDE9;
                    --radius-sm:   6px;
                    --radius-md:   10px;
                    --radius-lg:   14px;
                    --shadow-sm:   0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
                    --shadow-md:   0 4px 12px rgba(0,0,0,.08);
                    --font-display: 'DM Serif Display', Georgia, serif;
                    --font-body:    'DM Sans', system-ui, sans-serif;
                    --transition:   0.18s ease;
                }

                .cs-form-wrap {
                    font-family: var(--font-body);
                    color: var(--c-text);
                    background: var(--c-bg);
                    border-radius: var(--radius-lg);
                    padding: 2.5rem;
                    margin: 0 auto;
                    width: 100%;
                }

                /* ── Section headings ───────────────────────────────── */
                .cs-section {
                    margin-bottom: 2rem;
                }
                .cs-section-label {
                    font-family: var(--font-body);
                    font-size: 0.6875rem;
                    font-weight: 600;
                    letter-spacing: .12em;
                    text-transform: uppercase;
                    color: var(--c-accent);
                    margin-bottom: 1rem;
                    display: flex;
                    align-items: center;
                    gap: 0.5rem;
                }
                .cs-section-label::after {
                    content: '';
                    flex: 1;
                    height: 1px;
                    background: var(--c-border);
                }

                /* ── Field groups ───────────────────────────────────── */
                .cs-grid {
                    display: grid;
                    gap: 1.125rem;
                }
                .cs-grid-2 { grid-template-columns: 1fr 1fr; }

                @media (max-width: 640px) {
                    .cs-grid-2 { grid-template-columns: 1fr; }
                    .cs-form-wrap { padding: 1.25rem; }
                }

                /* ── Label ──────────────────────────────────────────── */
                .cs-label {
                    display: block;
                    font-size: 0.8125rem;
                    font-weight: 500;
                    color: var(--c-muted);
                    margin-bottom: 0.4rem;
                    letter-spacing: 0.01em;
                }

                /* ── Inputs ─────────────────────────────────────────── */
                .cs-input,
                .cs-select,
                .cs-textarea {
                    width: 100%;
                    background: var(--c-surface);
                    border: 1.5px solid var(--c-border);
                    border-radius: var(--radius-sm);
                    padding: 0.6rem 0.875rem;
                    font-family: var(--font-body);
                    font-size: 0.9375rem;
                    color: var(--c-text);
                    transition: border-color var(--transition), box-shadow var(--transition);
                    box-shadow: var(--shadow-sm);
                    outline: none;
                    appearance: none;
                    -webkit-appearance: none;
                    box-sizing: border-box;
                }
                .cs-input:focus,
                .cs-select:focus,
                .cs-textarea:focus {
                    border-color: var(--c-border-focus);
                    box-shadow: 0 0 0 3px rgba(44,44,44,.08);
                }
                .cs-input::placeholder,
                .cs-textarea::placeholder { color: #C4BFB9; }

                /* ── Select custom arrow ────────────────────────────── */
                .cs-select-wrap { position: relative; }
                .cs-select-wrap::after {
                    content: '';
                    pointer-events: none;
                    position: absolute;
                    right: 0.875rem;
                    top: 50%;
                    transform: translateY(-50%);
                    width: 0; height: 0;
                    border-left: 5px solid transparent;
                    border-right: 5px solid transparent;
                    border-top: 6px solid var(--c-muted);
                }
                .cs-select { padding-right: 2.25rem; cursor: pointer; }

                /* ── Field ──────────────────────────────────────────── */
                .cs-field { display: flex; flex-direction: column; }

                /* ── Category table ─────────────────────────────────── */
                .cs-table {
                    width: 100%;
                    border-collapse: collapse;
                    font-size: 0.875rem;
                }
                .cs-table th {
                    font-size: 0.6875rem;
                    font-weight: 600;
                    letter-spacing: .08em;
                    text-transform: uppercase;
                    color: var(--c-muted);
                    padding: 0.5rem 0.75rem;
                    border-bottom: 1px solid var(--c-border);
                    text-align: left;
                }
                .cs-table td {
                    padding: 0.75rem;
                    border-bottom: 1px solid var(--c-border);
                    vertical-align: middle;
                }
                .cs-table tr:last-child td { border-bottom: none; }
                .cs-table-wrap {
                    background: var(--c-surface);
                    border: 1.5px solid var(--c-border);
                    border-radius: var(--radius-md);
                    overflow: hidden;
                }

                /* ── Inline-edit form inside table ──────────────────── */
                .cs-input-sm {
                    width: 100%;
                    background: var(--c-surface);
                    border: 1.5px solid var(--c-border);
                    border-radius: var(--radius-sm);
                    padding: 0.4rem 0.65rem;
                    font-family: var(--font-body);
                    font-size: 0.875rem;
                    color: var(--c-text);
                    outline: none;
                    box-sizing: border-box;
                    transition: border-color var(--transition);
                }
                .cs-input-sm:focus { border-color: var(--c-border-focus); }
                .cs-select-sm {
                    width: 100%;
                    background: var(--c-surface);
                    border: 1.5px solid var(--c-border);
                    border-radius: var(--radius-sm);
                    padding: 0.4rem 0.65rem;
                    font-family: var(--font-body);
                    font-size: 0.875rem;
                    color: var(--c-text);
                    outline: none;
                    box-sizing: border-box;
                    appearance: none;
                    -webkit-appearance: none;
                    cursor: pointer;
                    transition: border-color var(--transition);
                }
                .cs-select-sm:focus { border-color: var(--c-border-focus); }

                /* ── Divider ────────────────────────────────────────── */
                .cs-divider {
                    border: none;
                    border-top: 1px solid var(--c-border);
                    margin: 2rem 0;
                }

                /* ── Footer actions ─────────────────────────────────── */
                .cs-footer {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    margin-top: 2.5rem;
                    padding-top: 1.5rem;
                    border-top: 1px solid var(--c-border);
                    gap: 1rem;
                }
                .cs-btn {
                    display: inline-flex;
                    align-items: center;
                    gap: 0.5rem;
                    padding: 0.65rem 1.35rem;
                    border-radius: var(--radius-sm);
                    font-family: var(--font-body);
                    font-size: 0.9rem;
                    font-weight: 500;
                    cursor: pointer;
                    border: none;
                    text-decoration: none;
                    transition: all var(--transition);
                    white-space: nowrap;
                }
                .cs-btn-ghost {
                    background: transparent;
                    color: var(--c-muted);
                    border: 1.5px solid var(--c-border);
                }
                .cs-btn-ghost:hover {
                    border-color: var(--c-text);
                    color: var(--c-text);
                    background: var(--c-surface);
                }
                .cs-btn-primary {
                    background: var(--c-dark);
                    color: #fff;
                    box-shadow: var(--shadow-sm);
                }
                .cs-btn-primary:hover {
                    background: var(--c-accent);
                    transform: translateY(-1px);
                    box-shadow: 0 6px 16px rgba(201,98,47,.25);
                }
                .cs-btn-warning {
                    background: #F0B429;
                    color: #fff;
                    box-shadow: var(--shadow-sm);
                    justify-content: center;
                }
                .cs-btn-warning:hover {
                    background: #d9a020;
                    transform: translateY(-1px);
                }
                .cs-btn-danger {
                    background: transparent;
                    color: #E53E3E;
                    border: 1.5px solid #E53E3E;
                    padding: 0.45rem 0.75rem;
                }
                .cs-btn-danger:hover {
                    background: #E53E3E;
                    color: #fff;
                }

                /* ── Empty state ────────────────────────────────────── */
                .cs-empty {
                    text-align: center;
                    color: var(--c-muted);
                    padding: 2rem 1rem;
                    font-size: 0.875rem;
                }

                /* ── Category thumbnail ─────────────────────────────── */
                .cs-thumb {
                    max-height: 52px;
                    border-radius: var(--radius-sm);
                    display: block;
                    margin: 0.4rem auto 0.4rem;
                }

                /* ── Categories grid (3 per row) ────────────────────── */
                .cs-categories-grid {
                    display: grid;
                    grid-template-columns: repeat(3, minmax(0, 1fr));
                    gap: 1rem;
                }
                .cs-category-card {
                    background: var(--c-surface);
                    border: 1.5px solid var(--c-border);
                    border-radius: var(--radius-md);
                    padding: 0.85rem;
                }
                .cs-category-actions {
                    margin-top: 0.75rem;
                    display: flex;
                    gap: 0.5rem;
                }
                .cs-category-actions .cs-btn-warning {
                    flex: 1;
                    margin-top: 0;
                }
                .cs-category-actions form {
                    margin: 0;
                }
                .cs-category-actions .cs-btn-danger {
                    height: 100%;
                    min-width: 44px;
                    display: inline-flex;
                    align-items: center;
                    justify-content: center;
                }
                @media (max-width: 1024px) {
                    .cs-categories-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
                }
                @media (max-width: 640px) {
                    .cs-categories-grid { grid-template-columns: 1fr; }
                }
            </style>

            <div class="cs-form-wrap">

                {{-- ── 1. Nova categoria ──────────────────────────────── --}}
                <div class="cs-section">
                    <div class="cs-section-label">{{ __('Nova categoria') }}</div>

                    <form method="post" action="{{ route('admin.cartaServicos.tipos.store') }}" enctype="multipart/form-data">
                        @csrf
                        <div class="cs-grid cs-grid-2" style="margin-bottom: 1.125rem;">
                            <div class="cs-field">
                                <label class="cs-label">{{ __('Descrição') }}</label>
                                <input type="text" name="tip_descricao" class="cs-input" required placeholder="{{ __('Nome da categoria…') }}">
                            </div>

                            <div class="cs-field">
                                <label class="cs-label">{{ __('Status') }}</label>
                                <div class="cs-select-wrap">
                                    <select class="cs-select" name="tip_status" required>
                                        <option value="A">{{ __('Ativo') }}</option>
                                        <option value="I">{{ __('Inativo') }}</option>
                                    </select>
                                </div>
                            </div>
                        </div>

                        <div class="cs-field" style="margin-bottom: 1.5rem;">
                            <label class="cs-label">{{ __('Foto da categoria') }}</label>
                            <input type="file" name="tip_foto" class="cs-input" accept=".jpg,.jpeg,.png,.webp">
                        </div>

                        <div>
                            <button class="cs-btn cs-btn-primary" type="submit">
                                {{ __('Salvar categoria') }} →
                            </button>
                        </div>
                    </form>
                </div>

                <hr class="cs-divider">

                {{-- ── 2. Categorias cadastradas ───────────────────────── --}}
                <div class="cs-section">
                    <div class="cs-section-label">{{ __('Categorias cadastradas') }}</div>

                    @forelse($tipos as $tipo)
                        @if($loop->first)
                            <div class="cs-categories-grid">
                        @endif

                        <div class="cs-category-card">
                            <form id="form-update-tipo-{{ $tipo->id_tipo }}" method="post" action="{{ route('admin.cartaServicos.tipos.update', $tipo->id_tipo) }}" enctype="multipart/form-data">
                                @csrf
                                @method('put')

                                <input type="text" name="tip_descricao" class="cs-input-sm" style="margin-bottom: 0.5rem;" value="{{ $tipo->tip_descricao }}" required>

                                @if(\Illuminate\Support\Str::startsWith((string) $tipo->tip_class, 'storage/'))
                                    <img src="{{ asset($tipo->tip_class) }}" alt="{{ $tipo->tip_descricao }}" class="cs-thumb">
                                @endif

                                <input type="file" name="tip_foto" class="cs-input-sm" style="margin-bottom: 0.5rem;" accept=".jpg,.jpeg,.png,.webp">

                                <div style="position: relative; margin-bottom: 0.5rem;">
                                    <select class="cs-select-sm" name="tip_status">
                                        <option value="A" @selected($tipo->tip_status === 'A')>{{ __('Ativo') }}</option>
                                        <option value="I" @selected($tipo->tip_status === 'I')>{{ __('Inativo') }}</option>
                                    </select>
                                </div>
                            </form>

                            <div class="cs-category-actions">
                                <button class="cs-btn cs-btn-warning" type="submit" form="form-update-tipo-{{ $tipo->id_tipo }}">
                                    {{ __('Salvar') }}
                                </button>
                                <form method="post" action="{{ route('admin.cartaServicos.tipos.destroy', $tipo->id_tipo) }}" onsubmit="return confirmDelete(this)">
                                    @csrf
                                    @method('delete')
                                    <input type="hidden" name="confirm" value="{{ __('* Esta categoria será excluída e esta ação não poderá ser desfeita, deseja continuar?') }}">
                                    <button type="submit" class="cs-btn cs-btn-danger">
                                        <i class="fa-solid fa-trash"></i>
                                    </button>
                                </form>
                            </div>
                        </div>

                        @if($loop->last)
                            </div>
                        @endif
                    @empty
                        <div class="cs-table-wrap">
                            <div class="cs-empty">{{ __('Nenhuma categoria cadastrada.') }}</div>
                        </div>
                    @endforelse
                </div>

                {{-- ── Footer ─────────────────────────────────────────── --}}
                <div class="cs-footer">
                    <a href="{{ route('admin.cartaServicos.index', request()->only(['q', 'tipo_id', 'page'])) }}" class="cs-btn cs-btn-ghost">
                        ← {{ __('Voltar') }}
                    </a>
                </div>

            </div>

        </div>
@endsection