<?php

namespace App\Http\Controllers;

use App\Models\Cover;
use App\Models\Member;
use App\Models\User;
use App\Notifications\CompletedTask;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CoverController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
       return view('covers/new_cover');
    }

    public function all()
    {
        $covers = Cover::all();        
        return view('covers/allcovers', compact('covers'));
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'sum_assured' => 'required',
            'monthly_pay' => 'required',
            'underwritter_name' => 'required',
            'waiting_period' => 'required',
            'member_spouse_amount' => 'required',
            '_15_to_21_amount' => 'required',
            '_6_to_14_amount' => 'required',
            '_1_to_5_amount' => 'required',
            'stillborn_to_11_amount' => 'required',
            'min_age_entry' => 'required',
            'max_age_entry' => 'required'                     
        ]);
        $cover = new Cover();
        
        $cover->name = $request->name;
        $cover->sum_assured = $request->sum_assured;
        $cover->monthly_pay = $request->monthly_pay;
        $cover->underwritter_name = $request->underwritter_name;
        $cover->waiting_period = $request->waiting_period;
        $cover->member_spouse_amount = $request->member_spouse_amount;
        $cover->_15_to_21_amount = $request->_15_to_21_amount;
        $cover->_6_to_14_amount = $request->_6_to_14_amount;
        $cover->_1_to_5_amount = $request->_1_to_5_amount;
        $cover->stillborn_to_11_amount = $request->stillborn_to_11_amount;
        $cover->min_age_entry= $request->min_age_entry;
        $cover->max_age_entry = $request->max_age_entry;
        $cover->user_id = Auth::id();

        $cover->save();
        return redirect('allcovers')->with('success', 'Saved Successfully!');
    }

    public function view($id)
    {
        $cover = Cover::find($id);
        return view('covers.coverView', compact('cover'));
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required',
            'sum_assured' => 'required',
            'monthly_pay' => 'required',
            'underwritter_name' => 'required',
            'waiting_period' => 'required',
            'member_spouse_amount' => 'required',
            '_15_to_21_amount' => 'required',
            '_6_to_14_amount' => 'required',
            '_1_to_5_amount' => 'required',
            'stillborn_to_11_amount' => 'required',
            'min_age_entry' => 'required',
            'max_age_entry' => 'required'                     
        ]);

        $cover = Cover::find($id);
        
        $cover->name = $request->name;
        $cover->sum_assured = $request->sum_assured;
        $cover->monthly_pay = $request->monthly_pay;
        $cover->underwritter_name = $request->underwritter_name;
        $cover->waiting_period = $request->waiting_period;
        $cover->member_spouse_amount = $request->member_spouse_amount;
        $cover->_15_to_21_amount = $request->_15_to_21_amount;
        $cover->_6_to_14_amount = $request->_6_to_14_amount;
        $cover->_1_to_5_amount = $request->_1_to_5_amount;
        $cover->stillborn_to_11_amount = $request->stillborn_to_11_amount;
        $cover->min_age_entry= $request->min_age_entry;
        $cover->max_age_entry = $request->max_age_entry;

        $cover->save();

        return redirect()->back()->with('success', 'Updated Successfully!');
    }

    public function delete($id)
    {
       /* Cover::find($id)->delete();*/

       //SEND NOTIFICATION ADMIN            
       $message = Auth::user()->name . " attempted to delete member # " . $id . " on " . Carbon::now();
       $users = User::where('role', 3)->get();
       foreach ($users as $user) {
           $user->notify(new CompletedTask($message));
       }

        return redirect()->back()->with('warning', 'contact System Admin!');
    }
}
