<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
include '../../../config/index.php';
$data = array();
$obj = json_decode(file_get_contents('php://input'), true);

$sql = "SELECT * FROM global_settings ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {

        $sql_two = "UPDATE global_settings SET
                CreditDiscountThreshold='" . $obj["CreditDiscountThreshold"] . "',
                MentorSessionCreditValue='" . $obj["CreditValue"] . "',
                FreeParentCredits='" . $obj["FreeParentCredits"] . "'
                WHERE global_settings_id='" . $row["global_settings_id"] . "'
                ";

        if (mysqli_query($conn, $sql_two)) {
            $sql_delete = "DELETE FROM  CreditMentorPaymentRates WHERE global_settings_id='" . $row["global_settings_id"] . "'";
            if (mysqli_query($conn, $sql_delete)) {
            }
            foreach ($obj["selectedRates"] as $SelectedRate) {

                $sql_credit = "INSERT INTO CreditMentorPaymentRates VALUES(NULL,
                    '" . $row["global_settings_id"] . "',
                    '" . $SelectedRate . "',
                    'false', 
                    'false',  
                    now())";

                if (mysqli_query($conn, $sql_credit)) {
                }
            }
            $new_array = array(
                'Success' => "true",
                'Reason' => "You have succesfully Updated settings",


            );
            echo json_encode($new_array);
        } else {
            $new_array = array(
                'Success' => "false",
                'Reason' => "You were unable to succesfully Update settings",

            );
            echo json_encode($new_array);
        }
    }
} else {

    $sql_two = "INSERT INTO global_settings VALUES(NULL,
         '0', '0', '0', '0', '0', '0', '0', '0',
         'KES', '0', '0', '0',
         '0',  '0','0', '" . $obj["CreditDiscountThreshold"] . "', '" . $obj["CreditValue"] . "', '" . $obj["FreeParentCredits"] . "','false', 'false', 'false', 'false',  now() )";
    if (mysqli_query($conn, $sql_two)) {
        $global_settings_id = mysqli_insert_id($conn);
        foreach ($obj["selectedRates"] as $SelectedRate) {
            $sql_credit = "INSERT INTO CreditMentorPaymentRates VALUES(NULL,
                '" . $global_settings_id . "',
                '" . $SelectedRate . "',
                'false', 
                'false',  
                now())";

            if (mysqli_query($conn, $sql_credit)) {
            }
        }
        $new_array = array(
            'Success' => "true",
            'Reason' => "You have succesfully Updated settings",
        );
        echo json_encode($new_array);
    } else {
        $new_array = array(
            'Success' => "false",
            'Reason' => "You were unable to succesfully Update settings",
        );
        echo json_encode($new_array);
    }
}
