<?php

namespace App\Http\Controllers\Admin;

use App\Helper\BreadcrumbsRegister;
use App\DataTables\Admin\LeadHistoryDataTable;
use App\Http\Requests\Admin;
use App\Http\Requests\Admin\CreateLeadHistoryRequest;
use App\Http\Requests\Admin\UpdateLeadHistoryRequest;
use App\Repositories\Admin\LeadHistoryRepository;
use App\Http\Controllers\AppBaseController;
use Laracasts\Flash\Flash;
use Illuminate\Http\Response;

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

    /** BreadCrumbName */
    private $BreadCrumbName;

    /** @var  LeadHistoryRepository */
    private $leadHistoryRepository;

    public function __construct(LeadHistoryRepository $leadHistoryRepo)
    {
        $this->leadHistoryRepository = $leadHistoryRepo;
        $this->ModelName = 'lead-histories';
        $this->BreadCrumbName = 'Lead Histories';
    }

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

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

    /**
     * Store a newly created LeadHistory in storage.
     *
     * @param CreateLeadHistoryRequest $request
     *
     * @return Response
     */
    public function store(CreateLeadHistoryRequest $request)
    {
        $leadHistory = $this->leadHistoryRepository->saveRecord($request);

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

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

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

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

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

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

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

    /**
     * Update the specified LeadHistory in storage.
     *
     * @param  int              $id
     * @param UpdateLeadHistoryRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateLeadHistoryRequest $request)
    {
        $leadHistory = $this->leadHistoryRepository->findWithoutFail($id);

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

        $leadHistory = $this->leadHistoryRepository->updateRecord($request, $leadHistory);

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

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

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

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

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