<?php

require_once '../admin/database.php';

class subscribe extends database
{
    public function insertData($data)
    {
        if(!$this->fetchRow('tb_subscriptions', $data))
        {
            return $this->insert('tb_subscriptions', $data);
            exit;
        }
        return 1;
    }
}

$error = '';

if(!isset($_POST['phone']) || trim($_POST['phone']) == '')
{
    $error.= 'Tālruņa numurs ir norādīts nekorekti.<br/>';
}

$phone = $_POST['phone'];



			if($phone != strip_tags($phone))
			{
				$error.= 'Sorry. Cannot insert this string<br/>'; 
			}
			else if(strpos($phone, '\'"'))
			{
				$error.= 'Sorry. Cannot insert this string'; 
			}
			
			
			if(trim($error) != '')
			{
			    http_response_code(400);
			    echo json_encode(['error_text' => $error]);
			    exit;
			}

$obj = new subscribe();

$data['cl_phone'] = $phone;
$res = $obj->insertData($data);


if(!$res)
{
    http_response_code(500);
    echo json_encode(['error_text' => 'Kaut kas nogāja greizi!Lūdzu mēģini atkal.']);
    exit;
}


http_response_code(200);
    echo json_encode(['message' => 'Paldies!']);
    exit;