<?php
/**
 * Created by PhpStorm.
 * User: stana
 * Date: 19.02.2017
 * Time: 11:06
 */

namespace App\Http\Controllers;

use Auth;
use Illuminate\Http\Request;
use Validator;
use Log;
use App\Accommodation;
use App;
use App\Address;
use Illuminate\Support\Facades\Input;
use App\User;


class NewAccoController
{
    public function __construct()
    {
    }

    public function index()
    {
        $equipment = App\Equipment::all('equipment_id', 'equipment_name');
        $accommodation_id = User::where('user_id','=',Auth::user()->id)->pluck('accommodation_id')->first();

        if($accommodation_id == null){
            return view('new_acco', ['equipment' => $equipment]);
        } else{
            flash('Již máte vytvořené ubytování', 'info');
            return redirect('edit_rooms');
        }
    }

    public function storeAccommodation(Request $request)
    {
        $acco_exists = User::where('user_id','=', Auth::user()->user_id)->where('accommodation_id','<>', null)->first();
        if($acco_exists != null){
            flash('Nelze vytvořit na jeden profil více ubytování než jedno','danger');
            return redirect('home');
        }
        // MESSAGES
        $messages = [
            'accommodation_name.required' => 'Vyplňte název ubytování',
            'accommodation_description.required' => 'Vyplňte popis ubytování',
            'town.required' => 'Zadejte název města nebo vesnice',
            'street_name.required' => 'Zadejte název ulice',
            'street_number.required' => 'Zadejte číslo domu',
            'street_number.integer' => 'Neplatné znaky, zadejte číslice',
            'website.unique' => 'Na tuto stránku se již odkazuje jiné ubytování',
            'website.max' => 'Adresa je příliš dlouhá',
            'website.regex' => 'Adresa nesplňuje formu',
            'image.max' => 'Zadejte obrázek s maximální velikostí 1 MB',
            'image.mimes' => 'Zadejte pouze obrázky ve formátu PNG nebo JPEG',
            'accommodation_phone_number.required' => 'Zadejte kontaktní telefonní číslo na ubytovní',
            'accommodation_phone_number.regex' => 'Telefonní číslo není ve správném formátu: (+ 420) XXX XXX XXX',
            'accommodation_email.unique' => 'Zadaný e-mail již existuje v systému',
            'accommodation_email.required' => 'Zadejte kontaktní e-mail na ubytování',
            'accommodation_email.email' => 'Zadejte e-mail ve správném tvaru'

        ];

        // VALIDATION
        $validator = Validator::make($request->all(), [
            'accommodation_name' => 'required|max:100',
            'accommodation_description' => 'required|max:255',
            'town' => 'required|max:45',
            'street_name' => 'required|max:45',
            'street_number' => 'required|integer|max:100000',
            'image' => 'max:1000|mimes:jpeg,jpg,png',
            'website' => 'required|unique:accommodations|max:100|regex:/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/',
            'accommodation_email' => 'required|email|max:255|unique:accommodations',
            'accommodation_phone_number' => 'required|regex:/^(\+420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/',
        ], $messages);

        // IN CASE OF WRONG INPUT
        if ($validator->fails()) {
            return redirect('new_acco')->withErrors($validator)->withInput();
        }

        // CREATE ADDRESS TO ACCOMMODATION
        $address = new Address([
            'town' => $request->town,
            'street_name' => $request->street_name,
            'street_number' => $request->street_number,
        ]);
        $address->save();

        // CREATE NEW ACCOMMODATION
        $accommodation = new Accommodation([
            'accommodation_name' => $request->accommodation_name,
            'accommodation_description' => $request->accommodation_description,
            'website' => $request->website,
            'accommodation_phone_number' => $request->accommodation_phone_number,
            'accommodation_email' => $request->accommodation_email,
            'address_id' => $address->address_id
        ]);

        if($request->image != null){
            $image = new App\Image([
                'image' => file_get_contents($request->file('image'))
            ]);
            $image->save();
            $accommodation->image_id = $image->image_id;
        }
        $accommodation->save();

        $user = User::find(Auth::user()->user_id);
        $user->accommodation_id = $accommodation->accommodation_id;
        $user->owner = 'true';
        $user->save();

        // SAVE SELECTED EQUIPMENT IN ACCOMMODATION
        $equip = Input::get('equip');
        if ($equip != null) {
            foreach ($equip as $item) {
                $accoequip = new App\AccommodationEquipment([
                    'accommodation_id' => $accommodation->accommodation_id,
                    'equipment_id' => $item
                ]);
                $accoequip->save();
            }
        }



        // REDIRECT TO ROOM LIST
        return redirect('edit_rooms');
    }
}