{{-- Member Profile --}}
<x-app-layout title="My Profile">

    <x-module-hero :title="$member->full_name" :description="$member->membership_code" icon="fa-user">
        <x-slot name="tabs">
            <a href="{{ route('member.dashboard') }}" class="module-hero-tab">
                <i class="fas fa-tachometer-alt"></i> Dashboard
            </a>
            <a href="{{ route('member.profile') }}" class="module-hero-tab active-tab">
                <i class="fas fa-user"></i> Profile
            </a>
            <a href="{{ route('member.contributions') }}" class="module-hero-tab">
                <i class="fas fa-hand-holding-usd"></i> Contributions
            </a>
            <a href="{{ route('member.loans.index') }}" class="module-hero-tab">
                <i class="fas fa-money-bill-wave"></i> Loans
            </a>
        </x-slot>
    </x-module-hero>

    <div class="p-6">
        @if(session('success'))
            <div class="mb-6 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-center">
                <i class="fas fa-check-circle mr-3"></i> {{ session('success') }}
            </div>
        @endif
        @if($errors->any())
            <div class="mb-6 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg">
                <p class="font-semibold mb-2 text-sm">Please correct the following errors:</p>
                <ul class="list-disc list-inside text-sm">
                    @foreach($errors->all() as $error)<li>{{ $error }}</li>@endforeach
                </ul>
            </div>
        @endif

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">

            {{-- Main Content --}}
            <div class="lg:col-span-2 space-y-6">

                {{-- Profile Information --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                        <h3 class="text-base font-semibold text-gray-900 flex items-center gap-2">
                            <i class="fas fa-user text-green-600"></i> Profile Information
                        </h3>
                    </div>
                    <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">First Name</label>
                            <p class="text-gray-900 font-medium text-sm">{{ $member->first_name }}</p>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Last Name</label>
                            <p class="text-gray-900 font-medium text-sm">{{ $member->last_name }}</p>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Email</label>
                            <p class="text-gray-900 text-sm">{{ $member->email }}</p>
                            <p class="text-xs text-gray-400 mt-0.5">Contact support to change email</p>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Member Code</label>
                            <p class="text-gray-900 font-mono text-sm">{{ $member->membership_code }}</p>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Status</label>
                            @php
                                $sc = ['active'=>'bg-green-100 text-green-700','inactive'=>'bg-yellow-100 text-yellow-700','suspended'=>'bg-red-100 text-red-700'];
                                $sc2 = $sc[$member->status] ?? 'bg-gray-100 text-gray-700';
                            @endphp
                            <span class="px-2 py-0.5 rounded-full text-xs font-semibold {{ $sc2 }}">{{ ucfirst($member->status) }}</span>
                        </div>
                        <div>
                            <label class="block text-xs font-medium text-gray-500 mb-1">Member Since</label>
                            <p class="text-gray-900 text-sm">{{ $member->created_at->format('F d, Y') }}</p>
                        </div>
                    </div>
                </div>

                {{-- Update Profile Form --}}
                <form action="{{ route('member.profile.update') }}" method="POST">
                    @csrf
                    @method('PUT')

                    <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden mb-6">
                        <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                            <h3 class="text-base font-semibold text-gray-900 flex items-center gap-2">
                                <i class="fas fa-edit text-green-600"></i> Update Contact Information
                            </h3>
                        </div>
                        <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div>
                                <label for="phone" class="block text-sm font-medium text-gray-700 mb-1">Phone Number</label>
                                <input type="tel" id="phone" name="phone" value="{{ old('phone', $member->phone) }}"
                                       class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm"
                                       placeholder="+256 7XX XXX XXX">
                                @error('phone')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                            </div>
                            <div class="md:col-span-2">
                                <label for="notes" class="block text-sm font-medium text-gray-700 mb-1">Personal Notes</label>
                                <textarea id="notes" name="notes" rows="3"
                                          class="w-full px-4 py-2.5 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 text-sm"
                                          placeholder="Add any personal notes or preferences...">{{ old('notes', $member->notes) }}</textarea>
                            </div>
                        </div>
                    </div>

                    <div class="flex justify-end gap-3">
                        <a href="{{ route('member.dashboard') }}" class="px-6 py-2.5 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition text-sm font-medium">Cancel</a>
                        <button type="submit" class="px-8 py-2.5 bg-green-600 text-white rounded-lg hover:bg-green-700 transition text-sm font-semibold shadow-sm">
                            <i class="fas fa-save mr-1"></i> Save Changes
                        </button>
                    </div>
                </form>

                {{-- Membership Categories --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                        <h3 class="text-base font-semibold text-gray-900 flex items-center gap-2">
                            <i class="fas fa-tags text-green-600"></i> Membership Categories
                        </h3>
                    </div>
                    <div class="p-6">
                        @if($member->membershipCategories->count() > 0)
                            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                                @foreach($member->membershipCategories as $category)
                                    <div class="border border-gray-200 rounded-lg p-4">
                                        <h4 class="font-semibold text-gray-900 text-sm mb-1">{{ $category->name }}</h4>
                                        @if($category->description)
                                            <p class="text-xs text-gray-600">{{ Str::limit($category->description, 100) }}</p>
                                        @endif
                                        @if($category->pivot?->assigned_date)
                                            <p class="text-xs text-gray-400 mt-2">Assigned: {{ \Carbon\Carbon::parse($category->pivot->assigned_date)->format('M d, Y') }}</p>
                                        @endif
                                    </div>
                                @endforeach
                            </div>
                        @else
                            <p class="text-gray-500 text-sm">No membership categories assigned.</p>
                        @endif
                    </div>
                </div>
            </div>

            {{-- Sidebar --}}
            <div class="space-y-6">
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                        <h3 class="text-base font-semibold text-gray-900">Quick Links</h3>
                    </div>
                    <div class="p-6 space-y-3">
                        <a href="{{ route('member.dashboard') }}" class="btn-action-view w-full justify-center">
                            <i class="fas fa-tachometer-alt"></i> Dashboard
                        </a>
                        <a href="{{ route('member.contributions') }}" class="btn-action-view w-full justify-center">
                            <i class="fas fa-hand-holding-usd"></i> Contributions
                        </a>
                        <a href="{{ route('member.loans.index') }}" class="btn-action-view w-full justify-center">
                            <i class="fas fa-money-bill-wave"></i> My Loans
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

</x-app-layout>
