<?php
session_start();

require("../includes/db_connect.php");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    if ((!empty($_POST['btcAddress'])) && (!empty($_POST['ethAddress'])) && (!empty($_POST['usdtAddress'])) && (!empty($_POST['minWithdrawal'])) && (!empty($_POST['maxWithdrawal'])) && (!empty($_POST['ercAddress'])) && (!empty($_POST['trcAddress'])) && (!empty($_POST['ltcAddress']))
    ) {
        if ($conn) {
            try {
                $btcAddress = $_POST['btcAddress'];
                $ethAddress = $_POST['ethAddress'];
                $usdtAddress = $_POST['usdtAddress'];
                $ercAddress = $_POST['ercAddress'];
                $trcAddress = $_POST['trcAddress'];
                $ltcAddress = $_POST['ltcAddress'];
                $minWithdrawal = $_POST['minWithdrawal'];
                $maxWithdrawal = $_POST['maxWithdrawal'];

                if (
                    is_numeric($minWithdrawal) && is_numeric($maxWithdrawal)
                ) {
                    $minWithdrawal = round($minWithdrawal, 2);
                    $maxWithdrawal = round($maxWithdrawal, 2);

                    $settings_sql = "UPDATE app_settings SET value=? WHERE action=?";

                    $conn->prepare($settings_sql)->execute([$btcAddress, "btc_address"]);
                    $conn->prepare($settings_sql)->execute([$ethAddress, "eth_address"]);
                    $conn->prepare($settings_sql)->execute([$usdtAddress, "usdt_address"]);
                    $conn->prepare($settings_sql)->execute([$ercAddress, "erc_address"]);
                    $conn->prepare($settings_sql)->execute([$trcAddress, "trc_address"]);
                    $conn->prepare($settings_sql)->execute([$ltcAddress, "ltc_address"]);
                    $conn->prepare($settings_sql)->execute([$minWithdrawal, "min_withdrawal"]);
                    $conn->prepare($settings_sql)->execute([$maxWithdrawal, "max_withdrawal"]);

                    die("success");
                } else {
                    die("Make sure to enter numbers only for number fields.");
                }
            } catch (PDOException $e) {
                die('An unexpected error occurred. Connection failed');
            }
        } else {
            die('An unexpected error occurred. Connection failed.');
        }
    } else {
        die('Invalid user information.');
    }
} else {
    die('Invalid request.');
}