<?php

namespace App\Http\Controllers;

use App\Models\Company;
use App\Models\HistoryContractsUser;
use App\Models\User;
use App\Models\WorkCertificate;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade as PDF;
use Carbon\Carbon;


class DownloadController extends Controller
{
    public function index()
    {

    }

    function downloadCertificatePDF($id)
    {
        $work_certificate = WorkCertificate::find($id);
        if ($work_certificate != null){
            if ($work_certificate->status == '1'){ //Si el certificado esta aprobado
                $user = User::find($work_certificate->user_id);
                if (EmployeeOtherCompany($user->id,false,true) == false) {
                    return redirect()->route('dashboard');
                }
                if (InfoNotEmployee($user->id) == true) {
                    return abort(403, 'No puedes descargar este certificado, es de otro empleado');
                }
                $company_id = $work_certificate->company_id;
                $info_contract = HistoryContractsUser::select('job_position', 'salary_type', 'base_salary', 'performance_factor',
                                                                'salary', 'payment_method', 'start_date', 'end_date',
                                                                'contracts.type')
                                                    ->join('contracts', 'contracts.id', '=', 'history_contracts_users.contract_id')
                                                    ->where('user_id', $user->id)
                                                    ->where('company_id', $company_id)
                                                    ->where('history_contracts_users.status', '!=', '0')
                                                    ->orderBy('history_contracts_users.id', 'desc')->get();
                if ($info_contract != null){

                    $company = Company::find($company_id);
                    if ($user->gender_user == 'Masculino')
                        $prefix = 'el señor';
                    else
                        $prefix = 'la señora';

                    $company_user_info = Company::find($company_id)->users()->wherePivot('user_id', $work_certificate->reviewed_by)->get()->first();
                    //dd($company_user_info);
                    $reviewed = User::find($work_certificate->reviewed_by);
                    if ($company_user_info->pivot->job_position_administrative_area != null) //Si el usuario tiene cargo administrativo asignado en su perfil
                        $job_position_administrative_area = $company_user_info->pivot->job_position_administrative_area;
                    else
                        $job_position_administrative_area = $reviewed->getRoleNames()[0]; //Si no, asignamos el nombre del rol administrativo

                    $info =[
                        'requested_to' => $work_certificate->requested_to,
                        'first_name' => $user->first_name,
                        'last_name' => $user->last_name,
                        'city_name' => $company->city_name,
                        'company_name' => $company->name,
                        'solicitation_date' => $work_certificate->solicitation_date,
                        'document_type_name' => $user->document_type_name,
                        'document_number' => $user->document,
                        'document_expedition' => $user->document_expedition,
                        'prefix' => $prefix,
                        'salary_include' => $work_certificate->salary_include,
                        'reviewed_id' => $reviewed->id,
                        'reviewed_document_type_name' => $reviewed->document_type_name_abbreviation,
                        'reviewed_document_number' => $reviewed->document,
                        'reviewed_name' => $reviewed->first_name.' '.$reviewed->last_name,
                        'reviewed_position' => $job_position_administrative_area,
                        'reviewed_signature' => $reviewed->signature,
                    ];

                    $info = ['info' => $info];
                    $info_contract = ['info_contract' => $info_contract];
                    //dd($info_contract);
                    $pdf = PDF::loadView('pdf.work_certificate_employee', $info, $info_contract);
                    /*
                    return response()->streamDownload(function() use ($pdf) {
                        echo $pdf;
                    }, 'certificado_laboral_'.$user->first_name.'_'.$user->last_name.'_'.Carbon::now()->format('d-m-Y').'.pdf');
                    */
                    return $pdf->stream('certificado_laboral_'.$user->first_name.'_'.$user->last_name.'_'.Carbon::parse($work_certificate->solicitation_date)->format('d-m-Y').'.pdf');


                }
                else{
                    return redirect()->back()->with('error', 'No se encontró información para el usuario seleccionado.');
                }
            }
        }
        else{
            return abort(403, 'No se encontró el certificado solicitado.');
        }


    }

}
