@extends('layouts.admin') @section('content')
<div class="card mb-3">
    <div class="card-header position-relative min-vh-25 mb-7">
        <div class="bg-holder rounded-3 rounded-bottom-0" style="background-image:url('{{ asset('admin/img/profile_back.jpg')}}');"></div>
        <!--/.bg-holder-->
        <div class="avatar avatar-5xl avatar-profile"><img class="rounded-circle img-thumbnail shadow-sm" src="{{ asset ('admin/img/user-profile.png')}}" width="200" alt="" /></div>
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col-lg-12">
                <h4 class="mb-1">{{$member->name}} {{$member->surname}}
                    <span data-bs-toggle="tooltip" data-bs-placement="right" title="{{$status}}"><small class="fa fa-check-circle {{$color}}" data-fa-transform="shrink-4 down-2"></small></span>
                    {!! QrCode::generate($member->national_id) !!}
                    <a href='{{ url("/card", $member->id)}}' class="btn btn-secondary"><i class="fas fa-print"></i> Print QRCode</a>
                </h4>

                <p class="text-500">
                    Age: {{$age}} <br>
                    Registration Date: {{$member->registration_date}} <br>
                    Group: {{ $member->group ? $member->group->name : 'N/A' }}
                </p>
                <p class="text-500">{{$member->postal_address}} </p>

                @if($overAgeCount > 0)
<div class="alert alert-warning">
    ⚠️ {{ $overAgeCount }} dependant(s) are above 21 years and may not be eligible.
    <br>
    <small>Please review or remove them from cover.</small>
