<div class="content-wrapper">
    <div class="container-xxl flex-grow-1 container-p-y">
        <div class="row">
            <input type="hidden" class="form-control" id="villageId" value="<?php echo e($villageId); ?>">
            <div class="col-xl-12 col-lg-12 col-md-12">
                <!-- Village Details Card -->
                <div class="card">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-12 d-flex justify-content-end">
                                <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#villageDetailsModal">
                                    <i class="ti ti-edit"></i> Update Village Details
                                </button>
                            </div>

                            <div class="col-6">
                                <small class="card-text text-uppercase text-muted small">Village Details</small>
                                <ul class="list-unstyled my-3 py-1">

                                    <li class="d-flex align-items-center mb-4">
                                        <i class="ti ti-user ti-lg"></i>
                                        <span class="fw-medium mx-2">Village Name :</span>
                                        <span><?php echo e($getVillageData->villageName ?? '-'); ?></span>
                                    </li>

                                    <li class="d-flex align-items-center mb-4">
                                        <i class="ti ti-user ti-lg"></i>
                                        <span class="fw-medium mx-2">Village Leader:</span>
                                        <span>
                                            <?php if($getVillageFinalData): ?>
                                                <?php
                                                    $leader = $getAllMemberData->firstWhere('id', $getVillageFinalData->villageLeader);
                                                ?>
                                                <?php echo e($leader ? $leader->firstName . ' ' . $leader->lastName : '-'); ?>

                                            <?php else: ?>
                                                -
                                            <?php endif; ?>
                                        </span>
                                    </li>

                                    <li class="d-flex align-items-center mb-4">
                                        <i class="ti ti-user ti-lg"></i>
                                        <span class="fw-medium mx-2">Secretary :</span>
                                        <span>
                                            <?php if($getVillageFinalData): ?>
                                                <?php
                                                    $secretary = $getAllMemberData->firstWhere('id', $getVillageFinalData->secretary);
                                                ?>
                                                <?php echo e($secretary ? $secretary->firstName . ' ' . $secretary->lastName : '-'); ?>

                                            <?php else: ?>
                                                -
                                            <?php endif; ?>
                                        </span>
                                    </li>

                                    <li class="d-flex align-items-center mb-4">
                                        <i class="ti ti-user ti-lg"></i>
                                        <span class="fw-medium mx-2">Staff:</span>
                                        <span>
                                            <?php if($getVillageFinalData): ?>
                                                <?php
                                                    $staffArray = json_decode($getVillageFinalData->staff, true) ?? [];
                                                    $staffNames = $getAllMemberData
                                                        ->whereIn('id', $staffArray)
                                                        ->pluck('firstName')
                                                        ->toArray();
                                                ?>
                                                <?php if(count($staffNames) > 0): ?>
                                                    <?php echo e(implode(', ', $staffNames)); ?>

                                                <?php else: ?>
                                                    -
                                                <?php endif; ?>
                                            <?php else: ?>
                                                -
                                            <?php endif; ?>
                                        </span>
                                    </li>

                                    <li class="d-flex align-items-center mb-4">
                                        <i class="ti ti-user ti-lg"></i>
                                        <span class="fw-medium mx-2">FO Name:</span>
                                        <span>
                                            <?php if($getVillageFinalData): ?>
                                                <?php
                                                    $foUser = $getUsers->firstWhere('id', $getVillageFinalData->foName);
                                                ?>
                                                <?php echo e($foUser ? $foUser->name : '-'); ?>

                                            <?php else: ?>
                                                -
                                            <?php endif; ?>
                                        </span>
                                    </li>

                                </ul>
                            </div>

                            <div class="col-6">
                                <small class="card-text text-uppercase text-muted small">Contacts</small>
                                <ul class="list-unstyled my-3 py-1">
                                    <li class="d-flex align-items-center mb-4">
                                        <i class="ti ti-phone-call ti-lg"></i>
                                        <span class="fw-medium mx-2">Phone Number:</span>
                                        <span><?php echo e($getVillageFinalData->phone ?? '-'); ?></span>
                                    </li>
                                </ul>
                            </div>
                        </div>

                    </div>
                </div>
                <!--/ Village Details Card -->
            </div>

            <!-- 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;
                        ?>

                        <?php $__currentLoopData = $memberData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $member): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <?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;
                            ?>

                            <tr>
                                <td><?php echo e($count++); ?></td>
                                <td>
                                    <a href="<?php echo e(route('view_member', $encryptedId)); ?>">
                                        <?php echo e($member->firstName); ?> <?php echo e($member->lastName); ?>

                                    </a>
                                </td>
                                <td><?php echo e($member->address); ?></td>
                                <td><?php echo e($member->nicNumber); ?></td>
                                <td><?php echo e($member->phoneNumber); ?></td>
                                <td><?php echo e($member->oldAccountNumber); ?></td>

                                <!-- Savings -->
                                <td>
                                    <?php if($saving && isset($saving->savingsId)): ?>
                                        <a href="<?php echo e(route('view_saving_history', encrypt($saving->savingsId))); ?>">
                                            <?php echo e(number_format($rowSaving, 2)); ?>

                                        </a>
                                    <?php else: ?>
                                        0.00
                                    <?php endif; ?>
                                </td>

                                <!-- Other Income -->
                                <td><?php echo e(number_format($rowOtherIncome, 2)); ?></td>

                                <!-- Death Subscription -->
                                <td>
                                    <?php if($death && isset($death->deathId)): ?>
                                        <a href="<?php echo e(route('view_death_history', encrypt($death->deathId))); ?>">
                                            <?php echo e(number_format($rowDeath, 2)); ?>

                                        </a>
                                    <?php else: ?>
                                        0.00
                                    <?php endif; ?>
                                </td>

                                <!-- Loan -->
                                <td><?php echo e(number_format($rowLoan, 2)); ?></td>

                                <!-- Profession -->
                                <td><?php echo e($profession->name ?? '-'); ?></td>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tbody>

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

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


