<?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)
    {
        $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)
    {
        $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']);
    }
}