<?php
namespace front\Controllers;

use Domain\Producer\ProducerRepository;

class ShopProducerController
{
  private ProducerRepository $repository;

  public function __construct( ProducerRepository $repository )
  {
    $this->repository = $repository;
  }

  public function products()
  {
    global $page, $lang_id;

    $producerId = (int)\Shared\Helpers\Helpers::get( 'producer_id' );
    $producer = $this->repository->findForFrontend( $producerId, $lang_id );

    if ( !$producer )
      return '';

    $page['show_title'] = true;
    $page['language']['title'] = $producer['name'];

    $bs = (int)\Shared\Helpers\Helpers::get( 'bs' );
    $results = $this->repository->producerProducts( $producer['id'], 12, $bs ?: 1 );

    $pager = '';
    if ( $results['ls'] > 1 )
    {
      $pager = \Shared\Tpl\Tpl::view( 'site/pager', [
        'ls' => $results['ls'],
        'bs' => $bs ?: 1,
        'page' => $page,
        'link' => 'producent/' . \Shared\Helpers\Helpers::seo( $producer['name'] )
      ] );
    }

    return \Shared\Tpl\Tpl::view( 'shop-producer/products', [
      'producer' => $producer,
      'products' => $results['products'],
      'pager' => $pager
    ] );
  }

  public function list()
  {
    global $page;

    $page['show_title'] = true;
    $page['language']['title'] = 'Producenci';

    $producers = $this->repository->allActiveProducers();

    return \Shared\Tpl\Tpl::view( 'shop-producer/list', [
      'producers' => $producers
    ] );
  }
}
