<?php

namespace App\Http\Controllers\Webprofile\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\Webprofile\GalleryRepository;

class GalleryController extends Controller
{
    private $repo;

    public function __construct(GalleryRepository $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.gallery.index')->withTitle(trans('feature.gallery'));
    }

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

    /**
     * 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',
            'gallery' => 'required|mimes:jpg,jpeg,png|max : 3072'
        ], [
            'title.required' => 'Nama gallery wajib diisi',
            // 'title.max' => 'Nama gallery terlalu panjang',
            'gallery.required' => 'Gambar wajib diupload',
            'gallery.mimes' => 'File yang diupload harus berupa JPG, JPEG, dan PNG',
            'gallery.max' => 'Ukuran gambar maksimal 3 MB'
        ]);

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

        return redirect()->route('gallery.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.gallery.edit', $data)->withTitle(trans('feature.edit_gallery'));
    }

    /**
     * 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',
            'gallery' => 'required|mimes:jpg,jpeg,png|max : 3072'
        ], [
            'title.required' => 'Nama gallery wajib diisi',
            // 'title.max' => 'Nama gallery terlalu panjang',
            'gallery.required' => 'Gambar wajib diupload',
            'gallery.mimes' => 'File yang diupload harus berupa JPG, JPEG, dan PNG',
            'gallery.max' => 'Ukuran gambar maksimal 3 MB'
        ]);

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

        return redirect()->route('gallery.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, 'gallery');

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