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


<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">
    Transactions
  </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="#">User</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>Transactions</li>
  </ul>
</div>
<div class="card p-4">
  <div x-data="{activeTab:'tabHome'}" class="tabs flex flex-col">
    <div
      class="is-scrollbar-hidden overflow-x-auto rounded-lg bg-slate-200 text-slate-600 dark:bg-navy-800 dark:text-navy-200">
      <div class="tabs-list flex px-1.5 py-1">
        <button @click="activeTab = 'tabHome'"
          :class="activeTab === 'tabHome' ? 'bg-white shadow dark:bg-navy-500 dark:text-navy-100' : 'hover:text-slate-800 focus:text-slate-800 dark:hover:text-navy-100 dark:focus:text-navy-100'"
          class="btn shrink-0 space-x-2 px-3 py-1.5 font-medium">

          <span> <i class="fas fa-download "></i> Deposits</span>
        </button>
        <button @click="activeTab = 'tabProfile'"
          :class="activeTab === 'tabProfile' ? 'bg-white shadow dark:bg-navy-500 dark:text-navy-100' : 'hover:text-slate-800 focus:text-slate-800 dark:hover:text-navy-100 dark:focus:text-navy-100'"
          class="btn shrink-0 space-x-2 px-3 py-1.5 font-medium">

          <span><i class="fa fa-arrow-alt-circle-up "></i> Withdrawals</span>
        </button>
        <button @click="activeTab = 'tabMessages'"
          :class="activeTab === 'tabMessages' ? 'bg-white shadow dark:bg-navy-500 dark:text-navy-100' : 'hover:text-slate-800 focus:text-slate-800 dark:hover:text-navy-100 dark:focus:text-navy-100'"
          class="btn shrink-0 space-x-2 px-3 py-1.5 font-medium">
          <i class="fa fa-bank"></i>
          <span>Investments</span>
        </button>
      </div>
    </div>
    <div class="tab-content pt-4">
      <div x-show="activeTab === 'tabHome'" x-transition:enter="transition-all duration-500 easy-in-out"
        x-transition:enter-start="opacity-0 [transform:translate3d(1rem,0,0)]"
        x-transition:enter-end="opacity-100 [transform:translate3d(0,0,0)]">
        <div>
          <div class="mt-6">
            <div x-data x-init="$el._x_grid =  new Gridjs.Grid({
          from: $refs.table,
          sort: false,
          search: true,
          pagination: {
limit: 7
},
        }).render($refs.wrapper);">
              <div class="is-scrollbar-hidden min-w-full overflow-x-auto">
                <table x-ref="table" class="w-full text-left">
                  <thead>
                    <tr>

                      <th
                        class="whitespace-nowrap rounded-l-lg bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                        Date/Time</th>
                      <th
                        class="whitespace-nowrap bg-slate-200  px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                        Trans. ID</th>
                      <th
                        class="whitespace-nowrap bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                        Amount</th>
                      <th
                        class="whitespace-nowrap bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                        Coin</th>
                      <th
                        class="whitespace-nowrap bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                        Status</th>


                    </tr>
                  </thead>
                  <tbody>
                  <?php
                    $q = mysqli_query($link, "SELECT * FROM deposits WHERE userId = '$userId' ");
                    if(mysqli_num_rows($q) > 0){
                        while($row = mysqli_fetch_assoc($q)){
                            $method = $row['method'];
                            $img = $payMethods[$method];
                ?>
                  <tr>
                    <td class="whitespace-nowrap px-3 py-3"><?php echo date("D, j M Y", strtotime($row['date'])); ?></td>
                    <td class="whitespace-nowrap px-3 py-3"><?php echo $row['tranxId'] ?></td>
                    <td class="whitespace-nowrap px-3 py-3">$<?php echo $row['amount'] ?></td>
                    <td class="whitespace-nowrap px-3 py-3 inline ">
                      <p><img style='display:inline' src='<?php echo $img['image'] ?>' width='24'> <?php echo getCryptoValueByShortCode($payMethods[$method]['short_code'], $row['amount']); ?> </p>
                    </td>


                    <td class="whitespace-nowrap px-3 py-3"> <button type="button "
                        class="btn btn-rounded btn-sm btn-outline-warning">
                        <i class="bx bx-hourglass bx-spin font-size-16 align-middle me-2"></i>
                        <?php echo ucfirst($row['status']) ?>
                      </button></td>

                    <td> <a
                        href="deposit-payment.php?id=<?php echo $row['id'] ?>&tx=<?php echo $row['tranxId'] ?>&option=confirm"
                        class="btn space-x-2 border border-warning/30 bg-warning/10 text-xs+ rounded-full font-medium text-warning hover:bg-warning/20 focus:bg-warning/20 active:bg-warning/25"><i
                          class="fa fa-paperclip mr-1"> </i> Attach Proof</a></td>
                  </tr>
                <?php } } ?>
                  </tbody>
                </table>
              </div>
              <div>
                <div x-ref="wrapper"></div>
              </div>
            </div>
          </div>

        </div>
      </div>
      <div x-show="activeTab === 'tabProfile'" x-transition:enter="transition-all duration-500 easy-in-out"
        x-transition:enter-start="opacity-0 [transform:translate3d(1rem,0,0)]"
        x-transition:enter-end="opacity-100 [transform:translate3d(0,0,0)]">
        <div>
          <div class="mt-6">
            <div x-data x-init="$el._x_grid =  new Gridjs.Grid({
          from: $refs.table,
          sort: false,
          search: true,
          pagination: {
limit: 7
},
        }).render($refs.wrapper);">
              <div class="is-scrollbar-hidden min-w-full overflow-x-auto">
                <table x-ref="table" class="w-full text-left">
                  <thead>
                  <tr>

                    <th
                    class="whitespace-nowrap rounded-l-lg bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                    Date/Time</th>
                    <th
                    class="whitespace-nowrap bg-slate-200  px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                    Trans. ID</th>
                    <th
                    class="whitespace-nowrap bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                    Type</th>
                    <th
                    class="whitespace-nowrap bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                    Amount</th>
                    <th
                    class="whitespace-nowrap bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                    Coin</th>
                    <th
                    class="whitespace-nowrap bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                    Status</th>


                    </tr>
                  </thead>
                  <tbody>
                        <?php

                            $stmt = $conn->prepare("SELECT id, userId, email, type, method, amount, wallet_address, tranxid, status, date FROM withdrawals WHERE userId = ? ORDER BY date DESC");
                            $stmt->bind_param("i", $userId);
                            $stmt->execute();
                            $result = $stmt->get_result();

                            if ($result->num_rows > 0) {
                                while ($row = $result->fetch_assoc()) {
                                    ?>
                                    <tr>
                                        <td class="px-2 py-3"><?php echo date("Y-m-d H:i:s", strtotime($row['date'])); ?></td>
                                        <td class="px-2 py-3"><?php echo htmlspecialchars($row['tranxid']); ?></td>
                                        <td class="px-2 py-3"><?php echo htmlspecialchars($row['type'] == "refbonus" ? "Referral Balace" : "Account Balance"); ?></td>
                                        <td class="px-2 py-3">$<?php echo number_format($row['amount'], 2); ?></td>
                                        <td class="px-2 py-3"><?php echo htmlspecialchars($row['method']); ?></td>
                                        <td class="px-2 py-3">
                                            <?php
                                            if ($row['status'] == 'pending') {
                                                echo '<span class="text-yellow-500 font-semibold">Pending</span>';
                                            } elseif ($row['status'] == 'approved') {
                                                echo '<span class="text-green-500 font-semibold">Approved</span>';
                                            } else {
                                                echo '<span class="text-red-500 font-semibold">Rejected</span>';
                                            }
                                            ?>
                                        </td>
                                    </tr>
                                    <?php
                                }
                            } else {
                                echo '<tr><td colspan="6" class="px-2 py-3 text-center text-gray-500">No withdrawals found</td></tr>';
                            }

                    
                        ?>
                    </tbody>
                </table>
              </div>
              <div>
                <div x-ref="wrapper"></div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div x-show="activeTab === 'tabMessages'" x-transition:enter="transition-all duration-500 easy-in-out"
        x-transition:enter-start="opacity-0 [transform:translate3d(1rem,0,0)]"
        x-transition:enter-end="opacity-100 [transform:translate3d(0,0,0)]">
        <div>
          <div>
            <div x-data x-init="$el._x_grid =  new Gridjs.Grid({
          from: $refs.table,
          sort: false,
          search: false,
          pagination: {
limit: 5
},
        }).render($refs.wrapper);">
              <div class="is-scrollbar-hidden min-w-full overflow-x-auto">
                <table x-ref="table" class="w-full text-left">
                  <thead>
                  <tr>
                        <th
                          class="whitespace-nowrap rounded-l-lg bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                          Date/Time</th>
                        <!-- <th
                          class="whitespace-nowrap bg-slate-200  px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                          Trans. ID</th> -->
                        <th  class="whitespace-nowrap bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">Plan</th>
                        <th
                          class="whitespace-nowrap bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                          Amount</th>
                        <th
                          class="whitespace-nowrap bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                          ROI Date</th>
                        <!-- <th  class="whitespace-nowrap bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">Profit</th>-->
                        <th
                          class="whitespace-nowrap bg-slate-200 px-2 py-3 font-semibold text-slate-800 dark:bg-navy-800 dark:text-navy-100">
                          Status</th>


                      </tr>
                  </thead>
                  <tbody>

                  <?php  
                        $query = mysqli_query($link, "SELECT * FROM investments WHERE userId = '$userId' ORDER BY id DESC ");
                        if (mysqli_num_rows($query) > 0) {
                            while ($row = mysqli_fetch_assoc($query)) {
                    ?>
                        <tr>
                            <td><?php echo date("D, j M Y", strtotime($row['date'])); ?></td>
                            <td><?php echo $row['plan_name'] ?></td>
                            <td>$<?php echo $row['invest_amt'] ?></td>
                            <td><?php echo date("D, j M Y", strtotime($row['end_date'])); ?></td>
                            <td><?php echo ucfirst($row['status']); ?></td>
                        </tr>
                        <?php  
                            }
                        }
                        ?>

                  </tbody>
                </table>
              </div>
              <div>
                <div x-ref="wrapper"></div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

</main>



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