<?php
include 'header.php';

if(isset($_GET['tx']) && isset($_GET['id'])){
    $did = $_GET['id'];
    $tx = $_GET['tx'];
    $query = mysqli_query($link, "SELECT * FROM deposits WHERE id = '$did' ");
    if(mysqli_num_rows($query) > 0){
        $drow = mysqli_fetch_assoc($query);
        $amount = $drow['amount'];
        $status = $drow['status'];
        $method = $drow['method'];
        $date = $drow['date'];
        $wallet_address = $payMethods[$method]['address'];
        
    }
}else{
    echo "<script>window.location.href = 'deposit.php' </script>";
}

if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["sub-depo"])) {

    $deposit_id = text_input($_POST["deposit_id"]);
    $hashvalue = text_input($_POST["hashvalue"]);

    // Check if proof of payment is uploaded
    if (!isset($_FILES["prf"]) || $_FILES["prf"]["error"] != 0) {
        $error = "Please upload a valid proof of payment.";
    } else {
        // File upload settings
        $target_dir = "../uploads/proofs/";
        $file_name = basename($_FILES["prf"]["name"]);
        $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
        $allowed_exts = ["jpg", "jpeg", "png", "pdf"];
        
        // Validate file type
        if (!in_array($file_ext, $allowed_exts)) {
            $error = "Invalid file type. Only JPG, PNG, and PDF allowed.";
        } else {
            // Generate unique filename
            $new_filename = "proof_" . time() . "_" . uniqid() . "." . $file_ext;
            $target_file = $target_dir . $new_filename;

            // Move uploaded file
            if (move_uploaded_file($_FILES["prf"]["tmp_name"], $target_file)) {
                // Update deposit record
                $stmt = mysqli_prepare($conn, "UPDATE deposits SET tranxId = ?, status = 'submitted', proof = ? WHERE id = ? AND userId = ?");
                mysqli_stmt_bind_param($stmt, "ssii", $tx, $new_filename, $deposit_id, $userId);

                if (mysqli_stmt_execute($stmt)) {
                    $msg = "Deposit confirmation submitted successfully!";
                    
                    // Fetch user details
                    $user_stmt = mysqli_prepare($conn, "SELECT email, fname FROM users WHERE id = ?");
                    mysqli_stmt_bind_param($user_stmt, "i", $userId);
                    mysqli_stmt_execute($user_stmt);
                    $result = mysqli_stmt_get_result($user_stmt);
                    $user = mysqli_fetch_assoc($result);
                    
                    // Send confirmation email
                    $to = $user["email"];
                    $subject = "Deposit Confirmation Submitted";
                    $message = "Dear " . $user["fname"] . ",\n\nYour deposit confirmation has been received. Our team will review and approve it soon.\n\nTransaction Hash: $hashvalue\n\nBest regards,\nSupport Team";
                    sendMail($to, $subject, $message);

                    // SweetAlert success message with redirect
                    echo "<script>
                        Swal.fire({
                            icon: 'success',
                            title: 'Success!',
                            text: '$msg',
                            confirmButtonColor: '#28a745'
                        }).then(() => {
                            window.location.href = 'deposit.php';
                        });
                    </script>";
                } else {
                    $error = "Database error: " . mysqli_error($conn);
                }
            } else {
                $error = "Error uploading file.";
            }
        }
    }

    // Show error using SweetAlert
    if (!empty($error)) {
        echo "<script>
            Swal.fire({
                icon: 'error',
                title: 'Error!',
                text: '$error',
                confirmButtonColor: '#d33'
            });
        </script>";
    }
}
?>


