<?php

namespace App\Http\Controllers;

use App\DataTables\BranchesDataTable;
use App\Http\Requests\CreateBranchesRequest;
use App\Http\Requests\UpdateBranchesRequest;
use App\Http\Controllers\AppBaseController;
use App\Repositories\BranchesRepository;
use App\Services\ImageService;
use Illuminate\Http\Request;
use Flash;

class BranchesController extends AppBaseController
{
    /** @var BranchesRepository $branchesRepository*/
    private $branchesRepository;

    public function __construct(BranchesRepository $branchesRepo)
    {
        $this->branchesRepository = $branchesRepo;
    }

    /**
     * Display a listing of the Branches.
     */
    public function index(BranchesDataTable $branchesDataTable)
    {
    return $branchesDataTable->render('branches.index');
    }


    /**
     * Show the form for creating a new Branches.
     */
    public function create()
    {
        return view('branches.create');
    }

    /**
     * Store a newly created Branches in storage.
     */
    public function store(CreateBranchesRequest $request,ImageService $imageService)
    {
        $input = $request->all();

        if (request()->file('image_name')) {
            $image_name = request()->file('image_name');

            if (isset($image_name)) {
              $imageService = new ImageService();
              $file_name = $imageService->uploadWithSize(request(), null, 200);
              $input['image_name'] = $file_name;
            }
          }
          if(!isset($input['status'])){
            $input['status']=null;
          }
        $branches = $this->branchesRepository->create($input);

        Flash::success('Branches saved successfully.');

        return redirect(route('branches.index'));
    }

    /**
     * Display the specified Branches.
     */
    public function show($id)
    {
        $branches = $this->branchesRepository->find($id);

        if (empty($branches)) {
            Flash::error('Branches not found');

            return redirect(route('branches.index'));
        }

        return view('branches.show')->with('branches', $branches);
    }

    /**
     * Show the form for editing the specified Branches.
     */
    public function edit($id)
    {
        $branches = $this->branchesRepository->find($id);

        if (empty($branches)) {
            Flash::error('Branches not found');

            return redirect(route('branches.index'));
        }

        return view('branches.edit')->with('branches', $branches);
    }

    /**
     * Update the specified Branches in storage.
     */
    public function update($id, UpdateBranchesRequest $request)
    {
        $branches = $this->branchesRepository->find($id);
        $input = $request->all();
        if (empty($branches)) {
            Flash::error('Branches not found');

            return redirect(route('branches.index'));
        }

        if (request()->file('image_name')) {
            $image_name = request()->file('image_name');

            if (isset($image_name)) {
              $imageService = new ImageService();
              $file_name = $imageService->uploadWithSize(request(), null, 200);
              $input['image_name'] = $file_name;
            }
          }
          if(!isset($input['status'])){
            $input['status']=null;
          }
        $branches = $this->branchesRepository->update($input, $id);

        Flash::success('Branches updated successfully.');

        return redirect(route('branches.index'));
    }

    /**
     * Remove the specified Branches from storage.
     *
     * @throws \Exception
     */
    public function destroy($id)
    {
        $branches = $this->branchesRepository->find($id);

        if (empty($branches)) {
            Flash::error('Branches not found');

            return redirect(route('branches.index'));
        }

        $this->branchesRepository->delete($id);

        Flash::success('Branches deleted successfully.');

        return redirect(route('branches.index'));
    }
}
