<?php
include('includes/header.php');
include('includes/navbar.php');
?>
<?php
  $get_id = $_GET['edit_pid'];
  $sqldep = "SELECT * FROM packages WHERE package_id = '$get_id'";
  $dep_result = $connection->query($sqldep);
  while ($row = $dep_result->fetch_assoc()) {
    $package_id = $row['package_id'];
    $package_name = $row['package_name'];
    $package_min = $row['package_min'];
    $package_max = $row['package_max'];
    $return = $row['return_Percent'];
    $referral = $row['referral'];
    $gift_bonus = $row['gift_bonus'];
    $duration = $row['duration'];
    $insurance = $row['insurance'];
    $recommend = $row['recommend'];
    $other_features = $row['other_features'];
    $status = $row['status'];
}
if (empty($get_id) || $get_id != $package_id):
 ?>
 <div class="container-fluid">
   <?php
   if (isset($_POST['add_new'])) {
       $pname = $_POST['pname'];
       $min = $_POST['min'];
       $max = $_POST['max'];
       $return = $_POST['return'];
       $referral = $_POST['referral'];
       $gift_bonus = $_POST['gift_bonus'];
       $duration = $_POST['duration'];
       $insurance = $_POST['insurance'];
       $recommend = $_POST['recommend'];
       $other_features = $_POST['other_features'];
       $status = $_POST['status'];

       if(empty($pname) || empty($min) || empty($max) || empty($return) || empty($referral) || empty($gift_bonus) || empty($duration)
       || empty($insurance) || empty($recommend) || empty($other_features) || empty($status)) {
         $error = "empty";
       }else {
         $insert_wallet = "INSERT INTO packages(package_name, package_min, package_max, return_Percent, referral, gift_bonus, duration, insurance, recommend, other_features, status)
         VALUES('$pname', '$min', '$max', '$return', '$referral', '$gift_bonus', '$duration', '$insurance', '$recommend', '$other_features', '$status')";
         if ($connection->query($insert_wallet)===TRUE) {
             $error = "insert_success";
             }else {
               $error = "error";
             }
       }
   }
    ?>
     <div class="card shadow mb-4">
         <div class="card-header py-3">
             <h6 class="m-0 font-weight-bold text-primary">Add New Upgrade Plan </h6>
         </div>
         <div class="card-body">

           <br>
           <form class="" action="" method="post">
             <div class="form-group">
               <label>Plan Name</label>
               <input type="text" name="pname" value="<?php echo $package_name ?>" class="form-control" required>
               <br>

               <label>Min Amount</label>
               <input type="text" name="min" value="<?php echo $package_min ?>" class="form-control" required onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')">
               <br>

               <label>Max Amount</label>
               <input type="text" name="max" value="<?php echo $package_max ?>" class="form-control" required onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')">
               <br>

               <label>Return</label>
               <input type="text" name="return" value="<?php echo $return ?>"  class="form-control" required onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')">
               <br>

               <label>Referral</label>
               <input type="text" name="referral" value="<?php echo $referral ?>"  class="form-control" required>
               <br>

               <label>Gift Bonus</label>
               <input type="text" name="gift_bonus" value="<?php echo $gift_bonus ?>" class="form-control" required onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')">
               <br>

               <label>Duration (In days eg 1,5,10)</label>
               <input type="text" name="duration" value="<?php echo $duration ?>" class="form-control" required onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')">
               <br>

               <label>Insurance</label>
               <input type="text" name="insurance" value="<?php echo $insurance ?>"  class="form-control" required onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')">
               <br>

               <label>Recommended</label>
               <select class="form-control" name="recommend" style="text-transform:capitalize" required>
                   <option value="<?php echo $recommend ?>" selected><?php echo $recommend ?></option>
                   <option value="yes">YES</option>
                   <option value="no">NO</option>
               </select>
               <br>

               <label>Extra Features </label>
               <select class="form-control" name="other_features" style="text-transform:capitalize" required>
                   <option value="<?php echo $other_features ?>" selected><?php echo $other_features ?></option>
                   <option value="yes">YES</option>
                   <option value="no">NO</option>
               </select>
               <br>

               <label>Status</label>
               <select class="form-control" name="status" style="text-transform:capitalize" required>
                   <option value="<?php echo $status ?>" selected><?php echo $status ?></option>
                   <option value="active">Active</option>
                   <option value="inactive">Inactive</option>
               </select>

               <br>
               <button type="submit" name="add_new" class="btn btn-primary">Add Plan</button>
             </div>
           </form>
         </div>
     </div>
 </div>

