<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Deliveryinfo;
use App\User;
use App\Company;
use Auth;

class DeliveryinfoController extends Controller
{
    public function index()
    {
        $deliveryinfos = Deliveryinfo::all();  
        return response()->json($deliveryinfos, 200);
    }
    
    /**
     * Display the specified delivery-info.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Deliveryinfo $id)
    {
        //$deliveryinfo = Deliveryinfo::where('com_id', Auth::user()->company())->where('id', $id)->first();
        $deliveryinfo = Deliveryinfo::find($id)->first();
        return response()->json($deliveryinfo, 200);
    }

    /**
     * Store a newly delivery-info in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $user = Auth::user();

        $deliveryinfo = new Deliveryinfo;
        $deliveryinfo->deli_info = $request->input('deli_info');
        $deliveryinfo->address = $request->input('address');
        $deliveryinfo->city = $request->input('city');
        $deliveryinfo->postcode = $request->input('postcode');
        $deliveryinfo->state = $request->input('state');
        $deliveryinfo->country = $request->input('country');
        $deliveryinfo->contact_person = $request->input('contact_person');
        $deliveryinfo->contact_person_no = $request->input('contact_person_no');
        $deliveryinfo->com_id = $user->company()->first()->id;
        $deliveryinfo->save();

        return response()->json(['deliveryinfo'=>$deliveryinfo],201);
    }

    /**
     * Update the delivery-info.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $user = Auth::user();
        //$deliveryinfo = Deliveryinfo::where('com_id', Auth::user()->company())->where('id', $id)->firstOrFail();
        $deliveryinfo = Deliveryinfo::find($id)->first();
        if (!$deliveryinfo)
        {
            return response()->json(['message'=>'Access Denied!'], 404);
        }

        $deliveryinfo->deli_info = $request->input('deli_info');
        $deliveryinfo->address = $request->input('address');
        $deliveryinfo->city = $request->input('city');
        $deliveryinfo->postcode = $request->input('postcode');
        $deliveryinfo->state = $request->input('state');
        $deliveryinfo->country = $request->input('country');
        $deliveryinfo->contact_person = $request->input('contact_person');
        $deliveryinfo->contact_person_no = $request->input('contact_person_no');
        $deliveryinfo->com_id = $user->company()->first()->id;
        $deliveryinfo->save();

        return response()->json(['deliveryinfo'=>$deliveryinfo],200);
        
    }

    /**
     * Remove the delivery-info.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Deliveryinfo $id)
    {
        //$deliveryinfo = Deliveryinfo::where('com_id', Auth::user()->company())->where('id', $id)->firstOrFail();
        $deliveryinfo = Deliveryinfo::find($id)->first();
        if (!$deliveryinfo)
        {
            return response()->json(['message'=>'Access Denied!'], 404);
        }
        
        $deliveryinfo->delete($id);

        return response()->json(['deliveryinfo'=>$deliveryinfo], 204);
    }
}
