<?php
if (isset($_POST['deposit'])) {
    $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)) {
        $error = "<div class='alert alert-danger d-flex align-items-center' role='alert'>
          <i class='fa fa-warning'></i> &nbsp; Please select coin, enter deposit amount and upload proof of payment to complete deposit
        </div>";
    } else {
      $d_amount = number_format("$amount",2);
      $transaction_status = "pending";
      $transaction_type = "deposit";
      $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()){
            // send mail
            $to = "$email";
            $subject = "Deposit Alert";
            $message = "<div style='background: #000; padding: 10px;'>
            <br>
            <center><img src='$website_url/images/$logo_img' width='200px;'></center>
            <br>
            <div style='width: 90%; font-family: sans-serif; padding: 10px 4%; background: ; margin: 5px auto; border-radius: 0px; color:#fff;'>
            <center>
              <h3>Mail From $website_email</h3>
              <h4>Successsful Deposit</h4>
            </center>
            <hr>
            <p>
              <b>Hi $fname</b>, <br>
              Your deposit request of $currency$d_amount was successful. Your account will be funded once we verify your payment.
            <br><br>

            <div style='width:100%; padding:12px 1.5% 8px 1.5%; margin: 0; background: #151030;'>
              <h3 style='margin:0 0%;'>Transaction Summary</h3>
              <p style='font-size:80%; line-height:35px; margin:; text-align:left;'>
                Amount: <b> <span>$currency</span>$d_amount</b><br>
                Deposit Method: <b> $coin</b><br>
                $coin Address: <b> $wallet_address</b><br>
                Transaction ID: <b> $trnx_hash </b><br>
                IP address: <b> $userIP</b>
                <br><br>
              </p>
            </div>

            <br><br>
            Contact live support or email $website_email for any complain.
            </p>
            <hr>
            <br>

            </div>
            <br>
            <center><p style='font-size: 13px; color:#ccc'>$website_email<sup>TM</sup> </p></center>
            <br>
            </div>";

            // Always set content-type when sending HTML email
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

            // More headers
            $headers .= "From: $website_name <$website_email>" . "\r\n";
            $headers .= "Reply-To: $website_email \r\n";
            $headers .= "Return-Path: $website_email\r\n";
            // $headers .= "BCC: $website_email\r\n";

            if (mail($to,$subject,$message,$headers,"-f $website_email")){
              // send mail
              $to = "$website_email";
              $subject = "User Deposit Notification";
              $message = "
              <div style='background: #E4E9F0; padding: 10px;'>
              <br>
              <div style='font-family: arial; padding:10px 0 0 0; margin: 5px; background: white; margin: 5px 5%; border:1px solid rgba(0,0,0,0.4);'>
              <center><img src='$website_url/images/$logo_img' width='200px;'></center>
                <p style='font-size:100%; line-height:20px; margin: 0 3%'>
                  Hello Admin, a deposit just occur on the system.<br>
                  $full_name just make a payment of $currency$d_amount into their account.
                  <br>
                  Payment Method: $coin
                  <br>
                  Wallet Address: $wallet_address
                  <br>
                  Date: $transaction_date
                </p>

                <br>

                <p style='font-size:100%; line-height:20px; margin:0 3%;'>
                  Best regards, <br>
                  $website_name.
                </p>
                <br><br>
              </div><br>
              </div>";

              // Always set content-type when sending HTML email
              $headers = "MIME-Version: 1.0" . "\r\n";
              $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

              // More headers
              $headers .= "From: $website_name <$website_email>" . "\r\n";
              $headers .= "Reply-To: $website_email \r\n";
              $headers .= "Return-Path: $website_email\r\n";
              // $headers .= "BCC: $website_email\r\n";

              if (mail($to, $subject, $message, $headers,"-f $website_email")) {
                echo "<script>
                  window.location.href='deposit.php?status=success'
                </script>";
                } else {
                  echo "<script>
                    window.location.href='deposit.php?status=error'
                  </script>";
                }
            }else {
              echo "<script>
                window.location.href='deposit.php?status=error'
              </script>";
            }
        }else {
          echo "<script>
            window.location.href='deposit.php?status=error'
          </script>";
        }
      }else {
        echo "<script>
          window.location.href='deposit.php?status=error'
        </script>";
      }
    }
  }


?>
