<?php

namespace App\Http\Controllers;

use App\Models\AcademicFormation;
use App\Models\Company;
use App\Models\Contract;
use App\Models\HistoryContractsUser;
use App\Models\User;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade as PDF;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;

class ContractEmployeePdfController extends Controller
{
    public function index($contract_id_user)
    {
        $contract_user = HistoryContractsUser::find($contract_id_user);
        if ($contract_user != null) {
            $userId= $contract_user->user_id;
            $company_id= $contract_user->company_id;
            $contract_id = $contract_user->contract_id;
            $company_plan_id = Company::find($company_id )->plan->id;
            UserAuthPermission('ContractEmployee_pdf');
            if (EmployeeOtherCompany($userId,false,true) == false) {
                return redirect()->route('dashboard');
            }
            if (ContractNotCompany($contract_id, $company_id, $company_plan_id, false, true)[0] == NULL)
            {
                return redirect()->route('dashboard');
            }
            if (!empty($contract_id_user))
            {
                //si el usuario tiene rol de empleado y el contrato esta firmado por él se redirecciona al dashboard
                if (Auth::user()->hasRole('Empleado')){
                    if ($contract_user->signature_employee != NULL) {
                        return redirect()->route('empleado-contratos')->with('error', 'El contrato ya tiene firma del empleado');
                    }
                }


                $content_contract = $contract_user->content_contract;
                $user = User::find($userId);
                $contract = Contract::find($contract_id);
                $pdf = PDF::loadView('pdf.contract_employee', compact('content_contract'));
                return $pdf->stream($contract->name.' - '.$user->first_name.'_'.$user->last_name.'.pdf');
                //return $pdf->download('cv_employee_' . $user->id . '_' . Carbon::now()->format('Y-m-d') . '.pdf');
            }
        }
        else{
            abort(403, 'El contrato no existe');
        }
    }
    public function test($contract_id_user)
    {
        //$user = User::find($userId)->id();
        $contract_user = HistoryContractsUser::find($contract_id_user);
        if ($contract_user != NULL) {
            $userId= $contract_user->user_id;
            $company_id= $contract_user->company_id;
            $contract_id = $contract_user->contract_id;
            $company_plan_id = Company::find($company_id )->plan->id;
            UserAuthPermission('ContractEmployee_pdf');
            if (EmployeeOtherCompany($userId,false,true) == false) {
                return redirect()->route('dashboard');
            }
            if (ContractNotCompany($contract_id, $company_id, $company_plan_id, false, true)[0] == NULL)
            {
                return redirect()->route('dashboard');
            }
            if (!empty($contract_id_user))
            {
                $content_contract = $contract_user->content_contract;
                $user = User::find($userId);
                $contract = Contract::find($contract_id);

                return view('pdf.contract_employee', compact('content_contract'))
                ->extends('layouts.theme.admin.app')
            ->section('content');
            }
        }
        else {
            abort(403, 'El contrato no existe');
        }

    }
    public function signature($contract_id_user)
    {
        //$user = User::find($userId)->id();
        $contract_user = HistoryContractsUser::find($contract_id_user);
        if ($contract_user != NULL) {
            $userId= $contract_user->user_id;
            $company_id= $contract_user->company_id;
            $contract_id = $contract_user->contract_id;
            $company_plan_id = Company::find($company_id )->plan->id;
            UserAuthPermission('ContractEmployee_pdf');
            if (EmployeeOtherCompany($userId,false,true) == false) {
                return redirect()->route('dashboard');
            }
            if (ContractNotCompany($contract_id, $company_id, $company_plan_id, false, true)[0] == NULL)
            {
                return redirect()->route('dashboard');
            }
            if (!empty($contract_id_user))
            {
                $content_contract = $contract_user->content_contract;
                $user = User::find($userId);
                $contract = Contract::find($contract_id);

                return view('pdf.contract_employee_signature', compact('content_contract'))
                ->extends('layouts.theme.admin.app')
            ->section('content');
            }
        }
        else {
            abort(403, 'El contrato no existe');
        }

    }
}
