<?php

namespace App\Http\Controllers;

use App\Models\AcademicFormation;
use App\Models\LanguageUser;
use App\Models\Reference;
use App\Models\Skill;
use App\Models\Training;
use App\Models\User;
use App\Models\WorkExperience;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade as PDF;
use Carbon\Carbon;

class CvEmployeePdfController extends Controller
{
    public function index($userId)
    {
        UserAuthPermission('CvEmployee_pdf');
        if (EmployeeOtherCompany($userId,false,true) == false) {
            return redirect()->route('dashboard');
        }
        if (!empty($userId))
        {
            $user = User::select('users.*', 'cities.name as city_residence_name', 'states.name as state_residence_name')
                    ->join('cities', 'users.city_residence', '=', 'cities.code')
                    ->join('states', 'users.state_residence', '=', 'states.code')
                    ->where('users.id', $userId)
                    ->first();
            $academic_formations = AcademicFormation::join('educational_levels', 'academic_formations.educational_level_id', '=', 'educational_levels.id')
                    ->select('academic_formations.*', 'educational_levels.name as educational_level_name')
                    ->where('academic_formations.user_id', $userId)
                    ->orderBy('academic_formations.initial_date_year', 'desc')
                    ->get();
            $work_experiences = WorkExperience::where('user_id',$userId)->get();
            $trainings = Training::where('user_id', $userId)->get();
            $skills = Skill::where('user_id', $userId)->get();
            $languageuser = LanguageUser::join('languages', 'language_users.language_id', '=', 'languages.id')
            ->join('level_languages','language_users.level_id','=','level_languages.id')
            ->select('language_users.*', 'languages.name as language_name', 'level_languages.name as level_name')
            ->where('language_users.user_id', $userId)
            ->orderBy('language_users.id', 'desc')
            ->get();


            $references = Reference::where('user_id', $userId)->get();
            $pdf = PDF::loadView('pdf.cv_employee', compact('user', 'academic_formations','skills', 'work_experiences','trainings','languageuser','references'))->setPaper('a4', 'portrait');
            return $pdf->stream($user->first_name.'_'.$user->last_name.'.pdf');
            //return $pdf->download('cv_employee_' . $user->id . '_' . Carbon::now()->format('Y-m-d') . '.pdf');
        }


    }
    public function test($userId)
    {
        //$user = User::find($userId)->id();
        if (!empty($userId))
        {
            $user = User::select('users.*', 'cities.name as city_residence_name', 'states.name as state_residence_name')
            ->join('cities', 'users.city_residence', '=', 'cities.code')
            ->join('states', 'users.state_residence', '=', 'states.code')
            ->where('users.id', $userId)
            ->first();
    $academic_formations = AcademicFormation::join('educational_levels', 'academic_formations.educational_level_id', '=', 'educational_levels.id')
            ->select('academic_formations.*', 'educational_levels.name as educational_level_name')
            ->where('academic_formations.user_id', $userId)
            ->orderBy('academic_formations.initial_date_year', 'desc')
            ->get();
    $work_experiences = WorkExperience::where('user_id',$userId)->orderBy('date_entry', 'desc')->get();
    $trainings = Training::where('user_id', $userId)->orderBy('year', 'desc')->get();
    $skills = User::find($userId)->skills;

            return view('pdf.cv_employee', compact('user', 'academic_formations', 'work_experiences','trainings'))
            ->extends('layouts.theme.admin.app')
        ->section('content');
        }


    }
}
