<?php
/**
 * Created by IntelliJ IDEA.
 * User: DennysO
 * Date: 6/3/2015
 * Time: 11:10 AM
 */
require_once "generic.php";

class EmailsSent extends Generic
{
    protected $table = 'emailssent';
    protected $fillable = ['id', 'userid', 'sent', 'replied', 'emailtemplateid', 'termid', 'lastupdated'];
    protected $primaryKey = 'id';

    public function send($id)
    {
        $template = new Templates();

        $term = new Terms();
        $termId = $term->getActiveTerm()->Id;

        $invite = (new Invitations())->whereRaw("ID = '".$id."' AND TermId = '".$termId."'")->get()[0];
        $settings = (new Settings())->getById($termId)->get()[0];

        $status = PROSPECTIVE;
        if(!$invite->Sent)
        {
            $status = PROSPECTIVE;
        }
        elseif(!$invite->Response)
        {
            $status = INVITED;
        }
        elseif(!$invite->Registerd)
        {
            $status = PENDING;
        }


        switch ($status) {
            case PROSPECTIVE:
                $sent = mail($invite->Email, $settings->Subject, $template->getEmailBody($id, $status),
                    "From: Masoud Sadjadi <sadjadi@cs.fiu.edu>\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n");
                break;

            case INVITED:
                $sent = mail($invite->Email,'Reminder to RSVP for ' . $settings->TermName . ' Senior Project showcase', $template->getEmailBody($id, $status), "From: Masoud Sadjadi <sadjadi@cs.fiu.edu>\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n");
                break;

            case PENDING:
                $sent = mail($invite->Email,'Reminder to complete registration for ' . $settings->TermName . ' Senior Project showcase', $template->getEmailBody($id, $status), "From: Masoud Sadjadi <sadjadi@cs.fiu.edu>\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=iso-8859-1\r\n");
                break;

        }

        return $sent;
    }
}