<?php

namespace App\Http\Controllers\Webprofile\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Webprofile\CategoriesFile;
use App\Repositories\Webprofile\FileRepository;

class FileController extends Controller
{
    private $repo;

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categoriesFile = CategoriesFile::where('is_active', 1)->orderBy('name', 'asc')->pluck('name', 'id');

        $data = [
            'categoriesFile' => $categoriesFile,
        ];
        
        return view('webprofile.backend.file.create', $data)->withTitle(trans('feature.create_file'));
    }

    /**
     * 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([
            'categories_file' => 'required',
            'title' => 'required',
            'file' => 'required|mimes:jpg,jpeg,png,doc,docx,pdf,xlsx,mp3,mp4,mkv,mpeg|max : 5120'
        ], [
            'categories_file.required' => 'Kategori dokumen wajib diisi',
            'title.required' => 'Nama dokumen wajib diisi',
            // 'title.max' => 'Nama dokumen terlalu panjang',
            'file.required' => 'File wajib diupload',
            'file.mimes' => 'File yang diupload harus berupa jpg, jpeg, png, doc, docx, pdf, xlsx, mp3, mp4, mkv, atau mpeg',
            'file.max' => 'Ukuran gambar maksimal 5 MB'
        ]);

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

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

        $categoriesFile = CategoriesFile::where('is_active', 1)->orderBy('name', 'asc')->pluck('name', 'id');

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

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

    /**
     * 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([
            'categories_file' => 'required',
            'title' => 'required',
            'file' => 'mimes:jpg,jpeg,png,doc,docx,pdf,xlsx,mp3,mp4,mkv,mpeg|max : 5120'
        ], [
            'categories_file.required' => 'Kategori dokumen wajib diisi',
            'title.required' => 'Nama dokumen wajib diisi',
            // 'title.max' => 'Nama dokumen terlalu panjang',
            // 'file.required' => 'File wajib diupload',
            'file.mimes' => 'File yang diupload harus berupa jpg, jpeg, png, doc, docx, pdf, xlsx, mp3, mp4, mkv, atau mpeg',
            'file.max' => 'Ukuran gambar maksimal 5 MB'
        ]);

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

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

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