<?php
session_start();
include("connection.php");
 

 include('header.php');
 
 
 $msg = "";
$err = "";
    $pdname = "";
   $pdincrease = "";
   $pdbonus = "";
   $pdduration ="";
   $pdfroms = "";
   $pdtos = "";
   $vid = "";

   

if(isset($_POST['package11'])){

$pname =$conn->real_escape_string( $_POST['pname']);
$increase =$conn->real_escape_string( $_POST['increase']);
$duration =$conn->real_escape_string($_POST['duration']);
$froms =$conn->real_escape_string($_POST['froms']);
$tos =$conn->real_escape_string($_POST['tos']);


if(isset($_POST['mid']) && $_POST['mid'] !=''){
  $mid = $conn->real_escape_string( $_POST['mid']);
  
  $sql1 = "UPDATE package1 SET pname='$pname',increase='$increase', duration='$duration', min_amt='$froms', max_amt = '$tos'  WHERE id='$mid'";






  if (mysqli_query($conn, $sql1)) {

$msg= " Package updated successfully!";

           } else {
        $msg= mysqli_error($conn);
         }
}else{


    $sql = "INSERT INTO package1 (pname,increase,duration,min_amt,max_amt) VALUES ('$pname','$increase','$duration','$froms','$tos')";






                if (mysqli_query($conn, $sql)) {

  
                            $msg= " Package has been successfully added";

                           } else {
                        $msg = mysqli_error($conn);
                         }
                }
                          
}
 ?>

        <!-- Page Content Start -->
        <div class="content-page">
            <div class="content">

                    <div class="container-fluid">
    	<?php 
    if ($msg != "") {
        echo "<script>alert('$msg')</script>"; 
    }
    if ($err != "") {
        echo "<script>alert('$err')</script>"; 
    }

 ?>
    	<h4 align="center"><i class="fa fa-gears"></i> PLANS MANAGEMENT</h4>
				</br>

	<?php
	  
	  if(isset($_GET['id']) && $_GET['id'] !=''){
	    $vid= "yes";
	    $id = $conn->real_escape_string( $_GET['id']);
	$sql1= "SELECT * FROM package1 WHERE id = '$id'";
	  $result1 = mysqli_query($conn,$sql1);
	  if(mysqli_num_rows($result1) > 0){
	   $row = mysqli_fetch_assoc($result1);
	   $pdname = $row['pname'];
	   $pdincrease = $row['increase'];
	   $pdduration = $row['duration'];
	   $pdfroms = $row['min_amt'];
     $pdtos = $row['max_amt'];
	 
	  }
	  }

	?>

	<form class="form-horizontal" action="addpackages.php" method="POST" >

           <legend>Package</legend>
      
            <div class="form-group">
            <label> Plan Name </label>
                <input type="text" name="pname" placeholder="Package Name" value="<?php echo $pdname;?>" class="form-control">
                </div>
                
                <br>

            <div class="form-group">
            <label> Percentage Increase(%) </label>
                <input type="text"  name="increase" placeholder="Percentage Increase" value="<?php echo $pdincrease;?>"  class="form-control">
            </div>
            
            <br>
        

            <div class="form-group">
            <label> Package Duration (Days) </label>
                <input type="text"  name="duration" placeholder="Package Duration" value="<?php echo $pdduration;?>"  class="form-control">
            </div>
            
            <br>
                
            
                <div class="form-group">
                <label> Package Minimum Amount </label>
                <input type="text"  name="froms" placeholder="Package Amount"  value="<?php echo $pdfroms;?>"  class="form-control">
                </div>
                
                <br>

                <div class="form-group">
                <label> Package Maximum Amount </label>
                <input type="text"  name="tos" placeholder="Package Amount"  value="<?php echo $pdtos;?>"  class="form-control">
                </div>
                
                <br>
                <?php if($vid == ""){
                ?>
            <button style="" type="submit" class="btn btn-info" name="package11" >Add Package</button>
            <?php }else{?>
            <input type="hidden" name="mid"  value="<?php echo $id;?>" class="form-control">
            <button style="" type="submit" class="btn btn-info" name="package11" >Update Package </button>
            <?php }?>


            </form>
            </div>



            </div> <!-- end contant-->
        </div>
        <!-- End Page Content-->
     


                <!-- Footer Start -->
                <footer class="footer">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12 text-center">
                                  <script>document.write(new Date().getFullYear())</script> ©  <?=$sitename?>
                            </div>
                        </div>
                    </div>
                </footer>
                <!-- end Footer -->

            </div>

            <!-- ============================================================== -->
            <!-- End Page content -->
            <!-- ============================================================== -->


        </div>
        <!-- END wrapper -->

       

        <!-- Right bar overlay-->
        <div class="rightbar-overlay"></div>

        <!-- Vendor js -->
        <script src="assets/js/vendor.min.js"></script>

        <!-- datatable js -->
        <script src="assets/libs/datatables/jquery.dataTables.min.js"></script>
        <script src="assets/libs/datatables/dataTables.bootstrap4.min.js"></script>
        <script src="assets/libs/datatables/dataTables.responsive.min.js"></script>
        <script src="assets/libs/datatables/responsive.bootstrap4.min.js"></script>
        
        <script src="assets/libs/datatables/dataTables.buttons.min.js"></script>
        <script src="assets/libs/datatables/buttons.bootstrap4.min.js"></script>
        <script src="assets/libs/datatables/buttons.html5.min.js"></script>
        <script src="assets/libs/datatables/buttons.flash.min.js"></script>
        <script src="assets/libs/datatables/buttons.print.min.js"></script>

        <script src="assets/libs/datatables/dataTables.keyTable.min.js"></script>
        <script src="assets/libs/datatables/dataTables.select.min.js"></script>

        <!-- Datatables init -->
        <script src="assets/js/pages/datatables.init.js"></script>

        <!-- App js -->
        <script src="assets/js/app.min.js"></script>
        
    </body>
</html>