<?php
require_once 'ACP/database.php';
require_once "send_email.php";

class Submit extends database
{
    public function index($data)
    {
        return $this->insert('submission', $data);
    }
	
    public function getEmail()
    {
        return $this->fetchRow('email', $where);
    }
}

if(!isset($_POST['data']) || $_POST['data']== "")
{
    http_response_code(400);
    echo json_encode('Data Not Found');
    exit;
}
$data = $_POST['data'];

$errorMessage = '';
if(trim($data['repair']) == '')
{
    $errorMessage.= "Repair is required<br/>";
}

if(trim($data['name']) == '')
{
    $errorMessage.= "Name is required<br/>";
}
if(trim($data['email']) == '')
{
    $errorMessage.= "Email is required<br/>";
}
else if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL))
{
    $errorMessage.= "Please enter a valid email<br/>";
}

if(trim($data['phone']) == '')
{
    $errorMessage.= "Phone is required<br/>";
}

if(trim($errorMessage) == '')
{
    $ins = new Submit();

    $res = $ins->index($data);
    if($res)
    {
        $email = $ins->getEmail();
       
	    send_email($email['email'], $email['email'], 'New Contact Request', 'Repair:' . $data['repair'] . '<br/>Name:' . $data['name'] . '<br/>Email:' . $data['email'] . '<br/>Phone:' . $data['phone']);
        http_response_code(200);
        echo json_encode('Submitted Successfully');
        exit;
    }
                    
    http_response_code(500);
    echo json_encode(['Something went wrong']);
    exit;
}

http_response_code(400);
echo json_encode([$errorMessage]);
exit;
?>
