<?php

namespace App\Modules\Bookings;

use Illuminate\Support\Facades\View;
use App\Models\User;
use App\Models\Severity;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\BaseController;
use App\Models\Status;

class FollowupController extends BaseController
{
    /**
     *	The layout that should be used for responses.
     */
    protected $layout = 'layouts.master';

    /**
     *	Returns the index page.
     */
    public function getIndex($id = 0)
    {

        //If user has not permissions redirect to dashboard
        if (! Auth::user()->hasPermission('read_followup_tasks')) {
            return Redirect::to('/');
        }

        if (! $id) {
            return Redirect::to('/');
        }

        $followup = Followup::find($id);

        // Redirect back to the dashboard if the ID was not
        // found or the task is not a followup one.
        if (! $followup or $followup->type != 'followup') {
            return Redirect::to('/');
        }

        $severities = [];
        foreach (Severity::all() as $severity) {
            $severities[$severity->id] = $severity->name;
        }

        $onsite = Onsite::find($followup->followup_id);

        $data = [
            'users'        => User::getActiveList(),
            'severities'   => $severities,
            'statuses'     => Status::repairs()->get(),
            'task'         => $onsite,
            'followup'     => $followup,
        ];
        $this->layout->content = View::make('bookings::task', $data);
    }
}