<main class="main-content w-full px-[var(--margin-x)] pb-8">


      <div class="flex items-center space-x-4 py-5 lg:py-6">
        <h2 class="text-xl font-medium text-slate-800 dark:text-navy-50 lg:text-2xl">
          Deposit-Payment 
        </h2>
        <div class="hidden h-full py-1 sm:flex">
          <div class="h-full w-px bg-slate-300 dark:bg-navy-600"></div>
        </div>
        <ul class="hidden flex-wrap items-center space-x-2 sm:flex">
          <li class="flex items-center space-x-2">
            <a class="text-primary transition-colors hover:text-primary-focus dark:text-accent-light dark:hover:text-accent"
              href="#"><?php echo ucfirst($fname) ?></a>
            <svg x-ignore xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24"
              stroke="currentColor">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
            </svg>
          </li>
          <li>Deposit-Payment</li>
        </ul>
      </div>
      <style>
        @media (max-width:680px) {
          .mw {
            width: 100% !important;
          }
        }
      </style>
      <div class="grid grid-cols-12 gap-12 sm:gap-5 lg:gap-6">

        <div class="col-span-12 lg:col-span-4 mw">
          <div class="card space-y-3 p-5 sm:p-5">



            <div class="flex items-center justify-between ">
              <div
                class=" text-base  rounded-full  border-slate-300 text-slate-800 dark:border-navy-450 dark:text-navy-50">
                Payment Info
              </div>



            </div>

            <div class=" text-center  ">
              <div
                class=" text-base  rounded-full  border-slate-300 text-slate-800 dark:border-navy-450 dark:text-navy-50">
                <code class="text-xs"> You are depositing $<?php echo $amount ?> into your account </code>
              </div>



            </div>


            <div class=" flex  items-center justify-between ">
              <div class="text-xs+ font-medium  border-slate-300 text-slate-800 dark:border-navy-450 dark:text-navy-50">
                <span>Trans ID</span>
              </div>
              <div
                class="text-base  rounded-full  border-slate-300 text-slate-800 dark:border-navy-450 dark:text-navy-50">
                <span class=" h-8 px-4 text-xs+ font-medium text-info "><?php echo $tx ?>
                </span>
              </div>


            </div>


            <div class=" flex items-center justify-between ">
              <div
                class=" text-xs+ font-medium  border-slate-300 text-slate-800 dark:border-navy-450 dark:text-navy-50">
                <span>Date/Time</span>
              </div>
              <div
                class="text-base  rounded-full  border-slate-300 text-slate-800 dark:border-navy-450 dark:text-navy-50">
                <span class=" h-8 px-4 text-xs+ font-medium text-info">
                  <?php echo date("D, j M Y", strtotime($date)); ?> </span>
              </div>


            </div>



            <div class="flex  items-center justify-between ">
              <div class="text-xs+ font-medium  border-slate-300 text-slate-800 dark:border-navy-450 dark:text-navy-50">
                Amount
              </div>
              <div
                class="text-base  rounded-full  border-slate-300 text-slate-800 dark:border-navy-450 dark:text-navy-50">
                <span class=" h-8 px-4 text-xs+ font-medium text-info ">
                  $ <?php echo $amount ?> </span>
              </div>


            </div>


            <div class=" text-center  ">
              <div class="">
                <span
                  class="text-xs btn h-10 px-4 py-4 bg-error/10 text-error hover:bg-error/20 focus:bg-error/20 active:bg-error/25"><i
                    class="fa fa-exclamation-circle mr-1"></i> Review Deposit and Confirm </span>

              </div>



            </div>


          </div>
        </div>


        <div class="col-span-12 grid lg:col-span-8">
          <div class="card">
            <div class="border-b border-slate-200 p-4 dark:border-navy-500 sm:px-5">
              <div class="flex items-center space-x-2">
                <div
                  class="flex h-7 w-7 items-center justify-center rounded-lg bg-primary/10 p-1 text-primary dark:bg-accent-light/10 dark:text-accent-light">
                  <i class="fa-solid fa-layer-group"></i>
                </div>
                <h4 class=" text-base font-medium text-slate-700 dark:text-navy-100">
                  Deposit-Payment
                </h4>
              </div>
            </div>
            <div class="space-y-4 p-4 sm:p-5  items-center ">
              <div class="text-center">
                <span class="font-medium text-xl"> </span> <br>
                <span class="font-medium text-xs text-success"><?php echo strtoupper($method) ?> ADDRESS</span>


              </div>

              <div class="flex flex-col items-center space-y-3 text-center">
                <div class="h-32 w-32 rounded bg-gradient-to-r from-sky-400 to-blue-600 p-0.5">
                  <!--  <img class="h-full w-full rounded border-2 border-white object-cover object-center dark:border-navy-700" src="../masteradmin/QR/" alt="qrcode">-->
                  <img
                    class="h-full w-full rounded border-2 border-white object-cover object-center dark:border-navy-700"
                    src="https://api.qrserver.com/v1/create-qr-code/?data=<?php echo $wallet_address ?>&amp;size=300x300" alt="Wallet Barcode"
                    title="Wallet Barcode" />
                </div>

              </div>

              <div class="grid grid-cols-1 gap-4 sm:grid-cols-1">


                <div class="grid grid-cols-1 ">





                  <div class="relative flex -space-x-px">
                    <script>
                      function myFunction() {
                        var copyText = document.getElementById("myInput");
                        copyText.select();
                        copyText.setSelectionRange(0, 99999);
                        navigator.clipboard.writeText(copyText.value);

                        var tooltip = document.getElementById("toolbut");
                        tooltip.innerHTML = "Copied";

                      }

                      function getout() {
                        var tooltip2 = document.getElementById("toolbut");
                        tooltip2.innerHTML = "  <i class='fa fa-copy mr-1'></i> Copy ";
                      }


                    </script>
                    <input
                      class="form-input peer w-full rounded-l-lg border border-slate-300 bg-transparent px-3 py-2 pl-9 placeholder:text-slate-400/70 hover:z-10 hover:border-slate-400 focus:z-10 focus:border-primary dark:border-navy-450 dark:hover:border-navy-400 dark:focus:border-accent"
                      type="text" value="<?php echo $wallet_address ?>" id="myInput" />

                    <div
                      class="pointer-events-none absolute flex h-full w-10 items-center justify-center text-slate-400 peer-focus:text-primary dark:text-navy-300 dark:peer-focus:text-accent">
                      <i>#</i>
                    </div>

                    <button
                      class="btn rounded-l-none bg-primary font-medium text-white hover:bg-primary-focus focus:bg-primary-focus active:bg-primary-focus/90 dark:bg-accent dark:hover:bg-accent-focus dark:focus:bg-accent-focus dark:active:bg-accent/90"
                      onclick="myFunction()" onmouseout="setTimeout(getout, 1500)" id="toolbut">
                      <i class="fa fa-copy mr-1"></i>
                      Copy
                    </button>
                  </div>

                  <form enctype="multipart/form-data" method="post">
                    <input type="hidden" name="deposit_id" value="<?php echo $did ?>">
                    <label class="block mt-4">
                      <div class="text-center">
                        <code>REFERENCE/TRANSACTION HASH (For Confirmation)</code>
                      </div>

                      <input
                        class="form-input mt-1.5 w-full rounded-lg border border-slate-300 bg-transparent px-3 py-2 placeholder:text-slate-400/70 hover:border-slate-400 focus:border-primary dark:border-navy-450 dark:hover:border-navy-400 dark:focus:border-accent"
                        placeholder="Enter Transaction Hash" type="text" name="hashvalue" />


                    </label> <br>

                    <label
                      class="btn block text-center bg-slate-150 font-medium text-slate-800 hover:bg-slate-200 focus:bg-slate-200 active:bg-slate-200/80 dark:bg-navy-500 dark:text-navy-50 dark:hover:bg-navy-450 dark:focus:bg-navy-450 dark:active:bg-navy-450/90">
                      <input tabindex="-1" type="file" class="pointer-events-none text-center absolute opacity-0"
                        name="prf" />
                      <div class="flex items-center text-center space-x-2">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24"
                          stroke="currentColor">
                          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                            d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12" />
                        </svg>
                        <span>Uplood Proof of Payment</span>
                      </div>
                    </label>
                </div>
              </div>

              <div class="flex justify-center space-x-2 pt-4">

                <button
                  class="btn space-x-2 bg-primary font-medium text-white hover:bg-success-focus focus:bg-success-focus active:bg-success-focus/90 dark:bg-success dark:hover:bg-success-focus dark:focus:bg-success-focus dark:active:bg-success/90"
                  type="submit" name="sub-depo">
                  <span>Submit</span>
                  <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                    <path fill-rule="evenodd"
                      d="M12.293 5.293a1 1 0 011.414 0l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-2.293-2.293a1 1 0 010-1.414z"
                      clip-rule="evenodd" />
                  </svg>
                </button>

              </div> <br>
              <div class="border border-slate-200 p-4 dark:border-navy-500 sm:px-5">
                <div class="flex items-center space-x-2">

                  <code class="text-sm font-medium text-error">
                    <i class="fa fa-exclamation-circle" aria-hidden="true"> </i> Note that this deposit will be voided, if you send any other amount.
                  </code>




                </div>

                <div class="flex items-center space-x-2">

                  <code class="text-sm font-medium text-warning">
                    <i class="fa fa-exclamation-circle" aria-hidden="true"> </i> Account will be funded, after proper review.
                  </code>




                </div>
              </div>


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


        </div>
      </div>


    </main>




<?php
include 'footer.php';
?>