<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FootwearSecondController extends Controller
{
    public function index(Request $request) // Tambahkan parameter Request
    {
        $allProducts = [
            [
                'name'        => 'Adidas LA Trainer',
                'category'    => 'Footwear Second',
                'status'      => 'SECOND',
                'price'       => '650.000',
                'image'       => ['fs-1-a.png', 'fs-1-b.png', 'fs-1-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['41'],
                'upload_date' => '2026-05-01 15:30:00',
                'stock'       => 1,
                'views'       => 2741,
            ],
            [
                'name'        => 'Adidas Samba OG',
                'category'    => 'Footwear Second',
                'status'      => 'SECOND',
                'price'       => '550.000',
                'image'       => ['fs-2-a.png', 'fs-2-b.png', 'fs-2-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['40'],
                'upload_date' => '2026-04-30 11:20:15',
                'stock'       => 1,
                'views'       => 1093,
            ],
            [
                'name'        => 'Adidas Spezial Orbit Indigo',
                'category'    => 'Footwear Second',
                'status'      => 'SECOND',
                'price'       => '600.000',
                'image'       => ['fs-3-a.png', 'fs-3-b.png', 'fs-3-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['43'],
                'upload_date' => '2026-04-29 09:45:10',
                'stock'       => 1,
                'views'       => 1876,
            ],
            [
                'name'        => 'Adidas TRX Vintage',
                'category'    => 'Footwear Second',
                'status'      => 'SECOND',
                'price'       => '950.000',
                'image'       => ['fs-4-a.png', 'fs-4-b.png', 'fs-4-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['42'],
                'upload_date' => '2026-04-27 16:15:45',
                'stock'       => 1,
                'views'       => 462,
            ],
            [
                'name'        => 'Adidas Dragon',
                'category'    => 'Footwear Second',
                'status'      => 'SECOND',
                'price'       => '350.000',
                'image'       => ['fs-5-a.png', 'fs-5-b.png', 'fs-5-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['39'],
                'upload_date' => '2026-04-25 14:10:30',
                'stock'       => 1,
                'views'       => 2158,
            ],
            [
                'name'        => 'Adidas Munchen Grey',
                'category'    => 'Footwear Second',
                'status'      => 'SECOND',
                'price'       => '750.000',
                'image'       => ['fs-6-a.png', 'fs-6-b.png', 'fs-6-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['44'],
                'upload_date' => '2026-04-23 08:05:15',
                'stock'       => 1,
                'views'       => 729,
            ],
        ];

        // Bungkus array ke dalam Laravel Collection
        $productCollection = collect($allProducts);

        // Eksekusi filter jika pengguna mengisi search bar
        if ($request->has('q') && $request->q != '') {
            $keyword = $request->q;
            
            // Mencocokkan nama produk dengan kata kunci (tidak case-sensitive)
            $productCollection = $productCollection->filter(function ($item) use ($keyword) {
                return stripos($item['name'], $keyword) !== false;
            });
        }

        // Ambil data hasil akhir dalam bentuk array
        $products = $productCollection->values()->all();

        return view('footwearsecond', compact('products'));
    }
}