<?php
session_start();
error_reporting(0);
include('phpaccess/linkme.php');
if(strlen($_SESSION['alogin'])==0)
	{	
header('location:index.php');
}
else{
date_default_timezone_set('Africa/Lagos');// change according timezone
$currentTime = date( 'd-m-Y h:i:s A', time () );

//<!--sn	title	type	picture	content	date	  -->
  

if(isset($_POST['submit']))
{
$cid=$_SESSION['sn'];
$title=$_POST['title'];
$type=$_POST['type'];
$content=$_POST['content'];
$date=date("d-M-Y");


$imgfile=$_FILES["image"]["name"];

// get the image extension
$extension = substr($imgfile,strlen($imgfile)-4,strlen($imgfile));
// allowed extensions
$allowed_extensions = array(".jpg","jpeg",".png",".gif");
//$allowed_extensions = array(".jpg","jpeg",".png",".gif");

// Validation for allowed extensions .in_array() function searches an array for a specific value.
if(!in_array($extension,$allowed_extensions))
{
echo "<script>alert('Invalid format. Only pdf format allowed');</script>";
}
else
{
//rename the image file
$docnewfile=md5($imgfile).$extension;
// Code for move image into directory
move_uploaded_file($_FILES["image"]["tmp_name"],"../assets/img/news/".$docnewfile);


$query=mysqli_query($con,"insert into news (title,type,content,picture,date) values('$title','$type','$content','$docnewfile','$date')");

// code for show complaint number
$sql=mysqli_query($con,"select sn from news  order by sn desc limit 1");
while($row=mysqli_fetch_array($sql))
{
 $cmpn=$row['sn'];
}
$complainno=$cmpn;
echo '<script> alert("Your Registration has been successfully Submitenmited )</script>';
echo "<script type='text/javascript'> document.location = 'news.php'; </script>";
}

if(isset($_GET['del']))
		  {
		  mysqli_query($con,"delete from staff where sn = '".$_GET['sn']."'");
         $_SESSION['delmsg']="Staff deleted !!";
		  }
		  }

?>


<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="favicon.ico">
    <title>News Room - ENL Consortium Limited</title>
    <!-- Simple bar CSS -->
    <link rel="stylesheet" href="css/simplebar.css">
    <!-- Fonts CSS -->
    <link href="https://fonts.googleapis.com/css2?family=Overpass:ital,wght@0,100;0,200;0,300;0,400;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
    <!-- Icons CSS -->
    <link rel="stylesheet" href="css/feather.css">
    <link rel="stylesheet" href="css/dataTables.bootstrap4.css">
    <!-- Date Range Picker CSS -->
    <link rel="stylesheet" href="css/daterangepicker.css">
    <!-- App CSS -->
    <link rel="stylesheet" href="css/app-light.css" id="lightTheme" disabled>
    <link rel="stylesheet" href="css/app-dark.css" id="darkTheme">
 
</head>
  </head>
  <body class="vertical  light ">
    <div class="wrapper">
      
        <?php include("phpaccess/topnav_fin.php");?>
		
	  <!--Insert NAV Here-->
      <aside class="sidebar-left border-right bg-white shadow" id="leftSidebar" data-simplebar>
        
        <?php include("phpaccess/nav_fin.php");?>
      
	  </aside>
	  <!--End Insert NAV Here-->
		  
      <main role="main" class="main-content">
        <div class="container-fluid">
          <div class="row justify-content-center">
            <div class="col-12">
              <h2 class="page-title">News Room</h2>
              <p class="text-muted">What is New !!!</p>
              <div class="row">
			  
			  
                <div class="col-md-6">
                  <div class="card shadow mb-4">
                    <div class="card-body">
                      
					  
					  <table class="table datatables" id="dataTable-1">
									<?php if(isset($_POST['submit']))
{?>
									<div class="alert alert-success">
										<button type="button" class="close" data-dismiss="alert">×</button>
									<strong>Well done!</strong>	<?php echo htmlentities($_SESSION['msg']);?><?php echo htmlentities($_SESSION['msg']="");?>
									</div>
<?php } ?>


									<?php if(isset($_GET['del']))
{?>
									<div class="alert alert-error">
										<button type="button" class="close" data-dismiss="alert">×</button>
									<strong>Oh snap!</strong> 	<?php echo htmlentities($_SESSION['delmsg']);?><?php echo htmlentities($_SESSION['delmsg']="");?>
									</div>
<?php } ?>

									<br />
									
									

<!--sn	title	type	picture	content	date	  -->
                        <thead>
                          <tr>			
							<th>SN</th>				
							<th>Title</th>				
						    <th>Type</th>
							<th>Date</th>
							<th>Action</th>
                           </tr>
                        </thead>
                        <tbody>
						<?php 
$query=mysqli_query($con,"select * from news ORDER BY sn DESC");
while($row=mysqli_fetch_array($query))
{
?>	  
                          <tr>
									<td><?php echo htmlentities($row['sn']);?></td>
									<td><?php echo htmlentities($row['title']);?></td>
									<td><?php echo htmlentities($row['type']);?></td>
									<td><?php echo htmlentities($row['date']);?></td>
                          
							<td>
								<div class="dropdown">
                                <button class="btn btn-sm dropdown-toggle" type="button" id="dr1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                  <span class="text-muted sr-only">Manage</span>
                                </button>
                                <div class="dropdown-menu dropdown-menu-right" aria-labelledby="dr1">
                                  <a class="dropdown-item" href="news_detail.php?cid=<?php echo htmlentities($row['sn']);?>">Edit</a>
<a class="dropdown-item"  href="news.php?cid=<?php echo $row['sn']?>&del=delete" onClick="return confirm('Are you sure you want to delete?')">Delete </a>

                                </div>
                              </div>
							  </td>
							
                          </tr>
						  
						  <?php } ?>
							
							</tbody>
                      </table>
					  
					  
					  
                    </div> <!-- /.card-body -->
                  </div> <!-- /.card -->
                </div> <!-- /.col -->
                
				<div class="col-md-6">
                  <div class="card shadow mb-4">
                    <div class="card-body">
                      <p class="mb-2"><strong>New Update</strong></p>
			<!--sn  name  type  title  body  picture  dating  -->
		  
					 	<form class="needs-validation" novalidate method="post" name="director" enctype="multipart/form-data" >
		
                      <div class="form-group mb-3">
                        <label for="custom-placeholder">News Subject / Title</label>
                        <input class="form-control input-placeholder" name="title" id="custom-placeholder" type="text" placeholder="Enter News Subject / Title">
                      </div>
                      <div class="form-group mb-3">
                        <label for="custom-placeholder">Catigories</label>
                        <input class="form-control input-placeholder" name="type" id="custom-placeholder" type="text" placeholder="Enter Full Title">
                      </div>
                      <div class="form-group mb-3">
                        <label for="custom-money">Full Details</label>
						<textarea  class="form-control" name="content" cols="10" rows="10" placeholder="Enter Full Details" required="required"></textarea>
                      </div>
                      <div class="form-group mb-3">
                        <label for="custom-mixed">Picture</label>
						 <input type="file" class="form-control" required="required" name="image" id="formFile" placeholder="Only jpg is accepted" />
<!--sn	title	type	picture	content	date	  -->

                      </div>
					  
                      <button type="submit" name="submit" class="btn btn-primary">Submit</button>
					  
						  
                    </div> <!-- /.card-body -->
                  </div> <!-- /.card -->
                </div> <!-- /.col -->
				
				
				
              </div> <!-- end section -->
              
		
            </div> <!-- .col-12 -->
          </div> <!-- .row -->
        </div> <!-- .container-fluid -->
        
		
		
        
      </main> <!-- main -->
	  
    </div> <!-- .wrapper -->
    <script src="js/jquery.min.js"></script>
    <script src="js/popper.min.js"></script>
    <script src="js/moment.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/simplebar.min.js"></script>
    <script src='js/daterangepicker.js'></script>
    <script src='js/jquery.stickOnScroll.js'></script>
    <script src="js/tinycolor-min.js"></script>
    <script src="js/config.js"></script>
    <script src='js/jquery.dataTables.min.js'></script>
    <script src='js/dataTables.bootstrap4.min.js'></script>
    <script>
      $('#dataTable-1').DataTable(
      {
        autoWidth: true,
        "lengthMenu": [
          [16, 32, 64, -1],
          [16, 32, 64, "All"]
        ]
      });
    </script>
   
  </body>
</html>
<?php } ?>