<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'admin/database.php';
//require_once "send_email.php";

class Submit extends database
{
    public function index($data)
    {
        
        return $this->insert('new_submit', $data);
    }
	
  
}

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

$errorMessage = '';


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

if(trim($data['preffered_time']) == '')
{
    $errorMessage.= "Time is required<br/>";
    
}
if(trim($data['preferred_date']) == '')
{
    $errorMessage.= "Preferred Date is required<br/>";
}
else
{
      $data['preferred_date'];
     $data['preferred_date'] = date("Y-m-d", strtotime($data['preferred_date']));
}


if(trim($data['no_of_persons']) == '')
{
    $errorMessage.= "No. of Persons is required<br/>";
}


if(trim($data['terms']) != 1)
{
    $errorMessage.= "Please check Terms. <br/>";
}

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

    $res = $ins->index($data);
    if($res)
    {
         $message = 'Paldies par veikto rezervāciju.
Gadījumā,ja vēlaties pārcelt rezervācijas laikus vai datumus lūgums zvanīt uz tālruni +371 27997660

Jauku dienu vēlot
Silrak restorāns.';
        file_get_contents('https://api.text2reach.com/sms/send?api_key=7CA3176DACFA18AC84354AC614DA8398&unicode=true&phone=' . urlencode($data['phone']) . '&from='. urlencode('SILRAK REST') . '&message=' . urlencode($message));
        http_response_code(200);
        echo json_encode(['Form Submitted Successfully']);
        exit;
    }
                    
   http_response_code(500);
    echo json_encode(['error_description' => 'Something went wrong']);
    exit;
}

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