<div class="content-wrapper">
    <div class="container-xxl flex-grow-1 container-p-y">
        <div class="row">
            <input type="hidden" class="form-control" id="smallGroupId" value="{{ $smallGroupId }}">

            <!-- Small Group Details Card -->
            <div class="col-xl-12 col-lg-12 col-md-12">
                <!-- Small Group Header -->
                <div class="text-center mb-4">
                    <h3 class="fw-bold">{{ $smallGroupName ?? '' }} Small Group Details</h3>
                    <p class="text-muted mb-0">Division: {{ $getDivisionName }} | Village: {{ $getVillageName }}</p>
                </div>
                <div class="card mb-3">
                    <div class="card-body">
                        <div class="row mb-2">
                            <div class="col-12 d-flex justify-content-between align-items-center">
                                <h4 class="fw-bold text-primary mb-0">
                                    
                                </h4>
                                <button class="btn btn-primary btn-sm" data-bs-toggle="modal"
                                    data-bs-target="#smallgroupDetailsModal">
                                    <i class="ti ti-edit"></i> Update Small Group Details
                                </button>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-6">
                                
                                <ul class="list-unstyled my-3 py-1">
                                    <li class="d-flex align-items-center mb-3">
                                        <i class="ti ti-map-pin ti-lg"></i>
                                        <span class="fw-medium mx-2">Division Name:</span>
                                        <span>{{ $getDivisionName }}</span>
                                    </li>
                                    <li class="d-flex align-items-center mb-3">
                                        <i class="ti ti-home ti-lg"></i>
                                        <span class="fw-medium mx-2">Village Name :</span>
                                        <span>{{ $getVillageName }}</span>
                                    </li>
                                    <li class="d-flex align-items-center mb-3">
                                        <i class="ti ti-user ti-lg"></i>
                                        <span class="fw-medium mx-2">Group Leader:</span>
                                        <span>
                                            @if ($getSmallgroupFinalData)
                                                @php
                                                    $leader = $getAllMemberData->firstWhere('id', $getSmallgroupFinalData->groupLeader);
                                                @endphp
                                                {{ $leader->firstName ?? '-' }} {{ $leader->lastName ?? '' }}
                                            @else
                                                -
                                            @endif
                                        </span>
                                    </li>
                                    <li class="d-flex align-items-center mb-3">
                                        <i class="ti ti-user ti-lg"></i>
                                        <span class="fw-medium mx-2">Secretary:</span>
                                        <span>
                                            @if ($getSmallgroupFinalData)
                                                @php
                                                    $secretary = $getAllMemberData->firstWhere('id', $getSmallgroupFinalData->secretary);
                                                @endphp
                                                {{ $secretary->firstName ?? '-' }} {{ $secretary->lastName ?? '' }}
                                            @else
                                                -
                                            @endif
                                        </span>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- / Small Group Details Card -->

            <!-- Member Table -->
