<?php

namespace App\Http\Controllers;

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

use Yajra\DataTables\DataTables;


use App\Models\CompanyProject;
use App\Models\Equipment;
use App\Models\EquipmentType;
use App\Models\Unit;
use App\Models\AmbitsEquipment;


class PreparedProjectController extends Controller
{
    public function PreparedProject($ProjectId)
    {

        $numberProject = CompanyProject::find($ProjectId);

        $equipmentsProjects = Equipment::all()->where('company_projects_id', $ProjectId);
        $equipmentsTypes = EquipmentType::all();

        //Retorna todas as Fabricas Unit, com base na instalacao 
        $checkUnits = DB::table('units')
            ->join('plants', 'units.plant_id', '=', 'plants.plant_id')
            ->join('company_projects', 'plants.plant_id', '=', 'company_projects.plant_id')
            ->select('units.*')
            ->where('company_projects.company_projects_id', '=', $numberProject->company_projects_id)
            ->get();

        return view('projectsClients/preparedProject')
            // ->with('equipmentsProjects', $equipmentsProjects)
            ->with('equipmentsTypes', $equipmentsTypes)
            ->with('units', $checkUnits)
            ->with('numberProject', $numberProject);
    }

    public function getAmbits($equipmentType)
    {
        $ambits = DB::table('ambits_equipments')
            ->select('ambits_equipments.*')
            ->where('ambits_equipments.equipment_type_id', $equipmentType)
            ->get();
        return response()->json($ambits);
    }

    public function editProjectForArticulated(Request $request)
    {

        $numberProject = CompanyProject::find($request->ProjectId);

        $numberProject->order_project = 1;
        $numberProject->save();

        return redirect()->route('home');
    }

    public function getData1()
    {

        $equipment_type_id = request('equipment_type_id');
        $unit_id = request('unit_id');
        $ambits_id = request('ambits_id');

        // Equipment::all()->where('company_projects_id', $ProjectId);

        // 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 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)
                ->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);
            }
        }
        if(request()->has('inspec') && request('inspec') != '#') {
            $inspectionFilter = request('inspec') === 'Sim';
            $model->whereHas('orderEquipmentTasks', function ($query) use ($inspectionFilter) {
                $query->where('inspection', $inspectionFilter ? 'Sim' : 'Nao');
            });
        }
        

        // Aplica o filtro de 'unit_id', se aplicável
        if ($unit_id != '#') {
            $unit_id = intval($unit_id);
            $model->where('equipments.unit_id', $unit_id);
        }

        // Gera a tabela de dados
        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('Inspec', function ($row) {
                return $row->hasInspectionYes() ? 'Sim' : 'Nao';
            })
            ->toJson();
    }
}
