<?php

$menu_modules = unserialize($this->request->session()->read('modules'));

?>
<style>
    .note-red{
        color:red !important;
    }
</style>

<div class="row">
    <div class="col-md-12">
        <div class="box box-primary">
            <div class="box-body">
                <div class="row">
                    <div class="col-md-4">
                        <h3 class="box-title no-margin"><?php echo __('&nbspStudent Management'); ?></h3>
                    </div>

                    <div class="col-md-8">

                        <div class="row">

                            <div class="col-md-12 text-right" >
                                <?php
                                if (array_key_exists("add", $module_pages)):
                                    $page_id = $module_pages["add"];
                                    foreach ($student_permission as $permission):
                                        if ($permission['page_id'] == $page_id):
                                            ?>
                                            <a href="<?php echo $this->Url->build('/student/add/'); ?>"  class="btn btn-success btn-flat btn-grid">
                                                <i class="fa fa-plus-square"></i>
                                                <?php echo('&nbspAdd Student'); ?>
                                            </a>

                                            <?php
                                            break;
                                        endif;
                                    endforeach;
                                endif;
                                ?>

                            </div>
                        </div>

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

<div class="row">
    <div class="col-md-12">
        <div class="box box-primary">
            <div class="box-header">

                <div class="row">
                    <div class="col-md-12">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="row">
                                    <div class="col-md-6 ">
                                        <div class="row">
                                            <?= $this->Form->create(null, array('type' => 'get', 'id' => 'limitForm')); ?>
                                            <div class="col-md-2"><strong>Show</strong></div>
                                            <div class="col-md-5">
                                                <?php echo $this->element('pagination_dropdown');?>
                                            </div>
                                            <?= $this->Form->end(); ?>
                                        </div>
                                    </div>
                                </div>
                            </div><!-- /col-md-6 -->
                            <div class="col-md-6">

                                <div class="row">
                                    <?= $this->Form->create(null, array('type' => 'get')); ?>
                                    <div class="col-md-12">
                                        <div class="row">

                                            <div class="col-md-4">
                                                <div class="input-group input-group-sm">
                                                <label for="agent_id" class="control-label">Agent:</label>
                                                <select  name="agent_id" id="agent_id" class="form-control">
                                                    <option selected=""  value="">Select One</option>
                                                    <?php foreach ($agents as $key => $value): ?>
                                                        <option value="<?= h($key) ?>"><?= h($value) ?></option>
                                                    <?php endforeach; ?>
                                                </select>
                                                </div>
                                            </div>
                                            <div class="col-md-8 pull-right">
                                                <label for="agent_id" class="control-label"> </label>
                                                <div class="box-tools">
                                                    <div class="input-group input-group-sm">

                                                        <?= $this->Form->input('', [ 'placeholder' => __('SEARCH'), 'class' => 'form-control', 'type' => 'text', 'name' => 'search', 'value' => $searchText, 'id' => 'search']); ?>

                                                        <div class="input-group-btn">
                                                            <?= $this->Form->submit(__('Search students'), [ 'class' => 'btn btn-success btn-flat']); ?>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <?= $this->Form->end(); ?>
                                    <div class="col-md-12">
                                        &nbsp;
                                    </div>

                                    <!-- /row -->
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- /.box-header -->
                <div class="box-body table-responsive index-table-section">
                    <table class="table table-bordered table-hover" id="index-table">

                        <thead>
                        <tr>
                            <th><?= $this->Paginator->sort('id', ['value' => __('ID')]) ?></th>
                            <th><?= $this->Paginator->sort('Reference No', ['value' => __('Reference No')]) ?></th>
                            <th><?= $this->Paginator->sort('Name', ['value' => __('Name')]) ?></th>
                            <th><?= $this->Paginator->sort('Email', ['value' => __('Email')]) ?></th>
                            <th><?= $this->Paginator->sort('Birth Date', ['value' => __('Birth Date')]) ?></th>
                            <th><?= $this->Paginator->sort('Mobile Number', ['value' => __('Phone Number')]) ?></th>
                            <th><?= $this->Paginator->sort('Gender', ['value' => __('Gender')]) ?></th>
                            <th><?= $this->Paginator->sort('Nationality', ['value' => __('Nationality')]) ?></th>
                            <th><?= $this->Paginator->sort('referring_agent_name', ['value' => __('Agent Name')]) ?></th>
                            <th><?= $this->Paginator->sort('status', ['value' => __('Status')]) ?></th>
                            <th><?=  __('Action') ?></th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($students as $student):?>
                            <tr>
                                <td><?= h($student->id)?></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/student/view/".$student->id ); ?>"><?= h($student->reference_number)?></a></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/student/view/".$student->id); ?>"><?= h($student->title.' '.$student->first_name.' '.$student->surname)?></a></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/student/view/".$student->id ); ?>"><?= h($student->email)?></a></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/student/view/".$student->id ); ?>"><?= h($student->date_of_birth->format('d-m-Y'))?></a></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/student/view/".$student->id ); ?>"><?= h($student->phone_number)?></a></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/student/view/".$student->id ); ?>"><?= h($student->gender)?></a></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/student/view/".$student->id ); ?>"><?= h($student->nationality)?></a></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/student/view/".$student->id ); ?>"><?= h($student->referring_agent_name)?></a></td>
                                <td><a id="label-success" href="<?php echo $this->Url->build("/student/view/".$student->id ); ?>"><?= h(json_decode(MEMBER_STATUS)[$student->status])?></a></td>

                                <td class="center-block">

                                    <div class="btn-group">
                                        <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown">
                                            <i class="fa fa-list"></i> <span class="caret"></span>
                                            <span class="sr-only">Toggle Dropdown</span>
                                        </button>
                                        <ul class="dropdown-menu" role="menu">
                                            <?php

                                            $added = [];
                                            foreach ($student_permission as $permission) { ?>


                                                        <?php if ($permission['page_id'] == $page_list['view']) { ?>
                                                            <li>

                                                                <?= $this->Html->link('<i class="fa fa-eye"></i>'.__('View Student'), ['controller' => $permission['controller'] ,'action' => $permission['method'],$student->id] ,array('escape'=>false)) ?>

                                                            </li>

                                                        <?php  }?>

                                                        <?php if ($permission['page_id'] == $page_list['registration']) { ?>
                                                                <li>

                                                                    <?= $this->Html->link('<i class="fa fa-pencil"></i>'.__('Edit Student'), ['controller' => $permission['controller'] ,'action' => $permission['method'],$student->id],array('escape'=>false)) ?>


                                                                </li>

                                                        <?php  } ?>

                                                        <?php if ($permission['page_id'] == $page_list['enrollstudent']) { ?>

                                                                <?php if($student->status != 1 ) {?>
                                                                    <li>
                                                                        <?= $this->Form->postLink('<i class="fa fa-university"></i>'.__('Student Enrollment'), ['controller' => $permission['controller'] ,'action' => $permission['method'],$student->id], ['confirm' => __('Are you sure you want to enroll student for {0}?', $student->title. ' '.$student->first_name),'escape'=>false]) ?>
                                                                    </li>
                                                                <?php }?>

                                                        <?php  } ?>

                                                        <?php if ($permission['page_id'] == $page_list['delete']) { ?>
                                                                <li>
                                                                    <?= $this->Form->postLink('<i class="fa fa-recycle"></i>'.__('Student Delete'), ['controller' => $permission['controller'] ,'action' => $permission['method'],$student->id], ['confirm' => __('Are you sure you want to delete student for {0}?', $student->title. ' '.$student->first_name),'escape'=>false]) ?>
                                                                </li>
                                                        <?php  } ?>


                                            <?php } ?>


                                        </ul>
                                    </div>
                                </td>
                            </tr>

                        <?php endforeach; ?>
                        </tbody>
                    </table>


                </div>
                <!-- /.box-body -->
                <?php
                //debug($this->Paginator->params());
                if ($this->Paginator->params()['nextPage']) {
                    $start_limit = (($this->Paginator->params()['page'] * $this->Paginator->params()['current']) - $this->Paginator->params()['perPage']) + 1;
                    $end_limit = $this->Paginator->params()['page'] * $this->Paginator->params()['current'];
                } else {
                    if ($this->Paginator->params()['count'] != 0) {
                        $start_limit = ($this->Paginator->params()['count'] - $this->Paginator->params()['current']) + 1;
                    } else {
                        $start_limit = 0;
                    }
                    $end_limit = $this->Paginator->params()['count'];
                }
                ?>
                <div class="box-footer clearfix">
                    <div class="row">
                        <div class="col-md-6"><p style="padding-top:20px;float:left;"><?php echo 'Showing ' . $start_limit . ' to ' . $end_limit . ' of ' . $this->Paginator->params()['count'] . ' entries'; ?></p></div>
                        <div class="col-md-6">

                            <div class="paginator pull-right">
                                <ul class="pagination ">
                                    <?= $this->Paginator->prev('< ' . __('previous')) ?>
                                    <?= $this->Paginator->numbers() ?>
                                    <?= $this->Paginator->next(__('next') . ' >') ?>
                                </ul>

                            </div>

                        </div>

                    </div>
                </div>

            </div>
            <!-- /.box -->


        </div>
    </div>




</div>

