<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\EmergencyContact;

class EmergencyController 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.Employeeinfo', compact(['departments','businessunits','emergency','dependants']));
    }

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

    /**
     * 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',
            'phone_no'   =>'required',
            'email' =>'required',
            'relation'=>'required',
            
          ]);
          $emergency = new EmergencyContact([
            'name' => $request->get('name'),
            'phone_no'=> $request->get('phone_no'),
            'email'=> $request->get('email'),
            'relation'=> $request->get('relation'),

          ]);
          $emergency->save();
          return redirect('/employees/create')->with('success', 'Emergency Contact 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($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'name'=>'required',
            'phone_no'   =>'required|regex:/(07)[0-9]{8}/',
            'relation'=>'required',
          ]);
    
          $emergency = EmergencyContact::find($id);
          $emergency->name = $request->get('name');
          $emergency->phone_no = $request->get('phone_no');
          $emergency->email = $request->get('email');
          $emergency->relation = $request->get('relation');
          $emergency->save();
    
          return redirect('/employees/add')->with('success', 'Emergency Contact has been updated');
    }

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