<?php

namespace App\Http\Controllers;

use App\Models\AcademicFormation;
use App\Models\AnnexOther;
use App\Models\AnnexOthersContractUser;
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 AnnexContractEmployeePdfController extends Controller
{
    public function index($contract_id_user)
    {
        if ($contract_id_user == NULL) {
            return redirect()->route('dashboard');
        }
        $annex_contract_user = AnnexOthersContractUser::where('contract_user_id', $contract_id_user)->get();
        //dd($annex_contract_user);
        if ($annex_contract_user->count() > 0) {
            $employee_id = $annex_contract_user[0]->user_id;//id del empleado del anexo otro si
            $company = Company::find(Auth::user()->CompanyId());
            $company_id = $company->id;
            $company_plan_id = $company->plan->id;

            UserAuthPermission('AnnexContractEmployee_pdf');
            if (EmployeeOtherCompany($employee_id,false,true) == false) {
                return redirect()->route('dashboard');
            }
            if (ContractNotCompany($contract_id_user, $company_id, $company_plan_id, false, true)[0] == NULL)
            {
                return redirect()->route('dashboard');
            }

                //si el usuario tiene rol de empleado y el contrato esta firmado por él se redirecciona al dashboard
                if (Auth::user()->hasRole('Empleado')){
                    //if ($annex_contract_user->signature_employee != NULL) {
                        return redirect()->route('empleado-contratos')->with('error', 'El anexo no esta disponible');
                    //}
                }

                $user = User::find($employee_id);
                $content_annexes='';
                foreach ($annex_contract_user as $annex_contract) {

                    $annex=[
                        'annex_user_id' => $annex_contract->id,
                        'contract_user_id' => $annex_contract->contract_user_id,
                        'user_id_company' => $annex_contract->created_by,
                        'signature_company' => $annex_contract->signature_company,
                        'signature_full_name_company' => $annex_contract->signature_full_name,
                        'signature_position_company' => $annex_contract->signature_position,
                        'signature_document_type_company' => $annex_contract->signature_document_type,
                        'signature_document_number_company' => $annex_contract->signature_document_number,
                        'user_id_employee' => $user->id,
                        'signature_employee' => $annex_contract->signature_employee,
                        'signature_full_name_employee' => $user->first_name . ' ' . $user->last_name,
                        'signature_document_type_employee' => $user->document_type,
                        'signature_document_number_employee' => $user->document,
                    ];
                    //dd($annex);
                    $content_annexes.= $annex_contract->content_annex;
                    $content_annexes.=createTableAnnexesSignatures($annex);
                    $content_annexes.='<div class="breakNow"></div>';
                }
                //$content_annex = $annex_contract_user->content_annex;
                //$annexOthers = AnnexOther::find($annex_other_id);
                //$content_annexes='';
                $pdf = PDF::loadView('pdf.annexes_contract_employee', compact('content_annexes'));
                return $pdf->stream('Anexos-otros-si-contrato - '.$user->first_name.'_'.$user->last_name.'.pdf');
                //return $pdf->download('cv_employee_' . $user->id . '_' . Carbon::now()->format('Y-m-d') . '.pdf');
            }
            else{
                return redirect()->route('dashboard');
            }

    }
    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');
        }

    }
}
