<?php
include 'header.php';

$err = $msg = "";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
	$planId = text_input($_POST['plan']);
	$amount = text_input($_POST['amount']);

	if (empty($planId)) {
		$err = "Select a plan";
	}else{
		$qq = mysqli_query($link, "SELECT * FROM package1 WHERE id = '$planId' ");
		if (mysqli_num_rows($qq) > 0) {
			$pln = mysqli_fetch_assoc($qq);
			$plan_name = $pln['pname']; 
			$plan_increase = $pln['increase']; 
			$plan_duration = $pln['duration']; 
			$min_amt = $pln['min_amt']; 
			$max_amt = $pln['max_amt']; 
		}
	}

	if (empty($amount) || $amount <= 0 ) {
		$err = "Input your investment amount";
	}

	if ($amount > $balance) {
		$err = "Insufficient fund to purchase this plan";
	}

	if ($amount < $min_amt) {
		$err = "Invest amount should not be lower than $".$min_amt;
	}elseif ($amount > $max_amt) {
		$err = "Invest amount should not be higher than $".$max_amt;
	}

	if (empty($err)) {
		$start_date = date('d-m-Y H:i:s');
		$end_date  = date('d-m-Y H:i:s', strtotime(' + '.$plan_duration.' days'));
		$total_profit = ($plan_increase/100) * $amount;

		$sql = mysqli_query($link, "INSERT INTO investments (`userId`, `planId`, `invest_amt`, `plan_name`, `increase`, `duration`, `total_profit`, `start_date`, `end_date`, `status`) VALUES ('$userId', '$planId', '$amount', '$plan_name', '$plan_increase', '$plan_duration', '$total_profit', '$start_date', '$end_date', 'running' ) ");

		if ($sql) {
      mysqli_query($link, "UPDATE users SET balance = balance - $amount WHERE id = '$userId' ");
			$msg = "Investment started successfully";
		}
	}

}
?>
<script>
document.addEventListener("DOMContentLoaded", function () {
    <?php if (!empty($err)) { ?>
        Swal.fire({
            icon: 'error',
            title: 'Oops...',
            text: '<?php echo $err; ?>',
            confirmButtonColor: '#d33'
        });
    <?php } ?>

    <?php if (!empty($msg)) { ?>
        Swal.fire({
            icon: 'success',
            title: 'Success!',
            text: '<?php echo $msg; ?>',
            confirmButtonColor: '#28a745'
        });
    <?php } ?>
});
</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">
          Investment
        </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>Investment</li>
        </ul>
      </div>



      <div>




        <div class="py-5 text-center lg:py-6">
          <p class="text-sm uppercase">Select your Investment Package</p>
          <h3 class="mt-1 text-xl font-semibold text-slate-600 dark:text-navy-100">
            Welcome. Where do you like to Start?
          </h3>
        </div>
        <div class="grid place-items-center  grid-cols-2 gap-2 sm:grid-cols-4 sm:gap-3 lg:gap-2">
         <?php
            $q = mysqli_query($link, "SELECT * FROM package1 ");
            if(mysqli_num_rows($q) > 0){
                while($Row = mysqli_fetch_assoc($q)){
            
         ?>
          <div class="rounded-xl bg-gradient-to-br from-pink-500 to-rose-500 p-1"
            style="border-color:#F3BA2F;background:#F3BA2F">
            <div class="rounded-xl bg-slate-50 p-4 text-center dark:bg-navy-900 sm:p-5">
              <div class="mt-8">
                <svg xmlns="http://www.w3.org/2000/svg"
                  class="inline h-16 w-16 text-secondary dark:text-secondary-light" fill="none" viewBox="0 0 24 24"
                  stroke="currentColor">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1"
                    d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
                </svg>
              </div>
              <div class="mt-5">
                <h4 class="text-xl font-semibold text-slate-700 dark:text-navy-100">
                  <?php echo $Row['pname'] ?>
                </h4>
                <!-- <p>Our Unique Start Up <br> Plan</p> -->
              </div>
              <div class="mt-5">
                <span class="text-xl font-semibold text-secondary dark:text-secondary-light">$<?php echo $Row['min_amt'] ?> - $<?php echo $Row['max_amt'] ?></span>
              </div>
              <div class="mt-8 space-y-4 text-left">
                <div class="flex items-start space-x-3">
                  <div class="flex h-6 w-6 shrink-0 items-center justify-center rounded-full">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-4.5 w-4.5" viewBox="0 0 20 20" fill="currentColor">
                      <path fill-rule="evenodd"
                        d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
                        clip-rule="evenodd" />
                    </svg>
                  </div>
                  <span class="font-medium"> Interest - <?php echo $Row['increase'] ?>%</span>
                </div>
                <div class="flex items-start space-x-3">
                  <div class="flex h-6 w-6 shrink-0 items-center justify-center rounded-full">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-4.5 w-4.5" viewBox="0 0 20 20" fill="currentColor">
                      <path fill-rule="evenodd"
                        d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
                        clip-rule="evenodd" />
                    </svg>
                  </div>
                  <span class="font-medium">24/7 dedicated support</span>
                </div>
                <div class="flex items-start space-x-3">
                  <div class="flex h-6 w-6 shrink-0 items-center justify-center rounded-full">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-4.5 w-4.5" viewBox="0 0 20 20" fill="currentColor">
                      <path fill-rule="evenodd"
                        d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
                        clip-rule="evenodd" />
                    </svg>
                  </div>
                  <span class="font-medium">Duration - <?php echo $Row['duration'] ?> Days</span>
                </div>


              </div>
              <div class="mt-8">
                <div>

                  <div x-data="{showModal:false}">
                    <button @click="showModal = true"
                      class="btn mt-3 rounded-full bg-secondary font-medium text-white hover:bg-secondary-focus focus:bg-secondary-focus active:bg-secondary-focus/90">

                      <span> Choose Plan </span>
                    </button>
                    <template x-teleport="#x-teleport-target">
                      <div
                        class="fixed inset-0 z-[100] flex flex-col items-center backdrop-blur justify-center overflow-hidden px-4 py-6 sm:px-5"
                        x-show="showModal" role="dialog" @keydown.window.escape="showModal = false">
                        <div class="absolute inset-0 bg-slate-900/60 transition-opacity duration-300"
                          @click="showModal = false" x-show="showModal" x-transition:enter="ease-out"
                          x-transition:enter-start="opacity-0" x-transition:enter-end="opacity-100"
                          x-transition:leave="ease-in" x-transition:leave-start="opacity-100"
                          x-transition:leave-end="opacity-0"></div>
                        <div
                          class="relative w-full max-w-lg origin-top rounded-lg bg-white transition-all duration-300 dark:bg-navy-700"
                          x-show="showModal" x-transition:enter="easy-out" x-transition:enter-start="opacity-0 scale-95"
                          x-transition:enter-end="opacity-100 scale-100" x-transition:leave="easy-in"
                          x-transition:leave-start="opacity-100 scale-100" x-transition:leave-end="opacity-0 scale-95">
                          <div
                            class="flex justify-between rounded-t-lg bg-slate-200 px-4 py-3 dark:bg-navy-800 sm:px-5">
                            <h3 class="text-base font-medium text-slate-700 dark:text-navy-100">
                              Choose Investment - <span
                                class="bg-gradient-to-r from-fuchsia-600 to-pink-600 bg-clip-text text-lg font-semibold text-transparent">
                                <?php echo $Row['pname'] ?>
                              </span>
                            </h3>
                            <button @click="showModal = !showModal"
                              class="btn -mr-1.5 h-7 w-7 rounded-full p-0 hover:bg-slate-300/20 focus:bg-slate-300/20 active:bg-slate-300/25 dark:hover:bg-navy-300/20 dark:focus:bg-navy-300/20 dark:active:bg-navy-300/25">
                              <svg xmlns="http://www.w3.org/2000/svg" class="h-4.5 w-4.5" fill="none"
                                viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"></path>
                              </svg>
                            </button>
                          </div>
                          <div class="px-4 py-4 sm:px-5">



                            <form action="investment.php" method="post">
                            <input type="hidden" name="plan" id="investment_id" value="<?php echo $Row['id'] ?>">
                              <div class="mt-4 space-y-4">

                                <h3 class="text-md font-semibold"><span
                                    class="btn h-10 px-4 text-sm font-semibold bg-success/10 font-medium text-success hover:bg-success/20 focus:bg-success/20 active:bg-success/25">
                                    Wallet Balance - $<?php echo $balance?> </span></h3>

                                <label class="block">
                                  <span>Enter Amount </span>
                                  <code class="text-xs+ text-error"> (Min-$<?php echo $Row['min_amt'] ?>)</code>
                                  <input
                                    class="form-input mt-1.5 h-12 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 Amount" type="number" name="amount" />
                                </label>



                                <div class="space-x-2 text-right">
                                  <button type="button" @click="showModal = false"
                                    class="btn min-w-[7rem] rounded-full border border-slate-300 font-medium text-slate-800 hover:bg-slate-150 focus:bg-slate-150 active:bg-slate-150/80 dark:border-navy-450 dark:text-navy-50 dark:hover:bg-navy-500 dark:focus:bg-navy-500 dark:active:bg-navy-500/90">
                                    Close
                                  </button>
                                  <button @click="showModal = false"
                                    class="btn min-w-[7rem] rounded-full 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"
                                    type="submit" name="sub-upd">
                                    Submit
                                  </button>
                                </div>

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

                </div>
              </div>
            </div>
          </div>
       <?php } } ?>

       





          <!--ends-->
        </div>

        <!-- Hoverable Rows -->
        <div class="card px-4 pb-4 mt-4 sm:px-5">
          <div class="mt-3 flex h-8 items-center justify-between ">
            <div
              class=" mt-2 text-base  rounded-full  border-slate-300 text-slate-800 dark:border-navy-450 dark:text-navy-50">
              My Investments
            </div>
            <div
              class=" mt-2 text-base  rounded-full  border-slate-300 text-slate-800 dark:border-navy-450 dark:text-navy-50">
              <a href="history.php"
                class="btn h-8 px-4 text-xs+ font-medium  bg-info/10 font-medium text-info hover:bg-info/20 focus:bg-info/20 active:bg-info/25">
                View History
              </a>
            </div>




          </div>







          <div class="mt-6 card pb-4">

            <div>
              <div x-data x-init="$el._x_grid =  new Gridjs.Grid({
                from: $refs.table,
                sort: false,
                search: true,
                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>
    </main>


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