<?php

namespace Modules\Inventory\Controllers;

use App\Controllers\BaseController;
use App\Models\ProductModel;

class BarcodeController extends BaseController
{
    protected ProductModel $products;

    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->products = new ProductModel();
    }

    public function index()
    {
        $search = trim((string) $this->request->getGet('q'));

        $builder = $this->products
            ->scopeForList($search ?: null, 'active')
            ->orderBy('products.name', 'ASC');

        $products = $builder->findAll();

        return view('Modules\Inventory\Views\barcodes\index', array_merge($this->baseViewData(), [
            'title'    => 'Barcodes & Labels',
            'products' => $products,
            'filters'  => ['search' => $search],
        ]));
    }
}
