<?php

namespace App\Modules\Bookings;

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

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

    /**
     *	Returns the index page.
     */
    public function getIndex($id = 0)
    {
        if (! $id) {
            return Redirect::to('/');
        }

        $repair = Repair::find($id);

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

        // Optimize: Use pluck() instead of foreach loop to reduce memory usage
        $severities = Severity::pluck('name', 'id')->toArray();

        $data = [
            'users'      => User::getActiveList(),
            'severities' => $severities,
            'statuses'   => Status::repairs()->get(),
            'task'       => $repair,
        ];

        $this->layout->content = View::make('bookings::task', $data);
    }
}
