<?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-6 col-xs-6 tile">
                        <div class="icon-frame col-md-2" id="light" style="padding-top: 6px;padding-left: 13px;">
                            <i class="fa fa-users" style="color:white"></i>
                        </div>
                        <h3 class="box-title no-margin" style="padding-top: 20px; float: left; text-align: left;"><?php echo __('&nbspMember Management'); ?></h3>
                    </div>
                    <div class="col-md-6">
                        <div class="row">

                            <div class="col-md-2 col-xs-6 col-md-offset-6">
                                 <?php echo $this->element('watch_now',['section_name'=>'Member Management']); ?>
                            </div>
                            <div class="col-md-2 col-xs-6 text-right" style="line-height: 4;">
                                <?php
                                if (array_key_exists("add", $module_pages)):
                                    $page_id = $module_pages["add"];
                                    foreach ($user_permission as $permission):
                                        if ($permission['page_id'] == $page_id):
                                            ?>
                                <a href="<?php echo $this->Url->build('/member/add/'); ?>"  class="btn btn-success btn-flat btn-grid">
                                    <i class="fa fa-plus-square"></i>
                                                <?php echo('&nbspAdd Member'); ?>
                                </a>

                                            <?php
                                            break;
                                        endif;
                                    endforeach;
                                endif;
                                ?>
                          <!--      <div class="btn-group">
                                    <button type="button" class="btn btn-primary"><?php //echo __('User Management'); ?></button>
                                    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
                                        <span class="caret"></span>
                                        <span class="sr-only">Toggle Dropdown</span>
                                    </button>
                                    <ul class="dropdown-menu" role="menu">
                                        <?php
                                        $access_submodule = array();
                                        foreach ($user_permission as $row) {
                                            if (!in_array($row['submodule_id'], $access_submodule))
                                                $access_submodule[] = $row['submodule_id'];
                                        }
                                        $current_module = array_values($current_module);
                                        $current_sub_module = $this->request->params['controller'];
                                        foreach ($menu_modules as $module) {
                                            if ($module["id"] == $current_module[0]) {
                                                foreach ($module['sub_modules'] as $submodule) {
                                                    if (in_array($submodule['id'], $access_submodule)) {
                                                        if (strtolower($submodule['controller_name']) != strtolower($current_sub_module)) {
                                                            $sub_icon = '<i class="fa fa-circle-o"></i>';
                                                            if ($submodule['icon'] != "") {
                                                                $sub_icon = $submodule['icon'];
                                                            }
                                                            ?>
                                        <li>
                                            <a href="<?php //echo $this->Url->build(["controller" => $submodule['controller_name'], "action" => "index"]); ?>"><?php echo $sub_icon . $submodule['name'] ?></a>
                                        </li>
                                                            <?php
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        ?>
                                    </ul>
                                    </ul>
                                </div>-->
                            </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">
                                    <?php
                                    if (array_key_exists("delete", $module_pages)) :
                                        $page_id = $module_pages ["delete"];
                                        foreach ($user_permission as $permission) :
                                            if ($permission ['page_id'] == $page_id) :
                                                ?>
                                                <?= $this->Form->create(null, ['url' => ['controller' => 'affiliate', 'action' => 'bulkaction']]); ?>
                                                    <?= $this->Form->hidden('selected_content'); ?>
<!--                                    <div class="col-md-6">
            <?php// echo $this->Form->input('bulk_action', array('label' => false, 'options' => $bulk_actions, 'empty' => __('Bulk Action'), 'required' => true, 'class' => 'form-control')); ?>
                                    </div>-->

<!--                                    <div class="col-md-6 bulk-btn-section">
                                                <?= $this->Form->button(__('Apply'), ['id' => 'bulk_action_apply', 'class' => 'btn btn-success btn-flat', 'style'=>'float:left']) ?>
                                    </div>-->
                                                <?= $this->Form->end(); ?>
                                                <?php
                                                break;


                                            endif;
                                        endforeach
                                        ;


                                    endif;
                                    ?>

                                </div>
                                
                                <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">

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

                                        <div class="row">

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

                                                <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 Users'), [ 'class' => 'btn btn-success btn-flat']); ?>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

<?= $this->Form->end(); ?>
                                    </div>
                                    <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('Name', ['value' => __('Name')]) ?></th>
                                <th><?= $this->Paginator->sort('Roll No', ['value' => __('Roll No')]) ?></th>
                                <th><?= $this->Paginator->sort('Batch No', ['value' => __('Batch No')]) ?></th>
                                <th><?= $this->Paginator->sort('Organization', ['value' => __('Organization')]) ?></th>
                                <th><?= $this->Paginator->sort('Designation', ['value' => __('Designation')]) ?></th>
                                <th><?= $this->Paginator->sort('Email', ['value' => __('Email')]) ?></th>
                                <th><?= $this->Paginator->sort('Mobile Number', ['value' => __('Mobile Number')]) ?></th>
                                <th><?= $this->Paginator->sort('status', ['value' => __('Status')]) ?></th>
                                <th width="20%"><?=  __('Action') ?></th>
                            </tr>
                        </thead>
                        <tbody>

                                <?php foreach ($users as $user): 
                                    $note_status = "";
                                    $disabled = false;
                                    if($user->note_status > 0){
                                        $disabled = true;
                                        $note_status = "note-red";//note-red
                                    }
                                    ?>
                            <tr>
                               
                                <td><?= h($user->id)?></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/member/view/".$user->id); ?>"><?= h($user->full_name)?></a></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/member/view/".$user->id ); ?>"><?= h($user->roll_no)?></a></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/member/view/".$user->id ); ?>"><?= h($user->batch_no)?></a></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/member/view/".$user->id ); ?>"><?= h($user->organization)?></a></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/member/view/".$user->id ); ?>"><?= h($user->designation)?></a></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/member/view/".$user->id ); ?>"><?= h($user->email)?></a></td>
                                <td><a style="color:black;word-break: break-word;" href="<?php echo $this->Url->build("/member/view/".$user->id ); ?>"><?= h($user->mobile_number)?></a></td>
                                <td><a id="label-success" href="<?php echo $this->Url->build("/member/view/".$user->id ); ?>"><?= h(json_decode(MEMBER_STATUS)[$user->status])?></a></td>
                                <td class="text-right">
                                <?php
                                        $added = [];
                                        foreach ($user_permission as $permission) {

                                            if ($permission['page_id'] == $page_list['edit'] && !in_array($page_list['edit'], $added)) {
                                                array_push($added, $page_list['edit']);
                                                ?>

                                                <a   href="<?php echo $this->Url->build(["controller" => 'Member', "action" => "edit", $user->id]) ?>" title="Edit">
                                                    <span style="color:#00a65a;padding-left:5px;font-size:18px;">
                                                        <i class="fa fa-pencil" aria-hidden="true"></i>
                                                    </span>
                                                </a>

                                                <?php
                                            } elseif ($permission['page_id'] == $page_list['delete'] && !in_array($page_list['delete'], $added)) {
                                                array_push($added, $page_list['delete']);
                                                ?>
                                                <?= $this->Form->postLink('<span style="color:#dd4b39;padding-left:5px;font-size:18px;"><i class="fa fa-trash-o"></i></span>', ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete {0}?', $client->client->name), 'escape' => false]) ?>

                                                <?php
                                            } elseif ($permission['page_id'] == $page_list['view'] && !in_array($page_list['view'], $added)) {
                                                array_push($added, $page_list['view']);
                                                ?>
                                                <a href="<?php echo $this->Url->build(["controller" => 'Member', "action" => "view", $user->id]) ?>"  title="View">
                                                    <span style="color:#3c8dbc;padding-left:5px;font-size:18px;">
                                                        <i class="fa fa-eye" aria-hidden="true"></i>
                                                    </span>
                                                </a>
                                                <?php
                                            } elseif ($permission['page_id'] == $page_list['note'] && !in_array($page_list['note'], $added)) {
                                                array_push($added, $page_list['note']);
                                                ?>
                                                <a href="<?php echo $this->Url->build('/Client?show_client_checkbox=2&affiliate_client_id='.$user->id); ?>"  title="Note">
                                                    <span style="color:#3c8dbc;padding-left:5px;font-size:18px;" class="">
                                                        <i class="fa fa-users" aria-hidden="true"></i>
                                                    </span>
                                                </a>
                                    
                                                <a href="<?php echo $this->Url->build(["controller" => 'Member', "action" => "note", $user->id, $user->user_name]); ?>"  title="Note">
                                                    <span style="color:#3c8dbc;padding-left:5px;font-size:18px;" class="<?php echo $note_status; ?>">
                                                        <i class="fa fa-list-alt" aria-hidden="true"></i>
                                                        <?php if ($user->note_status > 0) { ?>
                                                            <span class="badge badge-light" style="background-color:#e94b35;position: absolute;"><?= $user->note_status ?></span>
                                                        <?php } ?>
                                                    </span>
                                                </a>
                                    
                                                <a href="#" onclick="sendAffiliateMemberMessage(<?php echo $user->id;?>,<?php echo $_SESSION['Auth']['User']['id'];?>,'modalloading','per-affiliate-loader-<?php echo $user->id;?>')"  title="Message">
                                                    <span style="color:#3c8dbc;padding-left:5px;font-size:18px;" class="">
                                                        <i class="fa fa-send-o" aria-hidden="true"></i>
                                                    </span>
                                                </a>
                                                <i style="position: absolute;display:none;" id="per-affiliate-loader-<?php echo $user->id;?>" class="fa fa-spinner fa-spin"></i>
                                                <?php
                                            }
                                        }
                                        ?>
                                    </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>

<script>

    $(document).ready(function () {
        $(document).on('click', '.edit', function () {
            var cid = $(this).attr('id');
            window.location = '<?php echo $this->Url->build('/teams/edit/'); ?>' + cid;
            return false;
        });

        $("#bulk_action_apply").click(function (e) {
            var selectedContent = $("input[name='selected_content']").val();
            if (selectedContent == null || selectedContent == "") {
                e.preventDefault();
                $('#status-area').flash_message({
                    text: 'Select atleast one item in the list!',
                    how: 'append'
                });
            }
        });
    });
    
</script>