<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FootwearNewController extends Controller
{
    public function index(Request $request) // Tambahkan parameter Request
    {
        $allProducts = [
            [
                'name'        => 'Evo SL Core Black',
                'category'    => 'Footwear New',
                'status'      => 'NEW',
                'price'       => '2.650.000',
                'image'       => ['fn-1-a.png', 'fn-1-b.png', 'fn-1-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['40', '41', '42', '43'],
                'upload_date' => '2026-05-01 10:15:30',
                'stock'       => 4,
                'views'       => 2963,
            ],
            [
                'name'        => 'Adidas Spezial Ice Blue',
                'category'    => 'Footwear New',
                'status'      => 'NEW',
                'price'       => '2.350.000',
                'image'       => ['fn-2-a.png', 'fn-2-b.png', 'fn-2-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['39', '40', '44'],
                'upload_date' => '2026-04-29 14:20:15',
                'stock'       => 2,
                'views'       => 1784,
            ],
            [
                'name'        => 'Adidas Samba Classic Black',
                'category'    => 'Footwear New',
                'status'      => 'NEW',
                'price'       => '1.850.000',
                'image'       => ['fn-3-a.png', 'fn-3-b.png', 'fn-3-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['41', '42', '45'],
                'upload_date' => '2026-04-27 09:45:00',
                'stock'       => 5,
                'views'       => 2201,
            ],
            [
                'name'        => 'Adidas Samba Classic White',
                'category'    => 'Footwear New',
                'status'      => 'NEW',
                'price'       => '1.950.000',
                'image'       => ['fn-4-a.png', 'fn-4-b.png', 'fn-4-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['38', '39', '40'],
                'upload_date' => '2026-04-25 16:10:45',
                'stock'       => 1,
                'views'       => 548,
            ],
            [
                'name'        => 'Adidas Spezial Cw Oslo',
                'category'    => 'Footwear New',
                'status'      => 'NEW',
                'price'       => '3.150.000',
                'image'       => ['fn-5-a.png', 'fn-5-b.png', 'fn-5-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['42', '43', '44'],
                'upload_date' => '2026-04-21 11:30:20',
                'stock'       => 3,
                'views'       => 1129,
            ],
            [
                'name'        => 'Adidas Series Bali',
                'category'    => 'Footwear New',
                'status'      => 'NEW',
                'price'       => '3.150.000',
                'image'       => ['fn-6-a.png', 'fn-6-b.png', 'fn-6-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['40', '41', '42'],
                'upload_date' => '2026-04-18 08:05:10',
                'stock'       => 2,
                'views'       => 317,
            ],
        ];

        // Ubah array menjadi Collection agar bisa difilter
        $productCollection = collect($allProducts);

        // Logika pencarian berdasarkan input 'q' di navbar
        if ($request->has('q') && $request->q != '') {
            $keyword = $request->q;
            
            // Mencari nama produk yang mengandung kata kunci (tidak peka huruf besar/kecil)
            $productCollection = $productCollection->filter(function ($item) use ($keyword) {
                return stripos($item['name'], $keyword) !== false;
            });
        }

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

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