<?php
session_start();
$pageSequence = $_SESSION['sequence'];
$toDiv = "contentZone" . $pageSequence;
$resultZoneDiv = "resultZone" . $pageSequence;
$tableZone = "#tableZone" . $pageSequence;
$tblName = "tblReport" . $pageSequence;
$btAdd = "btnAdd" . $pageSequence;
$btDel = "btnDel" . $pageSequence;
$btDelObj = "btnDelObj" . $pageSequence;
$dataSection = "dataSection" . $pageSequence;
$_SESSION['sequence']++;
//echo  dirname(__FILE__);  
//echo $_SERVER['DOCUMENT_ROOT'] . "   ".dirname(__FILE__)."  ".str_replace($_SERVER['DOCUMENT_ROOT'],"",dirname(__FILE__)) ;

$currentBaseDir = dirname($_SERVER['PHP_SELF']);
include($_SESSION['basedir'] . "veriflog.php");
include($_SESSION['basedir'] . "core.php");
$profileId = $dba->getSCCProfileByUserId($_SESSION['ownerId']);
$queueList = $dba->SCCGetVoiceQueueAll();

$res = $dba->crmGetAgentList($profileId, $_SESSION['ownerId'], -1);
?>

<style type="text/css">
    /*@import url('https://fonts.googleapis.com/css?family=Open+Sans'); */

    body{
        background:#f3f3f4;
        font-family: 'Open Sans', sans-serif;
    }
    .userMain .userBlock{
        float: left;
        width: 100%;
        box-shadow: 0px 0px 23px -3px #ccc;
        padding-bottom: 12px;
        margin-bottom: 30px;
        overflow: hidden;
        background:#fff;


    }
    .userMain .userBlock .backgrounImg{
        float: left;
        overflow: hidden;
        height: 77px;
    }
    .userMain .userBlock .backgrounImg img{
        width: auto;
    }
    .userMain .userBlock .userImg{
        text-align: center;
    }
    .userMain .userBlock .userImg img{
        width: 80px;
        height: 80px;
        margin-top: -39px;
        border-radius: 100%;
        border: 5px solid #fff;

    }
    .userMain .userBlock .userDescription{
        text-align: center;
    }
    .userMain .userBlock .userDescription h5{
        margin-bottom: 2px;
        font-weight: 600;
    }
    .userMain .userBlock .userDescription p{
        margin-bottom: 5px;
    }
    .userMain .userBlock .userDescription .btn{
        padding: 0px 23px 0px 23px;
        height: 22px;
        border-radius: 0;
        font-size: 12px;
        background: #0198dd;
        color: #fff;
    }
    .userMain .userBlock .userDescription .btn:hover{

        opacity:0.7;
    }

    .userMain .userBlock .followrs{
        display: inline-flex;
        margin-right: 10px;
        border-right: 1px solid #ccc;
        padding-right: 10px;
    }
    .userMain .userBlock .followrs .number{
        font-size: 15px;
        font-weight: bold;
        margin-right: 5px;
        margin-top: -1px;
    }

</style>


<!-- Content Header (Page header) -->
<section class="content-header">
    <div class="header-icon">
        <i class="fa fa-user-circle"></i>
    </div>
    <div class="header-title">
        <h1>Statut des agents</h1>
        <small>Statut </small>
    </div>
