<?php
session_start();
require_once('../../inc/confNetisse.php');
$DATA = "";
$da = (isset($_SESSION['profiles']) && (in_array("DA",$_SESSION['profiles']) || in_array("DAM",$_SESSION['profiles'])|| in_array("DS",$_SESSION['profiles']) || in_array("DR",$_SESSION['profiles'])))?true:false;
function getUserProfiles($id_agent){
  $user_profiles = array();
  $qy = "SELECT p.nom FROM ca_profils p LEFT JOIN ca_conseiller_agence ca ON ca.id_profile = p.id_profil WHERE id_conseiller=:id_conseiller";
  $values = array(':id_conseiller' => intval($id_agent));
  $result = excuteQuery($qy, $values);
  $res = $result['stmt'];
  $ny = $res->rowCount();
  if ($ny > 0)
  {
    while ($o = $res->fetchObject()) {
      array_push($user_profiles, $o->nom);
    }
  }
  return $user_profiles;
}
if(isset($_SESSION['auth']) && isset($_REQUEST['IDAGENCE']) && $GLOBALS["NOTIF_CONF"]["VIEWNOTIFSTATUS"] != "NONE"){
  $dla = date('Y-m-d');
  $values = array('id_agence4'=>intval($_REQUEST['IDAGENCE']),':supp' => 0);
  $prenomColumn = ($GLOBALS["BO_CONF"]["CONSEILLER"]["prenom"]) ? ", c.prenom" : "";
  $dlgColumn = ($GLOBALS["BO_CONF"]["CONSEILLER"]["dlg"]) ? ", c.dlg" : "";
  if($GLOBALS["BO_CONF"]["MOBILITY"]){
     $profilsJoin = '
    LEFT JOIN ca_agences a ON ca.id_agence = a.id_agence
    LEFT JOIN ca_conseillers c ON c.id_conseiller = ca.id_conseiller ';
     //$values[':id_agence4'] = intval($_REQUEST['IDAGENCE']);
  }else{
     $profilsJoin = '';
  }
 
  if($GLOBALS["BO_CONF"]["CONSEILLER"]["multiple_profiles"]){
    $agenceFilter = '';
  }else{
    $agenceFilter = ($GLOBALS["BO_CONF"]["CONSEILLER"]["multiple_profiles"])?'':'AND ca.id_agence=:id_agence1';
    $values[':id_agence1'] = intval($_REQUEST['IDAGENCE']);
  }
  $values[':inactif'] = 0;
  $temporaryColumn = ($GLOBALS["BO_CONF"]["MOBILITY"])?", ca.temporaire":"";
  $lastActiveColumn = ($GLOBALS["BO_CONF"]["MOBILITY"])?", ca.last_active":"";
  $lastActiveSecColumn = ($GLOBALS["BO_CONF"]["MOBILITY"])?", TIMESTAMPDIFF(SECOND,ca.last_active,now()) last_active_sec":"";
  $activeTodayColumn  = ($GLOBALS["BO_CONF"]["MOBILITY"])?', IF(ca.last_active LIKE "'.$dla.'%",1,0) active_today':"";
  // if($GLOBALS["BO_CONF"]["MOBILITY"]){
  //   $whereActivity = " OR (ca.id_agence=:id_agence3)";
  //   $values[':id_agence3'] = intval($_REQUEST['IDAGENCE']);
  // }else{
  //   $whereActivity = "";
  // }
 
  //($GLOBALS["BO_CONF"]["MOBILITY"])?" OR (TIMESTAMPDIFF(SECOND,ca.last_active,now())<=".$GLOBALS["PILE_CONF"]["KEEPALIVE"].")":"";
  $notificationsColumn = ($GLOBALS["BO_CONF"]["MOBILITY"])?", ca.receive_notifications":", c.receive_notifications";
  $q = 'SELECT ca.id_conseiller, c.nom, p.nom AS profile'.$prenomColumn.', ca.status'.$notificationsColumn.', ca.id_agence, c.photo, c.inactif'.$temporaryColumn.$lastActiveColumn.$dlgColumn.$lastActiveSecColumn.$activeTodayColumn.'
  FROM ca_conseiller_agence ca
  '.$profilsJoin.'  
  LEFT JOIN ca_profils p ON ca.id_profile = p.id_profil
  WHERE ca.id_agence = :id_agence4
  AND c.supp=:supp '.$agenceFilter.' 
  AND c.inactif=:inactif
  AND c.photo != "../photos/incognito.jpg"
  ORDER BY c.nom ASC';
  $result = excuteQuery($q, $values);
  $res = $result['stmt'];
  $DATA .= "<table class='manage-notif' width='100%'><tr><th colspan=2></th>";
  if($GLOBALS["BO_CONF"]["MOBILITY"]) $DATA .= "<th>Connecté</th><th>Affichage Borne</th><th>Affiché</th>";
  $DATA .= ($da || $GLOBALS["NOTIF_CONF"]["EDITNOTIFS"] == "ALL") ? "<th>Notifs</th>" : "";
  $DATA .= "<tr/>";
  while($conseiller = $res->fetchObject()){
    // $connected_once = ($conseiller->last_active_sec == null)?false:true;
    // $last_active =  !empty($conseiller->last_active_sec)?$conseiller->last_active_sec:0;
    $active = $conseiller->active_today == 1 ? true : false;//($connected_once && isset($last_active)) && $last_active <= 10;
    $statusColor = $active ? 'blue' : 'red';
    // Liste de profils conseiller
    $userProfilesList = array();
    if($GLOBALS["BO_CONF"]["CONSEILLER"]["multiple_profiles"])
    $userProfilesList = getUserProfiles($conseiller->id_conseiller);
    if(!($conseiller->temporaire && !$active)){ // do not show temporary agents that are not active
      if ($conseiller->status == -1){
        $showBorne = $active;
      }
      else{
        $showBorne = ($conseiller->status == 1) ? 0 : 1;
      }
      $borneColor = ($showBorne == 1) ? "green" : "red";

      $agentProfile = "";
      if(in_array($conseiller->profile,$userProfilesList)){
        if($conseiller->profile == 'PRO'){
          $agentProfile = " (Mobile)";
        }else{
          $agentProfile = " (".$conseiller->profile.")";
        }
        
      }
      $nomCons = $conseiller->nom;
      $nomCons .= ($GLOBALS["BO_CONF"]["CONSEILLER"]["prenom"]) ? " ".$conseiller->prenom : "";
      $disabled = ($GLOBALS["NOTIF_CONF"]["EDITNOTIFSTATUS"] != "ALL" && !$da) ? " disabled" : "";
      $DATA .= "<tr class='agent-row' data-agent='".$conseiller->id_conseiller."'>";
      $DATA .= "<td><img class='photo-conseiller' src='".$SERVER.$conseiller->photo."' style='background-color:".$GLOBALS["BORNE_CONF"]["COLORS"]["PRIMARYCOLOR"]."' /></td><td style='text-align:left'>".$nomCons.$agentProfile."</td>";
      $DATA .= ($GLOBALS["BO_CONF"]["MOBILITY"])?"<td class='online-status-flag'><span class='square ".$statusColor."'></span></td>":"";
      $DATA .= ($GLOBALS["BO_CONF"]["MOBILITY"])?"<td class='manage-conseiller-status'><select name='pile_status' class='pile_status' data-temporary='".$conseiller->temporaire."' data-id='".$conseiller->id_conseiller."' ".$disabled."><option value='0' ".($conseiller->status == 0 ?"selected":"").">Toujours affiché</option><option value='1' ".($conseiller->status == 1 ?"selected":"").">Jamais affiché</option><option value='-1' ".($conseiller->status == -1 ?"selected":"").">Affiché suivant connexion</option></select></td>":"";
      $DATA .= ($GLOBALS["BO_CONF"]["MOBILITY"])?"<td class='borne-status-flag'><span class='square ".$borneColor."'></span></td>":"";
      $DATA .= ($da || $GLOBALS["NOTIF_CONF"]["EDITNOTIFS"] == "ALL")?"<td><input type='checkbox' class='manage-conseiller-notification' data-id='".$conseiller->id_conseiller."' ".($conseiller->receive_notifications?"checked":"")." ".$disabled."></td>":"";
      // $DATA .= ($da && $conseiller->id_agence == $_REQUEST['IDAGENCE'])?"<td><a href='#' class='edit-agent' onclick=\"updateAgent('".$conseiller->id_conseiller."')\">Modifier</a></td>":"";
      $DATA .= "</tr>";
    }
  }
  $DATA .= "</table>";
}
else{
  $DATA = "Permission insuffisante!";
}
echo $DATA;
?>