<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\CompanyProject;
use App\Models\ConstructionWorkstation;
use App\Models\ControlEquipmentWorkstation;
use App\Models\Equipment;
use App\Models\EquipmentComments;
use App\Models\EquipmentType;
use App\Models\OrderEquipmentTasks;

use Yajra\DataTables\Facades\DataTables;

class ExecutionProjectController extends Controller
{
    public function enterWorkstation()
    {
        return view('workstations/index');
    }

    public function receiveExecutionProject($ProjectId)
    {
        $DatasProject = CompanyProject::find($ProjectId);
        $equipmentsTypes = EquipmentType::all();

        // return view('projectsClients/executionProject')
        return view('projectsClients/executionProject')
            ->with('DatasProject', $DatasProject)
            ->with('equipmentsTypes', $equipmentsTypes);
    }

    public function receiveWorkstationExecutionProject($receiveNumberProject)
    {
        $model = ConstructionWorkstation::where('company_projects_id', $receiveNumberProject)->with('workstationsAssociationTasks');

        return DataTables::of($model)
            ->addColumn('workstations_Association_Tasks', function ($row) {
                return $row->workstationsAssociationTasks->map(function ($task) {
                    return $task->elementalTask->elemental_tasks_code;
                })->implode('-');
            })
            
            ->toJson();
    }

    public function receiveEquipmentIdForShowModal($EquipmentID)
    {
        // Recebe e encontra os dados do Equipamento indicada na Tabela.
        $equipment = Equipment::find($EquipmentID);

        // Recebe todas as tarefas e devolve em um Array
        $task_codes = $equipment->orderEquipmentTasks->map(function ($task) {
            return $task->elementalTask->elemental_tasks_code;
        })->toArray();

        $receveControlEquipment  = ControlEquipmentWorkstation::where('equipment_id',$EquipmentID)->get();

        $receiveCommentsEquipment = EquipmentComments::where('equipment_id',$EquipmentID)->get();
       
        // return view('projectsClients/executionProject',['receveControlEquipment'=>$receveControlEquipment]);
        return response()->json(['task_codes' => $task_codes,'receveControlEquipment' => $receveControlEquipment, 'receiveCommentsEquipment' => $receiveCommentsEquipment]);
    }

    // public function receiveEquipmentsExecutionProject($receiveNumberProject)
    // {
    //     // Recebe os dados vindos da funcao 'data' criada na view 
    //     $equipment_type_id = request('equipment_type_id');
    //     $ambits_id = request('ambits_id');

    //     //Recebe sempre apenas os equipamentos relacionados a obra
    //     $model = Equipment::where('company_projects_id', $receiveNumberProject);

    //     // Caso 'equipment_type_id' seja '#', mostra todos os equipamentos
    //     if ($equipment_type_id == '#') {
    //         $model = Equipment::query()->with(['equipmentType', 'unit', 'equipmentAssociationAmbit.ambitsEquipment']);
    //     }
    //     // Caso 'equipment_type_id' não seja '#', filtra os equipamentos por tipo e Âmbito (caso 'ambits_id' não seja '#')
    //     else {
    //         $equipment_type_id = intval($equipment_type_id);
    //         $model = Equipment::where('equipments.equipment_type_id', $equipment_type_id)
    //             ->join('equipment_association_ambits', 'equipments.equipment_id', '=', 'equipment_association_ambits.equipment_id')
    //             ->with(['equipmentType', 'unit', 'equipmentAssociationAmbit.ambitsEquipment']);

    //         if ($ambits_id != '#') {
    //             $ambits_id = intval($ambits_id);
    //             $model->where('equipment_association_ambits.ambits_id', $ambits_id);
    //         }
    //     }

    //     return DataTables::of($model)
    //         ->addColumn('equipment_type', function ($row) {
    //             return $row->equipmentType->equipment_type_name;
    //         })
    //         ->addColumn('Ambits', function ($row) {
    //             return $row->equipmentAssociationAmbit->ambitsEquipment->ambits_description;
    //         })
    //         ->addColumn('order_tasks', function ($row) {
    //             return $row->orderEquipmentTasks->map(function ($task) {
    //                 return $task->elementalTask->elemental_tasks_code;
    //             })->implode('-');
    //         })
    //         ->addColumn('current_task', function ($row) {
    //             return $row->controlEquipmentWorkstation->map(function ($task) {
    //                 return $task->elementalTask->elemental_tasks_code;
    //             })->implode('-');
    //         })
    //         ->toJson();
    // }
}
