<?php

session_start();

if (!isset($_POST['submit'])) {
    exit('Fail!');
}
include('../../conf/conn.php');

$_SESSION["address"] = mysqlSecurity($conn, $_POST["address"]);
$_SESSION["phone"] = mysqlSecurity($conn, $_POST["phone"]);
$_SESSION["page"] = 9;

$query = 'UPDATE `clients` SET `address` = "' . mysqlSecurity($conn, $_POST["address"]) . '", `phone` = "' . mysqlSecurity($conn, $_POST["phone"]) . '" WHERE `clients`.`id` = ' . $_SESSION["id"] . ';';
$result = mysqli_query($conn, $query);


//Submit for mailchimp service
include_once('../../mailchimp/mailchimp-rest-api.php');
$mc = new \VPS\MailChimp(MAILCHIMP_API_KEY);
$mc_Email = md5($_SESSION['email']);
$path = "/lists/" . MAILCHIMP_LIST_ID . "/members/{$mc_Email}";
if (isset($_POST['subscription'])) {
    $data = [
        'status' => 'subscribed'
    ];

    $mc->patch($path, $data);
} else {
    $data = [
        'status' => 'unsubscribed'
    ];

    $mc->patch($path, $data);
}

echo '<script>history.go(-1);alert("The editing was successful!");</script>';