<?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;
use Illuminate\Support\Facades\Auth;
use Luecano\NumeroALetras\NumeroALetras;

class TesterController extends Controller
{
    public function index($id)
    {
        UserAuthPermission('Company_work_certificate_download');
        $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);
                $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){
                    $formatter = new NumeroALetras(); //Instancia de la clase NumeroALetras
                    $company = Company::find($company_id);
                    if ($user->gender_user == 'Masculino')
                        $prefix = 'el señor';
                    else
                        $prefix = 'la señora';

                    $reviewed = User::find($work_certificate->reviewed_by);
                    if ($reviewed->job_position_administrative_area != null) //Si el usuario tiene cargo administrativo asignado en su perfil
                        $job_position_administrative_area = $reviewed->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];
                    debug("user info: ",$info);
                    debug("contract info: ",$info_contract);

                    return view('pdf.work_certificate_employee', $info, $info_contract)
                ->extends('layouts.admin.auth.app')
            ->section('content');
        }
    }
    }
    }

}
