<?php

// BACKEND CONTROLLER WITH MODEL,EVENT-LISTENER

namespace Rvsitebuilder\Queuesharedhost\Http\Controllers\Backend;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
//use Rvsitebuilder\Queuesharedhost\Events\AfterQueuesharedhostCreatedEvent;
use Illuminate\Support\Facades\DB;
use Rvsitebuilder\Queuesharedhost\Http\Requests\QueuesharedhostRequest;
use Rvsitebuilder\Queuesharedhost\Models\Queuesharedhost;

class QueueSharedHostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function index(Request $request)
    {
        $jobs = DB::table('jobs')->get();

        if ($request->ajax()) {
            return view('queuesharedhost::backend.queuesharedhost.index', compact('jobs'))->renderSections()['content'];
        }

        return view('queuesharedhost::backend.queuesharedhost.index', compact('jobs'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(QueuesharedhostRequest $request)
    {
        $response = [
                'status' => 'fail',
                'msg' => '',
                'debug_msg' => '',
        ];

        $queuesharedhost = new Queuesharedhost;
        $queuesharedhost->queue_name = $request->queue_name;
        $queuesharedhost->queue_xx = $request->queue_xx;
        $queuesharedhost->save();
        $insertedQueuesharedhostId = $queuesharedhost->id;

        $response = [
                'status' => 'success',
                'msg' => 'The item has create.',
                'item_id' => $insertedQueuesharedhostId,
                //'debug_msg' => $debug_msg
        ];

        return \Response::json($response);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param mixed   $itemid
     *
     * @return Response
     */
    public function update(QueuesharedhostRequest $request, $itemid)
    {
        $response = [
                'status' => 'fail',
                'msg' => '',
                'debug_msg' => '',
        ];

        $queuesharedhost = Queuesharedhost::find($itemid);

        if ($queuesharedhost) {
            $queuesharedhost->queue_name = $request->queue_name;
            $queuesharedhost->queue_xx = $request->queue_xx;
            $queuesharedhost->save();
        }

        $response = [
                'status' => 'success',
                'msg' => 'The item has update.',
                //'debug_msg' => $debug_msg
        ];

        return \Response::json($response);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param mixed $itemid
     *
     * @return Response
     */
    public function destroy($itemid)
    {
        $response = [
                'status' => 'fail',
                'msg' => '',
                'debug_msg' => '',
        ];

        $queuesharedhost = Queuesharedhost::find($itemid);

        if ($queuesharedhost) {
            $queuesharedhost->delete();
        }

        $response = [
                'status' => 'success',
                'msg' => 'The item has delete.'.$itemid,
                //'debug_msg' => $debug_msg
        ];

        return \Response::json($response);
    }
}
