<?php
/**
 * Created by PhpStorm.
 * User: stana
 * Date: 09.03.2017
 * Time: 12:02
 */

namespace App\Http\Controllers;

use App\Accommodation;
use App\Reviews;
use App\User;
use App\Room;
use App\Image;
use App\AccommodationEquipment;
use App\Reservation;
use App\Customer;
use App\Address;
use App\Booked;

class AdminController
{
    public function showAccommodations()
    {
        $users = User::where('role', '=', 'acco_admin')->where('owner', '=', 'true')->where('accommodation_id','!=',null)->get();
        foreach ($users as $user) {
            $accommodation_id = User::where('user_id', '=', $user->user_id)->pluck('accommodation_id')->first();
            $user->accommodation_name = Accommodation::where('accommodation_id', '=', $accommodation_id)->pluck('accommodation_name')->first();
            $user->permission = Accommodation::where('accommodation_id', '=', $accommodation_id)->pluck('permission')->first();
            $user->accommodation_id = $accommodation_id;
        }
        return view('acco_list', ['users' => $users]);
    }

    public function permissionOK($accommodation_id)
    {
        $accommodation = Accommodation::find($accommodation_id);
        $accommodation->permission = 'true';
        $accommodation->save();

        flash('Ubytování je povoleno', 'success');
        return redirect('acco_list');
    }

    public function permissionKO($accommodation_id)
    {
        $accommodation = Accommodation::find($accommodation_id);
        $accommodation->permission = 'false';
        $accommodation->save();

        flash('Ubytování je zakázáno', 'success');
        return redirect('acco_list');
    }


    public function deleteUser($user_id)
    {
        // GET AUTH USER
        $user = User::find($user_id);

        // GET HIS ACCOMMODATION
        $accommodation = Accommodation::find($user->accommodation_id);
        $users = User::where('accommodation_id', '=', $accommodation->accommodation_id)->get();
        ///echo dd($accommodation, $users);
        // GET ROOMS
        User::where('accommodation_id', '=', $accommodation->accommodation_id)->delete();
        $rooms = Room::where('accommodation_id', '=', $accommodation->accommodation_id)->get();

        // FOREACH ROOM
        foreach ($rooms as $room) {
            $booked = Booked::where('room_id', '=', $room->room_id)->get();
            // DELETE BOOKED
            if ($booked != null) {
                foreach ($booked as $item) {
                    $item->delete();
                }
            }

            $reservations = Reservation::where('room_id', '=', $room->room_id)->get();
            foreach ($reservations as $reservation) {
                $customer = Customer::where('customer_id', '=', $reservation->customer_id)->first();
                if($reservation <> null){
                    $reservation->delete();
                }
                if ($customer <> null){
                    $customer->delete();
                }
            }

            $image = Image::find($room->image_id);

            if ($image != null) {
                $image->delete();
            }
            $room->delete();
        }

        $address_id = Accommodation::where('accommodation_id', '=', $accommodation->accommodation_id)->pluck('address_id');
        $address = Address::find($address_id);
        $address->delete();

        $reviews = Reviews::where('accommodation_id', '=', $accommodation->accommodation_id)->get();
        if ($reviews != null) {
            foreach ($reviews as $review) {
                $review->delete();
            }
        }

        $image = Image::find($accommodation->image_id);
        if ($image != null) {
            $image->delete();
        }
        AccommodationEquipment::where('accommodation_id', '=', $accommodation->accommodation_id)->delete();
        $accommodation->delete();

        flash('Ubytování bylo i se správci úspěšně odstraněno', 'success');
        return redirect('acco_list');
    }
}