<?php

namespace App\Http\Livewire;

use App\Models\AnnexOthersContractUser;
use App\Models\Company;
use App\Models\HistoryContractsUser;
use App\Models\User;
use App\Traits\OrderByTable;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithFileUploads;

class Employee_ContractsController extends Component
{
    use WithFileUploads;
    use OrderByTable;

    public $user, $user_id, $company_id, $first_name, $last_name, $search, $count_reg;
    public $breadcrumb_show = true;

    private $pagination = 10;

    protected $paginationTheme = 'bootstrap';

    public function mount()
    {
        UserAuthPermission('Employee_contracts_index');
        $user = User::find(auth()->user()->id);
        EmployeeOtherCompany($user->id); //Verifica que el empleado pertenezca a la empresa

        $this->user = $user;
        $this->user_id = $user->id;
        $this->first_name = $user->first_name;
        $this->last_name = $user->last_name;
        $company = Company::find(Auth::user()->CompanyId());
        $this->company_id = $company->id;
        $this->pageTitle = 'Lista';
        $this->componentName = 'Contratos';
    }

    public function render()
    {
        if (strlen($this->search) > 0){

            $contracts_user = HistoryContractsUser::select('contracts.*', 'history_contracts_users.id as id_contract_user',
                'history_contracts_users.signature_company', 'history_contracts_users.signature_employee',
                'history_contracts_users.status as status_contract', 'history_contracts_users.active_signature_employee',
                'history_contracts_users.start_date', 'history_contracts_users.end_date', 'history_contracts_users.created_at',
                'history_contracts_users.updated_at', 'history_contracts_users.content_contract',
                'history_contracts_users.ref_reason_termination', 'history_contracts_users.user_termination', 'history_contracts_users.date_termination')
                ->join('contracts', 'contracts.id', '=', 'history_contracts_users.contract_id')
                ->join('users', 'users.id', '=', 'history_contracts_users.user_id')
                ->join('companies', 'companies.id', '=' , 'history_contracts_users.company_id')
                ->where(function($query){
                    $query->where('contracts.name', 'like', '%' . $this->search . '%')
                        ->orWhere('contracts.description', 'like', '%' . $this->search . '%');
                    })
                ->where('history_contracts_users.company_id', $this->company_id)
                ->where('history_contracts_users.user_id', $this->user_id)
                ->orderBy('history_contracts_users.id', 'desc')
                ->paginate($this->pagination);
        }
        else{
            $contracts_user = HistoryContractsUser::select('contracts.*', 'history_contracts_users.id as id_contract_user',
                'history_contracts_users.signature_company', 'history_contracts_users.signature_employee',
                'history_contracts_users.status as status_contract', 'history_contracts_users.active_signature_employee',
                'history_contracts_users.start_date', 'history_contracts_users.end_date', 'history_contracts_users.created_at',
                'history_contracts_users.updated_at', 'history_contracts_users.content_contract',
                'history_contracts_users.ref_reason_termination', 'history_contracts_users.user_termination', 'history_contracts_users.date_termination')
                ->join('contracts', 'contracts.id', '=', 'history_contracts_users.contract_id')
                ->join('users', 'users.id', '=', 'history_contracts_users.user_id')
                ->join('companies', 'companies.id', '=' , 'history_contracts_users.company_id')
                ->where('history_contracts_users.company_id', $this->company_id)
                ->where('history_contracts_users.user_id', $this->user_id)
                ->orderBy('history_contracts_users.id', 'desc')
                ->paginate($this->pagination);
            }
        $this->count_reg = $contracts_user->count();
        $list_annex_contract_user = AnnexOthersContractUser::select('annex_others.name as annex_name')
            ->join('history_contracts_users', 'history_contracts_users.id', '=', 'annex_others_contract_users.contract_user_id')
            ->join('annex_others', 'annex_others.id', '=', 'annex_others_contract_users.annex_other_id')
            ->where('history_contracts_users.company_id', $this->company_id)
            ->where('history_contracts_users.user_id', $this->user_id)
            ->get();

        return view('livewire.employee.contracts.component', compact('contracts_user', 'list_annex_contract_user'))
        ->extends('layouts.theme.admin.app')
        ->section('content');
    }
}