<!--/ Division Details Modal -->
<div class="modal fade" id="villageDetailsModal" 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 Village</h4>
                    <p>Update the village details</p>
                </div>
                <div class="row">
                    <div class="col-6 mb-4">
                        <label class="form-label" for="modalProfessionName">Village Leader</label> <span
                            class=" text-danger">*</span>
                        <select class="selectize" id="txtVillageLeader">
                            <option value="">---Select---</option>
                            <?php $__currentLoopData = $memberData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php if($getVillageFinalData != null): ?>
                                    <?php if($getVillageFinalData->villageLeader == $user->id): ?>
                                        <option value="<?php echo e($user->id); ?>" selected><?php echo e($user->firstName); ?></option>
                                    <?php else: ?>
                                        <option value="<?php echo e($user->id); ?>"><?php echo e($user->firstName); ?></option>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <option value="<?php echo e($user->id); ?>"><?php echo e($user->firstName); ?></option>
                                <?php endif; ?>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-6 mb-4">
                        <label class="form-label" for="modalProfessionName">Secretary</label> <span
                            class=" text-danger">*</span>
                        <select class="selectize" id="txtSecretary">
                            <option value="">---Select---</option>
                            <?php $__currentLoopData = $memberData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php if($getVillageFinalData != null): ?>
                                    <?php if($getVillageFinalData->secretary == $user->id): ?>
                                        <option value="<?php echo e($user->id); ?>" selected><?php echo e($user->firstName); ?></option>
                                    <?php else: ?>
                                        <option value="<?php echo e($user->id); ?>"><?php echo e($user->firstName); ?></option>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <option value="<?php echo e($user->id); ?>"><?php echo e($user->firstName); ?></option>
                                <?php endif; ?>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-6 mb-4">
                        <label class="form-label" for="modalProfessionName">Staff</label>
                        <span class="text-danger">*</span>
                        <select class="selectize" id="txtStaff" multiple>
                            <option value="">---Select---</option>
                            <?php
                                $selectStaff = $getVillageFinalData
                                    ? json_decode($getVillageFinalData->staff, true)
                                    : [];
                            ?>
                            <?php $__currentLoopData = $memberData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($user->id); ?>"
                                        <?php if(is_array($selectStaff) && in_array($user->id, $selectStaff)): ?> selected <?php endif; ?>>
                                        <?php echo e($user->firstName); ?>

                                    </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>

                    <div class="col-6 mb-4">
                        <label class="form-label" for="modalProfessionName">FO Name</label> <span
                            class=" text-danger">*</span>
                        <select class="selectize" id="txtFoName">
                            <option value="">---Select---</option>
                            <?php $__currentLoopData = $userDetails; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php if($getVillageFinalData != null): ?>
                                    <?php if($getVillageFinalData->foName == $user->id): ?>
                                        <option value="<?php echo e($user->id); ?>" selected><?php echo e($user->name); ?></option>
                                    <?php else: ?>
                                        <option value="<?php echo e($user->id); ?>"><?php echo e($user->name); ?></option>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <option value="<?php echo e($user->id); ?>"><?php echo e($user->name); ?></option>
                                <?php endif; ?>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-12 mb-4">
                        <label class="form-label" for="modalProfessionName">Phone Number</label> <span
                            class=" text-danger">*</span>
                        <input type="text" class="form-control" id="txtPhoneNumber"
                            value="<?php if($getVillageFinalData != null): ?> <?php echo e($getVillageFinalData->phone); ?><?php else: ?><?php echo e(''); ?> <?php endif; ?>">
                    </div>
                </div>


                <div class="col-12 text-center demo-vertical-spacing">
                    <button class="btn btn-primary me-4" id="btnUpdateVillageDetails">Update Village
                        Details</button>
                    <button type="reset" class="btn btn-label-secondary" data-bs-dismiss="modal"
                        aria-label="Close">
                        Discard
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
<!--/ Division Details Modal -->
<?php /**PATH /home/techhdyz/swd.technathi.cloud/resources/views/pages/allpages/divisionbyvillage/village_details.blade.php ENDPATH**/ ?>