</div>
@endif


                {{-- ✅ Deceased Status Toggle Button --}}
                <form action="{{ url('/toggle-deceased-status/' . $member->id) }}" method="POST" onsubmit="return confirm('Are you sure you want to change the deceased status?');">
                    @csrf
                    @method('PUT')
                    @if($member->deceased)
                    <button type="submit" class="btn btn-sm btn-danger">
                        <i class="fas fa-user-slash"></i> Mark as Alive
                    </button>
                    @else
                    <button type="submit" class="btn btn-sm btn-outline-danger">
                        <i class="fas fa-cross"></i> Mark as Deceased
                    </button>
                    @endif
                </form>
                <br>&nbsp;

                <a href='{{ url("/alldependants", $member->id)}}' class="btn btn-outline-primary"><i class="fas fa-users"></i> Dependents ({{$dependants}})</a>
                <a href='{{ url("/allbeneficiaries", $member->id)}}' class="btn btn-outline-primary"><i class="fas fa-users"></i> Beneficiaries ({{$beneficiaries}})</a>

                <button class="btn btn-outline-primary" type="button" data-bs-toggle="modal" data-bs-target="#error-modal"><i class="fas fa-hands-helping"></i> Premium cover:
                    @if (!$premium)
                    None
                    @else
                    {{$premium->cover->name}}
                    @endif
                </button>

                <a href='{{ url("/memberpayments", $member->id)}}' class="btn btn-outline-primary"><i class="fas fa-money-check"></i> Payment:
                    @if (!$payment)
                    None
                    @else
                    {{$payment}}
                    @endif
                </a>

                <br><br>
                <a href='{{ url("/additional", $member->id)}}' class="btn btn-outline-primary btn-sm"> <i class="fas fa-plus"></i> Additional Cover</a>

                <a href='{{ url("/print_profile", $member->id)}}' target="_blank" class="btn btn-primary btn-sm"> <i class="fas fa-print"></i> Print Profile</a>


                <a href='{{ url("/notify-member", $member->id) }}'
                    class="btn btn-primary btn-sm"
                    onclick="return confirmNotify({{ $notificationCount }}, {{ Auth::user()->role }})">
                    <i class="fas fa-bell"></i> ({{$notificationCount}}) Notify Member
                </a>

                <script>
                    function confirmNotify(notificationCount, userRole) {
                        let message = `This member has been notified ${notificationCount} times. Are you sure you want to notify?`;

                        if (notificationCount >= 1 && userRole !== 3) {
                            alert("Only an admin can send notifications more than once.");
                            return false; // Block the action for non-admins
                        }

                        return confirm(message); // Show confirmation for admins
                    }
                </script>


                <a href="{{ route('kyc.index', $member->id) }}" class="btn btn-primary btn-sm">
                    <i class="fas fa-id-card"></i> ( {{$kycCount }} ) Member KYC
                </a>

                <a href="{{ route('claims.member', $member->id) }}" class="btn btn-primary btn-sm">
                    <i class="fas fa-file"></i> ( {{ $claimsCount }} ) Member Claims
                </a>

                <a href='{{ url("/history", $member->id)}}' class="btn btn-primary btn-sm"> <i class="fas fa-history"></i> View Member History</a>
                <a href='{{ url("/member", $member->id)}}' class="btn btn-primary btn-sm"><i class="far fa-edit"></i> Edit</a>&nbsp;
                <!--<a href='{{ url("/premium_new", $member->id)}}' class="btn btn-primary btn-sm"> <i class="fas fa-history"></i>Premium Amount</a>-->
                <div class="modal fade" id="error-modal" tabindex="-1" role="dialog" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered" role="document" style="max-width: 500px">
                        <div class="modal-content position-relative">
                            <div class="position-absolute top-0 end-0 mt-2 me-2 z-index-1">
                                <button class="btn-close btn btn-sm btn-circle d-flex flex-center transition-base" data-bs-dismiss="modal" aria-label="Close"></button>
                            </div>
                            <div class="modal-body p-0">
                                <div class="rounded-top-lg py-3 ps-4 pe-6 bg-light">
                                    <h4 class="mb-1" id="modalExampleDemoLabel">Add a new premium </h4>
                                </div>
                                <div class="p-4 pb-0">
                                    <form class="form-material" method="POST" action="{{ url ('/premium')}}">
                                        @csrf
                                        @if(count($errors->all()))
                                        @foreach ($errors->all() as $error)
                                        <div class="alert alert-danger alert-dismissable"> <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a> <strong>Alert!</strong> {{ $error }} </div> @endforeach @endif
                                        <input hidden type="text" name="member_id" value="{{$member->id}}">
                                        <div class="mb-3">
                                            <select class="form-control" required name="cover_id" id="cover_id">
                                                <option value="{{$premium->cover->id}}"> {{$premium->cover->name}}</option>
                                                @foreach ($covers as $cover)
                                                <option value="{{$cover->id}}">{{$cover->name}}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                        <div class="mb-3">
                                            <select class="form-control" required name="sponsor_id" id="sponsor_id">
                                                <option value="{{$premium->sponsor->id}}"> {{$premium->sponsor->name}}</option>
                                                @foreach ($sponsors as $sponsor)
                                                <option value="{{$sponsor->id}}">{{$sponsor->name}}</option>
                                                @endforeach
                                            </select>
                                            <div class="text-center">
                                                <br><button type="submit" class="btn btn-primary btn-sm"><i class="far fa-save"></i> Save</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button class="btn btn-secondary" type="button" data-bs-dismiss="modal">Close</button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="border-dashed-bottom my-4 d-lg-none"></div>
            </div>
        </div>
    </div>
</div>

