@extends('layouts.minimal')

@section('title', 'Dismissal Details')

@section('content')
<div class="container-fluid px-3 px-md-4 py-3">
    <div class="row">
        <div class="col-12">
            <!-- Compact Page Header with Breadcrumb -->
            <div class="d-flex justify-content-between align-items-center mb-4">
                <div>
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb mb-1 p-0 bg-transparent">
                            <li class="breadcrumb-item"><a href="{{ route('dashboard') }}" class="text-decoration-none">Dashboard</a></li>
                            <li class="breadcrumb-item"><a href="{{ route('salary.dismissals.index') }}" class="text-decoration-none">Dismissals</a></li>
                            <li class="breadcrumb-item active" aria-current="page">Dismissal Details</li>
                        </ol>
                    </nav>
                    <h3 class="mb-0 fw-bold text-gray-800">
                        <i class="fas fa-user-slash me-2 text-danger"></i>Dismissal Details
                    </h3>
                    <p class="text-muted mb-0">Review and manage dismissal request information</p>
                </div>
                <div>
                    <button type="button" class="btn btn-outline-info me-1" onclick="window.print()">
                        <i class="fas fa-print me-1"></i> Print
                    </button>
                    <a href="{{ route('salary.dismissals.index') }}" class="btn btn-outline-secondary">
                        <i class="fas fa-arrow-left me-1"></i> Back
                    </a>
                </div>
            </div>

            <!-- Enhanced Status Banner with Progress -->
            <div class="alert alert-{{ $dismissal->status === 'approved' ? 'success' : ($dismissal->status === 'rejected' ? 'danger' : ($dismissal->status === 'processing' ? 'info' : 'warning')) }} py-3 px-4 mb-4">
                <div class="d-flex align-items-center justify-content-between flex-wrap">
                    <div class="d-flex align-items-center">
                        <i class="fas {{ $dismissal->status === 'approved' ? 'fa-check-circle' : ($dismissal->status === 'rejected' ? 'fa-times-circle' : ($dismissal->status === 'processing' ? 'fa-spinner fa-pulse' : 'fa-clock')) }} fa-2x me-3"></i>
                        <div>
                            <strong class="fs-6">Status: {{ ucfirst($dismissal->status) }}</strong>
                            <span class="text-muted mx-2">•</span>
                            <span class="text-muted">Updated: {{ $dismissal->updated_at->diffForHumans() }}</span>
                            <span class="text-muted mx-2">•</span>
                            <span class="text-muted">ID: #{{ $dismissal->id }}</span>
                        </div>
                    </div>
                    <div>
                        @if($dismissal->status === 'pending')
                            <span class="badge bg-warning text-dark px-3 py-2 fs-6">
                                <i class="fas fa-clock me-1"></i> Awaiting Approval
                            </span>
                        @elseif($dismissal->status === 'approved')
                            <span class="badge bg-success px-3 py-2 fs-6">
                                <i class="fas fa-check me-1"></i> Ready for Processing
                            </span>
                        @elseif($dismissal->status === 'processing')
                            <span class="badge bg-info px-3 py-2 fs-6">
                                <i class="fas fa-cogs me-1"></i> In Progress
                            </span>
                        @elseif($dismissal->status === 'completed')
                            <span class="badge bg-secondary px-3 py-2 fs-6">
                                <i class="fas fa-check-double me-1"></i> Completed
                            </span>
                        @elseif($dismissal->status === 'rejected')
                            <span class="badge bg-danger px-3 py-2 fs-6">
                                <i class="fas fa-ban me-1"></i> Rejected
                            </span>
                        @endif
                    </div>
                </div>
                <!-- Dynamic Progress Bar for processing status -->
                @if($dismissal->status === 'processing')
                @php
                    $progress = 0;
                    if($dismissal->exit_interview_completed) $progress += 50;
                    if($dismissal->clearance_completed) $progress += 50;
                @endphp
                <div class="mt-3">
                    <div class="progress" style="height: 6px;">
                        <div class="progress-bar progress-bar-striped progress-bar-animated" 
                             role="progressbar" 
                             style="width: {{ $progress }}%;" 
                             aria-valuenow="{{ $progress }}" 
                             aria-valuemin="0" 
                             aria-valuemax="100"></div>
                    </div>
                    <small class="text-muted mt-1 d-block">
                        <i class="fas fa-comments me-1"></i>Exit Interview: {{ $dismissal->exit_interview_completed ? '✓' : '○' }} | 
                        <i class="fas fa-check-double me-1"></i>Clearance: {{ $dismissal->clearance_completed ? '✓' : '○' }}
                    </small>
                </div>
                @endif
            </div>

            <!-- Statistics Cards Row - LARGER FONTS -->
            <div class="row g-3 mb-4">
                <div class="col-md-3 col-6">
                    <div class="card border-0 shadow-sm rounded-3">
                        <div class="card-body p-3 text-center">
                            <div class="text-muted mb-1">
                                <i class="fas fa-calendar-day me-1" title="Days until or since effective date"></i>
                                <span>Days to Effective Date</span>
                            </div>
                            <h4 class="mb-0 fw-bold">
                                @if($dismissal->effective_date)
                                    @php
                                        $daysLeft = \Carbon\Carbon::now()->diffInDays(\Carbon\Carbon::parse($dismissal->effective_date), false);
                                    @endphp
                                    @if($daysLeft > 0)
                                        <span class="text-warning">{{ ceil($daysLeft) }} days</span>
                                    @elseif($daysLeft == 0)
                                        <span class="text-info">Today</span>
                                    @else
                                        <span class="text-secondary">{{ abs(ceil($daysLeft)) }} days ago</span>
                                    @endif
                                @else
                                    <span class="text-muted">—</span>
                                    <div class="text-muted small mt-1">Not set</div>
                                @endif
                            </h4>
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-6">
                    <div class="card border-0 shadow-sm rounded-3">
                        <div class="card-body p-3 text-center">
                            <div class="text-muted mb-1">
                                <i class="fas fa-minus-circle me-1" title="Total deductions from final pay"></i>
                                <span>Total Deductions</span>
                            </div>
                            <h4 class="mb-0 fw-bold text-danger">
                                KES {{ number_format($dismissal->deduction->amount ?? 0, 2) }}
                            </h4>
                            @if(($dismissal->deduction->amount ?? 0) > 0)
                                <small class="text-muted d-block mt-1">
                                    <i class="fas fa-info-circle me-1"></i>See breakdown below
                                </small>
                            @endif
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-6">
                    <div class="card border-0 shadow-sm rounded-3">
                        <div class="card-body p-3 text-center">
                            <div class="text-muted mb-1">
                                <i class="fas fa-money-bill-wave me-1" title="Final settlement amount payable"></i>
                                <span>Settlement Amount</span>
                            </div>
                            <h4 class="mb-0 fw-bold text-success">
                                KES {{ number_format($dismissal->settlement_amount ?? 0, 2) }}
                            </h4>
                            @if(($dismissal->settlement_amount ?? 0) == 0 && ($dismissal->deduction->amount ?? 0) > 0)
                                <small class="text-warning d-block mt-1">
                                    <i class="fas fa-exclamation-triangle"></i> Deductions without settlement
                                </small>
                            @endif
                            @if(($dismissal->settlement_amount ?? 0) == 0 && ($dismissal->deduction->amount ?? 0) == 0)
                                <small class="text-muted d-block mt-1">
                                    No amount payable
                                </small>
                            @endif
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-6">
                    <div class="card border-0 shadow-sm rounded-3">
                        <div class="card-body p-3 text-center">
                            <div class="text-muted mb-1">
                                <i class="fas fa-user-check me-1" title="Person who processed this dismissal"></i>
                                <span>Processed By</span>
                            </div>
                            <h5 class="mb-0 fw-bold">
                                @if($dismissal->processed_by && $dismissal->processed_at)
                                    <span title="Processed on {{ $dismissal->processed_at ? \Carbon\Carbon::parse($dismissal->processed_at)->format('M d, Y H:i') : 'Unknown date' }}">
                                        {{ $dismissal->processedBy->name ?? 'Unknown' }}
                                    </span>
                                @elseif($dismissal->status === 'completed')
                                    <span class="text-warning" title="Completed but processing data missing">
                                        <i class="fas fa-exclamation-triangle"></i> Missing data
                                    </span>
                                @else
                                    <span class="text-muted">—</span>
                                    <div class="text-muted small mt-1">Not processed</div>
                                @endif
                            </h5>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Main Two-Column Grid -->
            <div class="row g-4">
                <!-- Left Column -->
                <div class="col-md-6">
                    <!-- Employee Information Card -->
                    <div class="card border-0 shadow-sm rounded-3 mb-4">
                        <div class="card-header bg-white border-bottom px-4 py-3 d-flex justify-content-between align-items-center">
                            <h5 class="mb-0 fw-bold">
                                <i class="fas fa-user-circle text-primary me-2" title="Employee details"></i>Employee Information
                            </h5>
                            <span class="badge bg-{{ $dismissal->employee->status === 'active' ? 'success' : 'secondary' }} px-3 py-2 fs-6" title="Current employment status">
                                {{ ucfirst($dismissal->employee->status ?? 'Unknown') }}
                            </span>
                        </div>
                        <div class="card-body p-4">
                            <div class="row g-3">
                                <div class="col-6">
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-user me-1"></i>Full Name
                                        </label>
                                        <span class="fw-semibold fs-6">{{ $dismissal->employee->full_name ?? 'N/A' }}</span>
                                    </div>
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-building me-1"></i>Department
                                        </label>
                                        <span class="fs-6">{{ $dismissal->employee->department->name ?? 'N/A' }}</span>
                                    </div>
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-calendar-alt me-1"></i>Join Date
                                        </label>
                                        <span class="fs-6">{{ $dismissal->employee->join_date ? \Carbon\Carbon::parse($dismissal->employee->join_date)->format('M d, Y') : 'N/A' }}</span>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-id-card me-1"></i>Employee ID
                                        </label>
                                        <span class="fs-6">{{ $dismissal->employee_id }}</span>
                                    </div>
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-briefcase me-1"></i>Position
                                        </label>
                                        <span class="fs-6">{{ $dismissal->employee->position ?? 'N/A' }}</span>
                                    </div>
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-envelope me-1"></i>Email
                                        </label>
                                        <span class="fs-6">{{ $dismissal->employee->email ?? 'N/A' }}</span>
                                    </div>
                                </div>
                            </div>
                            <div class="text-center mt-3 pt-2 border-top">
                                <span class="text-muted">
                                    <i class="fas fa-phone me-1"></i> 
                                    {{ $dismissal->employee->phone ?? 'No phone number' }}
                                </span>
                            </div>
                        </div>
                    </div>

                    <!-- Dismissal Information Card -->
                    <div class="card border-0 shadow-sm rounded-3 mb-4">
                        <div class="card-header bg-white border-bottom px-4 py-3">
                            <h5 class="mb-0 fw-bold">
                                <i class="fas fa-gavel text-danger me-2" title="Dismissal details"></i>Dismissal Information
                            </h5>
                        </div>
                        <div class="card-body p-4">
                            <div class="row g-3">
                                <div class="col-6">
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-calendar-plus me-1"></i>Request Date
                                        </label>
                                        <span class="fs-6">{{ $dismissal->created_at->format('M d, Y H:i') }}</span>
                                    </div>
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-tag me-1"></i>Dismissal Type
                                        </label>
                                        <span class="badge bg-danger bg-opacity-10 text-danger px-3 py-2 fs-6">
                                            <i class="fas fa-{{ $dismissal->dismissal_type === 'gross_misconduct' ? 'exclamation-triangle' : ($dismissal->dismissal_type === 'performance' ? 'chart-line' : 'clock') }} me-1"></i>
                                            {{ ucfirst(str_replace('_', ' ', $dismissal->dismissal_type ?? 'Standard')) }}
                                        </span>
                                    </div>
                                </div>
                                <div class="col-6">
                                    @if($dismissal->effective_date)
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-calendar-check me-1"></i>Effective Date
                                        </label>
                                        <span class="fs-6 text-danger fw-semibold">
                                            <i class="fas fa-hourglass-half me-1"></i>
                                            {{ \Carbon\Carbon::parse($dismissal->effective_date)->format('M d, Y') }}
                                        </span>
                                    </div>
                                    @endif
                                    @if($dismissal->last_working_date)
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-calendar-times me-1"></i>Last Working Day
                                        </label>
                                        <span class="fs-6">{{ \Carbon\Carbon::parse($dismissal->last_working_date)->format('M d, Y') }}</span>
                                    </div>
                                    @endif
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-user-edit me-1"></i>Requested By
                                        </label>
                                        <span class="fs-6">{{ $dismissal->requestedBy->name ?? 'System' }}</span>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="mt-2">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-file-alt me-1"></i>Dismissal Reason
                                        </label>
                                        <div class="bg-light p-3 rounded fs-6" style="border-left: 4px solid #dc3545;">
                                            {{ $dismissal->reason ?? $dismissal->dismissal_reason ?? 'N/A' }}
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Right Column -->
                <div class="col-md-6">
                    <!-- Approval Information Card -->
                    @if($dismissal->approved_by)
                    <div class="card border-0 shadow-sm rounded-3 mb-4">
                        <div class="card-header bg-white border-bottom px-4 py-3">
                            <h5 class="mb-0 fw-bold">
                                <i class="fas fa-check-circle text-success me-2" title="Approval details"></i>Approval Information
                            </h5>
                        </div>
                        <div class="card-body p-4">
                            <div class="row g-3">
                                <div class="col-6">
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-user-check me-1"></i>Approved By
                                        </label>
                                        <span class="fs-6 fw-semibold">
                                            <i class="fas fa-user-circle me-1"></i>
                                            {{ $dismissal->approvedBy->name ?? 'N/A' }}
                                        </span>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-clock me-1"></i>Approved At
                                        </label>
                                        <span class="fs-6">{{ $dismissal->approved_at ? \Carbon\Carbon::parse($dismissal->approved_at)->format('M d, Y H:i') : 'N/A' }}</span>
                                    </div>
                                </div>
                                @if($dismissal->approval_comments)
                                <div class="col-12">
                                    <div class="mt-2">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-comment me-1"></i>Comments
                                        </label>
                                        <div class="bg-success bg-opacity-10 p-3 rounded fs-6">
                                            <i class="fas fa-quote-left me-1 text-success"></i>
                                            {{ $dismissal->approval_comments }}
                                        </div>
                                    </div>
                                </div>
                                @endif
                            </div>
                        </div>
                    </div>
                    @endif

                    <!-- Rejection Information Card -->
                    @if($dismissal->rejected_by)
                    <div class="card border-0 shadow-sm rounded-3 mb-4">
                        <div class="card-header bg-white border-bottom px-4 py-3">
                            <h5 class="mb-0 fw-bold">
                                <i class="fas fa-times-circle text-danger me-2" title="Rejection details"></i>Rejection Information
                            </h5>
                        </div>
                        <div class="card-body p-4">
                            <div class="row g-3">
                                <div class="col-6">
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-user-times me-1"></i>Rejected By
                                        </label>
                                        <span class="fs-6">{{ $dismissal->rejectedBy->name ?? 'N/A' }}</span>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-clock me-1"></i>Rejected At
                                        </label>
                                        <span class="fs-6">{{ $dismissal->rejected_at ? \Carbon\Carbon::parse($dismissal->rejected_at)->format('M d, Y H:i') : 'N/A' }}</span>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="mt-2">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-comment-slash me-1"></i>Rejection Reason
                                        </label>
                                        <div class="bg-danger bg-opacity-10 p-3 rounded fs-6">
                                            <i class="fas fa-quote-left me-1 text-danger"></i>
                                            {{ $dismissal->rejection_reason }}
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    @endif

                    <!-- Settlement Breakdown Card -->
                    @if(($dismissal->deduction->amount ?? 0) > 0 || ($dismissal->settlement_amount ?? 0) > 0 || ($dismissal->gross_pay ?? 0) > 0)
                    <div class="card border-0 shadow-sm rounded-3 mb-4">
                        <div class="card-header bg-white border-bottom px-4 py-3">
                            <h5 class="mb-0 fw-bold">
                                <i class="fas fa-receipt text-primary me-2" title="Financial breakdown"></i>Settlement Breakdown
                            </h5>
                        </div>
                        <div class="card-body p-4">
                            <div class="table-responsive">
                                <table class="table table-borderless mb-0">
                                    <thead class="text-muted">
                                        <tr>
                                            <th class="fs-6"><i class="fas fa-tag me-1"></i>Description</th>
                                            <th class="text-end fs-6"><i class="fas fa-coins me-1"></i>Amount (KES)</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="text-success">
                                            <td class="fs-6"><i class="fas fa-money-bill-wave me-2"></i> Gross Final Pay</td>
                                            <td class="text-end fs-6">{{ number_format($dismissal->gross_pay ?? 0, 2) }}</td>
                                        </tr>
                                        <tr class="text-danger">
                                            <td class="fs-6"><i class="fas fa-minus-circle me-2"></i> Deductions</td>
                                            <td class="text-end fs-6">- {{ number_format($dismissal->deduction->amount ?? 0, 2) }}</td>
                                        </tr>
                                        @if(($dismissal->other_adjustments ?? 0) != 0)
                                        <tr class="text-info">
                                            <td class="fs-6"><i class="fas fa-adjust me-2"></i> Other Adjustments</td>
                                            <td class="text-end fs-6">{{ number_format($dismissal->other_adjustments ?? 0, 2) }}</td>
                                        </tr>
                                        @endif
                                        <tr class="fw-bold border-top">
                                            <td class="fs-6 pt-2"><i class="fas fa-calculator me-2"></i> Net Settlement</td>
                                            <td class="text-end fs-6 pt-2">KES {{ number_format($dismissal->settlement_amount ?? 0, 2) }}</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                            @if(($dismissal->settlement_amount ?? 0) == 0 && ($dismissal->deduction->amount ?? 0) > 0)
                                <div class="alert alert-warning py-2 px-3 mt-3 mb-0 fs-6">
                                    <i class="fas fa-info-circle me-2"></i> 
                                    Settlement is zero because deductions equal gross pay.
                                </div>
                            @endif
                            @if(($dismissal->settlement_amount ?? 0) == 0 && ($dismissal->gross_pay ?? 0) == 0 && ($dismissal->deduction->amount ?? 0) == 0)
                                <div class="alert alert-secondary py-2 px-3 mt-3 mb-0 fs-6">
                                    <i class="fas fa-info-circle me-2"></i> 
                                    No settlement calculations available yet.
                                </div>
                            @endif
                        </div>
                    </div>
                    @endif

                    <!-- Processing Information Card -->
                    @if($dismissal->processed_by || $dismissal->status === 'completed')
                    <div class="card border-0 shadow-sm rounded-3 mb-4">
                        <div class="card-header bg-white border-bottom px-4 py-3">
                            <h5 class="mb-0 fw-bold">
                                <i class="fas fa-cogs text-info me-2" title="Processing details"></i>Processing Information
                            </h5>
                        </div>
                        <div class="card-body p-4">
                            <div class="row g-3">
                                <div class="col-6">
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-user-gear me-1"></i>Processed By
                                        </label>
                                        <span class="fs-6">
                                            @if($dismissal->processed_by && $dismissal->processed_at)
                                                {{ $dismissal->processedBy->name ?? 'N/A' }}
                                            @elseif($dismissal->status === 'completed')
                                                <span class="text-warning" title="Completed but processor information missing">
                                                    <i class="fas fa-exclamation-triangle me-1"></i>Not recorded
                                                </span>
                                            @else
                                                N/A
                                            @endif
                                        </span>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-clock me-1"></i>Processed At
                                        </label>
                                        <span class="fs-6">
                                            @if($dismissal->processed_at)
                                                {{ \Carbon\Carbon::parse($dismissal->processed_at)->format('M d, Y H:i') }}
                                            @elseif($dismissal->status === 'completed')
                                                <span class="text-warning">Not recorded</span>
                                            @else
                                                N/A
                                            @endif
                                        </span>
                                    </div>
                                </div>
                                @if($dismissal->processing_notes)
                                <div class="col-12">
                                    <div class="mt-2">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-sticky-note me-1"></i>Processing Notes
                                        </label>
                                        <div class="bg-info bg-opacity-10 p-3 rounded fs-6">
                                            <i class="fas fa-quote-left me-1 text-info"></i>
                                            {{ $dismissal->processing_notes }}
                                        </div>
                                    </div>
                                </div>
                                @endif
                            </div>
                        </div>
                    </div>
                    @endif

                    <!-- Exit Interview & Clearance Card -->
                    <div class="card border-0 shadow-sm rounded-3 mb-4">
                        <div class="card-header bg-white border-bottom px-4 py-3">
                            <h5 class="mb-0 fw-bold">
                                <i class="fas fa-clipboard-list text-secondary me-2" title="Exit formalities status"></i>Exit Formalities
                            </h5>
                        </div>
                        <div class="card-body p-4">
                            <div class="row g-3">
                                <div class="col-6">
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-comments me-1"></i>Exit Interview
                                        </label>
                                        <span class="badge bg-{{ $dismissal->exit_interview_completed ? 'success' : 'warning' }} bg-opacity-10 text-{{ $dismissal->exit_interview_completed ? 'success' : 'warning' }} px-3 py-2 fs-6">
                                            <i class="fas fa-{{ $dismissal->exit_interview_completed ? 'check-circle' : 'clock' }} me-1"></i>
                                            {{ $dismissal->exit_interview_completed ? 'Completed' : 'Pending' }}
                                        </span>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-check-double me-1"></i>Clearance
                                        </label>
                                        <span class="badge bg-{{ $dismissal->clearance_completed ? 'success' : 'warning' }} bg-opacity-10 text-{{ $dismissal->clearance_completed ? 'success' : 'warning' }} px-3 py-2 fs-6">
                                            <i class="fas fa-{{ $dismissal->clearance_completed ? 'check-circle' : 'clock' }} me-1"></i>
                                            {{ $dismissal->clearance_completed ? 'Completed' : 'Pending' }}
                                        </span>
                                    </div>
                                </div>
                                @if($dismissal->exit_interview_date)
                                <div class="col-12">
                                    <div class="mt-2">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-calendar-day me-1"></i>Exit Interview Date
                                        </label>
                                        <span class="fs-6">{{ \Carbon\Carbon::parse($dismissal->exit_interview_date)->format('M d, Y') }}</span>
                                    </div>
                                </div>
                                @endif
                            </div>
                        </div>
                    </div>

                    <!-- Deduction Information Card -->
                    @if($dismissal->deduction)
                    <div class="card border-0 shadow-sm rounded-3 mb-4">
                        <div class="card-header bg-white border-bottom px-4 py-3">
                            <h5 class="mb-0 fw-bold">
                                <i class="fas fa-coins text-warning me-2" title="Deduction details"></i>Deduction Information
                            </h5>
                        </div>
                        <div class="card-body p-4">
                            <div class="row g-3">
                                <div class="col-6">
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-money-bill-wave me-1"></i>Deduction Amount
                                        </label>
                                        <span class="fs-6 text-danger fw-semibold">
                                            <i class="fas fa-minus-circle me-1"></i>
                                            KES {{ number_format($dismissal->deduction->amount ?? 0, 2) }}
                                        </span>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="mb-3">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-calendar-alt me-1"></i>Deduction Date
                                        </label>
                                        <span class="fs-6">{{ isset($dismissal->deduction->deduction_date) ? \Carbon\Carbon::parse($dismissal->deduction->deduction_date)->format('M d, Y') : 'N/A' }}</span>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <div class="mt-2">
                                        <label class="text-muted d-block mb-1">
                                            <i class="fas fa-file-invoice me-1"></i>Deduction Reason
                                        </label>
                                        <div class="bg-warning bg-opacity-10 p-3 rounded fs-6">
                                            <i class="fas fa-quote-left me-1 text-warning"></i>
                                            {{ $dismissal->deduction->reason ?? 'N/A' }}
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    @endif
                </div>
            </div>

            <!-- Action Buttons Card -->
            <div class="card border-0 shadow-sm rounded-3 mt-3">
                <div class="card-body p-4 text-center">
                    @if($dismissal->status === 'pending')
                        <button type="button" class="btn btn-success px-4 me-2 approve-dismissal" data-id="{{ $dismissal->id }}">
                            <i class="fas fa-check me-1"></i> Approve Dismissal
                        </button>
                        <button type="button" class="btn btn-danger px-4 me-2 reject-dismissal" data-id="{{ $dismissal->id }}">
                            <i class="fas fa-times me-1"></i> Reject Dismissal
                        </button>
                        <button type="button" class="btn btn-info px-4" onclick="sendReminder({{ $dismissal->id }})">
                            <i class="fas fa-bell me-1"></i> Send Reminder
                        </button>
                    @elseif($dismissal->status === 'approved')
                        <button type="button" class="btn btn-primary px-4 me-2 process-dismissal" data-id="{{ $dismissal->id }}">
                            <i class="fas fa-cogs me-1"></i> Start Processing
                        </button>
                        <button type="button" class="btn btn-warning px-4" onclick="generateLetter({{ $dismissal->id }})">
                            <i class="fas fa-file-pdf me-1"></i> Generate Letter
                        </button>
                    @elseif($dismissal->status === 'processing')
                        <button type="button" class="btn btn-primary px-4 complete-dismissal" data-id="{{ $dismissal->id }}">
                            <i class="fas fa-check-double me-1"></i> Mark as Completed
                        </button>
                    @endif
                    
                    <a href="{{ route('salary.dismissals.index') }}" class="btn btn-secondary px-4">
                        <i class="fas fa-arrow-left me-1"></i> Back to List
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Include modals -->
@include('salary.dismissals.partials.modals')

