@extends('layouts.admin')

@section('content')
<div class="card mb-3">
    <div class="card-header">
        <div class="row flex-between-end">
            <div class="col-auto align-self-center">
                <a href="{{ url()->previous() }}" class="btn btn-primary btn-sm">
                    <i class="fas fa-arrow-circle-left"></i> Back
                </a>
                <br><br>
                <h5 class="mb-0">
                    Claims for {{ $member->name }} {{ $member->surname }}
                </h5>
            </div>

            <!-- ✅ Create New Claim Button -->
            <div class="col-auto">
                <a href="{{ route('claims.create', $member->id) }}" class="btn btn-success btn-sm">
                    <i class="fas fa-plus-circle"></i> New Claim
                </a>
            </div>
        </div>
    </div>


    <div class="card-body">
        @if($claims->isEmpty())
        <p class="text-center">No claims found for this member.</p>
        @else
        <div class="table-responsive scrollbar">

            <table id="basic-btn" class="table table-bordered table-striped fs--1 mb-0">
                <thead class="bg-200 text-900">
                    <tr>
                        <th>#</th>
                        <th>Ref #</th>
                        <th>Deceased</th>
                        <th>Date of Death</th>
                        <th>Status</th>
                        <th>Created By</th>
                        <th>Last Action</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($claims as $claim)
                    <tr>
                        {{-- ROW NUMBER --}}
                        <td class="fw-bold text-center">
                            {{ $loop->iteration }}
                        </td>

                        <td class="fw-bold">{{ $claim->reference_number }}</td>

                        <td>{{ $claim->deceased_full_name }}</td>

                        <td>{{ \Carbon\Carbon::parse($claim->date_of_death)->format('d M Y') }}</td>


                        <td>
                            <span class="badge
                    {{ $claim->claim_status == 'Pending' ? 'bg-warning' : '' }}
                    {{ $claim->claim_status == 'Approved' ? 'bg-success' : '' }}
                    {{ $claim->claim_status == 'Rejected' ? 'bg-danger' : '' }}
                    {{ $claim->claim_status == 'Under Review' ? 'bg-info' : '' }}">
                                {{ ucfirst($claim->claim_status) }}
                            </span>
                        </td>

                        <td>
                            {{ $claim->createdBy->name ?? 'System' }}
                        </td>

                        <td>
                            @if($claim->action_at)
                            {{ \Carbon\Carbon::parse($claim->action_at)->format('d M Y H:i') }}
                            @else
                            <span class="text-muted">—</span>
                            @endif
                        </td>

                        <td>
                            {{-- VIEW --}}
                            <a href="{{ url('/claim', $claim->id) }}"
                                class="btn btn-primary btn-sm">
                                <i class="far fa-eye"></i>
                            </a>

                            {{-- EDIT (Pending only) --}}
                            <a href="{{ $claim->claim_status === 'Pending' ? url('/claim/edit', $claim->id) : '#' }}"
                                class="btn btn-warning btn-sm {{ $claim->claim_status !== 'Pending' ? 'disabled' : '' }}"
                                aria-disabled="{{ $claim->claim_status !== 'Pending' ? 'true' : 'false' }}">
                                <i class="far fa-edit"></i>
                            </a>

                            {{-- DELETE (Admin + Pending only) --}}
                            @if(auth()->user()->role == 3)
                            <a href="{{ $claim->claim_status === 'Pending' ? url('/claim/delete', $claim->id) : '#' }}"
                                class="btn btn-danger btn-sm {{ $claim->claim_status !== 'Pending' ? 'disabled' : '' }}"
                                aria-disabled="{{ $claim->claim_status !== 'Pending' ? 'true' : 'false' }}"
                                onclick="{{ $claim->claim_status === 'Pending'
                            ? 'return confirm(\'Delete this claim?\')'
                            : 'return false;' }}">
                                <i class="far fa-trash-alt"></i>
                            </a>
                            @endif
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        @endif
    </div>
</div>
@endsection