@extends('layouts.app')

@section('title', 'My HR records')
@section('heading', 'My HR records')

@section('content')
<div
    class="space-y-5"
    x-data="{
        tab: @js($errors->hasAny(['subject', 'complaint_date', 'against_user_id', 'description']) ? 'complaints' : null),
        showRaiseForm: @js($errors->hasAny(['subject', 'complaint_date', 'against_user_id', 'description'])),
        select(key) {
            this.tab = key;
            if (key !== 'complaints') {
                this.showRaiseForm = false;
            }
            this.$nextTick(() => this.$refs.detail?.scrollIntoView({ behavior: 'smooth', block: 'start' }));
        },
        init() {
            const allowed = ['warnings','appreciations','complaints','complaints-against','promotions','terminations'];
            const hash = (window.location.hash || '').replace('#','');
            if (allowed.includes(hash)) {
                this.tab = hash;
            }
            this.$watch('tab', (value) => {
                const nextUrl = value
                    ? '#' + value
                    : window.location.pathname + window.location.search;
                if (history.replaceState) {
                    history.replaceState(null, '', nextUrl);
                } else if (value) {
                    window.location.hash = value;
                }
            });
            window.addEventListener('hashchange', () => {
                const next = (window.location.hash || '').replace('#','');
                this.tab = allowed.includes(next) ? next : null;
            });
        }
    }"
