@extends('layouts.app')
@section('title','Discipline Management')
@section('page-title','Discipline Management')

@section('content')
@if(!auth()->user()->canAccess('discipline'))
    <div class="alert alert-danger"><i class="bi bi-lock-fill me-2"></i>You do not have permission to access Discipline Management.</div>
@else
<div class="page-header">
    <div class="d-flex align-items-start justify-content-between flex-wrap gap-2">
        <div>
            <h1>Discipline Management</h1>
            <nav aria-label="breadcrumb"><ol class="breadcrumb">
                <li class="breadcrumb-item active">
                    {{ auth()->user()->isDeptHead() ? 'Cases in your department' : 'All cases institution-wide' }}
                </li>
            </ol></nav>
        </div>
        @if(auth()->user()->isHrOrAdmin() || auth()->user()->isPresident())
        <a href="{{ route('discipline.create') }}" class="btn btn-primary">
            <i class="bi bi-plus-lg me-1"></i>New Case
        </a>
        @endif
    </div>
</div>

<div class="row g-3 mb-4">
    <div class="col-6 col-md-3">
        <div class="card p-3 text-center" style="border-left:4px solid #dc2626;">
            <div class="fw-black" style="font-size:1.8rem;color:#dc2626;">{{ $counts['open'] ?? 0 }}</div>
            <div style="font-size:.72rem;color:var(--text-2);text-transform:uppercase;letter-spacing:.04em;">Open</div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card p-3 text-center" style="border-left:4px solid #f59e0b;">
            <div class="fw-black" style="font-size:1.8rem;color:#f59e0b;">{{ $counts['in_progress'] ?? 0 }}</div>
            <div style="font-size:.72rem;color:var(--text-2);text-transform:uppercase;letter-spacing:.04em;">In Progress</div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card p-3 text-center" style="border-left:4px solid var(--text-2);">
            <div class="fw-black" style="font-size:1.8rem;color:var(--text-2);">{{ $counts['resolved'] ?? 0 }}</div>
            <div style="font-size:.72rem;color:var(--text-2);text-transform:uppercase;letter-spacing:.04em;">Resolved</div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card p-3 text-center" style="border-left:4px solid #059669;">
            <div class="fw-black" style="font-size:1.8rem;color:#059669;">{{ $counts['closed'] ?? 0 }}</div>
            <div style="font-size:.72rem;color:var(--text-2);text-transform:uppercase;letter-spacing:.04em;">Closed</div>
        </div>
    </div>
</div>

