<?php

namespace App\Http\Controllers\Admin;

use App\Helper\BreadcrumbsRegister;
use App\DataTables\Admin\TeamDataTable;
use App\Http\Requests\Admin;
use App\Http\Requests\Admin\CreateTeamRequest;
use App\Http\Requests\Admin\UpdateTeamRequest;
use App\Models\Unit;
use App\Models\User;
use App\Repositories\Admin\TeamRepository;
use App\Http\Controllers\AppBaseController;
use Laracasts\Flash\Flash;
use Illuminate\Http\Response;

class TeamController extends AppBaseController
{
    /** ModelName */
    private $ModelName;

    /** BreadCrumbName */
    private $BreadCrumbName;

    /** @var  TeamRepository */
    private $teamRepository;

    public function __construct(TeamRepository $teamRepo)
    {
        $this->teamRepository = $teamRepo;
        $this->ModelName      = 'teams';
        $this->BreadCrumbName = 'Teams';
    }

    /**
     * Display a listing of the Team.
     *
     * @param TeamDataTable $teamDataTable
     * @return Response
     */
    public function index(TeamDataTable $teamDataTable)
    {
        BreadcrumbsRegister::Register($this->ModelName, $this->BreadCrumbName);
        return $teamDataTable->render('admin.teams.index', ['title' => $this->BreadCrumbName]);
    }

    /**
     * Show the form for creating a new Team.
     *
     * @return Response
     */
    public function create()
    {
        $units    = Unit::pluck('name', 'id')->all();
        $leads_id = User::pluck('email', 'id')->all();
        BreadcrumbsRegister::Register($this->ModelName, $this->BreadCrumbName);
        return view('admin.teams.create')->with(['title' => $this->BreadCrumbName, 'units' => $units, 'leads_id' => $leads_id]);
    }

    /**
     * Store a newly created Team in storage.
     *
     * @param CreateTeamRequest $request
     *
     * @return Response
     */
    public function store(CreateTeamRequest $request)
    {
        $team = $this->teamRepository->saveRecord($request);

        Flash::success($this->BreadCrumbName . ' saved successfully.');
        if (isset($request->continue)) {
            $redirect_to = redirect(route('admin.teams.create'));
        } elseif (isset($request->translation)) {
            $redirect_to = redirect(route('admin.teams.edit', $team->id));
        } else {
            $redirect_to = redirect(route('admin.teams.index'));
        }
        return $redirect_to->with([
            'title' => $this->BreadCrumbName
        ]);
    }

    /**
     * Display the specified Team.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $team = $this->teamRepository->findWithoutFail($id);

        if (empty($team)) {
            Flash::error($this->BreadCrumbName . ' not found');
            return redirect(route('admin.teams.index'));
        }

        BreadcrumbsRegister::Register($this->ModelName, $this->BreadCrumbName, $team);
        return view('admin.teams.show')->with(['team' => $team, 'title' => $this->BreadCrumbName]);
    }

    /**
     * Show the form for editing the specified Team.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $team = $this->teamRepository->findWithoutFail($id);

        if (empty($team)) {
            Flash::error($this->BreadCrumbName . ' not found');
            return redirect(route('admin.teams.index'));
        }
        $units    = Unit::pluck('name', 'id')->all();
        $leads_id = User::pluck('email', 'id')->all();
        BreadcrumbsRegister::Register($this->ModelName, $this->BreadCrumbName, $team);
        return view('admin.teams.edit')->with(['team' => $team, 'title' => $this->BreadCrumbName, 'units' => $units, 'leads_id' => $leads_id]);
    }

    /**
     * Update the specified Team in storage.
     *
     * @param  int $id
     * @param UpdateTeamRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateTeamRequest $request)
    {
        $team = $this->teamRepository->findWithoutFail($id);

        if (empty($team)) {
            Flash::error($this->BreadCrumbName . ' not found');
            return redirect(route('admin.teams.index'));
        }

        $team = $this->teamRepository->updateRecord($request, $team);

        Flash::success($this->BreadCrumbName . ' updated successfully.');
        if (isset($request->continue)) {
            $redirect_to = redirect(route('admin.teams.create'));
        } else {
            $redirect_to = redirect(route('admin.teams.index'));
        }
        return $redirect_to->with(['title' => $this->BreadCrumbName]);
    }

    /**
     * Remove the specified Team from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $team = $this->teamRepository->findWithoutFail($id);

        if (empty($team)) {
            Flash::error($this->BreadCrumbName . ' not found');
            return redirect(route('admin.teams.index'));
        }

        $this->teamRepository->deleteRecord($id);

        Flash::success($this->BreadCrumbName . ' deleted successfully.');
        return redirect(route('admin.teams.index'))->with(['title' => $this->BreadCrumbName]);
    }
}
