@extends('layouts.admin_layout')

@push('styles')
<style>
  /* ===============================
       Base (mobile first)
    ================================ */
    
    /* Mobile pagination is visible by default */
    .pagination-mobile {
      display: flex;
    }
    
    /* Desktop pagination hidden by default */
    .pagination-desktop {
      display: none;
    }
    
    
    /* ===============================
       sm breakpoint (≥ 640px)
       Tailwind: sm:items-center sm:justify-between
    ================================ */
    
    @media (min-width: 640px) {
      .pagination-desktop {
        align-items: center;
        justify-content: space-between;
      }
    }
    
    
    /* ===============================
       md breakpoint (≥ 768px)
       Tailwind: sm:hidden md:flex md:flex-1
    ================================ */
    
    @media (min-width: 768px) {
      /* Hide mobile pagination */
      .pagination-mobile {
        display: none;
      }
    
      /* Show desktop pagination */
      .pagination-desktop {
        display: flex;
        flex: 1 1 0%;
      }
    }
    
</style>
@endpush


@section('content')

<main class="h-full overflow-y-auto">
  <div class="container px-6 mx-auto grid">
    <h2 class="my-6 text-2xl font-semibold text-gray-700 dark:text-gray-200">
      <span class="text-blue-500">Tra</span>des
    </h2>

    <!-- CTA -->
    <a
      class="flex items-center bg-blue-600 justify-between p-4 mb-8 text-sm font-semibold shadow-md focus:outline-none focus:shadow-outline-green rounded-lg hover:bg-blue-700 transition-colors"
      href="{{ url('/') }}" style="color: #fff;">
      <div class="flex items-center">
        <svg class="w-5 h-5 mr-2" fill="currentColor" viewBox="0 0 20 20">
          <path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"></path>
        </svg>
        <span>Go to main website</span>
      </div>
      <span>View &RightArrow;</span>
    </a>

    <!-- Advanced Search Section -->
    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6 mb-8">
      <div class="flex items-center justify-between mb-4">
        <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200">
          <svg class="w-5 h-5 inline-block mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
          </svg>
          Advanced Search & Filters
        </h3>
        <button 
          type="button" 
          onclick="toggleAdvancedSearch()" 
          class="text-gray-300 dark:text-gray-300 hover:text-blue-800 dark:hover:text-blue-300 text-sm font-medium focus:outline-none"
          id="toggleBtn">
          <span id="toggleText">Show Filters</span>
          <svg id="toggleIcon" class="w-4 h-4 inline-block ml-1 transform transition-transform" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
          </svg>
        </button>
      </div>

      <form method="GET" action="{{ route('admin.investments') }}" id="searchForm">
        <!-- Quick Search Bar (Always Visible) -->
        <div class="mb-4">
          <div class="relative">
            <input 
              type="text" 
              name="search" 
              value="{{ $searchTerm ?? '' }}"
              placeholder="Search by name, email, or username..." 
              class="block w-full pl-8 pr-3 py-2 text-sm border border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-300 rounded-lg focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500">
            <div class="absolute inset-y-0 left-0 flex items-center pointer-events-none" style="padding-left: 8px;">
              <svg class="h-5 w-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
              </svg>
            </div>
          </div>
        </div>

        <!-- Advanced Filters (Collapsible) -->
        <div id="advancedFilters" class="hidden">
          <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-5 mb-4" style="gap: 1rem;">
            
            <!-- Bot Model Filter -->
            <div>
              <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                Bot Model
              </label>
              <select
                name="plan"
                class="block w-full text-sm dark:text-gray-300 border border-gray-300 dark:border-gray-600 dark:bg-gray-700 rounded-lg focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500 py-2 px-3">
                <option value="All" {{ request('plan') == 'All' || !request('plan') ? 'selected' : '' }}>All Models</option>
                @foreach($plans as $plan)
                  <option value="{{ $plan->id }}" {{ request('plan') == $plan->id ? 'selected' : '' }}>
                    {{ $plan->name }}
                  </option>
                @endforeach
              </select>
            </div>

            <!-- Status Filter -->
            <div>
              <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                Status
              </label>
              <select
                name="status"
                class="block w-full text-sm dark:text-gray-300 border border-gray-300 dark:border-gray-600 dark:bg-gray-700 rounded-lg focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500 py-2 px-3">
                <option value="All" {{ request('status') == 'All' || !request('status') ? 'selected' : '' }}>All Status</option>
                <option value="Pending" {{ request('status') == 'Pending' ? 'selected' : '' }}>Pending</option>
                <option value="Ongoing" {{ request('status') == 'Ongoing' ? 'selected' : '' }}>Ongoing</option>
                <option value="Completed" {{ request('status') == 'Completed' ? 'selected' : '' }}>Completed</option>
              </select>
            </div>

            <!-- Date From Filter -->
            <div>
              <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                Date From
              </label>
              <input 
                type="date" 
                name="date_from" 
                value="{{ request('date_from') }}"
                class="block w-full text-sm dark:text-gray-300 border border-gray-300 dark:border-gray-600 dark:bg-gray-700 rounded-lg focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500 py-2 px-3">
            </div>

            <!-- Date To Filter -->
            <div>
              <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                Date To
              </label>
              <input 
                type="date" 
                name="date_to" 
                value="{{ request('date_to') }}"
                class="block w-full text-sm dark:text-gray-300 border border-gray-300 dark:border-gray-600 dark:bg-gray-700 rounded-lg focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500 py-2 px-3">
            </div>

            <!-- Amount Min Filter -->
            <div>
              <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                Min Amount (USDT)
              </label>
              <input 
                type="number" 
                name="amount_min" 
                value="{{ request('amount_min') }}"
                placeholder="0.00"
                step="0.01"
                class="block w-full text-sm dark:text-gray-300 border border-gray-300 dark:border-gray-600 dark:bg-gray-700 rounded-lg focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500 py-2 px-3">
            </div>

            <!-- Amount Max Filter -->
            <div>
              <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                Max Amount (USDT)
              </label>
              <input 
                type="number" 
                name="amount_max" 
                value="{{ request('amount_max') }}"
                placeholder="0.00"
                step="0.01"
                class="block w-full text-sm dark:text-gray-300 border border-gray-300 dark:border-gray-600 dark:bg-gray-700 rounded-lg focus:border-blue-500 focus:outline-none focus:ring-2 focus:ring-blue-500 py-2 px-3">
            </div>
          </div>
        </div>

        <!-- Action Buttons -->
        <div class="flex items-center justify-between pt-4 border-t border-gray-200 dark:border-gray-700">
          <div class="text-sm text-gray-600 dark:text-gray-400">
            Showing {{ $investments->firstItem() ?? 0 }} to {{ $investments->lastItem() ?? 0 }} of {{ $investments->total() }} results
          </div>
          <div class="flex" style="gap: 0.5rem">
            <a 
              href="{{ route('admin.investments') }}" 
              class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-blue-500">
              Clear Filters
            </a>
            <button 
              type="submit" 
              class="px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500">
              Apply Filters
            </button>
          </div>
        </div>
      </form>
    </div>

    <!-- Results Summary -->
    <div class="flex items-center justify-between mb-4">
      <p class="text-lg font-semibold text-gray-700 dark:text-gray-200">
        {{ $planName }} @if($planName == 'All') Models @endif | {{ $statusName }} Trades
      </p>
      <div class="text-sm text-gray-600 dark:text-gray-400">
        Total: <span class="font-semibold">{{ $investments->total() }}</span> trades
      </div>
    </div>

    <!-- Trades Table -->
    <div class="w-full overflow-hidden rounded-lg shadow-md">
      <div class="w-full overflow-x-auto">
        <table class="w-full whitespace-no-wrap">
          <thead>
            <tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b dark:border-gray-700 bg-gray-50 dark:text-gray-400 dark:bg-gray-800">
              <th class="px-4 py-3">User</th>
              <th class="px-4 py-3">Model</th>
              <th class="px-4 py-3">Entry Capital</th>
              <th class="px-4 py-3">Current Capital</th>
              <th class="px-4 py-3">AT Profit</th>
              <th class="px-4 py-3">Status</th>
              <th class="px-4 py-3">Date Started</th>
              <th class="px-4 py-3">End Date</th>
            </tr>
          </thead>
          <tbody class="bg-white divide-y dark:divide-gray-700 dark:bg-gray-800">
            @forelse($investments as $investment)
            <tr class="text-gray-700 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors">
              <td class="px-4 py-3">
                <div class="flex items-center text-sm">
                  <!-- Avatar with inset shadow -->
                  <div class="relative hidden w-8 h-8 mr-3 rounded-full md:block">
                    <img
                      class="object-cover w-full h-full rounded-full"
                      src="{{ $investment->user?->avatar ? asset('storage/images/'.$investment->user->avatar) : asset('images/toy-avatar.png') }}"
                      alt="{{ $investment->user?->first_name }}"
                      loading="lazy" />
                    <div class="absolute inset-0 rounded-full shadow-inner" aria-hidden="true"></div>
                  </div>
                  <div>
                    <a href="{{ route('admin.investment.show', $investment->id) }}" class="hover:text-blue-600 dark:hover:text-blue-400">
                      <p class="font-semibold">{{ $investment->user?->first_name }} {{ $investment->user?->last_name }}</p>
                      <p class="text-xs text-gray-600 dark:text-gray-400">
                        {{ $investment->user?->email }} - [{{ $investment->user?->username }}]
                      </p>
                    </a>
                  </div>
                </div>
              </td>
              <td class="px-3 py-3 text-sm">
                <span
                  style="
                    padding: 0.25rem 0.5rem;
                    font-size: 0.75rem;
                    line-height: 1rem;
                    font-weight: 500;
                    background-color: #dbeafe;
                    color: #1e40af;
                    border-radius: 9999px;
                  "
                >
                  {{ $investment->plan->name }}
                </span>
              </td>
              <td class="px-4 py-3 text-sm font-medium">
                USDT {{ number_format($investment->amount, 2) }}
              </td>
              <td class="px-4 py-3 text-sm font-medium">
                USDT {{ number_format($investment->balance, 2) }}
              </td>
              <td class="px-4 py-3 text-sm">
                <p class="font-medium text-green-600 dark:text-green-400">
                  $ {{ number_format($investment->pfit, 2) }}
                </p>
                <p class="text-xs text-gray-500 dark:text-gray-400">
                  <i>$ {{ number_format($investment->f_pfit, 2) }}</i>
                </p>
              </td>
              <td class="px-4 py-3 text-xs">
                @if($investment->status == 1)
                  <span class="px-2 py-1 font-semibold leading-tight text-red-700 bg-red-100 rounded-full dark:bg-red-700 dark:text-red-100">
                    Pending
                  </span>
                @elseif($investment->status == 2)
                  <span class="px-2 py-1 font-semibold leading-tight text-orange-700 bg-orange-100 rounded-full dark:bg-orange-100 dark:text-orange-500">
                    Ongoing
                  </span>
                @elseif($investment->status == 3)
                  <span class="px-2 py-1 font-semibold leading-tight text-green-700 bg-green-100 rounded-full dark:bg-green-700 dark:text-green-100">
                    Completed
                  </span>
                @endif
              </td>
              <td class="px-4 py-3 text-sm">
                {{ date('M j, Y', strtotime($investment->buy_date)) }}
              </td>
              <td class="px-4 py-3 text-sm">
                {{ date('M j, Y', strtotime($investment->end_date)) }}
              </td>
            </tr>
            @empty
            <tr>
              <td colspan="8" class="px-4 py-12 text-center">
                <div class="flex flex-col items-center justify-center text-gray-500 dark:text-gray-400">
                  <svg class="w-16 h-16 mb-4 text-gray-300 dark:text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 13V6a2 2 0 00-2-2H6a2 2 0 00-2 2v7m16 0v5a2 2 0 01-2 2H6a2 2 0 01-2-2v-5m16 0h-2.586a1 1 0 00-.707.293l-2.414 2.414a1 1 0 01-.707.293h-3.172a1 1 0 01-.707-.293l-2.414-2.414A1 1 0 006.586 13H4"></path>
                  </svg>
                  <p class="text-lg font-medium">No trades found</p>
                  <p class="text-sm mt-1">Try adjusting your filters or search criteria</p>
                </div>
              </td>
            </tr>
            @endforelse
          </tbody>
        </table>
      </div>

      <!-- Pagination -->
      @if($investments->hasPages())
      <div class="px-4 py-3 border-t dark:border-gray-700 bg-gray-50 dark:bg-gray-800">
        <div class="flex items-center justify-between">
          <div class="flex-1 flex justify-between pagination-mobile">
            @if ($investments->onFirstPage())
              <span class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-500 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 cursor-default rounded-md">
                Previous
              </span>
            @else
              <a href="{{ $investments->previousPageUrl() }}" class="relative inline-flex items-center px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md hover:bg-gray-50 dark:hover:bg-gray-600">
                Previous
              </a>
            @endif

            @if ($investments->hasMorePages())
              <a href="{{ $investments->nextPageUrl() }}" class="relative inline-flex items-center px-4 py-2 ml-3 text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md hover:bg-gray-50 dark:hover:bg-gray-600">
                Next
              </a>
            @else
              <span class="relative inline-flex items-center px-4 py-2 ml-3 text-sm font-medium text-gray-500 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 cursor-default rounded-md">
                Next
              </span>
            @endif
          </div>

          <div class="pagination-desktop">
            <div>
              <p class="text-sm text-gray-700 dark:text-gray-400">
                Showing
                <span class="font-medium">{{ $investments->firstItem() }}</span>
                to
                <span class="font-medium">{{ $investments->lastItem() }}</span>
                of
                <span class="font-medium">{{ $investments->total() }}</span>
                results
              </p>
            </div>
            <div>
              <nav class="relative z-0 inline-flex rounded-md shadow-sm -space-x-px" aria-label="Pagination">
                {{-- Previous Page Link --}}
                @if ($investments->onFirstPage())
                  <span class="relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-sm font-medium text-gray-500 cursor-default">
                    <svg class="h-5 w-5" fill="currentColor" viewBox="0 0 20 20">
                      <path fill-rule="evenodd" d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd" />
                    </svg>
                  </span>
                @else
                  <a href="{{ $investments->previousPageUrl() }}" class="relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-sm font-medium text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-600">
                    <svg class="h-5 w-5" fill="currentColor" viewBox="0 0 20 20">
                      <path fill-rule="evenodd" d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd" />
                    </svg>
                  </a>
                @endif

                {{-- Pagination Elements --}}
                @foreach ($investments->links()->elements[0] as $page => $url)
                  @if ($page == $investments->currentPage())
                    <span class="relative inline-flex items-center px-4 py-2 border border-blue-500 dark:border-blue-600 bg-blue-50 dark:bg-blue-900 text-sm font-medium text-blue-600 dark:text-blue-200">
                      {{ $page }}
                    </span>
                  @else
                    <a href="{{ $url }}" class="relative inline-flex items-center px-4 py-2 border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-sm font-medium text-gray-700 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-600">
                      {{ $page }}
                    </a>
                  @endif
                @endforeach

                {{-- Next Page Link --}}
                @if ($investments->hasMorePages())
                  <a href="{{ $investments->nextPageUrl() }}" class="relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-sm font-medium text-gray-500 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-600">
                    <svg class="h-5 w-5" fill="currentColor" viewBox="0 0 20 20">
                      <path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd" />
                    </svg>
                  </a>
                @else
                  <span class="relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700 text-sm font-medium text-gray-500 cursor-default">
                    <svg class="h-5 w-5" fill="currentColor" viewBox="0 0 20 20">
                      <path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd" />
                    </svg>
                  </span>
                @endif
              </nav>
            </div>
          </div>
        </div>
      </div>
      @endif
    </div>
  </div>
</main>

<script>
function toggleAdvancedSearch() {
  const filters = document.getElementById('advancedFilters');
  const toggleText = document.getElementById('toggleText');
  const toggleIcon = document.getElementById('toggleIcon');
  
  if (filters.classList.contains('hidden')) {
    filters.classList.remove('hidden');
    toggleText.textContent = 'Hide Filters';
    toggleIcon.classList.add('rotate-180');
  } else {
    filters.classList.add('hidden');
    toggleText.textContent = 'Show Filters';
    toggleIcon.classList.remove('rotate-180');
  }
}

// Auto-expand filters if any advanced filter is active
document.addEventListener('DOMContentLoaded', function() {
  const hasActiveFilters = {{ 
    request()->filled('date_from') || 
    request()->filled('date_to') || 
    request()->filled('amount_min') || 
    request()->filled('amount_max') ? 'true' : 'false' 
  }};
  
  if (hasActiveFilters) {
    toggleAdvancedSearch();
  }
});
</script>

@endsection