<?php

namespace App\Modules\Stock;

use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;

class SuppliersControllerREST extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        $query = Supplier::query()->select('*');
        $limit = 100;
        if ($request->input('all')) 
        {
            $limit = 9999;
        }
        // Conditional statements based on GET parameters
        if ($request->input('query')) {
            $query = $query->where('name', 'like', '%'.$request->input('query').'%');
        }

        // Apply limits and execute the query
        // $query = $query->take(Input::get('limit', 20))->skip(Input::get('skip', 0))->get();
        $query = $query->paginate(9999);

        $response = [
            'success'      => true,
            'data'         => $query->getCollection()->toArray(),
            'total'        => $query->total(),
            'current_page' => $query->currentPage(),
            'last_page'    => $query->lastPage(),
            'from'         => $query->firstItem(),
            'to'           => $query->lastItem(),
        ];

        return response()->json($response);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(Request $request)
    {
        $supplier = new Supplier();

        $supplier->fill([
            'name'              => $request->input('name'),
        ]);

        if ($supplier->save()) {
            return response()->json($supplier->toArray());
        } else {
            return response()->json(['errors' => $supplier->errors->all()], 400);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function show($id)
    {
        $supplier = Supplier::find($id);

        if ($supplier) {
            return response()->json($supplier->toArray());
        } else {
            return response()->json(['error' => 'Not found.'], 404);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function update($id, Request $request)
    {
        $supplier = Supplier::find($id);

        if (! $supplier) {
            return response()->json(['error' => 'Not found.'], 404);
        }

        $supplier->fill($request->all());

        if ($supplier->save()) {
            return response()->json($supplier->toArray());
        } else {
            return response()->json(['errors' => $supplier->errors->all()], 400);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        Stock::query()->where('supplier_id', $id)->update(['supplier_id' => null]);

        if (! Supplier::find($id)->delete()) {
            return response()->json(['error' => 'Unable to delete supplier'], 400);
        }
    }
}
