<?php

namespace App\Http\Controllers;

use App\Godown;
use App\Rack;
use App\Order;
use Illuminate\Http\Request;

class GodownController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Godowns = Godown::all();
        return view('Godown.view',compact('Godowns'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('Godown.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request-> validate([
            'title'=>'required|max:255',
            'narration'=>'max:255',
        ]);
         Godown::create($request->all());
        //dd($request);
        
        return redirect(route('Godown.index'))->with('Message','Saved Successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Godown  $godown
     * @return \Illuminate\Http\Response
     */
    public function show(Godown $godown)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Godown  $godown
     * @return \Illuminate\Http\Response
     */
    public function edit(Godown $Godown)
    {
        return view('Godown.edit',compact('Godown'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Godown  $godown
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Godown $Godown)
    {
        $request-> validate([
            'title'=>'required|max:255',
            'narration'=>'max:255',        
            
        ]);

        $Godown->update([
            'title'=>$request->title,
            'narration'=>$request->narration,
        ]);
        
           
        return redirect(route('Godown.index'))->with('Message','Updated Successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Godown  $godown
     * @return \Illuminate\Http\Response
     */
    public function destroy(Godown $Godown)
    {
        $Godown->delete();
        return redirect(route('Godown.index'))->with('Message','Deleted Successfully');
    }

    public static function GodownListForCombo($Godown_id)
    {
                    $Godowns=Godown::all();
                    return view('Godown.combo',compact('Godowns','Godown_id'));
    }

    public function AddNew()
    {
        $Godowns = Godown::all();
        return view('Godown.index',compact('Godowns'));
    }

    public function SelectGodown(Godown $Godown)
    {
        $Racks = Rack::where('godown_id',$Godown->id)->get();
        return view('Godown.RackView',compact('Racks','Godown'));
    }

    public function RackDetails(Request $request){ 
        
        $Orders = Order::where('rack_id',$request->rack_id)->where('status_id',"<", 8)->get();
        
        
        return view('Godown.RackDetails', compact('Orders')); 
      
    }
}