<style>
.bg-opacity-10 {
    --bs-bg-opacity: 0.1;
}
.bg-opacity-10.bg-danger {
    background-color: rgba(220, 53, 69, 0.1) !important;
}
.bg-opacity-10.bg-success {
    background-color: rgba(40, 167, 69, 0.1) !important;
}
.bg-opacity-10.bg-info {
    background-color: rgba(23, 162, 184, 0.1) !important;
}
.bg-opacity-10.bg-warning {
    background-color: rgba(255, 193, 7, 0.1) !important;
}
.card {
    transition: box-shadow 0.2s, transform 0.1s;
}
.card:hover {
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.1) !important;
}
/* Larger font sizes for better readability */
label, .text-muted {
    font-size: 0.85rem;
    font-weight: 500;
}
.fs-6 {
    font-size: 1rem !important;
}
h3 {
    font-size: 1.75rem;
}
h4 {
    font-size: 1.5rem;
}
h5 {
    font-size: 1.25rem;
}
.badge {
    font-size: 0.85rem;
}
.breadcrumb {
    font-size: 0.9rem;
}
.btn {
    font-size: 0.9rem;
    padding: 0.5rem 1rem;
}
.alert {
    font-size: 0.95rem;
}
.table td, .table th {
    padding: 0.75rem;
}
.progress {
    background-color: rgba(0, 0, 0, 0.05);
}
@media print {
    .btn, .breadcrumb, .card-tools, .action-buttons {
        display: none !important;
    }
    .card {
        box-shadow: none !important;
        border: 1px solid #ddd !important;
    }
}
</style>
@endsection

