<?php

namespace App\Http\Controllers\Administrator;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\ItemModel;
use Session;

class EditPOSController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $cart = Session::get('cart_edit');
        return response()->json([
            'success'=>true,'data'=>$cart
        ]);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $bag    = Session::get('cart_edit');
        $item   = ItemModel::find($id);
        
        if (isset($bag[$id])) {
            return response()->json(['success' => false, 'msg' => 'Item was already added!']);
        } else {
            $bag[$id] =    [
                'item_id'       => $id,
                'item_name'     => $item->item_house,
                'item_price'    => $item->item_price,
                'quantity'      => 1,
                'item_cost'     => $item->item_cost
            ];
            Session::put('cart_edit', $bag);
            return response()->json(['success'=>true,'msg'=>'Item Successfully Added to cart']);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cart = session()->get('cart_edit');
            if (isset($cart[$id])) {
                unset($cart[$id]);
                session()->put('cart_edit', $cart);
            }
            return response()->json(['success'=>true,'status'=>200,'data'=>'item removed successfully']);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
