<div class="container mx-auto p-4">
  <div class="bg-white rounded-lg drop-shadow-custom-bg-mobile md:drop-shadow-custom-bg p-6">
    <h1 class="text-2xl font-bold md:mb-4">My Listings</h1>

    <!-- Desktop view -->
    <div class="hidden md:block">
      <table class="w-full table-fixed bg-white drop-shadow-inner-faint rounded-lg overflow-hidden">
        <colgroup>
          <col class="w-auto" />
          <!-- Title: restliche Breite -->
          <col class="w-40" />
          <!-- Category -->
          <col class="w-60" />
          <!-- Located in -->
          <col class="w-32" />
          <!-- Price -->
          <col class="w-28" />
          <!-- Internal # -->
          <col class="w-40" />
          <!-- Publication Status -->
          <col class="w-36" />
          <!-- Actions -->
        </colgroup>
        <thead class="bg-gray-100">
          <!-- Header -->
          <tr>
            <th class="py-2 px-4 text-left">Title</th>
            <th class="py-2 px-4 text-left">Category</th>
            <th class="py-2 px-4 text-left">Located in</th>
            <th class="py-2 px-4 text-left">Price</th>
            <th class="py-2 px-4 text-left">Internal #</th>
            <th class="py-2 px-4 text-left">Publication Status</th>
            <th class="py-2 px-4 text-left whitespace-nowrap">Actions</th>
          </tr>

          <!-- Filter row (zwischen Header und Inhalt) -->
          <tr class="bg-white border-b">
            <th class="py-2 px-4">
              <input type="text" class="w-full border rounded px-2 py-1" placeholder="Filter title…" [(ngModel)]="filters.title" (input)="applyFilters()" />
            </th>
            <!-- Category Filter -->
            <th class="py-2 px-4">
              <select class="w-full border rounded px-2 py-1" [(ngModel)]="filters.category" (change)="applyFilters()">
                <option value="">All</option>
                <option value="business">Business</option>
                <option value="commercialProperty">Commercial Property</option>
              </select>
            </th>
            <th class="py-2 px-4">
              <input type="text" class="w-full border rounded px-2 py-1" placeholder="City/County/State…" [(ngModel)]="filters.location" (input)="applyFilters()" />
            </th>
            <th class="py-2 px-4">
              <!-- Preis nicht gefiltert, daher leer -->
            </th>
            <th class="py-2 px-4">
              <input type="text" class="w-full border rounded px-2 py-1" placeholder="Internal #" [(ngModel)]="filters.internalListingNumber" (input)="applyFilters()" />
            </th>
            <th class="py-2 px-4">
              <select class="w-full border rounded px-2 py-1" [(ngModel)]="filters.status" (change)="applyFilters()">
                <option value="">All</option>
                <option value="published">Published</option>
                <option value="draft">Draft</option>
              </select>
            </th>
            <th class="py-2 px-4">
              <button class="text-sm underline" (click)="clearFilters()">Clear</button>
            </th>
          </tr>
        </thead>

        <tbody>
          <tr *ngFor="let listing of myListings" class="border-b">
            <td class="py-2 px-4">{{ listing.title }}</td>
            <td class="py-2 px-4">{{ listing.listingsCategory === 'commercialProperty' ? 'Commercial Property' : 'Business' }}</td>
            <td class="py-2 px-4">{{ listing.location.name ? listing.location.name : listing.location.county ? listing.location.county : this.selectOptions.getState(listing.location.state) }}</td>
            <td class="py-2 px-4">${{ listing.price ? listing.price.toLocaleString() : '' }}</td>
            <td class="py-2 px-4 flex justify-center">
              {{ listing.internalListingNumber ?? '—' }}
            </td>
            <td class="py-2 px-4">
              <span class="{{ listing.draft ? 'bg-yellow-100 text-yellow-800' : 'bg-green-100 text-green-800' }} py-1 px-2 rounded-full text-xs font-medium">
                {{ listing.draft ? 'Draft' : 'Published' }}
              </span>
            </td>
            <td class="py-2 px-4 whitespace-nowrap">
              @if(listing.listingsCategory==='business'){
              <button class="bg-green-500 text-white p-2 rounded-full mr-2" [routerLink]="['/editBusinessListing', listing.id]">
                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                  <path d="M13.586 3.586a2 2 0 112.828 2.828l-.793.793-2.828-2.828.793-.793zM11.379 5.793L3 14.172V17h2.828l8.38-8.379-2.83-2.828z" />
                </svg>
              </button>
              } @if(listing.listingsCategory==='commercialProperty'){
              <button class="bg-green-500 text-white p-2 rounded-full mr-2" [routerLink]="['/editCommercialPropertyListing', listing.id]">
                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                  <path d="M13.586 3.586a2 2 0 112.828 2.828l-.793.793-2.828-2.828.793-.793zM11.379 5.793L3 14.172V17h2.828l8.38-8.379-2.83-2.828z" />
                </svg>
              </button>
              }
              <button class="bg-orange-500 text-white p-2 rounded-full" (click)="confirm(listing)">
                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                  <path
                    fill-rule="evenodd"
                    d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z"
                    clip-rule="evenodd"
                  />
                </svg>
              </button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <!-- Mobile view -->
    <div class="md:hidden">
      <!-- Mobile Filter -->
      <div class="bg-white drop-shadow-inner-faint rounded-lg p-4 mb-4 border">
        <div class="grid grid-cols-1 gap-3">
          <input type="text" class="w-full border rounded px-3 py-2" placeholder="Filter title…" [(ngModel)]="filters.title" (input)="applyFilters()" />
          <input type="text" class="w-full border rounded px-3 py-2" placeholder="City/County/State…" [(ngModel)]="filters.location" (input)="applyFilters()" />
          <input type="text" class="w-full border rounded px-3 py-2" placeholder="Internal #" [(ngModel)]="filters.internalListingNumber" (input)="applyFilters()" />
          <select class="w-full border rounded px-3 py-2" [(ngModel)]="filters.status" (change)="applyFilters()">
            <option value="">All</option>
            <option value="published">Published</option>
            <option value="draft">Draft</option>
          </select>
          <button class="text-sm underline justify-self-start" (click)="clearFilters()">Clear</button>
        </div>
      </div>

      <div *ngFor="let listing of myListings" class="bg-white drop-shadow-inner-faint rounded-lg p-4 mb-4">
        <h2 class="text-xl font-semibold mb-2">{{ listing.title }}</h2>
        <p class="text-gray-600 mb-2">Category: {{ listing.listingsCategory === 'commercialProperty' ? 'Commercial Property' : 'Business' }}</p>
        <p class="text-gray-600 mb-2">Located in: {{ listing.location?.name ? listing.location.name : listing.location?.county }} - {{ listing.location?.state }}</p>
        <p class="text-gray-600 mb-2">Price: ${{ listing.price.toLocaleString() }}</p>
        <p class="text-gray-600 mb-2">Internal #: {{ listing.internalListingNumber ?? '—' }}</p>
        <div class="flex items-center gap-2 mb-2">
          <span class="text-gray-600">Publication Status:</span>
          <span class="{{ listing.draft ? 'bg-yellow-100 text-yellow-800' : 'bg-green-100 text-green-800' }} py-1 px-2 rounded-full text-xs font-medium">
            {{ listing.draft ? 'Draft' : 'Published' }}
          </span>
        </div>
        <div class="flex justify-start">
          @if(listing.listingsCategory==='business'){
          <button class="bg-green-500 text-white p-2 rounded-full mr-2" [routerLink]="['/editBusinessListing', listing.id]">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
              <path d="M13.586 3.586a2 2 0 112.828 2.828l-.793.793-2.828-2.828.793-.793zM11.379 5.793L3 14.172V17h2.828l8.38-8.379-2.83-2.828z" />
            </svg>
          </button>
          } @if(listing.listingsCategory==='commercialProperty'){
          <button class="bg-green-500 text-white p-2 rounded-full mr-2" [routerLink]="['/editCommercialPropertyListing', listing.id]">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
              <path d="M13.586 3.586a2 2 0 112.828 2.828l-.793.793-2.828-2.828.793-.793zM11.379 5.793L3 14.172V17h2.828l8.38-8.379-2.83-2.828z" />
            </svg>
          </button>
          }
          <button class="bg-orange-500 text-white p-2 rounded-full" (click)="confirm(listing)">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
              <path
                fill-rule="evenodd"
                d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z"
                clip-rule="evenodd"
              />
            </svg>
          </button>
        </div>
      </div>
    </div>
  </div>
</div>

<app-confirmation></app-confirmation>
