<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Mail\Suscripcion;
use App\Models\Email;
use App\Models\Welcome;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class EmailAdminController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $emails = Email::OrderBy('id','DESC')->paginate();
        return view('admin.emails.index', compact('emails'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('admin.emails.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $email = new Email();
        $email->email = $request->email;
        $email->emailencryption = md5($request->email);
        $email->save();

        $mail = Welcome::find(1);

        Mail::to($request->email)->send(new Suscripcion($email->emailencryption, $mail));
        
        return redirect()->route('emails.index');
    }

    /**
     * Display the specified resource.
     */
    public function show(Email $email)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Email $email)
    {
         return view('admin.emails.edit', compact('email'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Email $email)
    {
        $email->update($request->all());
        return redirect()->route('emails.index');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Email $email)
    {
        $email->delete();
        return redirect()->route('emails.index');
    }
}
