<?php

namespace App\Http\Controllers;

use App\Inventory;
use App\Order;
use App\OrderedItems;
use App\Product;
use App\Stock;
use Illuminate\Http\Request;
use DB;
use Illuminate\Database\Eloquent;

class OrdersController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        $orders = Order::latest()
            ->get();


        return view('orders.index', ['orders' => $orders]);

    }

    public function storeOrders(Request $request)
    {
        $orders = Order::where('store_id', auth()->user()->store_id)
            ->latest()->get();

        return view('orders.index', compact('orders'));
    }


    public function getStoreOrders(Request $request)
    {
//        dd($request);
        $storeId = $request->storeId;
        if ($storeId == 0)
        {
            $orders = Order::latest()->get();
            return view('orders.load', compact('orders'));
        }

        $orders = Order::where('store_id', $storeId)
            ->latest()->get();

        return view('orders.load', compact('orders'));
    }



    public function create()
    {
        //
    }



    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Order $orders
     * @return \Illuminate\Http\Response
     */
    public function show(Order $order)
    {

        $items = DB::table('ordered_items')
            ->where('order_id', $order->id)
            ->join('products', 'products.id', '=', 'ordered_items.product_id')
            ->select('ordered_items.*', 'products.name')->get();

            return view('orders.show', compact('order', 'items'));
    }


//    ORDER ISSUE
    public function orderIssue(Request $request)
    {
        $issue = OrderedItems::where('id', '=', $request->orderedItemId)
            ->first();


        $avail = $this->checkAvailability($issue->product_id, $issue->quantity);

        switch ($avail)
        {
            case 'success' : $this->deductStock($issue->product_id, $issue->quantity);
                $issue->status = 1;
                $issue->save();
            return response()->json(['avail'=> 'available', 'message'=>'the Item has been issued successfully.']);
            break;
            case 'failed' : return response()->json(['avail'=>'unavailable', 'message'=>'The item issued is out of stock in the inventory']);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Order $orders
     * @return \Illuminate\Http\Response
     */

    public function edit(Order $orders)
    {
        //
    }


    public function update(Request $request, Order $orders)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Order $orders
     * @return \Illuminate\Http\Response
     */
    public function destroy(Order $orders)
    {

    }


    public function checkAvailability($productID, $quantity)
    {
        $stock = Inventory::where('product_id', $productID)
            ->where('store_id',  auth()->user()->store_id)
            ->first();
        if ($stock != null && $stock->total_stock >= $quantity) {
            return 'success';
        } else {
            return 'failed';
        }
    }


    public function deductStock($product_id, $quantity)
    {
        $this->deductInventory($product_id, $quantity);
        $stock = Stock::where('product_id', $product_id)->first();

        if ($stock != null) {

            $stock->quantity = $stock->quantity - $quantity;

            $stock->save();//

            return 'deducted';
        } else {
            return 'failed';
        }
    }

    public function deductInventory($product_id, $quantity)
    {
        $inventory = Inventory::where('product_id', $product_id)
            ->where('store_id',  auth()->user()->store_id)
            ->first();

        $inventory->total_stock -= $quantity;
        $inventory->save();
        return 'deducted';
    }
}
