<?php include('../config/setup.php');?>
<?php

if($_SERVER['REQUEST_METHOD'] == "POST"){
    
	$action = $_POST['action'];
// slider_master
    // slider update query starts
    if($action == 'edit_slider'){
        $slider_id = $_POST['slider_id'];
        $slider_status = $_POST['slider_status'];
        $images = $_FILES['slider_image']['name'];
        $old_filename = $_POST['old_slider_image'];
        $update_filename = "";
        if($images != NULL){
            $image_extension = pathinfo($images,PATHINFO_EXTENSION);
            $filename = time().'.'.$image_extension;
            $update_filename = $filename;
        }else{
            $update_filename = $old_filename;
        }
        
        $sql = "UPDATE slider_master SET slider_image = '$update_filename', slider_status = '$slider_status' WHERE slider_id = '$slider_id' " ;
        if($config->query($sql) === TRUE){
            if($images != NULL){
                if(file_exists('../../assets/img/slider/'.$old_filename)){
                    unlink('../../assets/img/slider/'.$old_filename);
                }
                move_uploaded_file($_FILES['slider_image']['tmp_name'],'../../assets/img/slider/'.$update_filename);
            }
            $_SESSION['status'] = "Updated successfully" ;
            $_SESSION['alert'] = "success" ;
        }else{
            $_SESSION['status'] = "Update Failed" ;
            $_SESSION['alert'] = "success" ;
        }
    }
    // slider update query ends
    
// // banner_master
//     // banner_master insert query starts
// 	if($action == "banner_add"){
//  		$banner_type = $_POST['banner_type'];
//  		$banner_status = $_POST['banner_status'];
//  		$banner_image = $_FILES['banner_image']['name'];
//  		$banner_extension = pathinfo($banner_image,PATHINFO_EXTENSION);
//         $filename = 'banner'.time().'.'.$banner_extension;
//  		move_uploaded_file($_FILES['banner_image']['tmp_name'],'../assets/img/banner/'.$filename);

// 		$sql =  "INSERT INTO banner_master (banner_type, banner_image, banner_status) VALUES ('$banner_type', '$filename', '$banner_status') ";
// 		if($conn->query($sql) === TRUE){
// 			$status = "Added Successfully";
// 			$alert = "success";
// 		} else {
// 			$status = "Add Failed";
// 			$alert = "danger";
// 		}
// 	}
// 	// banner_master insert query ends

// 	// banner_master delete query ends
// 	if($action == "banner_del"){
// 		$banner_id = $_POST['banner_id'];
// 		$sql =  "DELETE from banner_master WHERE banner_id= '$banner_id' ";
// 		if($conn->query($sql) === TRUE){
// 			$status = "Deleted Successfully";
// 			$alert = "success";
// 		} else {
// 			$status = "Delete Failed";
// 			$alert = "danger";
// 		}
// 	}
// 	// banner_master delete query ends
		
}

