<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SupportDistributionPartnersController extends Controller
{
    public function index(Request $request)
    {
        $catalogId = $request->get('cid');
        $catalogId = (!$catalogId || $catalogId < 1) ? 0 : $catalogId;
        $perPageItems = 20;
        $page = $request->get("p");
        $page = (!$page || $page < 1) ? 1 : $page;
        $pageOffset = $perPageItems * ($page - 1);

        $catalogRows = \App\Models\SupportDistributionPartnerCatalog::where('is_front_show', '=', true)
            ->orderBy('lft', 'asc')
            ->orderBy('id', 'desc')
            ->select('id', 'name');
        if ($catalogId == 0)
        {
            $catalogRowsGet = $catalogRows->get();
            foreach ($catalogRowsGet as $row)
            {
                $catalogId = $row->id;
                break;
            }
        }
        

        $dataRows = \App\Models\SupportDistributionPartner::where('support_distribution_partner_catalog_id', '=', $catalogId)
            ->where('is_front_show', '=', true)
            ->orderBy('title', 'desc')
            ->select('id', 'title', 'description', 'photos', 'website');

        return view('support-distribution-partners', [
            'catalogRows' => $catalogRows->get(),
            'catalogCurrentId' => $catalogId,
            'dataRows' => $dataRows->skip($pageOffset)->take($perPageItems)->get(),
            'dataCurrentPage' => $page,
            'dataPerPageItems' => $perPageItems,
            'dataTotalCount' => $dataRows->count()
        ]);

    }

}
