<?php

namespace App\Livewire\Articulado;

use Livewire\Component;

use App\Models\OrderEquipmentTasks;

use App\Models\FurtherTasks;

class AdditonalTask extends Component
{
    public $equipment;
    public $tasks = [];
    public $furtherTasks = [];
    public $furtherTaskRecords = [];
    public $selectedFurtherTask = 'null';
    public $showAdditionalTask = false;

    
    public function mount($equipment)
    {
        $this->equipment = $equipment;

        $this->tasks = OrderEquipmentTasks::where('equipment_id', $this->equipment->equipment_id)
            ->orderBy('execution_order', 'asc')
            ->get();

        // Coletando todos os registros onde further_tasks_id é diferente de null
        $this->furtherTaskRecords = OrderEquipmentTasks::where('equipment_id', $this->equipment->equipment_id)
            ->whereNotNull('further_tasks_id')
            ->get();

        // Coletando todos os further_tasks_id da coleção $tasks que são diferentes de null
        $existingFurtherTaskIds = $this->tasks->whereNotNull('further_tasks_id')->pluck('further_tasks_id');

        // Buscando furtherTasks que não estão presentes em $tasks
        $this->furtherTasks = FurtherTasks::where('company_projects_id', $this->equipment->company_projects_id)
            ->whereNotIn('further_tasks_id', $existingFurtherTaskIds)
            ->get();
    }

    public function toggle()
    {

        $this->showAdditionalTask = !$this->showAdditionalTask;
    }
    public function render()
    {
        return view('livewire.articulado.additonal-task');
    }
}
