<?php

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use Yajra\DataTables\Facades\DataTables;

use App\Models\Equipment;
use App\Models\Plant;
use App\Models\CompanyProject;






use App\Models\ConstructionWorkstation;

class ProjectoDatacontroller extends Controller
{

    public function HomePage()
    {
        $CompanyProject = CompanyProject::all();

        return view('Admin/index')
            //  return view('codePronto');
            ->with("CompanyProject", $CompanyProject);
    }

    public function ManageAssets()
    {

        $units = DB::table('plants')
            ->join('units', 'plants.plant_id', '=', 'units.plant_id')
            ->join('users', 'plants.user_id', '=', 'users.user_id')
            ->select('plants.*', 'units.unit_name', 'users.user_name as user_name')
            ->get();
        $equipments = Equipment::all();
        // $equipaments = DB::table('equipaments')
        //     ->join('factories','equipaments.factory_id', '=', 'factories.factories_id')
        //     ->join('equipament_types', 'equipaments.equipament_type_id', '=' , 'equipament_types.equipament_type_id')
        //     ->select('equipaments.*', 'factories.factories_name', 'equipament_types.equipment_type_name')
        //     ->get();

        // return view('Admin/DataManagement/manageassets', compact('units','equipaments'));
        return view('Admin/DataManagement/manageassets', compact('units', 'equipments'));
    }

    public function showUnit($id)
    {

        $equipaments = Equipment::where('equipment_id', $id)->firstOrFail();
        return view('Admin/DataManagement/showEquipament', compact('equipaments'));
    }

    

    public function receiveEquipmentsProject($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()
                ->where('company_projects_id', $receiveNumberProject)
                ->with(['equipmentType', 'unit', 'equipmentAssociationAmbit.ambitsEquipment']);

        }
        // Caso 'equipment_type_id' não seja '#', filtra os equipamentos por tipo e ambito (caso 'ambits_id' não seja '#')
        else {
            $equipment_type_id = intval($equipment_type_id);
            $model = Equipment::where('equipments.equipment_type_id', $equipment_type_id)
                ->where('company_projects_id', $receiveNumberProject)
                ->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)
                    ->where('company_projects_id', $receiveNumberProject);
            }
        }

        return DataTables::of($model)
            ->addColumn('equipment_type', function ($row) {
                return $row->equipmentType->equipment_type_name;
            })
            ->addColumn('Unit', function ($row) {
                return $row->unit->unit_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('-');
            })
            ->addColumn('Inspec', function ($row) {
                return $row->hasInspectionYes() ? 'Sim' : 'Nao';
            })
            ->toJson();
    }

    public function receiveWorkstationProject($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();
    }
}
