@extends('layouts.admin')

@section('content')
<div class="card shadow-sm mb-3">

    {{-- ================= HEADER ================= --}}
    <div class="card-header bg-white">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <a href="{{ route('claims.index') }}" class="btn btn-sm btn-outline-primary">
                    <i class="fas fa-arrow-left"></i> Back
                </a>
                <h6 class="mt-2 mb-0 text-muted">Claim Details</h6>
                <small class="text-muted">
                    <strong>Reference:</strong>
                    {{ $claim->reference_number ?? 'Pending Assignment' }}
                </small>
            </div>

            <div>
                @if($claim->claim_status === 'Pending')
                <a href="{{ route('claims.edit', $claim->id) }}"
                    class="btn btn-sm btn-warning">
                    <i class="fas fa-edit"></i> Edit
                </a>
                @endif


                <a href="{{ route('claims.print', $claim->id) }}" target="_blank"
                    class="btn btn-sm btn-secondary">
                    <i class="fas fa-print"></i> Print
                </a>
            </div>
        </div>
    </div>

    <div class="card-body small text-muted">

        {{-- ================= STATUS ================= --}}
        <p>
            <strong>Status:</strong>
            <span class="badge
                {{ $claim->claim_status === 'Pending' ? 'bg-warning' : '' }}
                {{ $claim->claim_status === 'Approved' ? 'bg-success' : '' }}
                {{ $claim->claim_status === 'Rejected' ? 'bg-danger' : '' }}">
                {{ $claim->claim_status }}
            </span>
        </p>

        @if($claim->action_at)
        <p class="small">
            <i class="fas fa-clock"></i> {{ $claim->claim_status }} on {{ $claim->action_at }} <br>
            <i class="fas fa-user"></i> By: {{ optional($claim->actionBy)->name ?? 'System' }} <br>
            <i class="fas fa-comment"></i> {{ $claim->action_comment ?? 'N/A' }}
        </p>
        @endif

        <hr>

        {{-- ================= POLICY HOLDER ================= --}}
        <h6 class="text-primary"><i class="fas fa-user-shield"></i> Policy Holder</h6>
        <div class="row">
            <div class="col-md-6">
                <p><strong>Name:</strong> {{ $claim->policyHolder->name }} {{ $claim->policyHolder->surname }}</p>
                <p><strong>National ID:</strong> {{ $claim->policyHolder->national_id }}</p>
            </div>
            <div class="col-md-6">
                <p><strong>Phone:</strong> {{ $claim->policyHolder->cell_number }}</p>
                <p><strong>Email:</strong> {{ $claim->policyHolder->email ?? 'N/A' }}</p>
            </div>
        </div>

        <hr>

        {{-- ================= CLAIMANT ================= --}}
        <h6 class="text-primary"><i class="fas fa-user"></i> Claimant</h6>
        <div class="row">
            <div class="col-md-6">
                <p><strong>Name:</strong> {{ $claim->claimant_full_name }}</p>
                <p><strong>National ID:</strong> {{ $claim->claimant_national_id ?? 'N/A' }}</p>
            </div>
            <div class="col-md-6">
                <p><strong>Phone:</strong> {{ $claim->claimant_phone ?? 'N/A' }}</p>
                <p><strong>Address:</strong> {{ $claim->claimant_address ?? 'N/A' }}</p>
            </div>
        </div>
        <p><strong>Relationship to Policy Holder:</strong> {{ $claim->relationship_to_policyholder }}</p>

        <hr>

        {{-- ================= DECEASED ================= --}}
        <h6 class="text-primary"><i class="fas fa-cross"></i> Deceased</h6>
        <div class="row">
            <div class="col-md-6">
                <p><strong>Name:</strong> {{ $claim->deceased_full_name }}</p>
                <p><strong>National ID:</strong> {{ $claim->deceased_national_id }}</p>
            </div>
            <div class="col-md-6">
                <p><strong>Gender:</strong> {{ $claim->deceased_gender }}</p>
                <p><strong>Date of Birth:</strong> {{ $claim->deceased_date_of_birth }}</p>
            </div>
        </div>
        <p><strong>Deceased Type:</strong> {{ $claim->deceased_type }}</p>

        <hr>

        {{-- ================= CLAIM DETAILS ================= --}}
        <h6 class="text-primary"><i class="fas fa-file-invoice"></i> Claim Information</h6>
        <div class="row">
            <div class="col-md-6">
                <p><strong>Date of Death:</strong> {{ $claim->date_of_death }}</p>
                <p><strong>Cause of Death:</strong> {{ $claim->cause_of_death }}</p>
            </div>
            <div class="col-md-6">
                <p><strong>Remarks:</strong> {{ $claim->remarks ?? 'N/A' }}</p>
            </div>
        </div>

        <hr>

        {{-- ================= PAYOUT ================= --}}
        <h6 class="text-primary"><i class="fas fa-coins"></i> Claim Payout</h6>
        <p><strong>Sum Assured:</strong> {{ number_format($sumAssured, 2) }}</p>
        <p><strong>Percentage Applied:</strong> {{ $percentageApplied }}%</p>
        <p class="fw-bold text-success">
            <strong>Amount Payable:</strong> {{ number_format($amountPayable, 2) }}
        </p>

        <hr>


        {{-- ================= DOCUMENTS ================= --}}
        <h6 class="text-primary">
            <i class="fas fa-folder-open"></i> Documents
        </h6>

        @if($claim->documents->isEmpty())
        <p class="small text-muted">No documents uploaded.</p>
        @else
        <table class="table table-sm table-bordered">
            <thead class="table-light">
                <tr>
                    <th>Type</th>
                    <th width="30%">Action</th>
                </tr>
            </thead>
            <tbody>
                @foreach($claim->documents as $document)
                <tr>
                    <td>{{ $document->document_type }}</td>
                    <td>
                        {{-- View always allowed --}}
                        <a href="{{ asset($document->document_path) }}"
                            target="_blank"
                            class="btn btn-sm btn-info">
                            <i class="fas fa-eye"></i>
                        </a>

                        {{-- Delete only if Pending --}}
                        @if($claim->claim_status === 'Pending')
                        <form method="POST"
                            action="{{ route('claims.documents.delete', $document->id) }}"
                            class="d-inline">
                            @csrf
                            @method('DELETE')
                            <button class="btn btn-sm btn-danger"
                                onclick="return confirm('Delete this document?')">
                                <i class="fas fa-trash"></i>
                            </button>
                        </form>
                        @endif
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
        @endif

        {{-- ================= UPLOAD DOCUMENT ================= --}}
        @if($claim->claim_status === 'Pending')
        <hr>

        <h6 class="text-primary">
            <i class="fas fa-upload"></i> Upload Document
        </h6>

        <form method="POST"
            action="{{ url('claims/'.$claim->id.'/documents') }}"
            enctype="multipart/form-data">
            @csrf

            <div class="row">
                <div class="col-md-6 mb-2">
                    <select name="document_type"
                        class="form-control form-control-sm"
                        required>
                        <option value="">Select Document</option>
                        <option value="Death Certificate">Death Certificate</option>
                        <option value="Policyholder ID">Policyholder ID</option>
                        <option value="Claimant ID">Claimant ID</option>
                        <option value="Bank Details">Bank Details</option>
                        <option value="Relationship Proof">Relationship Proof</option>
                    </select>
                </div>

                <div class="col-md-6 mb-2">
                    <input type="file"
                        name="document_file"
                        class="form-control form-control-sm"
                        required>
                </div>
            </div>

            <button class="btn btn-sm btn-success">
                <i class="fas fa-upload"></i> Upload
            </button>
        </form>
        @endif



        {{-- ================= ADMIN ACTIONS ================= --}}
        @if((Auth::user()->role == 3) && ($claim->claim_status === 'Pending'))
        <hr>

        <h6 class="text-primary">
            <i class="fas fa-user-shield"></i> Admin Actions
        </h6>

        <button class="btn btn-sm btn-success"
            data-bs-toggle="modal"
            data-bs-target="#approveModal">
            <i class="fas fa-check"></i> Approve
        </button>

        <button class="btn btn-sm btn-danger"
            data-bs-toggle="modal"
            data-bs-target="#rejectModal">
            <i class="fas fa-times"></i> Reject
        </button>
        @endif


    </div>
</div>

{{-- ================= APPROVE MODAL ================= --}}
<div class="modal fade" id="approveModal">
    <div class="modal-dialog modal-sm">
        <form method="POST" action="{{ route('claims.approve', $claim->id) }}">
            @csrf
            <div class="modal-content">
                <div class="modal-header bg-success text-white">
                    <h6 class="modal-title">Approve Claim</h6>
                </div>
                <div class="modal-body">
                    <textarea name="comment" class="form-control"
                        placeholder="Approval comment (optional)"></textarea>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-success btn-sm">Approve</button>
                </div>
            </div>
        </form>
    </div>
</div>

{{-- ================= REJECT MODAL ================= --}}
<div class="modal fade" id="rejectModal">
    <div class="modal-dialog modal-sm">
        <form method="POST" action="{{ route('claims.reject', $claim->id) }}">
            @csrf
            <div class="modal-content">
                <div class="modal-header bg-danger text-white">
                    <h6 class="modal-title">Reject Claim</h6>
                </div>
                <div class="modal-body">
                    <textarea name="comment" class="form-control"
                        placeholder="Reason for rejection" required></textarea>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-danger btn-sm">Reject</button>
                </div>
            </div>
        </form>
    </div>
</div>
@endsection