<!-- Member Table -->
<div class="col-xl-12 mt-3 col-lg-12 col-md-12">
    <div class="card shadow">
        <div class="card-body">
            <div class="table-responsive" style="max-height: 500px; overflow-y: auto;">
                <table class="table table-sm table-striped table-bordered">
                    <thead class="table-light">
                        <tr>
                            <th>#</th>
                            <th>Member Name</th>
                            <th>Address</th>
                            <th>NIC</th>
                            <th>Phone Number</th>
                            <th>Old Account Number</th>
                            <th>Savings</th>
                            <th>Other Income</th>
                            <th>Death Subscription</th>
                            <th>Total Loan</th>
                            <th>Profession</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php
                            $count = 1;
                            $sumSavings = 0;
                            $sumOtherIncome = 0;
                            $sumDeathSub = 0;
                            $sumLoans = 0;
                        @endphp

                        @foreach ($memberData as $member)
                            @php
                                $encryptedId = encrypt($member->id);

                                // 🔹 Lookup financial data by member uniqueId
                                $saving      = $getSavingData[$member->uniqueId] ?? null;
                                $otherIncome = $getOtherIncomeData[$member->uniqueId] ?? null;
                                $death       = $getDeathSubData[$member->uniqueId] ?? null;
                                $profession  = $getProfession->firstWhere('id', $member->profession);

                                $rowSaving      = floatval($saving->totalAmount ?? 0);
                                $rowOtherIncome = floatval($otherIncome->totalAmount ?? 0);
                                $rowDeath       = floatval($death->totalAmount ?? 0);
                                $rowLoan        = $getLoansData->where('memberId', $member->id)->sum('principal');

                                // 🔹 Accumulate totals
                                $sumSavings     += $rowSaving;
                                $sumOtherIncome += $rowOtherIncome;
                                $sumDeathSub    += $rowDeath;
                                $sumLoans       += $rowLoan;
                            @endphp

                            <tr>
                                <td>{{ $count++ }}</td>
                                <td>
                                    <a href="{{ route('view_member', $encryptedId) }}">
                                        {{ $member->firstName }} {{ $member->lastName }}
                                    </a>
                                </td>
                                <td>{{ $member->address }}</td>
                                <td>{{ $member->nicNumber }}</td>
                                <td>{{ $member->phoneNumber }}</td>
                                <td>{{ $member->oldAccountNumber }}</td>

                                <!-- Savings -->
                                <td>
                                    @if($saving && isset($saving->savingsId))
                                        <a href="{{ route('view_saving_history', encrypt($saving->savingsId)) }}">
                                            {{ number_format($rowSaving, 2) }}
                                        </a>
                                    @else
                                        0.00
                                    @endif
                                </td>

                                <!-- Other Income -->
                                <td>{{ number_format($rowOtherIncome, 2) }}</td>

                                <!-- Death Subscription -->
                                <td>
                                    @if($death && isset($death->deathId))
                                        <a href="{{ route('view_death_history', encrypt($death->deathId)) }}">
                                            {{ number_format($rowDeath, 2) }}
                                        </a>
                                    @else
                                        0.00
                                    @endif
                                </td>

                                <!-- Loan -->
                                <td>{{ number_format($rowLoan, 2) }}</td>

                                <!-- Profession -->
                                <td>{{ $profession->name ?? '-' }}</td>
                            </tr>
                        @endforeach
                    </tbody>

                    <!-- Totals Footer -->
                    <tfoot>
                        <tr>
                            <th colspan="6" class="text-end">Totals:</th>
                            <th>{{ number_format($sumSavings, 2) }}</th>
                            <th>{{ number_format($sumOtherIncome, 2) }}</th>
                            <th>{{ number_format($sumDeathSub, 2) }}</th>
                            <th>{{ number_format($sumLoans, 2) }}</th>
                            <th></th>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
</div>
<!-- / Member Table -->

        </div>
    </div>
</div>


<!-- Small Group Update Modal -->
<div class="modal fade" id="smallgroupDetailsModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg modal-simple">
        <div class="modal-content">
            <div class="modal-body">
                <button type="button" class="btn-close btn-pinned" data-bs-dismiss="modal" aria-label="Close"></button>
                <div class="text-center mb-6">
                    <h4 class="mb-2">Update Smallgroup</h4>
                    <p>Update the Smallgroup details</p>
                </div>
                <div class="row">
                    <div class="col-6 mb-4">
                        <label class="form-label" for="txtGroupLeader">Group Leader <span class="text-danger">*</span></label>
                        <select class="selectize" id="txtGroupLeader">
                            <option value="">---Select---</option>
                            @foreach ($memberData as $user)
                                <option value="{{ $user->id }}" 
                                    {{ $getSmallgroupFinalData && $getSmallgroupFinalData->groupLeader == $user->id ? 'selected' : '' }}>
                                    {{ $user->firstName }}  {{ $user->lastName }} /  {{ $user->oldAccountNumber }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-6 mb-4">
                        <label class="form-label" for="txtSecretary">Secretary <span class="text-danger">*</span></label>
                        <select class="selectize" id="txtSecretary">
                            <option value="">---Select---</option>
                            @foreach ($memberData as $user)
                                <option value="{{ $user->id }}" 
                                    {{ $getSmallgroupFinalData && $getSmallgroupFinalData->secretary == $user->id ? 'selected' : '' }}>
                                    {{ $user->firstName }}  {{ $user->lastName }} /  {{ $user->oldAccountNumber }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <div class="col-12 text-center demo-vertical-spacing">
                    <button class="btn btn-primary me-4" id="btnUpdateSmallgroupDetails">Update Smallgroup Details</button>
                    <button type="reset" class="btn btn-label-secondary" data-bs-dismiss="modal" aria-label="Close">Discard</button>
                </div>
<!-- / Small Group Update Modal -->
        </div>
    </div>
</div>
<!-- / Small Group Update Modal -->

<style>
    .table-responsive thead th {
        position: sticky;
        top: 0;
        background: #f8f9fa;
        z-index: 2;
    }
</style>