<?php

namespace App\Http\Controllers\Webprofile\Front;

use App\Http\Controllers\Controller;
use App\Models\Webprofile\Categories;
use App\Models\Webprofile\Design;
use App\Models\Webprofile\Menu;
use App\Models\Webprofile\Posts;
use Session;

class CategoryController extends Controller
{
    public function index($category)
    {
        $setting = webprofilesetting();

        if (!Session::has('selected_language')) {
            session(['selected_language' => 'id']);
        }

        if (Session::get('selected_language') == 'id') {
            $data = $this->getDataId($setting, $category);
            
            return view('webprofile.front.'.$setting['theme'].'.archive', $data)->withTitle($category);
        } elseif (Session::get('selected_language') == 'en') {
            $data = $this->getDataEn($setting, $category);
           
            return view('webprofile.front.'.$setting['theme'].'.en.archive', $data)->withTitle($data['category']);

        } elseif (Session::get('selected_language') == 'de') {
            $data = $this->getDataDe($setting, $category);

            return view('webprofile.front.'.$setting['theme'].'.de.archive', $data)->withTitle($data['category']);
        } elseif (Session::get('selected_language') == 'ar') {
            $data = $this->getDataSa($setting, $category);

            return view('webprofile.front.'.$setting['theme'].'.ar.archive', $data)->withTitle($data['category']);
        } elseif (Session::get('selected_language') == 'zh') {
            $data = $this->getDataZh($setting, $category);

            return view('webprofile.front.'.$setting['theme'].'.zh.archive', $data)->withTitle($data['category']);
        }
    }

    public function getDataId($setting, $category)
    {
        $setting = webprofilesetting();
        $findCategory = Categories::where('name', $category)->first();
        $data = Posts::where('categories', $findCategory->id)->where('post_status', '1')->orderBy('post_date', 'desc')->paginate($setting['post_per_page']);
        $resend = Posts::where('post_status', '1')->orderby('post_date', 'desc')->limit('5')->get();
        $hot = Posts::where('post_status', '1')->orderby('viewer', 'desc')->limit('5')->get();
        $categories = Categories::where('is_active', '1')->get();
        $menu = Menu::orderby('urutan', 'asc')->get();
        $widget_right = Design::where('name_design', 'widget_right')->orderBy('urutan', 'ASC')->get();
        $widget_left = Design::where('name_design', 'widget_left')->orderBy('urutan', 'ASC')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'setting' => $setting,
            'data' => $data,
            'menu' => $menu,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'resend' => $resend,
            'hot' => $hot,
            'categories' => $categories,
            'footer' => $footer,
        ];

