<?php

namespace App\Http\Controllers\Webprofile\Backend;

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

class CategoriesFileController extends Controller
{
    private $repo;

    public function __construct(CategoryFileRepository $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();
            return $this->repo->datatable($data);
        }

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

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

    /**
     * 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([
            'name' => 'required',
        ], [
            'name.required' => 'Kategori dokumen wajib diisi',
            // 'name.regex' => 'Kategori dokumen tidak valid! Isian hanya berupa angka dan huruf',
            // 'name.max' => 'Kategori dokumen terlalu panjang'
        ]);
        $data = $request->except('_token');

        array_key_exists('is_active', $data) ? $data['is_active'] = 1 : $data['is_active'] = 0;

        $this->repo->store($data);

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

    /**
     * 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([
            'name' => 'required',
        ], [
            'name.required' => 'Kategori dokumen wajib diisi',
            // 'name.regex' => 'Kategori dokumen tidak valid! Isian hanya berupa angka dan huruf',
            // 'name.max' => 'Kategori dokumen terlalu panjang',
        ]);
        
        $data = $request->except(['_token', 'id']);

        array_key_exists('is_active', $data) ? $data['is_active'] = 1 : $data['is_active'] = 0;

        $category = $this->repo->findId($id);
        $edit = $this->repo->update($data, $category);

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

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