?>
<?php include "head.php" ?>
	<div class="wrapper">
		<div class="main-header">
			<!-- Logo Header -->
	 		<?php include('logo.php');?>
			<!-- End Logo Header -->
			<!-- Navbar Header -->
			<?php include('topbar.php');?>
			 <!-- End Navbar -->
		</div>
		<!-- Sidebar -->
		<?php include('sidebar.php');?>
		
		<div class="main-panel">
			<div class="content">
				<div class="page-inner">
					<div class="page-header">
						<h4 class="page-title">Slider Master</h4> 
					</div>
					<div class="row">
						<div class="col-md-12">
							<div class="card">
								<div class="card-body">
								<!-- <div class="d-flex justify-content-center">
									<button type="button" class="btn btn-success" data-toggle="modal" data-target="#exampleModal">
                                    	<i class="fas fa-plus"></i>   Add New Slider
									</button>
								</div> -->
									<div class="table-responsive">
										<table id="basic-datatables" class="display table table-striped table-hover" >
											<thead>
												<tr>
													<th>S.No</th>
													<th>Slider</th>
													<!--<th>Slider Title</th>-->
													<!--<th>Display order</th>-->
													<th>Status</th>
													<th>Action</th>
												</tr>
											</thead>
											 
											<tbody>
												<?php 
												    $sno = 1;
													$sql = "SELECT * FROM slider_master ";
													$res = $config->query($sql);
													while($row = $res->fetch_assoc()){
													$status = $row['slider_status'];
												?>
												<tr>
													<td><?= $sno; ?></td>
													<td><img src="../../assets/img/slider/<?= $row['slider_image'] ?>" style="width: 180px;height: 102px; padding: 10px 0;"></td>
													<!--<td><?#= $row['slider_title'] ?></td>-->
													<td>
													    <?php if($status == 1){ ?>
													    <button class="btn btn-success">Active</button>
													    <?php }else{ ?>
													        <button class="btn btn-danger">In-Active</button>
													    <?php } ?>
                                                    </td>
													<td>
													    <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#slider<?= $sno ?>"> <i class="fas fa-pencil-alt"></i>  </a>
                                                    </td>
                                                    <!-- Modal -->
                                                    <div class="modal fade" id="slider<?= $sno ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                                        <div class="modal-dialog" role="document">
                                                            <div class="modal-content">
                                                                <div class="modal-header">
                                                                    <h5 class="modal-title" id="exampleModalLabel">Slider Edit</h5>
                                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                                        <span aria-hidden="true">&times;</span>
                                                                    </button>
                                                                </div>
                                                                <div class="modal-body">
                                                            	    <form method="post" enctype="multipart/form-data">
                                                               <!--         <div class="form-group">-->
                                                            			<!--    <label for="logo_name">Slider Title</label>-->
                                                            			<!--    <input type="text" class="form-control" id="logo_name" name="logo_name"  value="<?#= $row['logo_name'] ?>">-->
                                                            			<!--</div>-->
                                                            			<div class="form-group">
                                                                            <label for="email2">Current Image</label>
                                                                            <img src="../../assets/img/slider/<?= $row['slider_image'] ?>" style=" width:310px; height: 129px; ">
                                                            			    <input type="hidden" class="form-control" id="slider_id" name="slider_id" value="<?= $row['slider_id'] ?>">
                                                                        </div>
                                                                        <div class="form-group">
                                                            			    <label for="slider_image">Change Image (Image size 1920px X 600px )</label>
                                                            				<input type="file" class="form-control" id="slider_image" name="slider_image">
                                                            			    <input type="hidden" class="form-control" id="old_slider_image" name="old_slider_image" value="<?= $row['slider_image'] ?>">
                                                            			</div>
                                                                        <div class="form-group">
                        												    <label for="slider_status">Slider Status</label>
                        												    <select class="form-control" id="slider_status" name="slider_status">
                        												    <?php if($status == 1){ ?>
                            													<option value="1" >Active</option>
                            													<option value="0">In-Active</option>
                        													<?php }else{?>
                            													<option value="0" >In-Active</option>
                            													<option value="1" >Active</option>
                        													<?php } ?>
                            												</select>
                            											</div>	
                                                                        <div class="form-group">
                                        									<input type="hidden" name="action" value="edit_slider">
                                        									<button class="btn btn-warning" type="submit" name="submit">Submit</button>
                                         								</div>
                                                        		    </form>	
                                                                </div>
                                                                <div class="modal-footer">
                                                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </tr>
                                                <?php $sno++; } ?>
											</tbody>
										</table>
									</div>
								</div>
							</div>
						</div>
	                </div>
				</div>
			</div>
		<?php include('footer.php');?>	
		 
		</div>
		<!-- End Custom template -->
	</div>
	<script>
	<?php if(isset($_SESSION['status']) && !empty($_SESSION['status'])){ ?>
	    swal({
          title: "<?= $_SESSION['status'] ?>",
          text: "",
          icon: "<?= $_SESSION['alert'] ?>",
        });
    <?php unset($_SESSION['status']); } ?>
	</script>
	<script type="text/javascript" src="richtext.js"></script> 
  <script type="text/javascript">
 
        bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });
  </script>
				
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
	<script src="../assets/js/core/jquery.3.2.1.min.js"></script>
	<script src="../assets/js/core/popper.min.js"></script>
	<script src="../assets/js/core/bootstrap.min.js"></script>
	<!-- jQuery UI -->
	<script src="../assets/js/plugin/jquery-ui-1.12.1.custom/jquery-ui.min.js"></script>
	<script src="../assets/js/plugin/jquery-ui-touch-punch/jquery.ui.touch-punch.min.js"></script>
	
	<!-- jQuery Scrollbar -->
	<script src="../assets/js/plugin/jquery-scrollbar/jquery.scrollbar.min.js"></script>
	<!-- Datatables -->
	<script src="../assets/js/plugin/datatables/datatables.min.js"></script>
	<!-- Atlantis JS -->
	<script src="../assets/js/atlantis.min.js"></script>
	<!-- Atlantis DEMO methods, don't include it in your project! -->
	<script src="../assets/js/setting-demo2.js"></script>
	<script >
		$(document).ready(function() {
			$('#basic-datatables').DataTable({
			});

		 
 
			 
		});
	</script>
	<script>
 $(document).ready(function(){
    $('#image_file').on('change',function(){
        $('#upload_form').ajaxForm({           
            target:'#uploaded_images_preview',
            beforeSubmit:function(e){
                $('.file_uploading').show();
            },
            success:function(e){
                $('.file_uploading').hide();
            },
            error:function(e){
            }
        }).submit();
    });
});  </script>
</body>
</html>