<?php 

$dir = '../../';

require_once $dir.'includes/main.php';

$errors = array();

$name = clean($_POST['name']);
$paypal = clean($_POST['paypal']);
$contact = clean($_POST['contact']);
$default_status = clean($_POST['default_status']);
$daily_points = clean($_POST['daily_points']);
$points_deducted = clean($_POST['points_deducted']);
$point_price = clean($_POST['point_price']);

$tracking = mysqli_real_escape_string($con, $_POST['tracking']);
$ads = mysqli_real_escape_string($con, $_POST['ads']);

$default_status = strtolower($default_status);

if(!filter_var($paypal, FILTER_VALIDATE_EMAIL)){
    $errors[] = "PayPal email address is invalid";
}

if(!filter_var($contact, FILTER_VALIDATE_EMAIL)){
    $errors[] = "Contact email address is invalid";
}

if($daily_points < 0){
    $errors[] = "Daily points cannot be less than 0";
}

if($points_deducted < 0){
    $errors[] = "Points deducted cannot be less than 0";
}

// check errors
if(!empty($errors)){

    // send error
    $errors = implode('. ', $errors);
    $message = array('message' => $errors);
    echo json_encode($message);

}else{

    if(query("UPDATE _settings SET website_name = '{$name}', paypal = '{$paypal}', email = '{$contact}', default_status = '{$default_status}', daily_points = '{$daily_points}', daily_deducted = '{$points_deducted}', point_price = '{$point_price}', tracking_code = '{$tracking}', ads_code = '{$ads}'")){

        // send error
        $message = array('message' => 'Website has been updated successfully');
        echo json_encode($message);

    }else{

        // send error
        $message = array('message' => ERROR_DATABASE);
        echo json_encode($message);

    }

}

?>