<?php

namespace App\Http\Controllers;

use App\DataTables\StoreTypeDataTable;
use Illuminate\Http\Request;
use App\Models\StoreType;
use App\Http\Requests\StoreTypeRequest;

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreTypeRequest $request)
    {
        $data = $request->all();
        $storetype = StoreType::create($data);
        uploadMediaFile($storetype, $request->storetype_image, 'storetype_image');
        $message = __('message.save_form', ['form' => __('message.storetype')]);
        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)
    {
        //
    }

    /**
     * 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.storetype')]);
        $data = StoreType::findOrFail($id);

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

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

        $storetype->update($request->all());
        uploadMediaFile($storetype, $request->storetype_image, 'storetype_image');

        $message = __('message.update_form', ['form' => __('message.storetype')]);
        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)
    {
        $storetype = StoreType::find($id);

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

        if (request()->ajax()) {
            return response()->json(['status' => true, 'message' => $message]);
        }
        return redirect()->route('storetype.index')->withSuccess($message);
    }
}
