<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class NewsController extends Controller
{
    public function index(Request $request)
    {
        $perPageItems = 12;
        $page = $request->get("p");
        $page = (!$page || $page < 1) ? 1 : $page;
        $pageOffset = $perPageItems * ($page - 1);

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

        return view('news', [
            'dataRows' => $dataRows->skip($pageOffset)->take($perPageItems)->get(),
            'dataCurrentPage' => $page,
            'dataPerPageItems' => $perPageItems,
            'dataTotalCount' => $dataRows->count(),
        ]);
    }

    public function detail(Request $request, $id)
    {
        $dataRow = \App\Models\News::where('id', '=', $id)
            ->where('is_front_show', '=', true);
        
        return view('news_detail', [
            'dataRow' => $dataRow->first(),
        ]);
    }
}
