<?php
$total_selected = 0;

if(isset($data['list']) && !empty($data['list'])){
    foreach($data['list'] as $pkey1 => $pl1){
        foreach($pl1 as $pkey2 => $pl2){
            if(in_array($pkey2, $data['selected'])){
                $total_selected++;
            }
        }
    }
}  
$is_member_checked = false;
$is_admin_checked = false;
$is_admin_for_employee = false;
if($_SESSION['userType']== CRM_USER_INDICATOR && $_SESSION['Auth']['User']['type'] == 1){
    if($data['default'] == 1){
        $is_admin_checked = true;
        $is_member_checked = false;
        $total_selected = 1;
    }else{
        if($data['member_id'] > 0 && $data['member_id'] == $_SESSION['Auth']['User']['user_id']){
            $total_selected++;
            $is_member_checked = true;
        }
        
        if($data['admin_id'] > 0 && $_SESSION['Auth']['User']['id'] == $data['admin_id']){
            $total_selected++;
            $is_admin_for_employee = true;
        }
        
    }
}else{
    
    if($data['default'] == 1){
        $is_member_checked = true;
        $total_selected = 1;
    }else{
        if($data['member_id'] > 0 && $data['member_id'] == $_SESSION['Auth']['User']['id']){
            $total_selected++;
            $is_member_checked = true;
        }
    }
}

?>
<style>
    .assign-to .multiselect-container.dropdown-menu{
        left: 0;
        right: unset;
        top: unset;
        box-shadow: 0 0 5px;
    }
    
    .assign-to .multiselect .caret {
        position: absolute;
        top: 65%;
        right: 12px;
        margin-top: -8px;
        width: unset;
        text-align: right;
    }
    
    .assign-to li.active{
        border-bottom: 1px solid white;
    }
</style>
<span class="multiselect-native-select">
    <div class="btn-group" style="width: 100%;">
            <button type="button" class="multiselect dropdown-toggle btn btn-default" data-toggle="dropdown" title="Assign To" style="width: 100%; overflow: hidden; text-overflow: ellipsis;" aria-expanded="true">
                <span class="multiselect-selected-text selected-text"><?php echo $total_selected > 0 ? $total_selected:'No';?> selected</span> 
                <b class="caret"></b>
            </button>
        <ul class="multiselect-container dropdown-menu" style="width:100%">
            <?php if(isset($data['list']) && !empty($data['list'])){ ?>
                <?php foreach($data['list'] as $key1 => $l1){ ?>
                    <li class="multiselect-item multiselect-group">
                        <a href="javascript:void(0);">
                            <label style="padding:3px 20px 3px 20px"><b> <?php echo $key1; ?></b></label>
                        </a>
                    </li>
                    
                    <?php foreach($l1 as $key2 => $l2){ ?>
                    <li class="<?php echo in_array($key2, $data['selected']) || ($key2 == 0 && $is_member_checked) || ($key2 == -1 && $is_admin_checked) || ($key2 == -1 && $is_admin_for_employee) ? 'active':'';?>">
                        <a tabindex="0">
                            <label class="checkbox" style="font-weight:400;">
                                <input <?php echo $key2 == -1 ? 'disabled':''; ?> required="true" name="empIds[]" type="checkbox" class="<?php echo $data['class'] ?>" onclick="employeeAction(this,'<?php echo $data['url'];?>')" <?php echo in_array($key2, $data['selected']) || ($key2 == 0 && $is_member_checked) || ($key2 == -1 && $is_admin_checked) || ($key2 == -1 && $is_admin_for_employee) ? 'checked':'';?> value="<?php echo $key2;?>"/> 
                                <?php if($key2 == -1){ ?>
                                <input name="empIds[]" type="hidden" value="<?php echo $key2; ?>">
                                <?php } ?>
                                <?php echo $l2; ?>
                            </label>
                        </a>
                    </li>
                    <?php } ?>
                <?php } ?>
            <?php } ?>
        </ul>
    </div>
</span>

<script>
    function employeeAction(elem,url){
        if($(elem).prop('checked')){
            $(elem).parents('li').first().addClass('active')
        }else{
           $(elem).parents('li.active').first().removeClass('active')
        }
        
         var selected_count = 0;
        var final_ids = [];
        $(elem).parents('ul.multiselect-container').first().find('input[name="empIds[]"]').each(function(index,value){
            if($(this).prop('checked')){
               selected_count++;
            }
        });
        
        if(selected_count > 0){
            $('.selected-text').text(selected_count+' selected');
        }else{
             $('.selected-text').text('No selected');
        }
    }
    
</script>