<?php

namespace App\Http\Controllers\nn_site;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\nn_category;
use App\Models\nn_company;

class SiteCompaniesPage extends SiteController
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index($category_id = null)
    {
        $data['categories'] = nn_category::getCategory();

        $filters = [['nn_company_lang.lang', getCurrentLocale()]];

        if ($category_id) {
            $filters[] = ['nn_company_category.category_id' , $category_id];
        }

        $data['items'] = nn_company::join('nn_company_lang', 'nn_company_lang.company_id', '=', 'nn_company.id')
                        ->with([
                            'categories' => function ($q) {
                                $q->join('nn_category_lang', 'nn_category_lang.category_id', '=', 'nn_company_category.category_id')
                                // ->select('nn_category_lang.category_id', 'nn_category_lang.name')
                                ->where('nn_category_lang.lang', getCurrentLocale())
                                ->orderBy('nn_category_lang.created_at', 'desc');
                            }
                        ])
                        ->join('nn_company_category', 'nn_company_category.company_id', '=', 'nn_company.id')
                        ->select(
                                'nn_company.id',
                                'nn_company.min_budget',
                                'nn_company.number_of_staff',
                                'nn_company.cover',
                                'nn_company.logo',
                                'nn_company_lang.name',
                                'nn_company_lang.address'
                            )
                        ->where($filters)
                        ->orderBy('nn_company.created_at', 'desc')
                        ->groupBy('nn_company.id')
                        ->paginate(6);

        return view('nn_site.pages.companies', $data);
    }

    public function show($id)
    {
        $data['item'] = nn_company::join('nn_company_lang', 'nn_company_lang.company_id', '=', 'nn_company.id')
                        ->with([
                            'categories' => function ($q) {
                                $q->join('nn_category_lang', 'nn_category_lang.category_id', '=', 'nn_company_category.category_id')
                                ->where('nn_category_lang.lang', getCurrentLocale())
                                ->orderBy('nn_category_lang.created_at', 'desc');
                            }
                        ])
                        ->select(
                                'nn_company.id',
                                'nn_company.min_budget',
                                'nn_company.min_hour',
                                'nn_company.creation_date',
                                'nn_company.number_of_staff',
                                'nn_company.cover',
                                'nn_company.logo',
                                'nn_company_lang.name',
                                'nn_company_lang.address',
                                'nn_company_lang.description'
                            )
                        ->where([['nn_company.id', $id], ['nn_company_lang.lang', getCurrentLocale()]])
                        ->first();

        return view('nn_site.pages.company_details', $data);
    }
}
