<?php
session_start();
include_once "../config.php";
require_once '../database1.php';
require_once "../check_admin.php";


class MailBox extends database
{
   public function getAllEmails($page)
    {
        $start = $page - 1;
        return $this->fetchAll('tb_mailbox', null, 'cl_created_at', 'DESC', $start * 50, 50);
	}

    public function getAllCategories()
    {
        return $this->fetchAll('tb_product_category');
    }
}
$msg='';
if(isset($_GET['message']))
{
	$msg=$_GET['message'];
}
$page = 1;
if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 1)
{
    $page = htmlspecialchars($_GET['page']);
}

$name = '';
$categoryID = '';
if(isset($_POST['data']))
{
    $data = $_POST['data'];
    $name = $data['name'];
    $categoryID = $data['category_id'];
}
$obj = new MailBox();	
$mails = $obj->getAllEmails($page);

include '../header.php';
?>
					 
            <!-- MAIN CONTENT-->
            <div class="main-content">
                <div class="section__content section__content--p30">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="overview-wrap">
                                    <h2 class="title-1">Mail - Page <?php echo $page ?></h2>
                                    <a class="btn btn-primary pull-right" href="blocked.php">Blocked Contact</a>
                                    <a class="btn btn-success pull-right" href="add.php">Send Email</a>
                                </div>
								<?php if($msg !=''){echo '<span class="col-md-6 alert alert-danger pull-left">'.$msg.'</span>';}?>
                            </div>
                        </div>
                        
                        <div class="row m-t-25 m-row">
							<form class="col-lg-12" method="POST">
                                
                                <div class="table-responsive table--no-card m-b-30">
                                    <table class="table table-borderless table-data3 ACP_table" style="width:100%">
                                        <thead>
                                            <tr>
                                                <th>

                                                </th>
                                                <th>
                                                    Type
                                                </th>
                                                <th>
                                                    Subject
                                                </th>
                                                <th>Created At</th>
                                                <th>
                                                    Sent?
                                                </th>
												<th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                         <?php
                                         $count = 1;
											foreach($mails as $mail)
											{
											?>
											<tr class="action-container">
                                                <td>
                                                    <?php echo $count++ ?>
                                                </td>
                                                <td><?php echo htmlspecialchars($mail['cl_type']);?></td>
                                                <td><?php echo htmlspecialchars($mail['cl_subject']);?></td>

												<td><?php echo htmlspecialchars(date("d M Y H:i:s", strtotime($mail['cl_created_at'])));?></td>
                                                <td><?php if($mail['cl_sent'] == 1){echo 'Yes';}else{echo 'No';} ?></td>
												<td>
                                                    <a href="view.php?id=<?php echo htmlspecialchars($mail['cl_id']) ?>" class="btn btn-success">View</a>   
                                                  
                                                    <div class="error-input"></div>                              
                                                </td>
											</tr>
											<?php
											}
											?>
                                           
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <td colspan="6">
                                             <?php
                                        if($page > 1)
                                        {
                                        ?>
                                        <button type="submit" formaction="list?page=<?php echo htmlspecialchars($page) - 1 ?>" class="track-btn">Previous</button>
                                           
                                        <?php
                                        }
                                        if(count($mails) == 50)
                                        {
                                        ?>
                                        <button type="submit" formaction="list?page=<?php echo htmlspecialchars($page) + 1 ?>" class="track-btn">Next</button>
                                        <?php
                                        }
                                        ?>
                                    </td>
                                </tr>
                                        </tfoot>
                                    </table>
									<div class="pull-right col-xs-12 top-margin">
										
									</div>
                                </div>
                            </form>
                            
                          
                        </div>
                       
						
						
                       <script>
						$(document).ready(function(){
							$('.active').removeClass('active');
							$('#cat-menu').addClass('active');
						}); 
						</script>              

  <?php
  include '../footer.php';
  ?>