<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Dependants;

class DependantController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $departments = DB::table('departments')->get();
        $businessunits = DB::table('business_units')->get();
        $emergency = DB::table('emergency_contacts')->get();
        $dependants = DB::table('dependants')->get();
        return view('Employees.Registration', compact(['departments','businessunits','emergency','dependants']));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $dependants = Dependants::latest()->paginate(5);
        return response()->json($dependants);
    }

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

      $request->validate([
            'name'=>'required',
            'dateofBirth'   =>'required',
            'relation'=>'required',
            
          ]);
          $dependant = new Dependants([
            'name' => $request->get('name'),
            'dateofBirth'=> $request->get('dateofBirth'),
            'relation'=> $request->get('relation'),

          ]);
          $dependant->save();
          return redirect('/employees/create')->with('success', 'Dependant has been added'); 
   

    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request)
    {
        /* $businessunits = DB::table('business_units')->get();
        $departments = DB::table('departments')->get();
        $emergency = DB::table('emergency_contacts')->get();
        $dependants = DB::table('dependants')->get();
        if($request->ajax()){
            $dependant = Dependants::find($request->id);
            return Response($dependant);
        } */
      /*   $dependant = Dependants::find($id);
        return view('Employees.Registration', compact(['dependants','businessunits','departments','emergency'])); */
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $edit = Dependants::find($id)->update($request->all());
        return response()->json($edit);




        /* $request->validate([
            'name'=>'required',
            'dateofBirth'   => 'required',
            'relation'=>'required',
          ]);
    
          $dependant = Dependants::find($id);
          $dependant->name = $request->get('name');
          $dependant->dateofBirth = $request->get('dateofBirth');
          $dependant->relation = $request->get('relation');
          $dependant_id =$request->id;

          try {
            $dependant->save();
            $result = 0;
        } catch (\Exception $e) {
            $result = $e->errorInfo[1];
        }
    
        if ($result == 0) {
                $notification = array(
                'message' => 'Dependant Successfully Updated.',
                'alert-type' => 'success'
            );
            return redirect('/employees/create'.$dependant_id)->with($notification);
        } else {
            $notification = array(
                'message' => 'Something Error Found !, Please try again.',
                'alert-type' => 'error'
            );
            return redirect('/employees/create'.$dependant_id)->with($notification);
        } */
         
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
       
        $dependant = Dependants::find($id);
        $dependant->delete();
   
        return redirect('/employees/create')->with('success', 'Dependant has been deleted successfully');
    }
}




