<?php

namespace App\Http\Controllers\Webprofile\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\Webprofile\SliderRepository;
use Storage;

class SliderController extends Controller
{
    private $repo;

    public function __construct(SliderRepository $repo)
    {
        $this->repo = $repo;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $data = $this->repo->get(null, null, ['created_at', 'desc']);
            return $this->repo->datatable($data);
        }

        return view('webprofile.backend.sliders.index')->withTitle(trans('feature.slider'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('webprofile.backend.sliders.create')->withTitle(trans('feature.create_slider'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // dd($request);
        $request -> validate([
            'title' => 'required',
            'slider' => 'required|mimes:jpg,jpeg,png|max : 3072'
        ], [
            'title.required' => 'Nama slider wajib diisi',
            // 'title.max' => 'Nama slider terlalu panjang',
            'slider.required' => 'Gambar wajib diupload',
            'slider.mimes' => 'File yang diupload harus JPG, JPEG, dan PNG',
            'slider.max' => 'Ukuran gambar maksimal 3 MB'
        ]);

        $this->repo->store($request, 'slider');

        return redirect()->route('sliders.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = $this->repo->findId($id);

        $data = [
            'data' => $data,
        ];

        return view('webprofile.backend.sliders.edit', $data)->withTitle(trans('feature.edit_slider'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request -> validate([
            'title' => 'required',
            // 'slider' => 'required|mimes:jpg,jpeg,png|max : 3072'
        ], [
            'title.required' => 'Nama slider wajib diisi',
            // 'title.max' => 'Nama slider terlalu panjang',
            // 'slider.required' => 'Gambar wajib diupload',
            'slider.mimes' => 'File yang diupload harus berupa JPG, JPEG, dan PNG',
            'slider.max' => 'Ukuran gambar maksimal 3 MB'
        ]);

        $slider = $this->repo->findId($id);
        $edit = $this->repo->update($request, $slider, 'slider');

        return redirect()->route('sliders.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = $this->repo->findId($id);
        $this->repo->destroy($data, 'slider');

        return response()->json(['done']);
    }
}