        return $data;
    }

    public function getDataEn($setting, $category)
    {
        $setting = webprofilesetting();
        $findCategory = Categories::with(['rEn'])->where('name', $category)->first();
        // dd($findCategory);
        $data = Posts::with(['rEn'])->where('categories', $findCategory->id)->where('post_status', '1')->orderBy('post_date', 'desc')->paginate($setting['post_per_page']);
        $resend = Posts::with(['rEn'])->where('post_status', '1')->orderby('post_date', 'desc')->limit('5')->get();
        $hot = Posts::with(['rEn'])->where('post_status', '1')->orderby('viewer', 'desc')->limit('5')->get();
        $categories = Categories::with(['rEn'])->where('is_active', '1')->get();
        $menu = Menu::with(['rEn'])->orderby('urutan', 'asc')->get();
        $widget_right = Design::where('name_design', 'widget_right')->orderBy('urutan', 'ASC')->get();
        $widget_left = Design::where('name_design', 'widget_left')->orderBy('urutan', 'ASC')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'setting' => $setting,
            'data' => $data,
            'menu' => $menu,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'resend' => $resend,
            'hot' => $hot,
            'categories' => $categories,
            'footer' => $footer,
            'category' => $findCategory->rEn->name,
        ];

        return $data;
    }

    public function getDataDe($setting, $category)
    {
        $setting = webprofilesetting();
        $findCategory = Categories::with(['rDe'])->where('name', $category)->first();
        $data = Posts::with(['rDe'])->where('categories', $findCategory->id)->where('post_status', '1')->orderBy('post_date', 'desc')->paginate($setting['post_per_page']);
        $resend = Posts::with(['rDe'])->where('post_status', '1')->orderby('post_date', 'desc')->limit('5')->get();
        $hot = Posts::with(['rDe'])->where('post_status', '1')->orderby('viewer', 'desc')->limit('5')->get();
        $categories = Categories::with(['rDe'])->where('is_active', '1')->get();
        $menu = Menu::with(['rDe'])->orderby('urutan', 'asc')->get();
        $widget_right = Design::where('name_design', 'widget_right')->orderBy('urutan', 'ASC')->get();
        $widget_left = Design::where('name_design', 'widget_left')->orderBy('urutan', 'ASC')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'setting' => $setting,
            'data' => $data,
            'menu' => $menu,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'resend' => $resend,
            'hot' => $hot,
            'categories' => $categories,
            'footer' => $footer,
            'category' => $findCategory->rDe->name,
        ];

        return $data;
    }

    public function getDataSa($setting, $category)
    {
        $setting = webprofilesetting();
        $findCategory = Categories::with(['rSa'])->where('name', $category)->first();
        $data = Posts::with(['rSa'])->where('categories', $findCategory->id)->where('post_status', '1')->orderBy('post_date', 'desc')->paginate($setting['post_per_page']);
        $resend = Posts::with(['rSa'])->where('post_status', '1')->orderby('post_date', 'desc')->limit('5')->get();
        $hot = Posts::with(['rSa'])->where('post_status', '1')->orderby('viewer', 'desc')->limit('5')->get();
        $categories = Categories::with(['rSa'])->where('is_active', '1')->get();
        $menu = Menu::with(['rSa'])->orderby('urutan', 'asc')->get();
        $widget_right = Design::where('name_design', 'widget_right')->orderBy('urutan', 'ASC')->get();
        $widget_left = Design::where('name_design', 'widget_left')->orderBy('urutan', 'ASC')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'setting' => $setting,
            'data' => $data,
            'menu' => $menu,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'resend' => $resend,
            'hot' => $hot,
            'categories' => $categories,
            'footer' => $footer,
            'category' => $findCategory->rSa->name,
        ];

        return $data;
    }

    public function getDataZh($setting, $category)
    {
        $setting = webprofilesetting();
        $findCategory = Categories::with(['rZh'])->where('name', $category)->first();
        $data = Posts::with(['rZh'])->where('categories', $findCategory->id)->where('post_status', '1')->orderBy('post_date', 'desc')->paginate($setting['post_per_page']);
        $resend = Posts::with(['rZh'])->where('post_status', '1')->orderby('post_date', 'desc')->limit('5')->get();
        $hot = Posts::with(['rZh'])->where('post_status', '1')->orderby('viewer', 'desc')->limit('5')->get();
        $categories = Categories::with(['rZh'])->where('is_active', '1')->get();
        $menu = Menu::with(['rZh'])->orderby('urutan', 'asc')->get();
        $widget_right = Design::where('name_design', 'widget_right')->orderBy('urutan', 'ASC')->get();
        $widget_left = Design::where('name_design', 'widget_left')->orderBy('urutan', 'ASC')->get();
        $footer = Design::where('name_design', 'like', '%footer_row%')->get();

        $data = [
            'setting' => $setting,
            'data' => $data,
            'menu' => $menu,
            'widget_right' => $widget_right,
            'widget_left' => $widget_left,
            'resend' => $resend,
            'hot' => $hot,
            'categories' => $categories,
            'footer' => $footer,
            'category' => $findCategory->rZh->name,
        ];

        return $data;
    }
}
