<?php

namespace App\Http\Controllers;

use App\DataTables\ItemsDataTable;
use App\Http\Requests\CreateItemsRequest;
use App\Http\Requests\UpdateItemsRequest;
use App\Http\Controllers\AppBaseController;
use App\Repositories\ItemsRepository;
use App\Services\ImageService;
use Illuminate\Http\Request;
use Flash;

class ItemsController extends AppBaseController
{
    /** @var ItemsRepository $itemsRepository*/
    private $itemsRepository;

    public function __construct(ItemsRepository $itemsRepo)
    {
        $this->itemsRepository = $itemsRepo;
    }

    /**
     * Display a listing of the Items.
     */
    public function index(ItemsDataTable $itemsDataTable)
    {
    return $itemsDataTable->render('items.index');
    }


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

    /**
     * Store a newly created Items in storage.
     */
    public function store(CreateItemsRequest $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;
          }
        $items = $this->itemsRepository->create($input);

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

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

    /**
     * Display the specified Items.
     */
    public function show($id)
    {
        $items = $this->itemsRepository->find($id);

        if (empty($items)) {
            Flash::error('Items not found');

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

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

    /**
     * Show the form for editing the specified Items.
     */
    public function edit($id)
    {
        $items = $this->itemsRepository->find($id);

        if (empty($items)) {
            Flash::error('Items not found');

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

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

    /**
     * Update the specified Items in storage.
     */
    public function update($id, UpdateItemsRequest $request,ImageService $imageService)
    {
        $items = $this->itemsRepository->find($id);

        if (empty($items)) {
            Flash::error('Items not found');

            return redirect(route('items.index'));
        }
        $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;
          }
        $items = $this->itemsRepository->update($input, $id);

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

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

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

        if (empty($items)) {
            Flash::error('Items not found');

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

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

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

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