<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ApparelSecondController extends Controller
{
    public function index(Request $request) // Tambahkan Request $request
    {
        $allProducts = [
            [
                'name'        => 'Adidas Ringertee Camo',
                'category'    => 'Apparel Second',
                'status'      => 'SECOND',
                'price'       => '150.000',
                'image'       => ['as-1-a.png', 'as-1-b.png', 'as-1-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['M'],
                'upload_date' => '2026-04-30 08:15:22',
                'stock'       => 1,
                'views'       => 1847,
            ],
            [
                'name'        => 'Adidas Big Logo',
                'category'    => 'Apparel Second',
                'status'      => 'SECOND',
                'price'       => '120.000',
                'image'       => ['as-2-a.png', 'as-2-b.png', 'as-2-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['Xl'],
                'upload_date' => '2026-04-28 14:30:05',
                'stock'       => 1,
                'views'       => 634,
            ],
            [
                'name'        => 'Jaket TNF',
                'category'    => 'Apparel Second',
                'status'      => 'SECOND',
                'price'       => '450.000',
                'image'       => ['as-3-a.png', 'as-3-b.png', 'as-3-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['L'],
                'upload_date' => '2026-04-26 10:45:10',
                'stock'       => 1,
                'views'       => 2315,
            ],
            [
                'name'        => 'Stone Island Anora Multi',
                'category'    => 'Apparel Second',
                'status'      => 'SECOND',
                'price'       => '600.000',
                'image'       => ['as-4-a.png', 'as-4-b.png', 'as-4-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['Xxl'],
                'upload_date' => '2026-04-22 19:20:45',
                'stock'       => 1,
                'views'       => 981,
            ],
            [
                'name'        => 'Tracktop Adidas Grey',
                'category'    => 'Apparel Second',
                'status'      => 'SECOND',
                'price'       => '250.000',
                'image'       => ['as-5-a.png', 'as-5-b.png', 'as-5-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['S'],
                'upload_date' => '2026-04-15 16:10:30',
                'stock'       => 1,
                'views'       => 1452,
            ],
            [
                'name'        => 'Adidas Logo Camo',
                'category'    => 'Apparel Second',
                'status'      => 'SECOND',
                'price'       => '130.000',
                'image'       => ['as-6-a.png', 'as-6-b.png', 'as-6-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['M'],
                'upload_date' => '2026-04-10 09:05:15',
                'stock'       => 1,
                'views'       => 276,
            ],
        ];

        // Konversi array ke Collection
        $productCollection = collect($allProducts);

        // Cek jika ada input pencarian 'q'
        if ($request->has('q') && $request->q != '') {
            $keyword = $request->q;
            
            // Filter data berdasarkan nama (tidak peka huruf besar/kecil)
            $productCollection = $productCollection->filter(function ($item) use ($keyword) {
                return stripos($item['name'], $keyword) !== false;
            });
        }

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

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