<?php

namespace App\Http\Livewire;

use App\Models\Company;
use App\Models\DocumentType;
use App\Models\PaymentVoucher;
use App\Models\User;
use App\Models\UserDocument;
use App\Models\WorkPermit;
use App\Traits\OrderByTable;
use Carbon\Carbon;

use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Support\Str as Str;


class Employee_PaymentVouchersController extends Component
{
    use WithPagination;
    use OrderByTable;

    public $user, $user_id, $company_id, $first_name, $last_name, $count_reg;
    public $document_support, $month, $year;

    public $breadcrumb_show = true;

    private $pagination = 10;

    protected $paginationTheme = 'bootstrap';

    public function updatingSearch(){
        $this->resetPage();
    }

    public function mount()
    {
        UserAuthPermission('Employee_payment_voucher_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()->CompanyInfo('id'));
        $this->company_id = $company->id;
        $this->pageTitle = 'Lista';
        $this->componentName = 'Desprendibles del Pago del empleado';
    }

    public function render()
    {
        $vouchers_user = PaymentVoucher::where('user_id', $this->user_id)
            ->where('company_id', $this->company_id)
            ->where(function ($query) {
                if ($this->year != null) {
                    $query->where('payment_vouchers.year', $this->year);
                }
            })
            ->where(function ($query) {
                if ($this->month != null) {
                    $query->where('payment_vouchers.month', $this->month);
                }
            })
            ->orderBy('payment_vouchers.id', 'desc')
            ->paginate($this->pagination);


        $this->count_reg = $vouchers_user->count();
        $payment_vouchers = PaymentVoucher::where('user_id', $this->user_id)
            ->where('company_id', $this->company_id);
        $count_payment_vouchers = $payment_vouchers->count();
        if ($count_payment_vouchers > 0){

            $this->min_year = $payment_vouchers->min('year');
            $this->max_year= $payment_vouchers->max('year');
        }
        else{
            //seleccionar año actual
            $this->min_year = date('Y');
            $this->max_year = date('Y');
        }
        return view('livewire.employee.payment-vouchers.component', compact('vouchers_user'))
        ->extends('layouts.theme.admin.app')
        ->section('content');
    }

}
