<?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_job;
use App\Models\nn_company;

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

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $data['categories'] = nn_category::getCategory();

        if ($request->input('category')) {
            dd ($request->input('category'));
        }

        $data['items'] = nn_job::join('nn_job_lang', 'nn_job_lang.job_id', '=', 'nn_job.id')
                        ->with([
                            'categories' => function ($q) {
                                $q->join('nn_category_lang', 'nn_category_lang.category_id', '=', 'nn_job_category.category_id')
                                ->where('nn_category_lang.lang', getCurrentLocale())
                                ->orderBy('nn_category_lang.created_at', 'desc');
                            }
                        ])
                        ->join('nn_job_category', 'nn_job_category.job_id', '=', 'nn_job.id')
                        ->join('nn_company', 'nn_company.id', '=', 'nn_job.company_id')
                        ->join('nn_company_lang', 'nn_company_lang.company_id', 'nn_company.id')
                        ->select(
                                'nn_job.id',
                                'nn_job.budget',
                                'nn_job.created_at',
                                'nn_job.end_date',
                                'nn_job_lang.name',
                                \DB::raw('nn_company.logo AS company_logo'),
                                \DB::raw('nn_company.created_at AS company_created_at'),
                                \DB::raw('nn_company_lang.name AS company_name')
                            )
                        ->where([
                                ['nn_job_lang.lang', getCurrentLocale()],
                                ['nn_company_lang.lang', getCurrentLocale()]
                            ])
                        ->orderBy('nn_job.created_at', 'desc')
                        ->groupBy('nn_job.id')
                        ->paginate(6);

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

    public function show($jobId)
    {
        $data['item'] = nn_job::join('nn_job_lang', 'nn_job_lang.job_id', '=', 'nn_job.id')
                        ->with([
                            'categories' => function ($q) {
                                $q->join('nn_category_lang', 'nn_category_lang.category_id', '=', 'nn_job_category.category_id')
                                ->where('nn_category_lang.lang', getCurrentLocale())
                                ->orderBy('nn_category_lang.created_at', 'desc');
                            }
                        ])
                        ->join('nn_job_category', 'nn_job_category.job_id', '=', 'nn_job.id')
                        ->join('nn_company', 'nn_company.id', '=', 'nn_job.company_id')
                        ->select(
                                'nn_job.id',
                                'nn_job.budget',
                                'nn_job.created_at',
                                'nn_job.end_date',
                                'nn_job_lang.name',
                                'nn_job_lang.description',
                                \DB::raw('nn_company.id AS company_id')
                            )
                        ->where([
                                ['nn_job.id', $jobId],
                                ['nn_job_lang.lang', getCurrentLocale()]
                            ])
                        ->first();

        $data['company'] = 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.number_of_staff',
                                'nn_company.cover',
                                'nn_company.logo',
                                'nn_company_lang.name',
                                'nn_company_lang.address'
                            )
                        ->where([
                                ['nn_company.id', $data['item']->company_id],
                                ['nn_company_lang.lang', getCurrentLocale()]
                            ])
                        ->first();

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