<?php

namespace App\Http\Controllers;

use App\Models\Booking;
use App\Models\BookingDraftItem;
use App\Repositories\BookingRepository;
use Input;

class BookingController extends Controller
{

    protected $bookingRepo;

    public function __construct(BookingRepository $bookingRepo)
    {
        $this->middleware('admin');
        $this->bookingRepo = $bookingRepo;
    }

    public function index($step = false)
    {
        $data = [
            //'bookings' => Booking::all()->sortByDesc("id"),
            'step' => $step
        ];
        return view('booking.index', $data);
    }

    public function detail($id)
    {
        if($id == "new") {
            $booking = new Booking();
            $id = 'new';

        }else{
            $booking = Booking::findOrFail($id);
            $id = $booking->id;
        }
        $data = [
            'booking' => $booking,
            'id' => $id,
        ];
        return view('booking.detail', $data);

    }

    public function store($id)
    {
          // \Session()->flash('alert-save', '1');


        $data = Input::all();
        if($id == "new") {
            $booking = new Booking();
        }else{
            $booking = Booking::findOrFail($id);
        }

        $booking->merlin_order_number = $data['merlin_order_number'];
        $booking->save();

        $i = 1;

        if($data['action'] == 'addItemUp'){
            $travel_program_id = null;
            $request_date = null;
            $comfort = 0;
            if(count($booking->booking_draft_items)){
                $first_booking_draft_item = $booking->booking_draft_items()->first();
                $travel_program_id = $first_booking_draft_item->travel_program_id;
                $request_date = $first_booking_draft_item->request_date;
                $comfort = $first_booking_draft_item->comfort;
            }
            $booking->booking_draft_items()->create([
                'booking_id' => $booking->id,
                'travel_program_id' => $travel_program_id,
                'fewo_lodging_id' => null,
                'travel_class_id' => null,
                'draft_item_id' => null,
                'draft_type_id' => null,
                'request_date' => $request_date,
                'days_start' => null,
                'days_duration' =>  null,
                'start_date' => null,
                'end_date' => null,
                'service' => '',
                'price_adult' => null,
                'adult' => null ,
                'price_children' => 0,
                'children' => 0,
                'price' => 0,
                'pos' => $i,
                'in_pdf' => true,
                'comfort' => $comfort
            ]);
            $i++;
        }


        if(isset($data['draft_item'])){

            foreach ($data['draft_item'] as $booking_draft_item_id => $draft_item){
                $di = BookingDraftItem::findOrFail($booking_draft_item_id);
                $di->draft_type_id = $draft_item['draft_type_id'];
                $di->start_date = $draft_item['start_date'];
                $di->end_date = $draft_item['end_date'];
                if(isset($draft_item['days_duration'])){
                    $di->days_duration = $draft_item['days_duration'];
                }
                if(isset($draft_item['price'])){
                    $di->price = $draft_item['price'];
                }
                $di->service = $draft_item['service'];
                $di->in_pdf = isset($draft_item['in_pdf']) ? true : false;
                $di->pos =  $i++;
                $di->save();
            }
        }

        if($data['action'] == 'addItemDown'){
            $travel_program_id = null;
            $request_date = null;
            $comfort = 0;
            if(count($booking->booking_draft_items)){
                $first_booking_draft_item = $booking->booking_draft_items()->first();
                $travel_program_id = $first_booking_draft_item->travel_program_id;
                $request_date = $first_booking_draft_item->request_date;
                $comfort = $first_booking_draft_item->comfort;
            }
            $booking->booking_draft_items()->create([
                'booking_id' => $booking->id,
                'travel_program_id' => $travel_program_id,
                'fewo_lodging_id' => null,
                'travel_class_id' => null,
                'draft_item_id' => null,
                'draft_type_id' => null,
                'request_date' => $request_date,
                'days_start' => null,
                'days_duration' =>  null,
                'start_date' => null,
                'end_date' => null,
                'service' => '',
                'price_adult' => null,
                'adult' => null ,
                'price_children' => 0,
                'children' => 0,
                'price' => 0,
                'pos' => $i,
                'in_pdf' => true,
                'comfort' => $comfort
            ]);
        }
        if(strpos($data['action'], 'up_') !== false) {
            $reId = intval(str_replace('up_', '', $data['action']));
            $d_from = BookingDraftItem::findOrFail($reId);
            $d_to = $booking->findBeforeDraftItemRelation($reId);
            if($d_to) {
                $t_pos = $d_from->pos;
                $d_from->pos = $d_to->pos;
                $d_to->pos = $t_pos;
                $d_from->save();
                $d_to->save();
            }
        }
        if(strpos($data['action'], 'down_') !== false) {
            $reId = intval(str_replace('down_', '', $data['action']));
            $d_from = BookingDraftItem::findOrFail($reId);
            $d_to = $booking->findAfterDraftItemRelation($reId);
            if($d_to) {
                $t_pos = $d_from->pos;
                $d_from->pos = $d_to->pos;
                $d_to->pos = $t_pos;
                $d_from->save();
                $d_to->save();
            }
        }
        \Session()->flash('alert-save', '1');
        return redirect(route('booking_detail', [$booking->id]));

    }

    public function draftItemDelete($id){
        $boking_draft_item = BookingDraftItem::findOrFail($id);
        $booking_id = $boking_draft_item->booking_id;
        $boking_draft_item->delete();
        \Session()->flash('alert-success', __('Eintrag gelöscht'));
        return redirect(route('booking_detail', [$booking_id]));
    }




}


