<?php
session_start();
if(!isset($_SESSION["username"])){
header("Location: index.php");
exit(); }

require('../includes/config.php');
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <title>Blog Admin</title>
    <link href="assets/css/bootstrap.css" rel="stylesheet" />
    <link href="assets/css/font-awesome.css" rel="stylesheet" />
    <link href="assets/css/style.css" rel="stylesheet" />

</head>
<body>

    <!-- HEADER END-->
    <div class="navbar navbar-inverse set-radius-zero">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="admin.php">

                    <img src="assets/img/logo.png" />
                </a>

            </div>

            <div class="left-div">
                <i class="fa fa-user-plus login-icon" ></i>
			</div>
		</div>
	</div>
	
    <section class="menu-section">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="navbar-collapse collapse ">
                        <ul id="menu-top" class="nav navbar-nav navbar-right">
                            
							<li><a  href="admin.php">Dashboard</a></li>
							<li><a  href="add.php">Add New Post</a></li>
                            <li><a href="logout.php">Log Out</a></li>

                        </ul>
                    </div>
                </div>

            </div>
        </div>
    </section>
	
	
<div class="content-wrapper">
        <div class="container">
            <div class="row">
                    <div class="col-md-12">
                        <h1 class="page-head-line"><span class="glyphicon glyphicon-th-list"></span> &nbsp;Posts Tables</h1>
                    </div>
                </div>
                <div class="row">
                
				<div class="col-md-12">
                  <!--   Kitchen Sink -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
						  <div class="row">
                            <div class="col-md-6">
								Blog Posts
							</div>
							<div class="col-md-6 text-right">
								<span class="text-right"><a href="add.php" class="btn btn-info">Add New Post</a></span>
							</div>
						  </div>
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>id</th>
                                            <th>post title</th>
                                            <th>date</th>
                                            <th>Edit or delete</th>
                                        </tr>
                                    </thead>
                                    
									<tbody>
									
									
<?php

$sql = 'SELECT * FROM posts';
$result = mysqli_query($conn, $sql);

         if (mysqli_num_rows($result) > 0) {
            
			
			
			while($row = mysqli_fetch_assoc($result)) {
			   echo '<tr>';
			   echo '<td>'.$row["post_id"].'</td>';
			   echo '<td>'.$row["h1"].'</td>';
			   echo '<td>'.$row["date"].'</td>';
			   echo '<td> <a href="../post'.$row["post_id"].'-'.$row["permalink"].'.html">View</a> | <a href="edit.php?p='.$row["post_id"].'">Edit</a> | <a href="delete.php?p='.$row["post_id"].'">Delete</a></td>';
			   echo '</tr>';
            }
			
			
			
         } else {
            echo "0 results";
         }
         mysqli_close($conn);
		 
		 
?>
                                        

                                        
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                     <!-- End  Kitchen Sink -->
                </div>
               
            </div>
        </div>
    </div>
    <!-- CONTENT-WRAPPER SECTION END-->
		
    <footer>
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    &copy; Blog Admin
                </div>

            </div>
        </div>
    </footer>

    <script src="assets/js/jquery-1.11.1.js"></script>
    <script src="assets/js/bootstrap.js"></script>
	
<script>
        const dateToday = new Date();

        const date = new Date(dateToday);
        date.setDate(date.getDate() + 1);

        const monthName = date.toLocaleString('default', { month: 'long' });
        const currentYear = date.getFullYear();

        window.onload = function () {
            const dateSelect = document.getElementById('service_date');
            dateSelect.value = monthName;
            const yearElement = document.getElementById("service_year");
            yearElement.value = currentYear;
            AddMonthDays(monthName);
        }

        function AddMonthDays(monthNameValue) {
            let monthDays = 31;
            const currentYear = date.getFullYear();
            const dayOfMonth = date.getDate();
            if (monthNameValue == 'February') monthDays = 28;
            if (monthNameValue == 'February' && currentYear == '2028') monthDays = 29;
            if (monthNameValue == 'February' && currentYear == '2032') monthDays = 29;
            if (monthNameValue == 'February' && currentYear == '2036') monthDays = 29;
            if (monthNameValue == 'February' && currentYear == '2040') monthDays = 29;
            if (monthNameValue == 'February' && currentYear == '2044') monthDays = 29;
            if (monthNameValue == 'February' && currentYear == '2048') monthDays = 29;
            if (monthNameValue == 'April') monthDays = 30;
            if (monthNameValue == 'June') monthDays = 30;
            if (monthNameValue == 'September') monthDays = 30;
            if (monthNameValue == 'November') monthDays = 30;

            const selectElement = document.getElementById('service_day');
            selectElement.options.length = 0;
            const newOption = new Option('Pick a Day', '');
            selectElement.add(newOption);

            const monthDaysPlusOne = monthDays + 1;
            const SelectHtmlCode = '';
            for (let i = 1; i < monthDaysPlusOne; i++) {
                const newOption = new Option(i.toString(), i.toString());
                selectElement.add(newOption);
            }
            selectElement.value = dayOfMonth;
        }

        function validatePhoneNumber(phoneNumber) {
            const phoneNumberPattern = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
            const cleanedPhoneNumber = phoneNumber.replace(/\s/g, '');
            if (phoneNumberPattern.test(cleanedPhoneNumber)) {
                return;
            } else {
                alert('Please enter correct phone number format: ###-###-####');
            }
        }
    </script>
</body>
</html>

