<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ApparelNewController extends Controller
{
    public function index(Request $request) // WAJIB tambahkan Request $request
    {
        // 1. Data asli produk Anda
        $products = [
            [
                'name'        => 'Stone Island Polo Shirt',
                'category'    => 'Apparel New',
                'status'      => 'NEW',
                'price'       => '1.250.000',
                'image'       => ['an-1-a.png', 'an-1-b.png', 'an-1-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['S', 'M', 'L', 'Xl'],
                'upload_date' => '2026-04-30 14:30:00',
                'stock'       => 3,
                'views'       => 2847,
            ],
            [
                'name'        => 'Tracktop BW Bird',
                'category'    => 'Apparel New',
                'status'      => 'NEW',
                'price'       => '1.350.000',
                'image'       => ['an-2-a.png', 'an-2-b.png', 'an-2-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['M', 'L'],
                'upload_date' => '2026-04-28 10:15:00',
                'stock'       => 5,
                'views'       => 1563,
            ],
            [
                'name'        => 'Tracktop Ellese Black',
                'category'    => 'Apparel New',
                'status'      => 'NEW',
                'price'       => '1.650.000',
                'image'       => ['an-3-a.png', 'an-3-b.png', 'an-3-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['L', 'Xl', 'Xxl'],
                'upload_date' => '2026-04-25 16:45:00',
                'stock'       => 2,
                'views'       => 2190,
            ],
            [
                'name'        => 'Jersey Adidas Oasis',
                'category'    => 'Apparel New',
                'status'      => 'NEW',
                'price'       => '2.250.000',
                'image'       => ['an-4-a.png', 'an-4-b.png', 'an-4-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['Xs', 'S', 'M'],
                'upload_date' => '2026-04-22 09:20:00',
                'stock'       => 4,
                'views'       => 874,
            ],
            [
                'name'        => 'Jaket Napapijri',
                'category'    => 'Apparel New',
                'status'      => 'NEW',
                'price'       => '3.150.000',
                'image'       => ['an-5-a.png', 'an-5-b.png', 'an-5-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['Xl', 'Xxl', '3xl'],
                'upload_date' => '2026-04-18 11:05:00',
                'stock'       => 1,
                'views'       => 431,
            ],
            [
                'name'        => 'Rugby Adidas',
                'category'    => 'Apparel New',
                'status'      => 'NEW',
                'price'       => '950.000',
                'image'       => ['an-6-a.png', 'an-6-b.png', 'an-6-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['M', 'L', '14'],
                'upload_date' => '2026-04-10 08:30:00',
                'stock'       => 3,
                'views'       => 1208,
            ],
        ];

        // 2. Ubah array biasa menjadi Laravel Collection agar mudah di-filter
        $productCollection = collect($products);

        // 3. LOGIKA PENCARIAN
        // Jika ada input 'q' dari search bar
        if ($request->has('q') && $request->q != '') {
            $keyword = $request->q;
            
            // Filter koleksi: kembalikan item yang namanya mengandung kata kunci
            // Menggunakan stripos agar tidak sensitif huruf besar/kecil
            $productCollection = $productCollection->filter(function ($item) use ($keyword) {
                return stripos($item['name'], $keyword) !== false;
            });
        }

        // 4. Kembalikan data yang sudah difilter menjadi array kembali
        $filteredProducts = $productCollection->values()->all();

        // 5. Kirim data ke view
        return view('apparelnew', ['products' => $filteredProducts]);
    }
}