<?php

namespace App\Http\Livewire;

use App\Models\Company;
use App\Models\ContractCompany;
use Livewire\Component;
use Illuminate\Support\Facades\DB;
use App\Traits\OrderByTable;

class ContractCompaniesController extends Component
{
    public $componentName,$selected_CC_id,$company_id,$contract_sel_id,$name_company, $search_modal, $companies_modal, $count_reg_modal;
    public $limit_companies = 10;

    use OrderByTable;

    public function mount($contract_sel_id)
    {
        $this->contract_sel_id=$contract_sel_id;
        $this->componentName = 'Empresas asignadas al contrato';
    }

    public function render()
    {

        $contractcompanies = ContractCompany::where('contract_id',$this->contract_sel_id)
        ->join('companies', 'contract_companies.company_id', '=', 'companies.id')
        ->select('contract_companies.*', 'companies.name as name_company')
        ->get();
        $companies = Company::OrderBy('name','asc')
                    ->WhereNotExists(function ($query) {
                        $query->select(DB::raw(1))
                            ->from('contract_companies')
                            ->whereRaw('contract_companies.company_id = companies.id')
                            ->whereRaw('contract_companies.contract_id = '.$this->contract_sel_id);
                    })
                    ->take($this->limit_companies)
                    ->get();
        $this->count_reg_modal = $companies->count();
        if (strlen($this->search_modal) > 0) {
            $this->companies_modal = Company::OrderBy('name','asc')
                    ->Where('name', 'like', '%' . $this->search_modal . '%')
                    ->WhereNotExists(function ($query) {
                        $query->select(DB::raw(1))
                            ->from('contract_companies')
                            ->whereRaw('contract_companies.company_id = companies.id')
                            ->whereRaw('contract_companies.contract_id = '.$this->contract_sel_id);
                    })
                    ->take($this->limit_companies)
                    ->get();
        }
        else {
            $this->companies_modal = Company::OrderBy('name','asc')
                    ->WhereNotExists(function ($query) {
                        $query->select(DB::raw(1))
                            ->from('contract_companies')
                            ->whereRaw('contract_companies.company_id = companies.id')
                            ->whereRaw('contract_companies.contract_id = '.$this->contract_sel_id);
                    })
                    ->take($this->limit_companies)
                    ->get();
        }

        return view('livewire.contracts.detail.company.component',compact('contractcompanies','companies'))
        ->extends('layouts.theme.admin.app')
        ->section('content');
    }


    public function openAddCC()
    {
        $this->emit('show-modal-CC','Abrir Modal');
    }

    public function resetUI()
    {
        $this->search_modal = '';
        $this->selected_CC_id = 0;
    }

    public function AddCompanyContract(Company $company)
    {
        $company = Company::find($company->id);
        if ($company) {
            $contractcompany = new ContractCompany;
            $contractcompany->contract_id = $this->contract_sel_id;
            $contractcompany->company_id = $company->id;
            $contractcompany->save();
            $this->emit('item-added-CC','Empresa asignada al contrato');
        }
        else {
            $this->emit('item-added-CC','error','Empresa no encontrada');
        }
    }

    protected $listeners = ['deleteRowCC' => 'DestroyCC'];

    public function DestroyCC(ContractCompany $contractcompany)
    {
        $contractcompany = ContractCompany::find($contractcompany->id);
        if ($contractcompany) {
            $contractcompany->delete();
            $this->emit('item-deleted-CC','Empresa eliminada del contrato');
        }
        else {
            $this->emit('item-deleted-CC','error','Empresa no encontrada');
        }
    }

}
