@extends('layouts.master')

@section('title', 'Edit Client - ')

@section('content')
<style>
:root{--sky:#0ea5e9;--sky-deep:#0369a1;--sky-ghost:rgba(14,165,233,.07);--sky-border:rgba(14,165,233,.2);--ink-mid:#3f4a5a;--white:#fff;--off:#f7fbff;--surface:#eef6fd;--border:#daeaf8;--danger:#ef4444;--danger-bg:rgba(239,68,68,.08);--danger-border:rgba(239,68,68,.2);--amber:#f59e0b;--amber-bg:rgba(245,158,11,.08);--amber-border:rgba(245,158,11,.22);--fd:'Cormorant Garamond',Georgia,serif;--fb:'DM Sans',system-ui,sans-serif;--fm:'DM Mono',monospace}
.main-content{background:var(--off);font-family:var(--fb)}
.main-content .container,.main-content .tx-page,.main-content .page-wrap,.main-content .content-wrapper,.main-content .wrap{background:var(--white);border:1px solid var(--border);border-radius:16px;box-shadow:0 8px 24px rgba(14,165,233,.1)}
.main-content h1,.main-content h2,.main-content h3{font-family:var(--fd);color:var(--sky-deep)}
.main-content .btn,.main-content .back-btn,.main-content .option-btn,.main-content .submit-btn,.main-content .refresh-btn,.main-content .action-btn,.main-content .act-btn,.main-content .link{border-radius:10px!important;font-family:var(--fb)!important;font-weight:600!important}
.main-content .btn-secondary,.main-content .back-btn,.main-content .refresh-btn,.main-content .btn-muted,.main-content .btn-ghost{background:#fff!important;color:var(--ink-mid)!important;border:1px solid var(--sky-border)!important}
.main-content .btn-primary,.main-content .option-btn,.main-content .submit-btn{background:linear-gradient(135deg,var(--sky-deep),var(--sky))!important;color:#fff!important;border:1px solid var(--sky-deep)!important}
.main-content table,.main-content .table{border-color:var(--border)!important}
.main-content thead,.main-content thead tr,.main-content th,.main-content .table thead th,.main-content .table-head-row{background:var(--surface)!important;color:var(--ink-mid)!important;border-color:var(--border)!important}
.main-content tbody tr:hover,.main-content .tx-row:hover{background:var(--sky-ghost)!important}
.main-content input,.main-content select,.main-content textarea{border:1px solid var(--border)!important;border-radius:10px!important;font-family:var(--fb)!important}
.main-content input:focus,.main-content select:focus,.main-content textarea:focus{border-color:var(--sky)!important;box-shadow:0 0 0 3px rgba(14,165,233,.12)!important}
.main-content .rate-badge,.main-content .badge,.main-content .status-badge{font-family:var(--fm)}
.main-content .actions,.main-content .cell-actions{display:flex;gap:7px;align-items:center;flex-wrap:wrap}
.main-content a[href*='/edit'],.main-content .act-edit,.main-content .btn-warning{background:var(--amber-bg)!important;color:#92400e!important;border:1px solid var(--amber-border)!important}
.main-content .act-delete,.main-content form button[type='submit'].btn-danger,.main-content form button[onclick*='delete']{background:var(--danger-bg)!important;color:#991b1b!important;border:1px solid var(--danger-border)!important}
.main-content a[href*='/show'],.main-content .act-view{background:var(--sky-ghost)!important;color:var(--sky-deep)!important;border:1px solid var(--sky-border)!important}
</style>
<style>
    .container {
        max-width: 1200px;
        margin: 40px auto;
        background-color: #f8fafc;
        padding: 30px;
        border-radius: 12px;
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    }

    h1 {
        text-align: center;
        color: #1e40af;
        margin-bottom: 30px;
        font-size: 2.5rem;
        font-weight: 700;
    }

    .form-section {
        background: white;
        border-radius: 8px;
        padding: 25px;
        margin-bottom: 25px;
        border: 1px solid #e2e8f0;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    }

    .section-title {
        color: #2d3748;
        font-size: 1.5rem;
        font-weight: 600;
        margin-bottom: 20px;
        border-bottom: 2px solid #e2e8f0;
        padding-bottom: 10px;
    }

    .form-group {
        margin-bottom: 20px;
    }

    .form-row {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
        gap: 20px;
    }

    label {
        display: block;
        margin-bottom: 8px;
        font-weight: 600;
        color: #4a5568;
        font-size: 0.95rem;
    }

    input, select, textarea {
        width: 100%;
        padding: 12px 16px;
        border: 2px solid #e2e8f0;
        border-radius: 6px;
        font-size: 1rem;
        transition: all 0.3s ease;
        background-color: #ffffff;
    }

    input:focus, select:focus, textarea:focus {
        outline: none;
        border-color: #3b82f6;
        box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
    }

    textarea {
        resize: vertical;
        min-height: 100px;
    }

    .required::after {
        content: ' *';
        color: #e53e3e;
    }

    .submit-btn {
        background: linear-gradient(135deg, #3b82f6, #1d4ed8);
        color: white;
        padding: 15px 40px;
        border: none;
        border-radius: 8px;
        font-size: 1.1rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        box-shadow: 0 4px 6px rgba(59, 130, 246, 0.3);
    }

    .submit-btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 12px rgba(59, 130, 246, 0.4);
    }

    .submit-btn:active {
        transform: translateY(0);
    }

    .refresh-btn {
        background: linear-gradient(135deg, #6b7280, #4b5563);
        color: white;
        padding: 15px 40px;
        border: none;
        border-radius: 8px;
        font-size: 1.1rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        box-shadow: 0 4px 6px rgba(107, 114, 128, 0.3);
    }

    .refresh-btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 12px rgba(107, 114, 128, 0.4);
    }

    .refresh-btn:active {
        transform: translateY(0);
    }

    .lawyer-dropdown {
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        background: white;
        border: 1px solid #e2e8f0;
        border-radius: 6px;
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        max-height: 200px;
        overflow-y: auto;
        z-index: 1000;
        display: none;
    }

    .lawyer-item {
        padding: 10px 16px;
        cursor: pointer;
        border-bottom: 1px solid #f1f5f9;
        transition: background-color 0.2s ease;
    }

    .lawyer-item:hover {
        background-color: #f8fafc;
    }

    .lawyer-item:last-child {
        border-bottom: none;
    }

    .lawyer-search {
        position: relative;
    }

    @media (max-width: 768px) {
        .container {
            margin: 20px;
            padding: 20px;
        }

        .form-row {
            grid-template-columns: 1fr;
        }

        h1 {
            font-size: 2rem;
        }
    }
</style>

<div class="container">
    <h1>Edit Client</h1>

    @if(session('success'))
        <div style="background-color: #d1fae5; color: #065f46; padding: 15px; border-radius: 6px; margin-bottom: 20px; border: 1px solid #a7f3d0;">
            {{ session('success') }}
        </div>
    @endif

    @if($errors->any())
        <div style="background-color: #fee2e2; color: #991b1b; padding: 15px; border-radius: 6px; margin-bottom: 20px; border: 1px solid #fecaca;">
            <ul style="margin: 0; padding-left: 20px;">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <form id="clientForm" action="{{ route('clients.update', $client->id) }}" method="POST" enctype="multipart/form-data" novalidate>
        @csrf
        @method('PUT')
        <!-- Core Identifiers -->
        <div class="form-section">
            <h2 class="section-title">Core Identifiers</h2>
            <div class="form-row">
                <div class="form-group">
                    <label for="client_id" class="required">Client ID</label>
                    <input type="text" id="client_id" name="client_id" value="{{ $client->client_id }}" style="background-color: #ffffff;" required>
                </div>
                <div class="form-group">
                    <label for="first_name" class="required">First Name</label>
                    <input type="text" id="first_name" name="first_name" value="{{ $client->first_name }}" required>
                </div>
                <div class="form-group">
                    <label for="last_name" class="required">Last Name</label>
                    <input type="text" id="last_name" name="last_name" value="{{ $client->last_name }}" required>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label for="national_id_passport">National ID / Passport Number</label>
                    <input type="text" id="national_id_passport" name="national_id_passport" value="{{ $client->national_id }}">
                </div>
                <div class="form-group">
                    <label for="date_of_birth">Date of Birth</label>
                    <input type="date" id="date_of_birth" name="date_of_birth" value="{{ $client->date_of_birth }}">
                </div>
                <div class="form-group">
                    <label for="gender">Gender</label>
                    <select id="gender" name="gender">
                        <option value="">Select Gender</option>
                        <option value="Male" {{ $client->gender == 'Male' ? 'selected' : '' }}>Male</option>
                        <option value="Female" {{ $client->gender == 'Female' ? 'selected' : '' }}>Female</option>
                        <option value="Other" {{ $client->gender == 'Other' ? 'selected' : '' }}>Other</option>
                    </select>
                </div>
            </div>
        </div>

        <!-- Contact Information -->
        <div class="form-section">
            <h2 class="section-title">Contact Information</h2>
            <div class="form-row">
                <div class="form-group">
                    <label for="phone_primary">Phone Number (Primary)</label>
                    <input type="tel" id="phone_primary" name="phone_primary" value="{{ $client->phone_primary }}" placeholder="+263 123 456 789">
                </div>
                <div class="form-group">
                    <label for="email">Email Address</label>
                    <input type="email" id="email" name="email" value="{{ $client->email }}" placeholder="client@example.com">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label for="address_street">Street Address</label>
                    <input type="text" id="address_street" name="address_street" value="{{ $client->street_address }}" placeholder="123 Main Street">
                </div>
                <div class="form-group">
                    <label for="address_city">City</label>
                    <input type="text" id="address_city" name="address_city" value="{{ $client->city }}" placeholder="Harare">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label for="address_state">State / Province</label>
                    <input type="text" id="address_state" name="address_state" value="{{ $client->state_province }}" placeholder="Mashonaland East">
                </div>
                <div class="form-group">
                    <label for="address_country">Country</label>
                    <input type="text" id="address_country" name="address_country" value="{{ $client->country }}" placeholder="Zimbabwe">
                </div>
            </div>
        </div>

        <!-- Professional / Legal Context -->
        <div class="form-section">
            <h2 class="section-title">Professional / Legal Context</h2>
            <div class="form-row">
                <div class="form-group">
                    <label for="occupation_company"> Employer name</label>
                    <input type="text" id="occupation_company" name="occupation_company" value="{{ $client->employer_name }}" placeholder="Software Engineer or ABC Corporation">
                </div>
                <div class="form-group">
                    <label for="position_role">Position / Role</label>
                    <input type="text" id="position_role" name="position_role" value="{{ $client->position_role }}" placeholder="Senior Developer or CEO">
                </div>
                <div class="form-group">
                    <label for="client_type">Client Type</label>
                    <select id="client_type" name="client_type">
                        <option value="">Select Client Type</option>
                        <option value="IND" {{ $client->client_type == 'IND' ? 'selected' : '' }}>IND – Individual clients</option>
                        <option value="CORP" {{ $client->client_type == 'CORP' ? 'selected' : '' }}>CORP – Corporate clients</option>
                        <option value="GOVT" {{ $client->client_type == 'GOVT' ? 'selected' : '' }}>GOVT – Government clients</option>
                        <option value="NGO" {{ $client->client_type == 'NGO' ? 'selected' : '' }}>NGO – Non-Governmental Organizations</option>
                        <option value="SME" {{ $client->client_type == 'SME' ? 'selected' : '' }}>SME – Small and Medium Enterprises</option>
                        <option value="FIN" {{ $client->client_type == 'FIN' ? 'selected' : '' }}>FIN – Financial institutions / banks</option>
                        <option value="REAL" {{ $client->client_type == 'REAL' ? 'selected' : '' }}>REAL – Real estate clients</option>
                        <option value="LEG" {{ $client->client_type == 'LEG' ? 'selected' : '' }}>LEG – Legal entity clients (trusts, estates)</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="client_category">Client Category</label>
                    <select id="client_category" name="client_category">
                        <option value="">Select Client Category</option>
                        @foreach($categories ?? [] as $category)
                            <option value="{{ $category->category_code }}" {{ $client->client_category == $category->category_code ? 'selected' : '' }}>{{ $category->category_description }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
        </div>

        <!-- Engagement Details -->
        <div class="form-section">
            <h2 class="section-title">Engagement Details</h2>
            <div class="form-row">
                <div class="form-group">
                    <label for="date_onboarding">Date of Onboarding</label>
                    <input type="date" id="date_onboarding" name="date_onboarding" value="{{ $client->date_onboarded }}">
                </div>
                <div class="form-group">
                    <label for="assigned_lawyer_case_manager_display">Assigned Lawyer / Case Manager</label>
                    <div class="lawyer-search">
                        <input type="text" id="assigned_lawyer_case_manager_display" name="assigned_lawyer_case_manager_display" value="{{ $assignedLawyerName ?? $client->assigned_lawyer }}" placeholder="Search for lawyer..." autocomplete="off">
                        <input type="hidden" id="assigned_lawyer_case_manager" name="assigned_lawyer_case_manager" value="{{ $client->assigned_lawyer }}">
                        <div class="lawyer-dropdown" id="assigned_lawyer_dropdown"></div>
                    </div>
                </div>
                <div class="form-group">
                    <label for="referred_lawyer_display">Referred Lawyer</label>
                    <div class="lawyer-search">
                        <input type="text" id="referred_lawyer_display" name="referred_lawyer_display" value="{{ $referredLawyerName ?? $client->referred_lawyer }}" placeholder="Search for lawyer..." autocomplete="off">
                        <input type="hidden" id="referred_lawyer_hidden" name="referred_lawyer" value="{{ $client->referred_lawyer }}">
                        <div class="lawyer-dropdown" id="referred_lawyer_dropdown"></div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label for="notes_remarks">Notes / Remarks</label>
                <textarea id="notes_remarks" name="notes_remarks" placeholder="Additional notes about the client...">{{ $client->notes }}</textarea>
            </div>
        </div>

        <div style="display: flex; justify-content: center; gap: 20px; margin-top: 30px;">
            <button type="submit" class="submit-btn">Update Client</button>
            <button type="button" class="refresh-btn" onclick="window.location.href='{{ route('modules.edit.add-clients') }}'">Back</button>
        </div>
    </form>
</div>

<script>
    // Lawyer search functionality
    document.addEventListener('DOMContentLoaded', function() {
        const lawyerInputs = document.querySelectorAll('.lawyer-search input[type="text"]');
        const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '{{ csrf_token() }}';

        lawyerInputs.forEach(input => {
            const hiddenInput = input.nextElementSibling;
            const dropdown = input.nextElementSibling.nextElementSibling;
            let timeout;

            input.addEventListener('input', function() {
                clearTimeout(timeout);
                const query = this.value.trim();

                if (query.length < 2) {
                    dropdown.style.display = 'none';
                    return;
                }

                timeout = setTimeout(() => {
                    fetch(`/modules/api/lawyers/search?q=${encodeURIComponent(query)}`, {
                        method: 'GET',
                        headers: {
                            'X-CSRF-TOKEN': csrfToken,
                            'Accept': 'application/json'
                        }
                    })
                    .then(response => response.json())
                    .then(data => {
                        dropdown.innerHTML = '';

                        if (data.length > 0) {
                            data.forEach(lawyer => {
                                const item = document.createElement('div');
                                item.className = 'lawyer-item';
                                item.textContent = lawyer.full_name;
                                item.dataset.id = lawyer.lawyer_id;
                                item.addEventListener('click', function() {
                                    input.value = lawyer.full_name;
                                    hiddenInput.value = lawyer.lawyer_id;
                                    dropdown.style.display = 'none';
                                });
                                dropdown.appendChild(item);
                            });
                            dropdown.style.display = 'block';
                        } else {
                            dropdown.style.display = 'none';
                        }
                    })
                    .catch(error => {
                        console.error('Error fetching lawyers:', error);
                        dropdown.style.display = 'none';
                    });
                }, 300);
            });

            // Hide dropdown when clicking outside
            document.addEventListener('click', function(e) {
                if (!input.contains(e.target) && !dropdown.contains(e.target)) {
                    dropdown.style.display = 'none';
                }
            });
        });
    });
</script>
@endsection


