<?php include ("header.php");?> 

<div class="dashboard-body">
<div class="d-flex justify-content-between mb-3 align-items-center dashboardBodyNav">
   <!----> 
</div>
<div class="container">
   <div class="d-flex justify-content-center">
      <div class="toggle-buttons">
         <button id="providerBtn" class=" active  " onclick="if (!window.__cfRLUnblockHandlers) return false; showProvider()">All Investment Plan</button>
      </div>
   </div>
   <div class="container content   active " id="providerContent">
      <div class="publisher-area">
         <div class="publisher">
            <div class="">
               <div class="row gy-4 justify-content-center">
                  <?php
                     // Fetch all trader data
                     $sql = "
                         SELECT * FROM package1";
                         
                     $result = $conn->query($sql);
                     ?>
                  <?php while ($trader = $result->fetch_assoc()): ?>
                  <div class="col-xxl-3 col-sm-6">
                     <div class="dashboard-cardo">
                        <div class="d-flex justify-content-between align-items-center">
                           <div class="dashboard-card__content">
                              <p class="dashboard-card__coin-titl d-flex justify-content-between">
                                 <span><?php echo $trader['pname']; ?></span>
                              </p>
                           </div>
                        </div>
                        <hr>
                        <div class="tab-content mt-2">
                           <div class="tab-pane fade show active d-flex d-xl-block flex-wrap gap-2">
                              <div class="trading-left__buy flex-fill">
                                 <div class="trading-left__header flex-between sell-side-order-list-header">
                                    <span class="price__title text-success switch">
                                    <?php echo $trader['increase']; ?>%
                                    <small class="alert__title text--smaller">ROI Percent</small>
                                    </span>
                                    
                                    
                                 </div>
                                 <div class="sell-side-order-list"></div>
                              </div>
                           </div>
                        </div>
                        <div class="d-flex justify-content-between align-items-center mt-3">
                           <div class="dashboard-card__content">
                              <p class="price__title text--small"><?php echo $trader['increase']; ?>% on ROI for <?=$trader['duration']?> Hours</p>
                           </div>
                           <div class="dashboard-card__content">
                              <button class="deposit__button btn btn--base w-100" style="padding: 10px 40px!important; font-size: 0.90rem;" data-toggle="modal" data-target="#copyModal<?php echo $trader['id']; ?>">
                              Invest
                              </button>
                           </div>
                        </div>
                     </div>
                  </div>
                  <!-- Modal Structure for Each Trader -->
                  <div class="modal fade" id="copyModal<?php echo $trader['id']; ?>" role="dialog">
                     <div class="modal-dialog modal-dialog-centered">
                        <div class="modal-content">
                           <div class="modal-header">
                              <h5 class="modal-title" id="exampleModalLongTitle">Invest on <?php echo $trader['pname']; ?> Plan</h5>
                              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                              <span aria-hidden="true">&times;</span>
                              </button>
                           </div>
                           <div class="modal-body" style="background-color: #05212a;">
                              <div class="hide--desktop row dashboard-card col-xxl-12 col-md-12" style="margin-left: 0px;">
                                 <div class="row d-flex" style="margin-left: 0px;">
                                    <div class="d-flex justify-content-between align-items-right col-6">
                                       <div class="dashboard-card__content">
                                          <p class="dashboard-card__coin-name mb-0 ">
                                             <span class="price__title text-success switch fs-15">
                                             <?php echo $trader['increase']; ?>%
                                             <small class="alert__title text--small">ROI</small>
                                             </span>
                                       </div>
                                       <div class="dashboard-card__content">
                                          <span class="text--base text--small switch fs-15"> <?php echo $trader['duration']; ?> Hours
                                          <small class="alert__title text--small">Duration</small>
                                          </span>
                                       </div>
                                       
                                    </div>
                                    <div class="d-flex justify-content-between align-items-right col-6">
                                       
                                       <div class="dashboard-card__content">
                                          <span class="amount__title switch">
                                             <p class="price__title just-center self-center fs-15"> $<?php echo number_format($trader['min_amt']); ?></p>
                                             <small class="alert__title text--small" style="display: block;">Minimum Capital</small>
                                          </span>
                                       </div>
                                       <div class="dashboard-card__content">
                                          <span class="amount__title switch">
                                             <p class="price__title just-center self-center fs-15"> $<?php echo number_format($trader['max_amt']); ?></p>
                                             <small class="alert__title text--small" style="display: block;">Maximum Capital</small>
                                          </span>
                                       </div>
                                    </div>
                                 </div>
                              </div>
                              <div class="hide--mobile row dashboard-card col-xxl-12 col-md-12" style="margin-left: 0px;">
                     
                                 <div class="row d-flex" style="margin-left: 0px;">
                                    <div class="d-flex justify-content-between align-items-right col-12">
                                       <div class="dashboard-card__content">
                                          <span class="amount__title switch d-flex">
                                             <div>
                                                <p class="just-center self-center risk-indicator circled low"><?php echo $trader['increase']; ?>%</p>
                                             </div>
                                             <div>
                                                <small class="alert__title text--smaller" style="display: block;">ROI</small>
                                             </div>
                                          </span>
                                       </div>
                                       <div class="dashboard-card__content">
                                          <p class="dashboard-card__coin-name mb-0 ">
                                             <span class="price__title text-success switch fs-15">
                                             <?php echo $trader['duration']; ?> Hours
                                             <small class="alert__title text--smaller">Duration</small>
                                             </span>
                                       </div>
                                       
                                    </div>
                                 </div>
                                 <div class="row d-flex" style="margin-left: 0px;">
                                    <div class="d-flex justify-content-between align-items-right col-12">
                                       <div class="dashboard-card__content">
                                          <p class="dashboard-card__coin-name mb-0 ">
                                             <span class="price__title text-success switch fs-15">
                                             <?php echo number_format($trader['min_amt'],2); ?> USD
                                             <small class="alert__title text--smaller">Minimum Capital</small>
                                             </span>
                                       </div>
                                       <div class="dashboard-card__content">
                                          <p class="dashboard-card__coin-name mb-0 ">
                                             <span class="price__title text-success switch fs-15">
                                             <?php echo number_format($trader['max_amt'],2); ?> USD
                                             <small class="alert__title text--smaller">Maximum Capital</small>
                                             </span>
                                       </div>
                                       
                                    </div>
                                 </div>
                              </div>
                           </div>
                           <div class="modal-body">
                              <div class="containero">

                                 <small>Total Balance: <span style="font-weight: 600;">$<?=number_format($total_balance,2)?> USD</span></small>

                                 
                                 <div class="warning" id="amountMessage<?php echo $trader['id']; ?>">Pay $<?php echo number_format($trader['min_amt']); ?> in order to continue</div>
                                 <form method="POST" id="investForm<?php echo $trader['id']; ?>">
                                    <input type="hidden" name="plan_id" value="<?php echo $trader['id'] ?>">
                                    <input type="text" class="form-control" placeholder="Enter invest amount" name="amount">
                                    <button type="button" class="deposit-btn" onclick="submitForm<?php echo $trader['id']; ?>()">INVEST</button>
                                 </form>
                                 

                                 <div class="copy-trades">
                                    <div>Open trades will be copied from the Leader under the current market conditions and rates of the assets.</div>
                                 </div>
                              </div>
                           </form>

                           <script>
                              function submitForm<?php echo $trader['id']; ?>() {
                                 const form = $("#investForm<?php echo $trader['id']; ?>");
                                 const amountInput = form.find("input[name='amount']");
                                 const amount = parseFloat(amountInput.val());

                                 // Basic validation
                                 if (!amountInput.val()) {
                                    alert("Please enter an investment amount.");
                                    amountInput.focus();
                                    return;
                                 }

                                 if (isNaN(amount) || amount <= 0) {
                                    alert("Please enter a valid positive number for the amount.");
                                    amountInput.focus();
                                    return;
                                 }

                                 const formData = form.serialize(); // serialize form data

                                 $.ajax({
                                    url: "processor.php?invest", // Your PHP backend script
                                    method: "POST",
                                    data: formData,
                                    success: function(response) {
                                          try {
                                             const res = JSON.parse(response);
                                             if (res.status === "success") {
                                                alert(res.message);
                                                window.location.href = "invest-history";
                                             } else {
                                                alert(res.message || "An error occurred.");
                                             }
                                          } catch (e) {
                                             console.error("Invalid JSON response", response);
                                             alert("Unexpected server response.");
                                          }
                                    },
                                    error: function(xhr) {
                                          alert("AJAX request failed: " + xhr.statusText);
                                    }
                                 });
                              }
                              </script>


                              <script type="4d45ef26de46de20206471d1-text/javascript">
                                 let originalAmount<?php echo $trader['id']; ?> = <?=$trader['investment_capital']?>;
                                     
                                 function decreaseAmount<?php echo $trader['id']; ?>() {
                                     
                                     let amount = parseInt(document.getElementById('amount<?php echo $trader['id']; ?>').value);
                                     
                                     if (amount > originalAmount<?php echo $trader['id']; ?>) {
                                         document.getElementById('amount<?php echo $trader['id']; ?>').value = amount - 50;
                                         updateAmountMessage<?php echo $trader['id']; ?>();
                                     }
                                     
                                     toggleDecreaseButton<?php echo $trader['id']; ?>();
                                 }
                                 
                                     function increaseAmount<?php echo $trader['id']; ?>() {
                                         let amount = parseInt(document.getElementById('amount<?php echo $trader['id']; ?>').value);
                                         document.getElementById('amount<?php echo $trader['id']; ?>').value = amount + 50;
                                         updateAmountMessage<?php echo $trader['id']; ?>();
                                     }
                                 
                                     function updateAmountMessage<?php echo $trader['id']; ?>() {
                                         let amount = parseInt(document.getElementById('amount<?php echo $trader['id']; ?>').value);
                                         document.getElementById('amountMessage<?php echo $trader['id']; ?>').innerText = `Deposit $${amount.toFixed(2)}, in order to continue`;
                                     }
                                     
                                 
                                     function toggleDecreaseButton<?php echo $trader['id']; ?>() {
                                     let amount = parseInt(document.getElementById('amount<?php echo $trader['id']; ?>').value);
                                     document.getElementById('decrease<?php echo $trader['id']; ?>').disabled = amount <= originalAmount<?php echo $trader['id']; ?>;
                                     }
                                 
                                     toggleDecreaseButton<?php echo $trader['id']; ?>();
                                 
                              </script>
                           </div>
                           <div class="modal-footer">
                              <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                           </div>
                        </div>
                     </div>
                  </div>
                  <?php endwhile; ?>
               </div>
            </div>
         </div>
      </div>
      <div class="" style="margin: 50px 0px auto;">
         <center>
            <a href="trader" style="text-decoration:none" class="sc-d56ac5c9-0 jVdhmX">
               <div color="#01a7d3" class="sc-496e28ab-0 dCOKro sc-84092d93-7 dvkGAp"><span>+1,233 MORE PRIVATE TRADERS</span></div>
            </a>
         </center>
      </div>
   </div>
</div>
<?php include "modals.php";?>