<?php

namespace App\Http\Controllers\Admin;

use App\Helper\BreadcrumbsRegister;
use App\DataTables\Admin\OldcustomerDataTable;
use App\Http\Requests\Admin;
use App\Http\Requests\Admin\CreateOldcustomerRequest;
use App\Http\Requests\Admin\UpdateOldcustomerRequest;
use App\Repositories\Admin\OldcustomerRepository;
use App\Http\Controllers\AppBaseController;
use Laracasts\Flash\Flash;
use Illuminate\Http\Response;

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

    /** BreadCrumbName */
    private $BreadCrumbName;

    /** @var  OldcustomerRepository */
    private $oldcustomerRepository;

    public function __construct(OldcustomerRepository $oldcustomerRepo)
    {
        $this->oldcustomerRepository = $oldcustomerRepo;
        $this->ModelName = 'oldcustomers';
        $this->BreadCrumbName = 'Oldcustomers';
    }

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

    /**
     * Show the form for creating a new Oldcustomer.
     *
     * @return Response
     */
    public function create()
    {
        BreadcrumbsRegister::Register($this->ModelName,$this->BreadCrumbName);
        return view('admin.oldcustomers.create')->with(['title' => $this->BreadCrumbName]);
    }

    /**
     * Store a newly created Oldcustomer in storage.
     *
     * @param CreateOldcustomerRequest $request
     *
     * @return Response
     */
    public function store(CreateOldcustomerRequest $request)
    {
        $oldcustomer = $this->oldcustomerRepository->saveRecord($request);

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

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

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

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

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

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

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

    /**
     * Update the specified Oldcustomer in storage.
     *
     * @param  int              $id
     * @param UpdateOldcustomerRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateOldcustomerRequest $request)
    {
        $oldcustomer = $this->oldcustomerRepository->findWithoutFail($id);

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

        $oldcustomer = $this->oldcustomerRepository->updateRecord($request, $oldcustomer);

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

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

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

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

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