@push('scripts')
<script>
$(document).ready(function() {
    // Initialize tooltips
    $('[title]').tooltip({
        trigger: 'hover',
        placement: 'top'
    });
    
    let currentDismissalId = null;
    
    // Approve Dismissal
    $('.approve-dismissal').click(function() {
        currentDismissalId = $(this).data('id');
        $('#approveModal').modal('show');
    });
    
    // Reject Dismissal
    $('.reject-dismissal').click(function() {
        currentDismissalId = $(this).data('id');
        $('#rejectModal').modal('show');
    });
    
    // Process Dismissal
    $('.process-dismissal').click(function() {
        currentDismissalId = $(this).data('id');
        $('#processModal').modal('show');
    });
    
    // Complete Dismissal
    $('.complete-dismissal').click(function() {
        currentDismissalId = $(this).data('id');
        $('#completeModal').modal('show');
    });
    
    // AJAX handlers
    $('#approveForm').submit(function(e) {
        e.preventDefault();
        $.ajax({
            url: `/salary/dismissals/${currentDismissalId}/approve`,
            method: 'POST',
            data: { 
                comments: $('#approve_comments').val(), 
                _token: '{{ csrf_token() }}' 
            },
            beforeSend: function() {
                $('#approveModal .btn-primary').prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> Processing...');
            },
            success: function(response) {
                if (response.success) {
                    showToast('success', response.message);
                    setTimeout(() => location.reload(), 1500);
                } else {
                    showToast('error', response.message || 'Failed to approve dismissal');
                }
                $('#approveModal').modal('hide');
            },
            error: function(xhr) {
                showToast('error', xhr.responseJSON?.message || 'An error occurred');
                $('#approveModal').modal('hide');
            },
            complete: function() {
                $('#approveModal .btn-primary').prop('disabled', false).html('Approve');
            }
        });
    });
    
    $('#rejectForm').submit(function(e) {
        e.preventDefault();
        $.ajax({
            url: `/salary/dismissals/${currentDismissalId}/reject`,
            method: 'POST',
            data: { 
                reason: $('#reject_reason').val(), 
                _token: '{{ csrf_token() }}' 
            },
            beforeSend: function() {
                $('#rejectModal .btn-danger').prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> Processing...');
            },
            success: function(response) {
                if (response.success) {
                    showToast('success', response.message);
                    setTimeout(() => location.reload(), 1500);
                } else {
                    showToast('error', response.message || 'Failed to reject dismissal');
                }
                $('#rejectModal').modal('hide');
            },
            error: function(xhr) {
                showToast('error', xhr.responseJSON?.message || 'An error occurred');
                $('#rejectModal').modal('hide');
            },
            complete: function() {
                $('#rejectModal .btn-danger').prop('disabled', false).html('Reject');
            }
        });
    });
    
    $('#processForm').submit(function(e) {
        e.preventDefault();
        $.ajax({
            url: `/salary/dismissals/${currentDismissalId}/process`,
            method: 'POST',
            data: { 
                effective_date: $('#process_effective_date').val(),
                notes: $('#process_notes').val(),
                settlement_amount: $('#process_settlement_amount').val(),
                _token: '{{ csrf_token() }}' 
            },
            beforeSend: function() {
                $('#processModal .btn-primary').prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> Processing...');
            },
            success: function(response) {
                if (response.success) {
                    showToast('success', response.message);
                    setTimeout(() => location.reload(), 1500);
                } else {
                    showToast('error', response.message || 'Failed to process dismissal');
                }
                $('#processModal').modal('hide');
            },
            error: function(xhr) {
                showToast('error', xhr.responseJSON?.message || 'An error occurred');
                $('#processModal').modal('hide');
            },
            complete: function() {
                $('#processModal .btn-primary').prop('disabled', false).html('Start Processing');
            }
        });
    });
    
    function showToast(type, message) {
        const bgColor = type === 'success' ? '#28a745' : '#dc3545';
        const icon = type === 'success' ? 'fa-check-circle' : 'fa-exclamation-circle';
        
        const toastHtml = `
            <div class="toast align-items-center text-white border-0 position-fixed" 
                 style="top: 20px; right: 20px; z-index: 10000; background: ${bgColor}; border-radius: 0.5rem;" 
                 role="alert" data-bs-autohide="true" data-bs-delay="5000">
                <div class="d-flex">
                    <div class="toast-body py-2">
                        <i class="fas ${icon} me-2"></i> ${message}
                    </div>
                    <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
                </div>
            </div>
        `;
        
        $('body').append(toastHtml);
        const toast = $('.toast').last();
        const bsToast = new bootstrap.Toast(toast, { autohide: true, delay: 5000 });
        bsToast.show();
        
        toast.on('hidden.bs.toast', function() { $(this).remove(); });
    }
});

// Additional utility functions
function sendReminder(dismissalId) {
    $.ajax({
        url: `/salary/dismissals/${dismissalId}/reminder`,
        method: 'POST',
        data: { _token: '{{ csrf_token() }}' },
        success: function(response) {
            if (response.success) {
                showToast('success', 'Reminder sent successfully');
            } else {
                showToast('error', response.message || 'Failed to send reminder');
            }
        },
        error: function() {
            showToast('error', 'Failed to send reminder');
        }
    });
}

function generateLetter(dismissalId) {
    window.open(`/salary/dismissals/${dismissalId}/letter`, '_blank');
}
</script>
@endpush