<?php



namespace App\Http\Controllers;



use App\Models\Category;

use App\Models\Link;

use App\Models\News;

use Illuminate\Http\Request;
use App\Models\Exchange;



class CategoryController extends Controller

{

    /**

     * Display a listing of the resource.

     *

     * @return \Illuminate\Http\Response

     */

    public function index()

    {

        //

    }



    public function Category($slug)

    {

         $category=Category::where('slug',$slug)->first();



            $ekonomikhaberlerSlider=News::where('status',1)->where('category_id',$category->id)->orderBy('created_at', 'desc')->limit(15)->get();
            $ekonomikhaberlerminiSlider=News::where('status',1)->where('category_id',$category->id)->orderBy('created_at', 'desc')->offset(15)->limit(1)->get();
            $ekonomikhaberlerNews=News::where('status',1)->where('category_id',$category->id)->orderBy('created_at', 'desc')->offset(17)->limit(20)->get();
         
            if($category->type==1)
            {
            $exchanges=Exchange::orderBy('exchange_order', 'asc')->where('type',1)->get();
            $exchangecoins=Exchange::orderBy('id', 'asc')->where('type',10)->get();


            return view('categories.economy', compact(['category','ekonomikhaberlerSlider','ekonomikhaberlerNews','ekonomikhaberlerminiSlider','exchanges','exchangecoins']));
            } else{
            return view('categories.category', compact(['category','ekonomikhaberlerSlider','ekonomikhaberlerNews','ekonomikhaberlerminiSlider']));
            }
    }


    public function mansetHaberler(){

            $ekonomikhaberlerSlider=News::where('status',1)->where('is_headline','1')->orderBy('headline_order', 'asc')->orderBy('created_at', 'desc')->limit(15)->get();
            $ekonomikhaberlerNews=News::where('status',1)->where('is_headline','1')->orderBy('created_at', 'desc')->limit(50)->get();

            return view('categories.manset', compact(['ekonomikhaberlerSlider','ekonomikhaberlerNews']));
    }

    public function loadMoreNews(Request $request)
{
    $category = Category::find($request->category_id);

    if ($category) {
        $ekonomikhaberlerNews = News::where('status', 1)
            ->where('category_id', $category->id)
            ->orderBy('created_at', 'desc')
            ->offset($request->offset)
            ->limit(20)  // Her seferinde 20 haber yüklenecek
            ->get();

            if($request->count==4){
             return view('categories.partial-news-three', compact('ekonomikhaberlerNews'))->render();
            }else{
             return view('categories.partial-news', compact('ekonomikhaberlerNews'))->render();
            }
    }

    return response()->json(['error' => 'Kategori bulunamadı.'], 404);
}


    public function loadMoreManset(Request $request)
{

        $ekonomikhaberlerNews = News::where('status', 1)
            ->where('is_headline', '1')
            ->orderBy('created_at', 'desc')
            ->offset($request->offset)
            ->limit(20)  // Her seferinde 20 haber yüklenecek
            ->get();

        return view('categories.partial-news', compact('ekonomikhaberlerNews'))->render();
 
}



    /**

     * Show the form for creating a new resource.

     *

     * @return \Illuminate\Http\Response

     */

    public function create()

    {

        //

    }



    /**

     * Store a newly created resource in storage.

     *

     * @param  \Illuminate\Http\Request  $request

     * @return \Illuminate\Http\Response

     */

    public function store(Request $request)

    {

        //

    }



    /**

     * Display the specified resource.

     *

     * @param  \App\Models\category  $category

     * @return \Illuminate\Http\Response

     */

    public function show(category $category)

    {

        //

    }



    /**

     * Show the form for editing the specified resource.

     *

     * @param  \App\Models\category  $category

     * @return \Illuminate\Http\Response

     */

    public function edit(category $category)

    {

        //

    }



    /**

     * Update the specified resource in storage.

     *

     * @param  \Illuminate\Http\Request  $request

     * @param  \App\Models\category  $category

     * @return \Illuminate\Http\Response

     */

    public function update(Request $request, category $category)

    {

        //

    }



    /**

     * Remove the specified resource from storage.

     *

     * @param  \App\Models\category  $category

     * @return \Illuminate\Http\Response

     */

    public function destroy(category $category)

    {

        //

    }

}

