<?php

namespace App\Http\Controllers\FrontEnd;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ProductsController extends Controller
{
    public function productsPage(Request $request)
    {
        if (empty($request['perpage'])) {
            $request['perpage'] = 24;
        }
        if(empty($request['page'])) {
            $request['page'] =1;
        }
        //$request['text'] = 1;
/*        $page = $request['page'] -5;
        if ($page < 0) {
            $page = 1;
        }*/
        $page = $request['page'];
        //dd($request->all());
        $products = sendRequest('post','http://panel.filmaa.ir/api/getproducts.json',$request->all(),1000);
        $products = $products['list'];

        $request['page'] = $request['page'] + 1;
        $nextpage = http_build_query($request->all(),'','&');

        $request['page'] - 2 < 1 ? $request['page'] = 1 : $request['page'] = $request['page'] - 2;
        $prevpage = http_build_query($request->all(),'','&');

        $request['count'] = 1;
        $request['perpage'] = 999999999;
        unset($request['page']);
        $listCount = sendRequest('post','http://panel.filmaa.ir/api/getproducts.json' , $request->all(),1000);
        $listCount = $listCount['list']['count'];
        $currentPageWithoutPageVal = http_build_query($request->all(),'','&');
        return view('frontend.products', compact('products','listCount','currentPageWithoutPageVal','nextpage','prevpage','page'));
        //dd($products);
    }
}
