<?php
if (isset($_POST['submit'])) {
    $coin = $_POST['coin'];
    $amount = $_POST['amount'];

    if ($coin == "BTC") {
      $wallet_address = $btc_wallet;
    }elseif ($coin == "ETH") {
      $wallet_address = $eth_wallet;
    }elseif ($coin == "LTC") {
      $wallet_address = $ltc_wallet;
    }elseif ($coin == "USDT") {
      $wallet_address = $usdt_wallet;
    }

    $proof = $_FILES["proof"]["name"];
    $tempname = $_FILES["proof"]["tmp_name"];
    $folder = "../images/$proof";





    if (empty($coin) || empty($amount) || empty($proof)) {
        $error = "<div class='alert alert-danger d-flex align-items-center' role='alert'>
          <i class='fa fa-warning'></i> &nbsp; Please select coin, enter amount and upload proof of payment to complete deposit
        </div>";
    } else {
      $d_amount = number_format("$amount",2);
      $transaction_status = "pending";
      $transaction_type = "Withdrawal Pin";
      $transaction_date = date("Y-m-d h:i:s");
      $character = '0123456789abcdefghijklmnopqrstuvwxyz';
      $randomString = '';
      for ($i=0; $i < 7; $i++) {
          $index = rand(0, strlen($character) -1);
          $randomString .=$character[$index];
      }
      $randomString;
      $trnx_hash = rand(100000, 999999);;

      if (move_uploaded_file($tempname, $folder)) {
        $sql = "INSERT INTO transaction (trans_ref, transaction_user_id, transaction_type, transaction_status, t_mode, transaction_amount, wallet_address, proof_upload, transaction_date) VALUES (?,?,?,?,?,?,?,?,?)";
        $stmt = $connection->prepare($sql);
        $stmt->bind_param("sssssssss", $trnx_hash, $user_id, $transaction_type, $transaction_status, $coin, $amount, $wallet_address, $proof, $transaction_date);
        if ($stmt->execute()){
          echo "<script>
            window.location.href='withdrawal_pin.php?status=success'
          </script>";
        }else {
          echo "<script>
            window.location.href='withdrawal_pin.php?status=error'
          </script>";
        }
      }else {
        echo "<script>
          window.location.href='withdrawal_pin.php?status=error'
        </script>";
      }
    }
  }


?>