</section>
<!-- Main content -->
<div id="<?php echo $dataSection; ?>">
    <section class="content">
        <div class="row">
            <div class="col-sm-12">
                <div class="panel panel-bd lobidrag">
                    <div class="panel-heading">
                        <div class="btn-group" id="buttonexport">
                            <a href="#">
                                <h4>Etat des conseillers clients</h4>
                            </a>
                        </div>
                    </div>
                    <div class="panel-body">

                        <div class="container">
                            <div class="row userMain">
                                <div class="col-md-10">    
                                    <?php
                                    $cmpt = 1;

                                    for ($i = 0; $i < count($res); $i++) {
                                        $userStatus = false;
                                        ?>



                                        <div class="col-md-3 col-sm-4">
                                            <div class="userBlock">
                                                <div class="backgrounImg">
                                                    <?php
                                                    
                                                 for($p = 0; $p < count($queueList); $p++){
                                                     
                                                     $userStatus = $custAstManager->getAgentStatusByQueue($res[$i]['mobile_phone_v'], $queueList[$p]['name']);
                                                     
                                                     if ($userStatus == "Actif" || $userStatus == "Pause" || $userStatus == "Busy" ) {
                                                         break;
                                                     }
                                                     
                                                 }
                                                    
                                                    

                                                    //  echo $res[$i]['mobile_phone_v']." - Statut detecté :>".$userStatus;


                                                    if ($userStatus == "Actif" || $userStatus == "Pause") {
                                                        ?>
                                                        <img src="images/bg-agent-online.jpg">  
                                                    <?php } elseif ($userStatus == "Busy") { ?>
                                                        <img src="images/bg-agent-busy.jpg">
                                                    <?php } else { ?>
                                                        <img src="images/bg-agent-offline.jpg">
                                                    <?php } ?>
                                                </div>
                                                <div class="userImg">
                                                    <img src="images/user.png">

                                                </div>
                                                <div class="userDescription">
                                                    <h5>  <?php echo $res[$i]['first_name_v'] . " " . $res[$i]['last_name_v']; ?> </h5>


                                                    <div class="followrs">
                                                        <span class="number"><?php echo $res[$i]['mobile_phone_v']; ?></span>
                                                        <span></span>
                                                    </div>

                                                    <?php
                                                    // echo count($queueList);

                                                    /* for ($i = 0; $i < count($queueList); $i++) {



                                                      / echo $aStatus;

                                                      if ($aStatus =='Actif')  {

                                                      continue;
                                                      }
                                                      }
                                                     */

                                                    
                                                    for($p = 0; $p < count($queueList); $p++){
                                                     
                                                
                                                      $aStatus = $custAstManager->getAgentStatusByQueue($res[$i]['mobile_phone_v'], $queueList[$p]['name']);
                                                   
                                                     
                                                     if ($aStatus == "Actif" || $aStatus == "Pause" || $aStatus =="Busy" ) {
                                                         break;
                                                     }
                                                     
                                                 }

                                                    // if($userStatus)$aStatus ='Actif';
                                                    if ($aStatus == 'Actif') {
                                                        ?>
                                                        <a href="#" data-href="<?php echo $currentBaseDir; ?>/updateStatus.php?mobilePhone=<?php echo $res[$i]['mobile_phone_v']; ?>&status=pause" class="btn-updateStatus"> <button class="btn">Libre</button></a>
                                                    <?php } elseif ($aStatus == 'Pause') { ?>
                                                        <a href="#" data-href="<?php echo $currentBaseDir; ?>/updateStatus.php?mobilePhone=<?php echo $res[$i]['mobile_phone_v']; ?>&status=unpause" class="btn-updateStatus"> <button class="btn">Pause</button></a>
                                                    <?php } elseif ($aStatus == 'Busy') { ?>
                                                        <button class="btn">Occupé</button>

                                                    <?php } else { ?>
                                                        <button class="btn disabled">Déconnecté</button>
                                                    <?php } ?>

                                                </div>

                                            </div>
                                        </div>



                                    <?php } ?>

                                </div>

                            </div>
                        </div>


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

    </section>
</div>
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables/dataTables.bootstrap.min.js"></script>
<script src="plugins/datatables/dataTables.buttons.min.js"></script>
<script src="plugins/datatables/buttons.html5.min.js"></script>
<script type="text/javascript" src="js/jquery.loading.min.js"></script>


<!-- /.content -->


<script type="text/javascript" >

    var time = new Date().getTime();
    $(document.body).bind("mousemove keypress", function (e) {
        time = new Date().getTime();
    });

    function refresh() {
        if (new Date().getTime() - time >= 5000) {
            //alert('Reloding Tab');
            $('#<?php echo $toDiv; ?>').load('<?php echo $currentBaseDir; ?>/crm_user_status.php');
        } else
            setTimeout(refresh, 10000);
    }

    setTimeout(refresh, 10000);



    $("a[class='btn-updateStatus']").on('click', function () {
        // create the backdrop and wait for next modal to be triggered
        // $('body').modalmanager('loading');
        var customerurl = $(this).attr('data-href');

// alert(customerurl);
        $('#loader').loading({
            message: "Chargement...",
            theme: 'dark'

        });

        $.confirm({
            title: 'Attention!',
            content: 'Voulez-vous changer le statut?',
            buttons: {
                confirm: {
                    text: 'Confirmer',
                    btnClass: 'btn-blue',
                    action: function () {
                        $.getJSON(customerurl,
                                function (obj) {
                                    if (obj.errorCode == 0) {
                                        $('#<?php echo $toDiv; ?>').load('<?php echo $currentBaseDir; ?>/crm_user_status.php');
                                        $.alert({
                                            icon: 'glyphicon glyphicon-ok',
                                            title: 'RESULTAT',
                                            content: obj.errorMessage,
                                            type: 'green',
                                            typeAnimated: true

                                        });
                                    } else {

                                        $.alert({
                                            icon: 'glyphicon glyphicon-warning-sign',
                                            title: 'RESULTAT',
                                            content: obj.errorMessage,
                                            type: 'red',
                                            typeAnimated: true
                                        });


                                    }
                                });
                    }

                },
                cancel: {
                    text: 'Annuler',
                    action: function () {

                    }
                }
            } // Closing button
        }); // Closing confirm

        // alert($(this).attr('data-href'));
        $('#loader').loading('stop');


    });



</script>

<link rel="stylesheet" href="css/jquery-confirm.min.css">
<script src="js/jquery-confirm.min.js"></script>



<?php /* include 'call_event_listener.php'*/ ?> ;