<div class="card mb-3">
    <div class="card-body py-3">
        <form method="GET" class="row g-2 align-items-end">
            <div class="col-12 col-sm-4 col-md-3">
                <input type="search" name="search" class="form-control" placeholder="Search case # or employee…" value="{{ request('search') }}">
            </div>
            <div class="col-6 col-md-2">
                <select name="status" class="form-select">
                    <option value="">All Status</option>
                    <option value="open" {{ request('status')=='open'?'selected':'' }}>Open</option>
                    <option value="in_progress" {{ request('status')=='in_progress'?'selected':'' }}>In Progress</option>
                    <option value="resolved" {{ request('status')=='resolved'?'selected':'' }}>Resolved</option>
                    <option value="closed" {{ request('status')=='closed'?'selected':'' }}>Closed</option>
                </select>
            </div>
            <div class="col-6 col-md-2">
                <select name="infraction_type" class="form-select">
                    <option value="">All Types</option>
                    <option>Attendance</option>
                    <option>Insubordination</option>
                    <option>Misconduct</option>
                    <option>Policy Violation</option>
                    <option>Harassment</option>
                </select>
            </div>
            <div class="col-12 col-md-2 d-flex gap-2">
                <button type="submit" class="btn btn-primary flex-fill"><i class="bi bi-search me-1"></i>Filter</button>
                <a href="{{ route('discipline.index') }}" class="btn btn-outline-secondary"><i class="bi bi-x-lg"></i></a>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <span>{{ $records->total() }} Cases
            @if(auth()->user()->isDeptHead())
            <span class="badge bg-secondary ms-1">Your Dept. Only</span>
            @endif
        </span>
        @if(auth()->user()->isHrOrAdmin())
        <button class="btn btn-sm btn-outline-success"><i class="bi bi-file-earmark-excel me-1"></i>Export</button>
        @endif
    </div>
    <div class="table-responsive">
        <table class="table mb-0">
            <thead><tr>
                <th>Case #</th><th>Employee</th><th>Infraction</th>
                <th class="d-none d-md-table-cell">Incident Date</th>
                <th class="d-none d-lg-table-cell">Action Taken</th>
                <th>Status</th>
                <th class="text-end">Actions</th>
            </tr></thead>
            <tbody>
            @forelse($records as $rec)
            @php
                $sevStyle = match($rec->infraction_type ?? '') {
                    'Harassment', 'Gross Negligence' => 'background:#fee2e2;color:#991b1b',
                    'Policy Violation', 'Insubordination' => 'background:#fef3c7;color:#92400e',
                    default => 'background:#f1f5f9;color:var(--text-1)',
                };
            @endphp
            <tr>
                <td class="fw-semibold" style="color:var(--ul-primary);font-size:.82rem;">{{ $rec->case_number }}</td>
                <td>
                    <div class="d-flex align-items-center gap-2">
                        <div class="avatar avatar-sm" style="background:linear-gradient(135deg,#dc2626,#7f1d1d);color:#fff;flex-shrink:0;">
                            {{ strtoupper(substr($rec->employee->first_name??'?',0,1)) }}
                        </div>
                        <div class="d-none d-sm-block">
                            <div class="fw-semibold" style="font-size:.83rem;">{{ $rec->employee->full_name ?? '—' }}</div>
                            <div style="font-size:.71rem;color:var(--text-2);">{{ $rec->employee->department->name ?? '' }}</div>
                        </div>
                    </div>
                </td>
                <td>
                    <span style="display:inline-flex;align-items:center;gap:.3rem;font-size:.73rem;font-weight:700;padding:.28em .65em;border-radius:6px;{{ $sevStyle }}">
                        <i class="bi bi-exclamation-triangle"></i>{{ $rec->infraction_type }}
                    </span>
                </td>
                <td class="d-none d-md-table-cell" style="font-size:.79rem;color:var(--text-2);">{{ $rec->incident_date?->format('M d, Y') }}</td>
                <td class="d-none d-lg-table-cell" style="font-size:.79rem;">
                    @if($rec->action_taken)
                    <span class="badge bg-{{ $rec->action_taken === 'termination' ? 'danger' : ($rec->action_taken === 'suspension' ? 'warning' : 'secondary') }}">
                        {{ ucfirst(str_replace('_',' ',$rec->action_taken)) }}
                    </span>
                    @else <span style="color:var(--text-3);">Pending</span>
                    @endif
                </td>
                <td>
                    <span class="badge bg-{{ $rec->status==='open'?'danger':($rec->status==='resolved'?'secondary':'success') }}">
                        {{ ucfirst($rec->status) }}
                    </span>
                </td>
                <td class="text-end">
                    <div class="d-flex gap-1 justify-content-end">
                        <a href="{{ route('discipline.show',$rec) }}" class="btn btn-sm btn-icon btn-outline-primary"><i class="bi bi-eye"></i></a>
                        @if(auth()->user()->isHrOrAdmin() && $rec->status === 'open')
                        <a href="{{ route('discipline.edit',$rec) }}" class="btn btn-sm btn-icon btn-outline-secondary"><i class="bi bi-pencil"></i></a>
                        @endif
                    </div>
                </td>
            </tr>
            @empty
            <tr><td colspan="7" class="text-center py-4" style="color:var(--text-2);">No discipline records found</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>
    @if($records->hasPages())
    <div class="card-body pt-0">{{ $records->links('pagination::bootstrap-5') }}</div>
    @endif
</div>
@endif
@endsection
