<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\TrendCategory;
use App\Models\Translation\TrendCategoryTranslation;
use Illuminate\Http\Request;

class TrendCategoriesController extends Controller
{
    public function index()
    {
        $this->authorize('admin_trending_categories');

        /*$trends = TrendCategory::with('category')
            ->orderBy('created_at', 'desc')
            ->paginate(10);*/
            
            $trends = TrendCategory::orderBy('created_at', 'desc')
            ->paginate(10);

        $data = [
            'pageTitle' => trans('home.trending_categories'),
            'trends' => $trends,
        ];

        return view('admin.categories.trends_lists', $data);
    }

    public function create(Request $request)
    {
        $this->authorize('admin_create_trending_categories');
         $defaultLocal = getDefaultLocale();
        $locale = $request->get('locale', mb_strtolower($defaultLocal));
        
        $categories = Category::where('parent_id', null)
            ->with('subCategories')
            ->get();

        $data = [
            'pageTitle' => trans('admin/pages/categories.new_trend'),
            'categories' => $categories,
             'selectedLocale' => $locale
        ];

        return view('admin.categories.create_trend', $data);
    }

    public function store(Request $request)
    {
        $this->authorize('admin_create_trending_categories');

        $this->validate($request, [
            'url' => 'required',
            'icon' => 'required',
            'color' => 'required',
            'courses_no'=>'required'
        ]);

        $data = $request->all();

        $cat=TrendCategory::create([
            //'category_id' => $data['category_id'],
            'icon' => $data['icon'],
            'url' => $data['url'],
            'color' => $data['color'],
            
            'created_at' => time(),
        ]);
        
        TrendCategoryTranslation::updateOrCreate([
              'trend_category_id'=>$cat->id,
             'locale' => mb_strtolower($data['locale']),
        ], [
            'title' => $data['title'],
            'courses_no'=>$data['courses_no'],
        ]);
      
        
        

        return redirect('/admin/categories/trends');
    }

    public function edit(Request $request, $trend_id)
    {
        $this->authorize('admin_edit_trending_categories');

        $trend = TrendCategory::findOrFail($trend_id);
$defaultLocal = getDefaultLocale();
        $locale = $request->get('locale', mb_strtolower($defaultLocal));
        $categories = Category::where('parent_id', null)
            ->with('subCategories')
            ->get();

        $data = [
            'pageTitle' => trans('admin/pages/categories.new_trend'),
            'categories' => $categories,
            'trend' => $trend,
               'selectedLocale' => $locale
        ];

        return view('admin.categories.create_trend', $data);
    }

    public function update(Request $request, $trend_id)
    {
        $this->authorize('admin_create_trending_categories');

        $this->validate($request, [
            //'category_id' => 'required',
             'url' => 'required',
            'icon' => 'required',
            'icon' => 'required',
            'color' => 'required',
            'courses_no'=>'required'
        ]);

        $trend = TrendCategory::findOrFail($trend_id);
        $data = $request->all();

        $trend->update([
            //'category_id' => $data['category_id'],
            'icon' => $data['icon'],
            'url' => $data['url'],
            'color' => $data['color'],
            //'courses_no'=>$data['courses_no'],
        ]);
        
          TrendCategoryTranslation::updateOrCreate([
              'trend_category_id'=>$trend->id,
             'locale' => mb_strtolower($data['locale']),
        ], [
            'title' => $data['title'],
            'courses_no'=>$data['courses_no'],
        ]);
        /*$trend_translation=TrendCategoryTranslation::where(['trend_category_id'=>$trend->id, 'locale' => mb_strtolower($data['locale'])])->firstOrFail();
      
        $trend_translation->update([
           
             'title' => $data['title'],
             
        ]);*/

        return redirect('/admin/categories/trends');
    }

    public function destroy($trend_id)
    {
        $this->authorize('admin_delete_trending_categories');

        $trend = TrendCategory::findOrFail($trend_id);

        $trend->delete();

        return redirect('/admin/categories/trends');
    }
}
