<div class="row">
    <div class="msnry animated fadeInUp">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 item">
            <div class="panel panel-message">
                <div class="panel-body">
                    <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th><?php echo __('Subject'); ?></th>
                                <th><?php echo __('Posted by'); ?></th>
                                <th><?php echo __('Replies'); ?></th>
                                <th><?php echo __('Last post'); ?></th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($items->data as $item) { ?>
                            <tr data-href="/messages/view/<?php echo $item['Message']['id']; ?>">
                                <td><?php echo $item['Message']['title']; ?></td>
                                <td><?php echo $item['User']['Profile']['first_name'] . ' <span class="semi-bold">' . $item['User']['Profile']['last_name'] . '</span>'; ?></td>
                                <td><?php echo count($item['MessageComment']); ?></td>
                                <td><?php echo isset($item['MessageComment'][0]) ? $this->Time->format(Configure::read('CFG.date_format') . ' ' . Configure::read('CFG.time_format'), $item['MessageComment'][0]['created'], null, Configure::read('CFG.timezone')) : null; ?></td>
                                <td>
                                <?php 
                                if (($this->Session->read('Auth.User.id') == $item['User']['id']) || ($this->Session->read('Auth.User.role') == 'admin')) {
                                    echo $this->Html->link('<i class="fa fa-pencil" title="' . __('Edit') . '"></i>', array('controller'=>'messages', 'action'=>'edit', $item['Message']['id']), array('escape' => false));
                                    echo '&nbsp;&nbsp;&nbsp;';
                                    echo $this->Html->link('<i class="fa fa-times" title="' . __('Delete') . '"></i>', '#', array('escape' => false, 'class' => 'confirm_action', 'data-toggle' => 'modal', 'data-target' => '#confirmModal', 'data-controller' => 'messages', 'data-action' => 'delete', 'data-item_id' => $item['Message']['id']));
                                }
                                ?>
                                </td>
                            </tr>
                        <?php } ?>
                        </tbody>
                    </table>
                    </div>
                    <?php
                    if(count($items->numbers) > 1) {
                        ?>
                        <div class="row">
                            <div class="col-xs-12 text-center paginate">
                                <ul class="pagination messages">
                                <?php
                                foreach($items->numbers as $num) {
                                    $url = '/messages/?p=' . $num;
                                    
                                    if((isset($_GET['p']) && $num == $_GET['p']) || ($num == 1 && !isset($_GET['p']))) {
                                        echo '<li class="active"><a href="'.$url.'">'.$num.'</a></li>';
                                    }else{
                                        echo '<li><a href="'.$url.'">'.$num.'</a></li>';
                                    }
                                }
                                ?>
                                </ul>
                            </div>
                        </div>
                        <?php
                    }
                    ?>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
$(document).ready(function(){
    $('.panel-message table td<?php echo (($this->Session->read('Auth.User.id') == $item['User']['id']) || ($this->Session->read('Auth.User.role') == 'admin')) ? ':not(:last-child)' : null; ?>').click(function(){
        window.location = $(this).parent().data('href');
        
    });
});
</script>