<?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()
    {
       
    }

    /**
     * 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|regex:/(07)[0-9]{8}/',
            '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');
    }
}
