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

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    
    $action = $_POST['action'];

// logo_master
    // logo update query starts
    if($action == 'edit_logo'){
        $logo_name = $_POST['logo_name'];
        $name_status = $_POST['name_status'];
        $logo_status = $_POST['logo_status'];
        $images = $_FILES['logo_image']['name'];
        $old_filename = $_POST['old_logo_image'];
        $update_filename = "";
        if($images != NULL){
            $image_extension = pathinfo($images,PATHINFO_EXTENSION);
            $filename = time().'.'.$image_extension;
            $update_filename = $filename;
        }else{
            $update_filename = $old_filename;
        }
        $favicon = $_FILES['fav_icon']['name'];
        $old_fav_icon = $_POST['old_fav_icon'];
        $update_fav_icon = "";
        if($favicon != NULL){
            $favicon_extension = pathinfo($favicon,PATHINFO_EXTENSION);
            $filename = time().'.'.$favicon_extension;
            $update_fav_icon = $filename;
        }else{
            $update_fav_icon = $old_fav_icon;
        }
        
        $sql = "UPDATE logo_master SET logo_name = '$logo_name' , logo_name_status = '$name_status', logo_image = '$update_filename', fav_icon = '$update_fav_icon', logo_status = '$logo_status' " ;
        if($config->query($sql) === TRUE){
            if($images != NULL){
                if(file_exists('../../assets/img/logo/'.$old_filename)){
                    unlink('../../assets/img/logo/'.$old_filename);
                }
                move_uploaded_file($_FILES['logo_image']['tmp_name'],'../../assets/img/logo/'.$update_filename);
            }
            if($favicon != NULL){
                if(file_exists('../../assets/img/logo/'.$old_fav_icon)){
                    unlink('../../assets/img/logo/'.$old_fav_icon);
                }
                move_uploaded_file($_FILES['fav_icon']['tmp_name'],'../../assets/img/logo/'.$update_fav_icon);
            }
            $_SESSION['status'] = "Updated successfully" ;
            $_SESSION['alert'] = "success" ;
        }else{
            $_SESSION['status'] = "Update Failed" ;
            $_SESSION['alert'] = "success" ;
        }
    }
    // logo update 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">App Logo and Name </h4> 
					</div>
					<div class="row">
						<div class="col-md-12">
							<div class="card">
								<div class="card-body">
									<div class="table-responsive">
										<table id="basic-datatables" class="display table table-striped table-hover" >
											<thead>
												<tr>
													<th>S.No</th>
													<th>App Name</th>
													<th> Status</th>
													<th> App Logo</th>
													<th> Fav Icon</th>
													<th> Status</th>
													<th>Action</th>
												</tr>
											</thead>
											<tbody>
												<?php 
												    $sno = 1;
													$sql = "SELECT * FROM logo_master ";
													$res = $config->query($sql);
													$row = $res->fetch_assoc();
													$name_status = $row['logo_name_status'];
													$status = $row['logo_status'];
												?>
												<tr>
													<td><?= $sno; ?></td>
													<td><?= $row['logo_name'] ?></td>
													<td>
													    <?php if($name_status == 1){ ?>
													    <button class="btn btn-success">Active</button>
													    <?php }else{ ?>
													        <button class="btn btn-danger">In-Active</button>
													    <?php } ?>
                                                    </td>
													<td><img src="../../assets/img/logo/<?= empty($row['logo_image']) ? 'nologo.png' : $row['logo_image'] ; ?>" alt="<?= $row['logo_name'] ?> logo" style="padding:10px;"></td>
													<td><img src="../../assets/img/logo/<?= empty($row['fav_icon']) ? 'nologo.png' : $row['fav_icon'] ; ?>" alt="<?= $row['logo_name'] ?> logo" style="padding:10px;"></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="#logo"> <i class="fas fa-pencil-alt"></i>  </a>
                                                    </td>

                                                    <!-- Modal -->
                                                    <div class="modal fade" id="logo" 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">Logo & Name 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">Name Change</label>
                                                            			    <input type="text" class="form-control" id="logo_name" name="logo_name"  value="<?= $row['logo_name'] ?>">
                                                            			    <input type="hidden" class="form-control" id="logo_id" name="logo_id" value="<?= $row['logo_id'] ?>">
                                                            			</div>
                                                                        <div class="form-group">
                        												    <label for="name_status">App Name Status</label>
                        												    <select class="form-control" id="name_status" name="name_status">
                        												    <?php if($name_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">
                                                                            <label for="logo_image">Change Image</label>
                                                                            <input type="file" name="logo_image" id="logo_image" class="form-control">
                                                                            <input type="hidden" name="old_logo_image" id="old_logo_image" value="<?= $row['logo_image'] ?>">
                                                                        </div>
                            											<div class="form-group">
                                                                            <label for="fav_icon">Change Favicon</label>
                                                                            <input type="file" name="fav_icon" id="fav_icon" class="form-control">
                                                                            <input type="hidden" name="old_fav_icon" id="old_fav_icon" value="<?= $row['fav_icon'] ?>">
                                                                        </div>
                                                                        <div class="form-group">
                        												    <label for="logo_status">Logo Image Status</label>
                        												    <select class="form-control" id="logo_status" name="logo_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_logo">
                                        									<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>
											</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>
	<!--   Core JS Files   -->
	<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>
</body>
</html>