<div class="row mb-3">
    <div class="col-8" style="font-size: 12px;">
        <div class="card mb-3">
            <div class="card-header">
                <div class="row flex-between-end">
                    <div class="col-auto align-self-center">
                        <h6 class="mb-0" data-anchor="data-anchor">Personal Information</h6>
                    </div>
                </div>
            </div>
            <div class="card-body bg-light">
                <div class="row">
                    <div class="col-sm-6">
                        <p><strong>Name: </strong> {{$member->name}} </p>
                    </div>
                    <div class="col-sm-6">
                        <p><strong>Surname: </strong> {{$member->surname}} </p>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <p><strong>Gender: </strong> {{$member->gender}} </p>
                    </div>
                    <div class="col-sm-6">
                        <p><strong>National ID: </strong> {{$member->national_id}} </p>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <p><strong>Date of birth: </strong> {{$member->date_of_birth}} </p>
                    </div>
                    <div class="col-sm-6">
                        <p><strong>Nationality: </strong> {{$member->nationality}} </p>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <p><strong>Passport Number: </strong> {{$member->passport_number}} </p>
                    </div>
                    <div class="col-sm-6">
                        <p><strong>Email: </strong> {{$member->email}} </p>
                    </div>
                </div>

                <div class="row">
                    <div class="col-sm-6">
                        <p><strong>Work Number: </strong> {{$member->work_number}} </p>
                    </div>
                    <div class="col-sm-6">
                        <p><strong>Cell Number: </strong> {{$member->cell_number}} </p>
                    </div>
                </div>

                <div class="row">
                    <div class="col-sm-6">
                        <p><strong>Postal Address: </strong> {{$member->postal_address}} </p>
                    </div>
                    <div class="col-sm-6">
                        <p><strong>Residential Address: </strong> {{$member->residential_address}} </p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-4" style="font-size: 12px;">
        <div class="card mb-3">
            <div class="card-header">
                <div class="row flex-between-end">
                    <div class="col-auto align-self-center">
                        <h6 class="mb-0" data-anchor="data-anchor">
                            Payment Information</h6>
                    </div>
                </div>
            </div>
            <div class="card-body bg-light">
                <div id="tableExample2" data-list='{"valueNames":["date","name","amount", "method"],"page":5,"pagination":true}'>
                    <div class="table-responsive scrollbar">
                        <table class="table table-bordered table-striped fs--1 mb-0">
                            <tbody class="list">
                                @if ($last_date >= $today)
                                <tr>
                                    <td>Lastest: {{$last_cover}}</td>
                                </tr>
                                <tr>
                                    <td>Paid in advance: {{$unpaidmonths}}
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Paid in advance:{{($premium->amount * $unpaidmonths)}}
                                    </td>
                                </tr>
                                @else
                                <tr>
                                    <td>Latest:
                                        <span class="text-danger">{{$last_cover}} </span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Unpaid months:
                                        <span class="text-danger">{{$unpaidmonths}} </span>
                                    </td>
                                </tr>

                                <tr>
                                    <td>Total Due:
                                        <span class="text-danger">{{$premium->amount * $unpaidmonths}}</span>
                                    </td>
                                </tr>
                                @endif
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="card-footer bg-light p-0">
                <div class="pagination d-none"></div><a class="btn btn-sm btn-link d-block py-2" href="{{ url ('memberpayments') }}/{{$member->id}}">Show full list<span class="fas fa-chevron-right ms-1 fs--2"></span></a>
            </div>
        </div>
    </div>

    <div class="col-4" style="font-size: 12px;">
        <div class="card mb-3">
            <div class="card-header">
                <div class="row flex-between-end">
                    <div class="col-auto align-self-center">
                        <h6 class="mb-0" data-anchor="data-anchor">Months Covered</h6>
                    </div>
                </div>
            </div>
            <div class="card-body bg-light">

                @foreach ($coverss as $cover)
                <div class="d-flex flex-between-center mb-1">
                    <div class="d-flex align-items-center"><span class="dot bg-300"></span><span class="fw-semi-bold">{{$cover->months}}</span></div>
                </div>
                @endforeach
            </div>
            <div class="card-footer bg-light p-0">
                <div class="pagination d-none"></div><a class="btn btn-sm btn-link d-block py-2" href="{{ url ('memberpayments') }}/{{$member->id}}">Show full list<span class="fas fa-chevron-right ms-1 fs--2"></span></a>
            </div>
        </div>
    </div>

    <div class="col-4" style="font-size: 12px; color:red;">
        <div class="card mb-3">
            <div class="card-header">
                <div class="row flex-between-end">
                    <div class="col-auto align-self-center">
                        <h6 class="mb-0" data-anchor="data-anchor">Arrears Paid</h6>
                    </div>
                </div>
            </div>
            <div class="card-body bg-light">
                @foreach ($arrears as $arrear)
                <div class="d-flex flex-between-center mb-1">
                    <div class="d-flex align-items-center"><span class="dot bg-300"></span><span class="fw-semi-bold">{{$arrear->months}}</span></div>
                </div>
                @endforeach
            </div>
            <div class="card-footer bg-light p-0">
                <div class="pagination d-none"></div><a class="btn btn-sm btn-link d-block py-2" href="{{ url ('arrears') }}/{{$member->id}}">Show full list<span class="fas fa-chevron-right ms-1 fs--2"></span></a>
            </div>
        </div>
    </div>
</div>
@endsection