<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Member Details') }}
        </h2>


    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <!-- Back Button -->
            <div class="mb-6 animate-fade-in">
                <a href="{{ route('members.index') }}" class="inline-flex items-center text-sm font-medium text-gray-500 hover:text-gray-700 transition">
                    <svg class="w-5 h-5 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path>
                    </svg>
                    Back to Members List
                </a>
            </div>

            <!-- Profile Header Card -->
            <div class="bg-white rounded-2xl shadow-soft overflow-hidden mb-8 animate-slide-up">
                <div class="h-32 bg-gradient-to-r from-crextio-accent to-yellow-300"></div>
                <div class="px-8 pb-8">
                    <div class="relative flex flex-col sm:flex-row items-center sm:items-end -mt-16 mb-6 gap-6">
                        <div class="h-32 w-32 rounded-2xl bg-white p-2 shadow-md">
                            <div class="h-full w-full rounded-xl bg-crextio-accent flex items-center justify-center text-4xl font-bold text-gray-900 border-2 border-white">
                                {{ substr($member->name, 0, 1) }}
                            </div>
                        </div>
                        <div class="flex-1 text-center sm:text-left pt-6 sm:pt-0">
                            <div class="flex flex-col sm:flex-row sm:items-center gap-3 mb-2">
                                <h1 class="text-3xl font-bold text-gray-900 font-display">{{ $member->name }}</h1>
                                @php
                                    $statusColor = $member->status == 'active' ? 'bg-green-100 text-green-800 ring-green-500/10' : 'bg-red-100 text-red-800 ring-red-500/10';
                                @endphp
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold {{ $statusColor }} ring-1 ring-inset w-fit mx-auto sm:mx-0">
                                    {{ ucfirst($member->status ?? 'inactive') }}
                                </span>
                            </div>
                            <p class="text-gray-500 flex items-center justify-center sm:justify-start">
                                <svg class="w-4 h-4 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2-2v12a2 2 0 002 2z"></path>
                                </svg>
                                Joined on {{ $member->join_date ? $member->join_date->format('F d, Y') : 'N/A' }}
                            </p>
                        </div>
                        <div class="flex gap-3">
                            @if(in_array(auth()->user()->role, ['leader', 'top-leader', 'admin']))
                            <a href="{{ route('members.statement', $member->id) }}" class="px-6 py-3 bg-crextio-accent hover:bg-yellow-500 text-gray-900 font-medium rounded-xl transition-all shadow-sm hover:shadow-md flex items-center gap-2">
                                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M9 19l3 3m0 0l3-3m-3 3V10"></path></svg>
                                Download Statement
                            </a>
                            <button @click="$dispatch('open-modal', 'edit-member-modal')" class="px-6 py-3 bg-white border border-gray-200 hover:bg-gray-50 text-gray-700 font-medium rounded-xl transition-all shadow-sm hover:shadow-md">
                                Edit Profile
                            </button>
                            @endif
                        </div>
                    </div>

                    <!-- Member Stats Overview -->
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-6 pt-8 border-t border-gray-100">
                        <div class="p-4 rounded-xl bg-gray-50 border border-gray-100">
                            <p class="text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1">Total Contributions</p>
                            <p class="text-2xl font-bold text-gray-900">TZS {{ number_format($member->contributions->sum('amount'), 0) }}</p>
                        </div>
                        <div class="p-4 rounded-xl bg-gray-50 border border-gray-100">
                            <p class="text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1">Active Loans</p>
                            <p class="text-2xl font-bold text-gray-900">0</p> {{-- Simplified for now --}}
                        </div>
                        <div class="p-4 rounded-xl bg-gray-50 border border-gray-100">
                            <p class="text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1">Leader / Region</p>
                            <p class="text-lg font-bold text-gray-900 truncate">{{ $member->leader->name ?? 'No Leader' }}</p>
                            <p class="text-xs text-blue-600 font-medium">{{ $member->leader->region ?? 'Unassigned' }}</p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
                <!-- Detailed Info -->
                <div class="lg:col-span-1 space-y-6">
                    <div class="bg-white rounded-2xl shadow-soft p-6 animate-slide-up" style="animation-delay: 0.1s;">
                        <h3 class="text-lg font-bold text-gray-900 mb-6 font-display">Information</h3>
                        
                        <div class="space-y-6">
                            <div>
                                <label class="block text-xs font-semibold text-gray-400 uppercase tracking-wider mb-1.5">Phone Number</label>
                                <p class="text-sm font-medium text-gray-900 bg-gray-50 px-3 py-2.5 rounded-lg border border-gray-100">{{ $member->phone }}</p>
                            </div>
                            <div>
                                <label class="block text-xs font-semibold text-gray-400 uppercase tracking-wider mb-1.5">Email Address</label>
                                <p class="text-sm font-medium text-gray-900 bg-gray-50 px-3 py-2.5 rounded-lg border border-gray-100">{{ $member->email ?? 'N/A' }}</p>
                            </div>
                            <div>
                                <label class="block text-xs font-semibold text-gray-400 uppercase tracking-wider mb-1.5">Church / Location</label>
                                <div class="bg-gray-50 p-4 rounded-xl border border-gray-100">
                                    <div class="flex items-start gap-3 mb-3 text-sm">
                                        <svg class="w-4 h-4 text-crextio-text mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"></path></svg>
                                        <span class="text-gray-900 font-medium">{{ $member->church ?? 'N/A' }}</span>
                                    </div>
                                    <div class="flex items-start gap-3 text-sm">
                                        <svg class="w-4 h-4 text-crextio-text mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
                                        <span class="text-gray-900 font-medium">{{ $member->location ?? 'N/A' }}</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Recent Contributions -->
                <div class="lg:col-span-2">
                    <div class="bg-white rounded-2xl shadow-soft p-6 animate-slide-up" style="animation-delay: 0.2s;">
                        <div class="flex items-center justify-between mb-6">
                            <h3 class="text-lg font-bold text-gray-900 font-display">Recent Contributions</h3>
                            <a href="#" class="text-xs font-bold text-crextio-text hover:underline uppercase tracking-wider">View All</a>
                        </div>

                        <div class="overflow-hidden bg-gray-50 rounded-xl border border-gray-100">
                            @if($contributions->count() > 0)
                                <table class="w-full text-left">
                                    <thead class="bg-white border-b border-gray-100">
                                        <tr>
                                            <th class="px-6 py-4 text-[10px] font-bold text-gray-400 uppercase tracking-widest">Date</th>
                                            <th class="px-6 py-4 text-[10px] font-bold text-gray-400 uppercase tracking-widest">Method</th>
                                            <th class="px-6 py-4 text-[10px] font-bold text-gray-400 uppercase tracking-widest">Amount</th>
                                        </tr>
                                    </thead>
                                    <tbody class="divide-y divide-gray-100">
                                        @foreach($contributions as $contribution)
                                        <tr class="hover:bg-gray-100 transition-colors">
                                            <td class="px-6 py-4 text-sm font-medium text-gray-700">
                                                {{ \Carbon\Carbon::parse($contribution->contribution_date)->format('M d, Y') }}
                                            </td>
                                            <td class="px-6 py-4">
                                                <span class="px-2 py-1 bg-white border border-gray-200 text-gray-600 text-[10px] font-bold rounded-md shadow-xs">
                                                    {{ strtoupper(str_replace('_', ' ', $contribution->payment_method)) }}
                                                </span>
                                            </td>
                                            <td class="px-6 py-4 text-sm font-bold text-gray-900">
                                                {{ number_format($contribution->amount, 0) }}
                                            </td>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            @else
                                <div class="py-12 text-center">
                                    <svg class="w-12 h-12 text-gray-200 mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg>
                                    <p class="text-sm text-gray-500 italic">No contribution history found</p>
                                </div>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </x-slot>


    <!-- Edit Member Modal -->
    <x-modal name="edit-member-modal" :show="$errors->isNotEmpty()" focusable>
        <div class="p-6">
            <h2 class="text-lg font-bold text-gray-900 font-display mb-6">
                Edit Member Profile
            </h2>

            <form method="POST" action="{{ route('members.update', $member->id) }}" class="space-y-4">
                @csrf
                @method('PUT')

                <div>
                    <x-input-label for="name" :value="__('Full Name')" />
                    <x-text-input id="name" name="name" type="text" class="mt-1 block w-full" :value="old('name', $member->name)" required />
                    <x-input-error class="mt-2" :messages="$errors->get('name')" />
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <x-input-label for="phone" :value="__('Phone Number')" />
                        <x-text-input id="phone" name="phone" type="text" class="mt-1 block w-full" :value="old('phone', $member->phone)" required />
                        <x-input-error class="mt-2" :messages="$errors->get('phone')" />
                    </div>
                    <div>
                        <x-input-label for="email" :value="__('Email (Optional)')" />
                        <x-text-input id="email" name="email" type="email" class="mt-1 block w-full" :value="old('email', $member->email)" />
                        <x-input-error class="mt-2" :messages="$errors->get('email')" />
                    </div>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <x-input-label for="church" :value="__('Church')" />
                        <x-text-input id="church" name="church" type="text" class="mt-1 block w-full" :value="old('church', $member->church)" />
                        <x-input-error class="mt-2" :messages="$errors->get('church')" />
                    </div>
                    <div>
                        <x-input-label for="location" :value="__('Location')" />
                        <x-text-input id="location" name="location" type="text" class="mt-1 block w-full" :value="old('location', $member->location)" />
                        <x-input-error class="mt-2" :messages="$errors->get('location')" />
                    </div>
                </div>

                <div>
                    <x-input-label for="join_date" :value="__('Join Date')" />
                    <x-text-input id="join_date" name="join_date" type="date" class="mt-1 block w-full" :value="old('join_date', $member->join_date ? $member->join_date->format('Y-m-d') : '')" required />
                    <x-input-error class="mt-2" :messages="$errors->get('join_date')" />
                </div>

                <div>
                    <x-input-label for="status" :value="__('Status')" />
                    <select id="status" name="status" class="mt-1 block w-full border-gray-300 focus:border-indigo-500 focus:ring-indigo-500 rounded-md shadow-sm">
                        <option value="active" {{ old('status', $member->status) == 'active' ? 'selected' : '' }}>Active</option>
                        <option value="inactive" {{ old('status', $member->status) == 'inactive' ? 'selected' : '' }}>Inactive</option>
                    </select>
                    <x-input-error class="mt-2" :messages="$errors->get('status')" />
                </div>

                <div class="mt-6 flex justify-end gap-3">
                    <x-secondary-button x-on:click="$dispatch('close')">
                        {{ __('Cancel') }}
                    </x-secondary-button>

                    <x-primary-button>
                        {{ __('Save Changes') }}
                    </x-primary-button>
                </div>
            </form>
        </div>
    </x-modal>
</x-app-layout>
