<style>
    .section-title {
        border-left: 5px solid #007bff;
        padding-left: 10px;
        margin-bottom: 15px;
        font-weight: 600;
        font-size: clamp(1rem, 1.5vw, 1.25rem);
        color: #343a40;
    }

    .profile-card .form-control-plaintext {
        background: #f8f9fa;
        padding: 8px 12px;
        border-radius: 5px;
        border: 1px solid #dee2e6;
    }

    .btn-styled {
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
        vertical-align: middle;
    }

    .profile-photo {
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        object-fit: cover;
    }

    .table th,
    .table td {
        vertical-align: middle;
    }

    @media (max-width: 350px) {

        body,
        .container-fluid {
            padding-left: 0.05rem !important;
            padding-right: 0.05rem !important;
        }

        .row {
            margin-left: 0 !important;
            margin-right: 0 !important;
        }

        .col-12,
        .col-sm-12 {
            padding-left: 0.25rem !important;
            padding-right: 0.25rem !important;
        }

        .profile-card {
            padding: 0.75rem !important;
        }

        .btn-styled {
            width: 100%;
            margin-bottom: 0.5rem;
        }
    }
</style>



<div class="container-fluid px-1">
    <div class="row mt-2 mx-0 g-1">
        
        <div class="col-12 d-none d-lg-flex justify-content-end gap-2 flex-wrap mb-3 px-1">
            <button class="btn btn-outline-primary btn-styled btn-sm" data-bs-toggle="modal"
                data-bs-target="#updateProfileModal">
                <i class="ti ti-password-user me-1"></i> Update Member Photo
            </button>
            <button class="btn btn-outline-secondary btn-styled btn-sm" data-bs-toggle="modal"
                data-bs-target="#updateSignatureModal">
                <i class="ti ti-certificate me-1"></i> Update Signature Photo
            </button>
            <a href="<?php echo e(route('view_pdf', $member->id)); ?>" target="_blank"
                class="btn btn-info btn-sm btn-styled text-white">
                <i class="ti ti-user me-1"></i> Summary Details PDF
            </a>
            <a href="<?php echo e(route('view_pdf_personal', $member->id)); ?>" target="_blank"
                class="btn btn-warning btn-sm btn-styled text-white">
                <i class="ti ti-user me-1"></i> Personal Details PDF
            </a>
            
        </div>

        
        <div class="col-12 text-center mb-3 px-1">
            <img src="../memberimages/<?php echo e($member->profiePhoto); ?>?v=<?php echo e($user->updated_at->timestamp ?? time()); ?> alt="Profile
                Photo" height="200px" width="200px" class="rounded-circle">

            <h4 class="fw-bold"><?php echo e($member->title); ?>. <?php echo e($member->firstName); ?> <?php echo e($member->lastName); ?></h4>
            <p class="text-muted selectable cursor-pointer" 
               onclick="copyMemberUniqueId('<?php echo e($member->uniqueId); ?>')"
               title="Click to copy Member Unique ID">
                <?php echo e($member->uniqueId); ?>

            </p>

        </div>



        
        <div class="col-12 mb-4 px-1">
            <div class="card shadow p-4 rounded profile-card">
                <div class="section-title">Member Details / அங்கத்தவர் விபரம் </div>
                <div class="row">
                    <?php
                        use Carbon\Carbon;

                        $ageString = '-';
                        if (!empty($member->dateOfBirth)) {
                            try {
                                $dob = Carbon::parse($member->dateOfBirth)->startOfDay();
                                $now = Carbon::now()->startOfDay();

                                $diff = $dob->diff($now);
                                $years = $diff->y; // whole years
                                $months = $diff->m; // whole months
                                $days = $diff->d; // whole days

                                $ageString = "{$years} years, {$months} months, {$days} days";
                            } catch (\Exception $e) {
                                $ageString = '-';
                            }
                        }

                        $fields = [
                            ['First Name / முதற்பெயர்', $member->firstName],
                            ['Last Name / கடைசி பெயர்', $member->lastName],
                            ['NIC / தே.அ. இலக்கம்', $member->nicNumber],
                            ['NIC Issue Date / தே.அ. வழங்கப்பட்ட திகதி ', $member->nicIssueDate],
                            ['Gender / பால் நிலை', $member->gender],
                            ['Marital Status / திருமண நிலை', $member->maritalStatus],
                            ['Phone Number / தொலைபேசி இலக்கம்', $member->phoneNumber],
                            [
                                'Date of Birth / பிறந்த தேதி',
                                $member->dateOfBirth ? Carbon::parse($member->dateOfBirth)->format('d-m-Y') : '-',
                            ],
                            ['Age / வயது', $ageString],
                            ['New Account Number / புதிய க. இலக்கம்', $member->newAccountNumber],
                            ['Old Account Number / பழைய க. இலக்கம்  ', $member->oldAccountNumber],
                            ['Address / முகவரி', $member->address],
                            ['Division / பிரதேசம் ', optional($member->division)->divisionName ?? '-'],
                            ['Village / கிராமம்', optional($member->village)->villageName ?? '-'],
                            ['GN Division / கிராம சேவையாளர் பிரிவு', optional($member->gnDivision)->gnDivisionName ?? '-'],
                            ['Group Name / குழுவின் பெயர்', optional($member->smallgroup)->smallGroupName ?? '-'],
                            ['Status / நிலை', $member->statusType],
                            [
                                'Profession Type / தொழில் பிரிவு ',
                                optional(collect($getPro)->firstWhere('id', $member->profession))->name ?? '-',
                            ],
                            [
                                'Profession / தொழில்',
                                optional(collect($getSubPro)->firstWhere('id', $member->subprofession))->name ?? '-',
                            ],
                            // [
                            //     'GN Division',
                            //     optional(collect($getGnDivision)->firstWhere('id', $member->gnDivisionId))
                            //         ->gnDivisionName ?? '-',
                            // ],
                            // [
                            //     'GN Division Small Group',
                            //     optional(
                            //         collect($gndivisionSmallgroup)->firstWhere('id', $member->gnDivisionSmallGroup),
                            //     )->smallGroupName ?? '-',
                            // ],
                        ];
                    ?>

                    <?php $__currentLoopData = $fields; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as [$label, $value]): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <div class="col-12 col-md-6 mb-3 px-1">
                            <label class="fw-semibold"><?php echo e($label); ?></label>
                            <p class="form-control-plaintext"><?php echo e($value); ?></p>
                        </div>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                    
                    <div class="section-title mt-4">Follower Information / பின்னுருத்தளி விபரம்</div>

                    <?php
                        $followers = [
                            ['Follower Name / பின்னுருத்தளி பெயர்', $member->followerName],
                            ['Follower NIC / பின்னுருத்தளி தே.அ. இலக்கம்', $member->followerNicNumber],
                            ['NIC Issue Date / தே.அ. வழங்கப்பட்ட திகதி', $member->followerIssueDate],
                            ['Follower Address / பின்னுருத்தளி முகவரி', $member->followerAddress],
                        ];
                    ?>

                    <?php $__currentLoopData = $followers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as [$label, $value]): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <div class="col-12 col-md-6 mb-3 px-1">
                            <label class="fw-semibold"><?php echo e($label); ?></label>
                            <p class="form-control-plaintext"><?php echo e($value); ?></p>
                        </div>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

                    
                    <div class="col-12 text-center mt-4">
                        <label class="fw-semibold d-block">Signature/ கையொப்பம்</label>
                        <img src="<?php echo e(asset('memberimages/' . $member->signature)); ?>?v=<?php echo e($user->updated_at->timestamp ?? time()); ?> alt="Signature"
                            class="border rounded shadow-sm mt-2" height="100" width="250">
                    </div>
                </div>
            </div>
        </div>

        
        <div class="col-12 px-1">
            <div class="section-title">Saving Details / சேமிப்பு விவரங்கள்</div>
            <div class="table-responsive">
                <table class="table table-striped table-bordered text-center shadow-sm">
                    <thead class="table-primary">
                        <tr>
                            <th>#</th>
                            <th>ID</th>
                            <th>Total Saving Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                            $count = 1;
                        ?>
                        <tr>
                            <?php
                                $encId = Crypt::encrypt($getSavings->savingsId);
                            ?>
                            <td><?php echo e($count++); ?></td>
                            <td>
                                <a href="<?php echo e(route('view_saving_history', $encId)); ?>">
                                    <?php echo e($getSavings->savingsId); ?>

                                </a>
                            </td>
                            <td><?php echo e($getSavings->totalAmount); ?></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>


        
        <div class="col-12 mb-4 px-1">
            <div class="section-title">Loan Details / கடன் விவரங்கள்</div>
            <div class="table-responsive">
                <table class="table table-bordered table-hover text-center shadow-sm">
                    <thead class="table-primary">
                        <tr>
                            <th>#</th>
                            <th>Loan ID</th>
                            <th>Purpose</th>
                            <th>Term</th>
                            <th>Interest</th>
                            <th>Amount</th>
                            <th>Paid</th>
                            <th>Collect Principal</th>
                            <th>Collect Interest</th>
                            <th>Total Arrears</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $count = 1; ?>
                        <?php $__empty_1 = true; $__currentLoopData = $getDataActive; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $data): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                            <?php
                                $loanEncId = Crypt::encrypt($data->id);
                                $monthlyPaymentTotal = 0;
                                foreach ($getLoanScheduleData as $schedule) {
                                    if ($schedule->loanId == $data->id && $schedule->status == 'paid') {
                                        $monthlyPaymentTotal += $schedule->monthlyPayment;
                                    }
                                }

                                $totalPayAmount = 0;
                                $totalPrincipalArreas = 0;
                                $totalInterestArreas = 0;
                                $totalArreasAmount = null;

                                foreach ($getRepaymentData as $repayment) {
                                    if ($repayment->loanId == $data->id) {
                                        $totalPayAmount += $repayment->repaymentAmount;
                                        $totalPrincipalArreas += $repayment->principalAmount;
                                        $totalInterestArreas += $repayment->interest;
                                        if (
                                            $totalArreasAmount === null ||
                                            $repayment->lastLoanBalance < $totalArreasAmount
                                        ) {
                                            $totalArreasAmount = $repayment->lastLoanBalance;
                                        }
                                    }
                                }

                                $totalArreasAmount ??= 0;

                                $statusClass = match ($data->loanStatus) {
                                    'Active' => 'bg-success',
                                    'Rejected' => 'bg-danger',
                                    default => 'bg-primary',
                                };
                            ?>
                            <tr>
                                <td><?php echo e($count++); ?></td>
                                <td><a href="<?php echo e(route('view_loan_details', $loanEncId)); ?>"><?php echo e($data->loanId); ?></a>
                                </td>
                                <td><?php echo e($getLoanPurpose->firstWhere('id', $data->loanPurpose)?->name ?? '-'); ?></td>
                                <td><?php echo e($data->loanterm); ?></td>
                                <td><?php echo e($data->interestRate); ?>%</td>
                                <td><?php echo e(number_format($data->principal, 2)); ?></td>
                                <td><?php echo e(number_format($totalPayAmount, 2)); ?></td>
                                <td><?php echo e(number_format($totalPrincipalArreas, 2)); ?></td>
                                <td><?php echo e(number_format($totalInterestArreas, 2)); ?></td>
                                <td><?php echo e(number_format($totalArreasAmount, 2)); ?></td>
                                <td><span class="badge <?php echo e($statusClass); ?>"><?php echo e($data->loanStatus); ?></span></td>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                            <tr>
                                <td colspan="11" class="text-muted">No loan records found.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

        
        <div class="col-12 px-1">
            <div class="section-title">Guarantor Details / உத்தரவாததாரர் விவரங்கள்</div>
            <div class="table-responsive">
                <table class="table table-striped table-bordered text-center shadow-sm">
                    <thead class="table-primary">
                        <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>NIC</th>
                            <th>Loan ID</th>
                            <th>Loan Amount</th>
                            <th>Saving Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                            $count = 1;
                            $allGurData = collect($getGurDetails);
                        ?>

                        <?php if($allGurData->isNotEmpty()): ?>
                            <?php $__currentLoopData = $allGurData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $gur): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php
                                    // Determine the correct ID field
                                    $guarantorId = $gur->memberId ?? ($gur->gurrantos ?? null);
                                    $guarantor = $getAllMemberData->firstWhere('id', $guarantorId);
                                ?>
                                <tr>
                                    <td><?php echo e($count++); ?></td>
                                    <td>
                                        <?php if($guarantor): ?>
                                            <a href="<?php echo e(route('view_member', encrypt($guarantor->id))); ?>">
                                                <?php echo e($guarantor->firstName ?? '-'); ?> <?php echo e($guarantor->lastName ?? ''); ?>

                                            </a>
                                        <?php else: ?>
                                            -
                                        <?php endif; ?>
                                    </td>
                                    <td><?php echo e($guarantor->nicNumber ?? '-'); ?></td>
                                    <td><?php echo e($gur->loanId); ?></td>
                                    <td><?php echo e(number_format($gur->principal, 2)); ?></td>
                                    <td><?php echo e(number_format($gur->savingAmount ?? 0, 2)); ?></td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="6" class="text-muted">No guarantor data available.</td>
                            </tr>
                        <?php endif; ?>

                    </tbody>
                </table>
            </div>
        </div>

        
        <div class="col-12 px-1">
            <div class="section-title">Feedback Details / கருத்து விவரங்கள்</div>
            <div class="table-responsive">
                <table class="table table-striped table-bordered text-center shadow-sm">
                    <thead class="table-primary">
                        <tr>
                            <th>#</th>
                            <th>Id</th>
                            <th>Staff Name</th>
                            <th>Date & Time</th>
                            <th>Title</th>
                            <th>Description</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                            $count = 1;
                        ?>
                        <?php $__currentLoopData = $getMemberNotes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $note): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <tr>
                                <td><?php echo e($count++); ?></td>
                                <td><?php echo e($note->randomId); ?></td>
                                <td>
                                    <?php $__currentLoopData = $getAllUser; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <?php if($user->id == $note->createdBy): ?>
                                            <?php echo e($user->name); ?>

                                        <?php endif; ?>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </td>
                                <td><?php echo e($note->created_at); ?></td>
                                <td><?php echo e($note->title); ?></td>
                                <td><?php echo e($note->description); ?></td>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tbody>
                </table>
            </div>
        </div>


    </div>
</div>

<script>
function copyMemberUniqueId(id) {
    if(!id) return;
    navigator.clipboard.writeText(id).then(() => {
        let toast = document.createElement("div");
        toast.innerText = "Member Unique ID " + id + " copied!";
        toast.className = "position-fixed bottom-0 end-0 m-3 p-2 bg-success text-white rounded shadow";
        document.body.appendChild(toast);
        setTimeout(() => toast.remove(), 2000);
    });
}
</script>

<?php /**PATH /home/techhdyz/swd.technathi.cloud/resources/views/pages/allpages/memberpages/personal_details.blade.php ENDPATH**/ ?>