<?php elseif($get_id == $package_id): ?>
<div class="container-fluid">
  <?php
  if (isset($_POST['update'])) {
    $pname = $_POST['pname'];
    $min = $_POST['min'];
    $max = $_POST['max'];
    $return = $_POST['return'];
    $referral = $_POST['referral'];
    $gift_bonus = $_POST['gift_bonus'];
    $duration = $_POST['duration'];
    $insurance = $_POST['insurance'];
    $recommend = $_POST['recommend'];
    $other_features = $_POST['other_features'];
    $status = $_POST['status'];

    if(empty($pname) || empty($min) || empty($max) || empty($return) || empty($referral) || empty($gift_bonus) || empty($duration)
    || empty($insurance) || empty($recommend) || empty($other_features) || empty($status)) {
      $error = "empty";
    }else {
      $update_plan = "UPDATE packages SET package_name = '$pname', package_min = '$min', package_max = '$max', return_Percent = '$return',
      referral = '$referral', gift_bonus = '$gift_bonus', duration = '$duration', insurance = '$insurance', recommend = '$recommend',
      other_features = '$other_features', status = '$status' WHERE package_id = '$package_id'";
      if ($connection->query($update_plan)===TRUE) {
          $error = "update_success";
          }else {
            $error = "error";
          }
      }
  }
   ?>
   <div class="card shadow mb-4">
       <div class="card-header py-3">
           <h6 class="m-0 font-weight-bold text-primary">Edit <?php echo $package_name ?> Upgrade Plan </h6>
       </div>
       <div class="card-body">

         <br>
         <form class="" action="" method="post">
           <div class="form-group">
             <label>Plan Name</label>
             <input type="text" name="pname" value="<?php echo $package_name ?>" class="form-control" required>
             <br>

             <label>Min Amount</label>
             <input type="text" name="min" value="<?php echo $package_min ?>" class="form-control" required onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')">
             <br>

             <label>Max Amount</label>
             <input type="text" name="max" value="<?php echo $package_max ?>" class="form-control" required onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')">
             <br>

             <label>Return</label>
             <input type="text" name="return" value="<?php echo $return ?>"  class="form-control" required onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')">
             <br>

             <label>Referral</label>
             <input type="text" name="referral" value="<?php echo $referral ?>"  class="form-control" required>
             <br>

             <label>Gift Bonus</label>
             <input type="text" name="gift_bonus" value="<?php echo $gift_bonus ?>" class="form-control" required onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')">
             <br>

             <label>Duration (In days eg 1,5,10)</label>
             <input type="text" name="duration" value="<?php echo $duration ?>" class="form-control" required onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')">
             <br>

             <label>Insurance</label>
             <input type="text" name="insurance" value="<?php echo $insurance ?>"  class="form-control" required onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')">
             <br>

             <label>Recommended</label>
             <select class="form-control" name="recommend" style="text-transform:capitalize" required>
                 <option value="<?php echo $recommend ?>" selected><?php echo $recommend ?></option>
                 <option value="yes">YES</option>
                 <option value="no">NO</option>
             </select>
             <br>

             <label>Extra Features </label>
             <select class="form-control" name="other_features" style="text-transform:capitalize" required>
                 <option value="<?php echo $other_features ?>" selected><?php echo $other_features ?></option>
                 <option value="yes">YES</option>
                 <option value="no">NO</option>
             </select>
             <br>

             <label>Status</label>
             <select class="form-control" name="status" style="text-transform:capitalize" required>
                 <option value="<?php echo $status ?>" selected><?php echo $status ?></option>
                 <option value="active">Active</option>
                 <option value="inactive">Inactive</option>
             </select>

             <br>
             <button type="submit" name="update" class="btn btn-primary">Update Plan</button>
           </div>
         </form>
       </div>
   </div>
</div>
<?php endif; ?>


</div>

<p id="error" style='display: none'><?php echo $error?></p>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
    var error = document.getElementById('error');

    if (error.textContent == 'empty') {
         swal("ERROR!", "All field are required!", "warning");

    }else if (error.textContent == "update_success") {
        swal("SUCCESS!", "<?php echo $pname ?> Upgrade Pla Updated Successfully", "success");
        setTimeout(() => {
            window.location.href = 'upgrade_plans.php'
        }, 2000);

    }else if (error.textContent == "insert_success") {
        swal("SUCCESS!", "<?php echo $pname ?> Upgrade Plan Added Successfully", "success");
        setTimeout(() => {
            window.location.href = 'upgrade_plans.php'
        }, 2000);

    }else if (error.textContent == "error") {
        swal("ERROR!", "Sorry an error occurred. Please try again later", "warning");

    }

</script>
<?php
include('includes/scripts.php');
include('includes/footer.php');
?>