>
    <p class="text-sm text-slate-500">Click a count to open that section below.</p>

    <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-3">
        <button type="button" @click="select('warnings')" class="block w-full rounded-2xl text-left focus:outline-none">
            <x-kpi-card x-bind:class="tab === 'warnings' ? '!border-amber-500' : ''" title="Warnings" :value="$kpis['warnings'] ?? 0" tone="{{ ($kpis['warnings'] ?? 0) > 0 ? 'warning' : 'positive' }}" accent="amber" subtitle="On your file" />
        </button>
        <button type="button" @click="select('appreciations')" class="block w-full rounded-2xl text-left focus:outline-none">
            <x-kpi-card x-bind:class="tab === 'appreciations' ? '!border-emerald-500' : ''" title="Appreciations" :value="$kpis['appreciations'] ?? 0" accent="emerald" subtitle="Recognition received" />
        </button>
        <button type="button" @click="select('complaints-against')" class="block w-full rounded-2xl text-left focus:outline-none">
            <x-kpi-card x-bind:class="tab === 'complaints-against' ? '!border-rose-500' : ''" title="Complaints against me" :value="$kpis['complaints_against_me'] ?? 0" tone="{{ ($kpis['complaints_against_me'] ?? 0) > 0 ? 'critical' : 'positive' }}" accent="rose" subtitle="Count only — details hidden" />
        </button>
        <button type="button" @click="select('complaints')" class="block w-full rounded-2xl text-left focus:outline-none">
            <x-kpi-card x-bind:class="tab === 'complaints' ? '!border-slate-500' : ''" title="Complaints I raised" :value="$kpis['complaints_raised'] ?? 0" accent="slate" subtitle="Submitted by you" />
        </button>
        <button type="button" @click="select('promotions')" class="block w-full rounded-2xl text-left focus:outline-none">
            <x-kpi-card x-bind:class="tab === 'promotions' ? '!border-violet-500' : ''" title="Promotions" :value="$kpis['promotions'] ?? 0" accent="violet" subtitle="On your file" />
        </button>
        <button type="button" @click="select('terminations')" class="block w-full rounded-2xl text-left focus:outline-none">
            <x-kpi-card x-bind:class="tab === 'terminations' ? '!border-orange-500' : ''" title="Terminations" :value="$kpis['terminations'] ?? 0" accent="orange" subtitle="On your file" />
        </button>
    </div>

    <div x-show="tab" x-cloak x-ref="detail" class="space-y-4 scroll-mt-24">
            <div x-show="tab === 'warnings'" x-cloak>
                <x-card>
                    <x-slot name="header">
                        <h2 class="text-sm font-semibold text-slate-800">Warnings</h2>
                    </x-slot>
                    <x-table>
                        <x-slot name="head">
                            <tr>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Subject</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Description</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Date</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Severity</th>
                            </tr>
                        </x-slot>
                        @forelse($warnings as $w)
                            <tr>
                                <td class="px-4 py-3 text-sm font-medium text-slate-900">{{ $w->subject }}</td>
                                <td class="px-4 py-3 text-sm text-slate-600">{{ \Illuminate\Support\Str::limit((string) ($w->description ?? '—'), 120) }}</td>
                                <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $w->warning_date?->format('M d, Y') }}</td>
                                <td class="whitespace-nowrap px-4 py-3 text-sm">
                                    @php
                                        $sevVariant = match ($w->severity) {
                                            'high' => 'danger',
                                            'low' => 'default',
                                            default => 'warning',
                                        };
                                    @endphp
                                    <x-badge :variant="$sevVariant">{{ ucfirst((string) $w->severity) }}</x-badge>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="px-4 py-8 text-center text-sm text-slate-500">No warnings on your file.</td></tr>
                        @endforelse
                    </x-table>
                </x-card>
            </div>

            <div x-show="tab === 'appreciations'" x-cloak>
                <x-card>
                    <x-slot name="header">
                        <h2 class="text-sm font-semibold text-slate-800">Appreciations</h2>
                    </x-slot>
                    <x-table>
                        <x-slot name="head">
                            <tr>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Title</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">From</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Date</th>
                            </tr>
                        </x-slot>
                        @forelse($appreciations as $a)
                            <tr>
                                <td class="px-4 py-3 text-sm text-slate-900">
                                    <div class="font-medium">{{ $a->title }}</div>
                                    @if($a->description)
                                        <div class="mt-1 text-xs text-slate-500">{{ \Illuminate\Support\Str::limit((string) $a->description, 120) }}</div>
                                    @endif
                                </td>
                                <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $a->givenByUser?->name ?? '—' }}</td>
                                <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $a->appreciation_date?->format('M d, Y') }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="3" class="px-4 py-8 text-center text-sm text-slate-500">No appreciations yet.</td></tr>
                        @endforelse
                    </x-table>
                </x-card>
            </div>

            <div x-show="tab === 'complaints-against'" x-cloak>
                <x-card>
                    <x-slot name="header">
                        <h2 class="text-sm font-semibold text-slate-800">Complaints against me</h2>
                    </x-slot>
                    <div class="space-y-3">
                        <p class="text-3xl font-semibold text-slate-900">{{ $kpis['complaints_against_me'] ?? 0 }}</p>
                        <p class="text-sm text-slate-600">
                            This is a count only. Complaint subject and description stay private to Admin, HR, and Managers.
                        </p>
                        @if(($kpis['complaints_against_me'] ?? 0) === 0)
                            <p class="text-sm text-slate-500">No complaints currently list you as the subject.</p>
                        @endif
                    </div>
                </x-card>
            </div>

            <div x-show="tab === 'complaints'" x-cloak>
                <x-card>
                    <x-slot name="header">
                        <div class="flex flex-wrap items-start justify-between gap-3">
                            <div>
                                <h2 class="text-sm font-semibold text-slate-800">Complaints I raised</h2>
                                <p class="mt-1 text-xs text-slate-500">
                                    Track complaints you submitted. Use the “Complaints against me” card for your count only.
                                </p>
                            </div>
                            @if($canRaiseComplaint)
                                <button
                                    type="button"
                                    class="inline-flex shrink-0 items-center rounded-lg bg-slate-800 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700"
                                    x-show="!showRaiseForm"
                                    @click="showRaiseForm = true"
                                >
                                    <i class="bi bi-plus-lg me-1"></i>Raise complaint
                                </button>
                            @endif
                        </div>
                    </x-slot>
                    <x-table>
                        <x-slot name="head">
                            <tr>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Against</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Subject</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Date</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Status</th>
                            </tr>
                        </x-slot>
                        @forelse($raisedComplaints as $c)
                            <tr>
                                <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $c->againstUser?->name ?? '—' }}</td>
                                <td class="px-4 py-3 text-sm text-slate-600">{{ $c->subject }}</td>
                                <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $c->complaint_date?->format('M d, Y') }}</td>
                                <td class="whitespace-nowrap px-4 py-3 text-sm">
                                    <x-badge :variant="$c->statusBadgeVariant()">{{ $c->statusLabel() }}</x-badge>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="px-4 py-8 text-center text-sm text-slate-500">You have not raised any complaints.</td></tr>
                        @endforelse
                    </x-table>
                </x-card>

                @if($canRaiseComplaint)
                <div x-show="showRaiseForm" x-cloak class="mt-4">
                    <x-card>
                        <x-slot name="header">
                            <div class="flex flex-wrap items-center justify-between gap-2">
                                <h2 class="text-sm font-semibold text-slate-800">Raise a complaint</h2>
                                <button
                                    type="button"
                                    class="rounded-lg bg-slate-100 px-3 py-1.5 text-sm font-medium text-slate-700 hover:bg-slate-200"
                                    @click="showRaiseForm = false"
                                >Cancel</button>
                            </div>
                        </x-slot>
                        <form action="{{ route('complaints.store') }}" method="POST" class="space-y-6" novalidate data-complaint-form="create">
                            @csrf
                            <div class="grid gap-4 sm:grid-cols-3">
                                <div>
                                    <label class="block text-sm font-medium text-slate-700">Subject <span class="text-red-500">*</span></label>
                                    <input type="text" name="subject" value="{{ old('subject') }}" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2" required>
                                    @error('subject')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                                    <p data-js-error="subject" class="mt-1 text-sm text-red-600 hidden"></p>
                                </div>
                                <div>
                                    <label class="block text-sm font-medium text-slate-700">Complaint Date</label>
                                    <input type="date" name="complaint_date" value="{{ old('complaint_date', date('Y-m-d')) }}" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2" required>
                                    @error('complaint_date')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                                </div>
                                <div>
                                    <label class="block text-sm font-medium text-slate-700">Against (optional)</label>
                                    <select name="against_user_id" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 js-select2">
                                        <option value="">—</option>
                                        @foreach($complaintUsers as $u)
                                            @if($u->id === auth()->id()) @continue @endif
                                            <option value="{{ $u->id }}" {{ old('against_user_id') == $u->id ? 'selected' : '' }}>{{ $u->name }}</option>
                                        @endforeach
                                    </select>
                                    @error('against_user_id')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                                </div>
                                <div class="sm:col-span-3">
                                    <label class="block text-sm font-medium text-slate-700">Description</label>
                                    <textarea name="description" rows="3" class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2">{{ old('description') }}</textarea>
                                    @error('description')<p class="mt-1 text-sm text-red-600">{{ $message }}</p>@enderror
                                </div>
                            </div>
                            <div class="flex gap-2">
                                <x-button type="submit" variant="primary">Submit</x-button>
                            </div>
                        </form>
                    </x-card>
                </div>
                @endif
            </div>

            <div x-show="tab === 'promotions'" x-cloak>
                <x-card>
                    <x-slot name="header">
                        <h2 class="text-sm font-semibold text-slate-800">Promotions</h2>
                    </x-slot>
                    <x-table>
                        <x-slot name="head">
                            <tr>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">From → To</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Department</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Date</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Notes</th>
                            </tr>
                        </x-slot>
                        @forelse($promotions as $p)
                            <tr>
                                <td class="px-4 py-3 text-sm text-slate-600">{{ $p->fromDesignation?->name ?? '—' }} → {{ $p->toDesignation?->name ?? '—' }}</td>
                                <td class="px-4 py-3 text-sm text-slate-600">{{ $p->fromDepartment?->name ?? '—' }} → {{ $p->toDepartment?->name ?? '—' }}</td>
                                <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $p->promotion_date?->format('M d, Y') }}</td>
                                <td class="px-4 py-3 text-sm text-slate-600">{{ \Illuminate\Support\Str::limit((string) ($p->notes ?? '—'), 80) }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="px-4 py-8 text-center text-sm text-slate-500">No promotions on your file.</td></tr>
                        @endforelse
                    </x-table>
                </x-card>
            </div>

            <div x-show="tab === 'terminations'" x-cloak>
                <x-card>
                    <x-slot name="header">
                        <h2 class="text-sm font-semibold text-slate-800">Terminations</h2>
                    </x-slot>
                    <x-table>
                        <x-slot name="head">
                            <tr>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Date</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Reason type</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Reason</th>
                                <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Status</th>
                            </tr>
                        </x-slot>
                        @forelse($terminations as $t)
                            <tr>
                                <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $t->termination_date?->format('M d, Y') }}</td>
                                <td class="px-4 py-3 text-sm text-slate-600">{{ $t->reason_type ?? '—' }}</td>
                                <td class="px-4 py-3 text-sm text-slate-600">{{ \Illuminate\Support\Str::limit((string) ($t->reason ?? '—'), 100) }}</td>
                                <td class="whitespace-nowrap px-4 py-3 text-sm"><x-badge :status="$t->status">{{ $t->status }}</x-badge></td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="px-4 py-8 text-center text-sm text-slate-500">No termination records on your file.</td></tr>
                        @endforelse
                    </x-table>
                </x-card>
            </div>
    </div>
</div>
@endsection

@push('scripts')
<script src="{{ asset('js/form-validation.js') }}"></script>
<script src="{{ asset('js/form-text-rules.js') }}"></script>
<script src="{{ asset('js/subject-form-validation.js') }}"></script>
@endpush
