<?php
/**
 * Created by PhpStorm.
 * User: stana
 * Date: 19.02.2017
 * Time: 10:38
 */

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App;
use App\User;
use App\Address;
use App\Room;

use Cornford\Googlmapper\Facades\MapperFacade as Mapper;

class DetailController extends Controller
{
    public function index($accommodation_id){
        $accommodation = App\Accommodation::find($accommodation_id);
        $user = User::where('accommodation_id','=',$accommodation_id)->where('owner','=','true')->first();

        $address = Address::find($accommodation->address_id);

        $rooms = DB::table('rooms')
            ->leftJoin('images','rooms.image_id','=','images.image_id')
            ->where('accommodation_id','=',$accommodation->accommodation_id)
            ->orderBy('rooms.room_name')
            ->get();

        $equipment = DB::table('accommodation_equipment')
            ->join('equipment','accommodation_equipment.equipment_id', '=', 'equipment.equipment_id')
            ->select('equipment_name')->where('accommodation_equipment.accommodation_id','=',$accommodation_id)
            ->get();

        $rooms_number = Room::where('accommodation_id', '=', $accommodation->accommodation_id)->count();
        $beds = Room::where('accommodation_id', '=', $accommodation->accommodation_id)->sum('beds');

        Mapper::location($address->town.' ,'. $address->street_name.' ,'.$address->street_number)->map(['zoom' => 18, 'center' => true, 'marker' => true, 'type' => 'HYBRID', 'overlay' => 'TRAFFIC']);
        return view('detail', ['accommodation' => $accommodation, 'rooms' => $rooms, 'address' => $address, 'user' => $user, 'equipment' => $equipment, 'rooms_number' => $rooms_number, 'beds' => $beds]);
    }

}