<?php

namespace App\Http\Controllers;

use App\DataTables\CategoryDataTable;
use App\Models\Category;
use Illuminate\Http\Request;
use App\Http\Requests\CategoryRequest;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(CategoryDataTable $dataTable)
    {
        $pageTitle = __('message.list_form_title', ['form' => __('message.category')]);
        $auth_user = authSession();
        $assets = ['datatable'];
        $button = $auth_user->can('category-add') ? '<a href="' . route('category.create') . '" class="float-right btn btn-sm btn-primary jqueryvalidationLoadRemoteModel"><i class="fa fa-plus-circle"></i> ' . __('message.add_form_title', ['form' => __('message.category')]) . '</a>' : '';
        $multi_checkbox_delete = $auth_user->can('category-delete') ? '<button id="deleteSelectedBtn" checked-title = "category-checked" class="float-left btn btn-sm ">' . __('message.delete_selected') . '</button>' : '';
        return $dataTable->render('global.datatable', compact('assets', 'pageTitle', 'button', 'auth_user','multi_checkbox_delete'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $pageTitle = __('message.add_form_title', ['form' => __('message.category')]);
        return view('category.form', compact('pageTitle'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CategoryRequest $request)
    {
        $data = $request->all();
        $category = Category::create($data);
        $message = __('message.save_form', ['form' => __('message.category')]);
        if (request()->is('api/*')) {
            return json_message_response($message);
        }
        return response()->json(['status' => true, 'event' => 'submited', 'message' => $message]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $pageTitle = __('message.view_form_title', ['form' => __('message.category')]);
        $data = Category::findOrFail($id);

        return view('category.show', compact('pageTitle', 'data', 'id'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $pageTitle = __('message.update_form_title', ['form' => __('message.category')]);
        $data = Category::findOrFail($id);
        $is_required = $data->is_required;

        return view('category.form', compact('data', 'pageTitle', 'id', 'is_required'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CategoryRequest $request, $id)
    {
        $category = Category::find($id);
        $message = __('message.not_found_entry', ['name' => __('message.category')]);
        if ($category == null) {
            return json_custom_response(['status' => false, 'message' => $message]);
        }

        $category->update($request->all());

        $message = __('message.update_form', ['form' => __('message.category')]);
        if ($request->is('api/*')) {
            return json_message_response($message);
        }
        return response()->json(['status' => true, 'event' => 'submited', 'message' => $message]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $category = Category::find($id);

        $message = __('message.not_found_entry', ['name' => __('message.category')]);
        if ($category == null) {
            return json_message_response($message, 400);
        }
        if ($category != '') {
            $category->delete();
            $message = __('message.delete_form', ['form' => __('message.category')]);
        }
        if (request()->is('api/*')) {
            return json_message_response($message);
        }

        if (request()->ajax()) {
            return response()->json(['status' => true, 'message' => $message]);
        }
        return redirect()->route('category.index')->withSuccess($message);
    }
    public function action(Request $request)
    {
        $id = $request->id;
        $category = Category::withTrashed()->where('id',$id)->first();
        $message = __('message.not_found_entry',['name' => __('message.category')] );
        if($request->type === 'restore'){
            $category->restore();
            $message = __('message.msg_restored',['name' => __('message.category')] );
        }

        if($request->type === 'forcedelete'){
            $category->forceDelete();
            $message = __('message.msg_forcedelete',['name' => __('message.category')] );
        }
        if (request()->is('api/*')) {
            return json_custom_response(['message'=> $message, 'status' => true]);
        }else{
            return redirect()->route('category.index')->withSuccess($message);
        }
    }
}
