<?php

namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;

class CompanysIdController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
       // $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */


    public function companysId($company=false,$vehiculo=false){


       

        $cadena="";

          if($company=="" && $vehiculo==""){

            $companys = DB::table('vehicles')
                    ->join('entities', 'entities.pk_entity', '=', 'vehicles.fk_entity_company')
                    
                     ->join('vehicles_catalog', 'vehicles_catalog.pk_vehicle_catalog','=', 'vehicles.fk_vehicle_catalog')

                    ->join('vehicles_types', 'vehicles_types.pk_vehicle_type','=', 'vehicles_catalog.fk_vehicle_type')

                    ->join('vehicles_models', 'vehicles_models.pk_vehicle_model','=', 'vehicles_catalog.fk_vehicle_model')

                    ->join('manufacturers', 'manufacturers.pk_manufacturer','=', 'vehicles_catalog.fk_manufacturer')

                    ->join('fuels', 'fuels.pk_fuel','=', 'vehicles_catalog.fk_fuel')

                    ->join('vehicle_usages', 'vehicle_usages.pk_vehicle_usage','=', 'vehicles.fk_vehicle_usage')

                    ->join('vehicle_status', 'vehicle_status.pk_vehicle_status','=', 'vehicles.fk_vehicle_status')

                    ->join('vehicle_status_group', 'vehicle_status_group.pk_vehicle_status_group','=', 'vehicles.fk_vehicle_status_group')

                   // ->join('vehicles_origins', 'vehicles_origins.pk_vehicle_origin','=', 'vehicles.fk_vehicle_origin')

                     ->select(DB::raw(
                            'fk_entity_company as "fkEntityCompany",
                            entities.name as nameentity,
                            pk_vehicle as "pkVehicle",
                            serial_number as "serialNumber",
                            manufacturers.name as manofacture,
                            vehicles_models.name as namemodel,
                            passengers,
                            engine_number as "engineNumber",
                            vehicle_year as "vehicleYear",
                            vehicles_types.name as nametype,
                            vehicle_status_group.name as "vehicleStatusGroup",
                            vehicle_usages.name as nameusage,
                            vehicle_status.name as namestatus,
                           fuels.name as fuel,
                            eco,
                            plates,
                            federal_plates as "federalPlates",
                            description,

                            last_odometer_fuel_ticket as "lastOdometerFuelTicket",
                            fk_parkinglot as parkinglots,

                            null as "lastCommunicationTime",
                            vehicles.fk_vehicle_origin as "vehicleOrigins"

                            '
      
                            ))->get();

                

        }


        if(isset($company) && $company!=""){

            if($vehiculo==""){

                 $companys = DB::table('vehicles')
                    ->join('entities', 'entities.pk_entity', '=', 'vehicles.fk_entity_company')
                    
                     ->join('vehicles_catalog', 'vehicles_catalog.pk_vehicle_catalog','=', 'vehicles.fk_vehicle_catalog')

                    ->join('vehicles_types', 'vehicles_types.pk_vehicle_type','=', 'vehicles_catalog.fk_vehicle_type')

                    ->join('vehicles_models', 'vehicles_models.pk_vehicle_model','=', 'vehicles_catalog.fk_vehicle_model')

                    ->join('manufacturers', 'manufacturers.pk_manufacturer','=', 'vehicles_catalog.fk_manufacturer')

                    ->join('fuels', 'fuels.pk_fuel','=', 'vehicles_catalog.fk_fuel')

                     ->select('pk_vehicle',
                            'eco',
                            'fk_vehicle_catalog',
                            'fk_color',
                            'plates',
                            'federal_plates',
                            'fuel_consumption',
                            'engine_number',
                            'serial_number',
                            'fk_insurance_contract',
                            'insurance_subsection',
                            'fk_lease_contract',
                            'fk_parkinglot',
                            'insurance_startup_date',
                            'insurance_expiration_date',
                            'fk_vehicle_status',
                            'fk_entity_company',
                            'fk_federal_entity',
                            'passengers',
                            'fk_seat_type',
                            'doors',
                            'fk_suspension_type',
                            'fk_branch',
                            'description',
                            'authorized_fuel_consumption',
                            'fk_vehicle_usage',
                            'bathroom',
                            'curtains',
                            'monitors',
                            'air_conditioning',
                            'trunk',
                            'seatbelt',
                            'boombox',
                            'advertisement',
                            'iave',
                            'radio_band',
                            'sim',
                            'activation_cost',
                            'last_odometer_fuel_ticket',
                            'imei',
                            'fk_vehicle_status_group',
                            'fk_gps_provider',
                            'fk_vehicle_origin',
                            'fk_vehicle_fleet',
                            'pk_entity',
                            'fk_entity_status',
                            'codename',
                            'fk_zone',
                            'fk_vehicle_type as type_id', 'vehicles_types.name as type_name', 'fk_manufacturer as manufacturer_id','manufacturers.name as manufacturer_name','version','vehicle_year as year','entities.name as company','fk_vehicle_model as model_id', 'vehicles_models.name as name_model',
                                'vehicles_catalog.fk_fuel as fuel_id', 'fuels.name as fuel_name')

                    ->where('vehicles.fk_entity_company',$company)->paginate(50);
            }

           






             if(isset($vehiculo) && $vehiculo!=""){

            $companys = DB::table('vehicles')
                    ->join('entities', 'entities.pk_entity', '=', 'vehicles.fk_entity_company')
                    
                     ->join('vehicles_catalog', 'vehicles_catalog.pk_vehicle_catalog','=', 'vehicles.fk_vehicle_catalog')

                    ->join('vehicles_types', 'vehicles_types.pk_vehicle_type','=', 'vehicles_catalog.fk_vehicle_type')

                    ->join('vehicles_models', 'vehicles_models.pk_vehicle_model','=', 'vehicles_catalog.fk_vehicle_model')

                    ->join('manufacturers', 'manufacturers.pk_manufacturer','=', 'vehicles_catalog.fk_manufacturer')

                    ->join('fuels', 'fuels.pk_fuel','=', 'vehicles_catalog.fk_fuel')

                     ->select('pk_vehicle',
                            'eco',
                            'fk_vehicle_catalog',
                            'fk_color',
                            'plates',
                            'federal_plates',
                            'fuel_consumption',
                            'engine_number',
                            'serial_number',
                            'fk_insurance_contract',
                            'insurance_subsection',
                            'fk_lease_contract',
                            'fk_parkinglot',
                            'insurance_startup_date',
                            'insurance_expiration_date',
                            'fk_vehicle_status',
                            'fk_entity_company' ,
                            'fk_federal_entity',
                            'passengers',
                            'fk_seat_type',
                            'doors',
                            'fk_suspension_type',
                            'fk_branch',
                            'description',
                            'authorized_fuel_consumption',
                            'fk_vehicle_usage',
                            'bathroom',
                            'curtains',
                            'monitors',
                            'air_conditioning',
                            'trunk',
                            'seatbelt',
                            'boombox',
                            'advertisement',
                            'iave',
                            'radio_band',
                            'sim',
                            'activation_cost',
                            'last_odometer_fuel_ticket',
                            'imei',
                            'fk_vehicle_status_group',
                            'fk_gps_provider',
                            'fk_vehicle_origin',
                            'fk_vehicle_fleet',
                            'pk_entity',
                            'fk_entity_status',
                            'codename',
                            'fk_zone',
                            'fk_vehicle_type as type_id', 'vehicles_types.name as type_name', 'fk_manufacturer as manufacturer_id','manufacturers.name as manufacturer_name','version','vehicle_year as year','entities.name as company','fk_vehicle_model as model_id', 'vehicles_models.name as name_model',
                                'vehicles_catalog.fk_fuel as fuel_id', 'fuels.name as fuel_name')

                    ->where('vehicles.fk_entity_company',$company)->where('vehicles.pk_vehicle',$vehiculo)->paginate(50);

             }

        }


         return $companys;

    }


    public function all(){


            $companys = DB::table('vehicles')
                    ->join('entities', 'entities.pk_entity', '=', 'vehicles.fk_entity_company')
                    
                     ->join('vehicles_catalog', 'vehicles_catalog.pk_vehicle_catalog','=', 'vehicles.fk_vehicle_catalog')

                    ->join('vehicles_types', 'vehicles_types.pk_vehicle_type','=', 'vehicles_catalog.fk_vehicle_type')

                    ->join('vehicles_models', 'vehicles_models.pk_vehicle_model','=', 'vehicles_catalog.fk_vehicle_model')

                    ->join('manufacturers', 'manufacturers.pk_manufacturer','=', 'vehicles_catalog.fk_manufacturer')

                    ->join('fuels', 'fuels.pk_fuel','=', 'vehicles_catalog.fk_fuel')

                     ->select('pk_vehicle',
                            'eco',
                            'fk_vehicle_catalog',
                            'fk_color',
                            'plates',
                            'federal_plates',
                            'fuel_consumption',
                            'engine_number',
                            'serial_number',
                            'fk_insurance_contract',
                            'insurance_subsection',
                            'fk_lease_contract',
                            'fk_parkinglot',
                            'insurance_startup_date',
                            'insurance_expiration_date',
                            'fk_vehicle_status',
                            'fk_entity_company',
                            'fk_federal_entity',
                            'passengers',
                            'fk_seat_type',
                            'doors',
                            'fk_suspension_type',
                            'fk_branch',
                            'description',
                            'authorized_fuel_consumption',
                            'fk_vehicle_usage',
                            'bathroom',
                            'curtains',
                            'monitors',
                            'air_conditioning',
                            'trunk',
                            'seatbelt',
                            'boombox',
                            'advertisement',
                            'iave',
                            'radio_band',
                            'sim',
                            'activation_cost',
                            'last_odometer_fuel_ticket',
                            'imei',
                            'fk_vehicle_status_group',
                            'fk_gps_provider',
                            'fk_vehicle_origin',
                            'fk_vehicle_fleet',
                            'pk_entity',
                            'fk_entity_status',
                            'codename',
                            'fk_zone',
                            'fk_vehicle_type as type_id', 'vehicles_types.name as type_name', 'fk_manufacturer as manufacturer_id','manufacturers.name as manufacturer_name','version','vehicle_year as year','entities.name as company','fk_vehicle_model as model_id', 'vehicles_models.name as name_model',
                                'vehicles_catalog.fk_fuel as fuel_id', 'fuels.name as fuel_name')->paginate(50);

           return $companys;

    }

 
   
}
