<?php
include('includes/header.php');
include('includes/navbar.php');
?>

<div class="container-fluid">

    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary" style="margin-bottom:5px">Add/Remove User Funds</h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
              <?php
                  if (isset($_POST['credit'])) {
                      $userid = $_POST['id'];
                      $account = $_POST['account'];
                      $amount = $_POST['amount'];

                      $d_amount = number_format("$amount",2);
                      $transaction_status = "success";
                      $transaction_type = "deposit";
                      $trxn_date = date("Y-m-d h:i:s");
                      $wallet_address = "****************";
                      $character = '0123456789abcdefghijklmnopqrstuvwxyz';
                      $randomString = '';
                      for ($i=0; $i < 7; $i++) {
                          $index = rand(0, strlen($character) -1);
                          $randomString .=$character[$index];
                      }
                      $randomString;
                      $trnx_hash = rand(100000, 999999);;


                      //get old balance
                      $sql_bal = "SELECT * FROM users WHERE user_id = '$userid'";
                      $result = $connection->query($sql_bal);
                      while ($row = $result->fetch_assoc()) {
                          $old_bonus_bal = $row['bonus'];
                          $old_profit_bal = $row['profit'];
                          $old_btc_bal = $row['btc'];
                          $old_eth_bal = $row['eth'];
                          $old_ltc_bal = $row['ltc'];
                          $old_usdt_bal = $row['usdt'];
                      }

                      if (empty($userid) || empty($account) || empty($amount)) {
                        $error = "empty";
                      }else {
                        if ($account == "bonus") {
                          $coin = "Bonus";
                          $new_bal = $old_bonus_bal + $amount;
                          $sql = "UPDATE users SET bonus = '$new_bal' WHERE user_id = '$userid'";
                          $sql_insert = "INSERT INTO 	transaction(trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, transaction_amount, wallet_address, transaction_date)
                          VALUES('$trnx_hash', '$userid', '$transaction_type', '$transaction_status', 'Bonus Topup', '$amount', '$wallet_address', '$trxn_date')";

                        }elseif ($account == "profit") {
                          $coin = "Profit";
                          $new_bal = $old_profit_bal + $amount;
                          $sql = "UPDATE users SET profit = '$new_bal' WHERE user_id = '$userid'";
                          $sql_insert = "INSERT INTO 	transaction(trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, transaction_amount, wallet_address, transaction_date)
                          VALUES('$trnx_hash', '$userid', '$transaction_type', '$transaction_status', 'Profit Topup', '$amount', '$wallet_address', '$trxn_date')";

                        }elseif ($account == "btc_bal") {
                          $coin = "BTC";
                          $new_bal = $old_btc_bal + $amount;
                          $sql = "UPDATE users SET btc = '$new_bal' WHERE user_id = '$userid'";
                          $sql_insert = "INSERT INTO 	transaction(trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, transaction_amount, wallet_address, transaction_date)
                          VALUES('$trnx_hash', '$userid', '$transaction_type', '$transaction_status', 'BTC Topup', '$amount', '$wallet_address', '$trxn_date')";

                        }elseif ($account == "eth_bal") {
                          $coin = "ETH";
                          $new_bal = $old_eth_bal + $amount;
                          $sql = "UPDATE users SET eth = '$new_bal' WHERE user_id = '$userid'";
                          $sql_insert = "INSERT INTO 	transaction(trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, transaction_amount, wallet_address, transaction_date)
                          VALUES('$trnx_hash', '$userid', '$transaction_type', '$transaction_status', 'ETH Topup', '$amount', '$wallet_address', '$trxn_date')";

                        }elseif ($account == "ltc_bal") {
                          $coin = "LTC";
                          $new_bal = $old_ltc_bal + $amount;
                          $sql = "UPDATE users SET ltc = '$new_bal' WHERE user_id = '$userid'";
                          $sql_insert = "INSERT INTO 	transaction(trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, transaction_amount, wallet_address, transaction_date)
                          VALUES('$trnx_hash', '$userid', '$transaction_type', '$transaction_status', 'LTC Topup', '$amount', '$wallet_address', '$trxn_date')";

                        }elseif ($account == "usdt_bal") {
                          $coin = "USDT";
                          $new_bal = $old_usdt_bal + $amount;
                          $sql = "UPDATE users SET usdt = '$new_bal' WHERE user_id = '$userid'";
                          $sql_insert = "INSERT INTO 	transaction(trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, transaction_amount, wallet_address, transaction_date)
                          VALUES('$trnx_hash', '$userid', '$transaction_type', '$transaction_status', 'USDT Topup', '$amount', '$wallet_address', '$trxn_date')";

                        }

                        if ($connection->query($sql)===TRUE && $connection->query($sql_insert)===TRUE) {
                          $error = "balance_success";
                          }else {
                            $error = "error";
                          }
                      }
                  }

              ?>

              <?php
                  if (isset($_POST['debit'])) {
                      $userid = $_POST['id'];
                      $account = $_POST['account'];
                      $amount = $_POST['amount'];

                      $d_amount = number_format("$amount",2);
                      $transaction_status = "success";
                      $transaction_type = "withdrawal";
                      $trxn_date = date("Y-m-d h:i:s");
                      $wallet_address = "****************";
                      $character = '0123456789abcdefghijklmnopqrstuvwxyz';
                      $randomString = '';
                      for ($i=0; $i < 7; $i++) {
                          $index = rand(0, strlen($character) -1);
                          $randomString .=$character[$index];
                      }
                      $randomString;
                      $trnx_hash = rand(100000, 999999);;


                      //get old balance
                      $sql_bal = "SELECT * FROM users WHERE user_id = '$userid'";
                      $result = $connection->query($sql_bal);
                      while ($row = $result->fetch_assoc()) {
                        $old_bonus_bal = $row['bonus'];
                        $old_profit_bal = $row['profit'];
                        $old_btc_bal = $row['btc'];
                        $old_eth_bal = $row['eth'];
                        $old_ltc_bal = $row['ltc'];
                        $old_usdt_bal = $row['usdt'];
                      }

                      if (empty($userid) || empty($account) || empty($amount)) {
                        $error = "empty";
                      }elseif ($account == "bonus" && $amount > $old_bonus_bal) {
                        $coin = "Bonus";
                        $error = "insufficient";
                      }elseif ($account == "profit" && $amount > $old_profit_bal) {
                        $coin = "Profit";
                        $error = "insufficient";
                      }elseif ($account == "btc_bal" && $amount > $old_btc_bal) {
                        $coin = "BTC";
                        $error = "insufficient";
                      }elseif ($account == "eth_bal" && $amount > $old_eth_bal) {
                        $coin = "ETH";
                        $error = "insufficient";
                      }elseif ($account == "ltc_bal" && $amount > $old_ltc_bal) {
                        $coin = "LTC";
                        $error = "insufficient";
                      }elseif ($account == "usdt_bal" && $amount > $old_usdt_bal) {
                        $coin = "USDT";
                        $error = "insufficient";
                      }else {
                        if ($account == "bonus") {
                          $coin = "Bonus";
                          $new_bal = $old_bonus_bal - $amount;
                          $sql = "UPDATE users SET bonus = '$new_bal' WHERE user_id = '$userid'";
                          $sql_insert = "INSERT INTO 	transaction(trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, transaction_amount, wallet_address, transaction_date)
                          VALUES('$trnx_hash', '$userid', '$transaction_type', '$transaction_status', 'Bonus withdrawal', '$amount', '$wallet_address', '$trxn_date')";

                        }elseif ($account == "profit") {
                          $coin = "Profit";
                          $new_bal = $old_profit_bal - $amount;
                          $sql = "UPDATE users SET profit = '$new_bal' WHERE user_id = '$userid'";
                          $sql_insert = "INSERT INTO 	transaction(trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, transaction_amount, wallet_address, transaction_date)
                          VALUES('$trnx_hash', '$userid', '$transaction_type', '$transaction_status', 'Profit withdrawal', '$amount', '$wallet_address', '$trxn_date')";

                        }elseif ($account == "btc_bal") {
                          $coin = "BTC";
                          $new_bal = $old_btc_bal - $amount;
                          $sql = "UPDATE users SET btc = '$new_bal' WHERE user_id = '$userid'";
                          $sql_insert = "INSERT INTO 	transaction(trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, transaction_amount, wallet_address, transaction_date)
                          VALUES('$trnx_hash', '$userid', '$transaction_type', '$transaction_status', 'BTC withdrawal', '$amount', '$wallet_address', '$trxn_date')";

                        }elseif ($account == "eth_bal") {
                          $coin = "ETH";
                          $new_bal = $old_eth_bal - $amount;
                          $sql = "UPDATE users SET eth = '$new_bal' WHERE user_id = '$userid'";
                          $sql_insert = "INSERT INTO 	transaction(trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, transaction_amount, wallet_address, transaction_date)
                          VALUES('$trnx_hash', '$userid', '$transaction_type', '$transaction_status', 'ETH withdrawal', '$amount', '$wallet_address', '$trxn_date')";

                        }elseif ($account == "ltc_bal") {
                          $coin = "LTC";
                          $new_bal = $old_ltc_bal - $amount;
                          $sql = "UPDATE users SET ltc = '$new_bal' WHERE user_id = '$userid'";
                          $sql_insert = "INSERT INTO 	transaction(trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, transaction_amount, wallet_address, transaction_date)
                          VALUES('$trnx_hash', '$userid', '$transaction_type', '$transaction_status', 'LTC withdrawal', '$amount', '$wallet_address', '$trxn_date')";

                        }elseif ($account == "usdt_bal") {
                          $coin = "USDT";
                          $new_bal = $old_usdt_bal - $amount;
                          $sql = "UPDATE users SET usdt = '$new_bal' WHERE user_id = '$userid'";
                          $sql_insert = "INSERT INTO 	transaction(trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, transaction_amount, wallet_address, transaction_date)
                          VALUES('$trnx_hash', '$userid', '$transaction_type', '$transaction_status', 'USDT withdrawal', '$amount', '$wallet_address', '$trxn_date')";

                        }

                        if ($connection->query($sql)===TRUE && $connection->query($sql_insert)===TRUE) {
                          $error = "balance_success";
                          }else {
                            $error = "error";
                          }
                      }
                  }

              ?>
              <form action="" method="POST">

                <div class="modal-body">

                    <div class="form-group">
                      <label for="">Select User </label>
                      <select name="id" style='color: var(--text); text-align: left' required class="form-control">
                          <option value="">Select User </option>
                          <?php
                              $sql = "SELECT * FROM users WHERE id != 1";
                              $result = $connection->query($sql);
                              while ($row = $result->fetch_assoc()) {
                                  $db_name = $row['full_name'];
                                  $db_id = $row['user_id'];
                                  $db_email = $row['email'];
                                  $db_balance = $row['balance'];
                                  $currency = $row['currency'];

                                  $d_bal = number_format("$db_balance",2);
                          ?>
                          <option value="<?php echo $db_id?>"><?php echo $db_name?> (<?php echo $db_email?>)</option>
                          <?php }?>
                      </select>
                    </div>

                    <div class="form-group">
                        <label for="">Select Account</label>
                        <select name="account" required class="form-control">
                            <option value="bonus">Bonus Balance</option>
                            <option value="profit">Profit Balance</option>
                            <option value="btc_bal">BTC Balance</option>
                            <option value="eth_bal">ETH Balance</option>
                            <option value="ltc_bal">LTC Balance</option>
                            <option value="usdt_bal">USDT Balance</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="">Amount</label>
                        <input type="text" class="form-control" name="amount" onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')" placeholder="Enter amount to Credit/Debit" required>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="submit" name="credit" class="btn btn-success">Credit</button>
                    <button type="submit" name="debit" class="btn btn-danger">Debit</button>
                </div>
              </form>

            </div>
        </div>
    </div>

</div>
<!-- /.container-fluid -->
<p id="error" style='display: none '><?php echo $error?></p>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
    var error = document.getElementById('error');

    if (error.textContent == "empty") {
      swal("EMPTY FIELD!", "Fill out all fields", "warning");

    }else if (error.textContent == "insufficient") {
      swal("Insufficient", "The amount is bigger than User <?php echo $coin ?> balance", "warning");

    }else if (error.textContent == "balance_success") {
      swal("Successful!", "User <?php echo $coin ?> balance updated successfully", "success");
        setTimeout(() => {
            window.location.href = ''
        }, 2000);
    }else if (error.textContent == "error") {
      swal("ERROR!", "Sorry!!!, Something went wrong, try later or contact support", "error");
    }

</script>
<?php
include('includes/scripts.php');
include('includes/footer.php');
?>
