<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/x-json");
include($_SERVER['DOCUMENT_ROOT'] .'/script/HeaderInc.php');
if('XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH'] && true === isset($_SESSION['SessionAuth']['id']) && 0 < $_SESSION['SessionAuth']['id']) {
  $ObjJqGridMod = new JqGridMod();
  $ObjJqGridMod->SetArrayIn($_GET);
  $VarWhere = '';
//  $VarWhere = " AND a.sprstatus_id IN (3)";
  if (true === isset($_GET['demands_statusdate_b']) || true === isset($_GET['demands_statusdate_e'])){
    $VarWhere .= SiteLibs::ConvertTextDateToSql('a.demands_statusdate', $_GET['demands_statusdate_b'], $_GET['demands_statusdate_e']);
  }
  if(true === isset($_GET['_search']) && 'true' == $_GET['_search']){
    if(true === isset($ObjJqGridMod->filters) && '' != $ObjJqGridMod->filters){
      $VarWhere .= ' AND '. $ObjJqGridMod->generateSearchStringFromFilter();
    }
    if(true === isset($ObjJqGridMod->searchField) && '' != $ObjJqGridMod->searchField){
      $VarWhere .= ' AND '. $ObjJqGridMod->generateSearchStringFromSearchField();
    }
  }
  $VarWhere = SiteLibs::StripAndOrFirst($VarWhere);
  $VarOrder = $ObjJqGridMod->GetOrder();
  $VarLimit = $ObjJqGridMod->GetLimit();
  $ObjDemandsMod = new DemandsMod($MS_SP);
  $count = $ObjDemandsMod->GetFromDb('SqlByEngineerCount', 'GetOne', $VarWhere);
  $total_pages = $ObjJqGridMod->GetTotalPages($count);
  $page = $ObjJqGridMod->GetPage();
  $ArrUsers = $ObjDemandsMod->GetFromDb('SqlByEngineer', 'GetAll', $VarWhere, $VarOrder, $VarLimit);
  $VarDemandsCountTotal = 0;
  foreach ($ArrUsers AS $key => $row){
    if(1 == $row['users_status']){
      $VarUsersStatus = 'Активен';
    }
    else{
      $VarUsersStatus = 'Отключен';
    }
    if(true === is_numeric($row['demands_count']) && 0 < $row['demands_count']){
      //$VarDemandsCount = '<a class="ui-state-default" href="'. $MS_SP->Sp_PathTop .'/ErrorNoJs.php" OnClick="DemandsByEngineerList('. $row['users_id'] .'); return false;" title="Посмотреть заявки">'. $row['demands_count'] .'</a>';
      $VarDemandsCount = '<a class="ui-state-default" target="_blank" href="'. $MS_SP->Sp_PathTop .'/OpDemands.php?demands_masterid='. $row['users_id'] .'&demands_statusdate_b='. $_GET['demands_statusdate_b'] .'&demands_statusdate_e='. $_GET['demands_statusdate_e'] .'">'. $row['demands_count'] .'</a>';
    }
    else{
      $row['demands_count'] = 0;
      $VarDemandsCount = 0;
    }
    $VarDemandsCountTotal += $row['demands_count'];
    $json->rows[$key]['id'] = $row['users_id'];
    $json->rows[$key]['cell'][] = $VarUsersStatus;
    $json->rows[$key]['cell'][] = $row['users_fio'].'';
    $json->rows[$key]['cell'][] = $VarDemandsCount;
    $json->rows[$key]['cell'][] = $row['structureorganization_title'].'';
    $json->rows[$key]['cell'][] = $row['users_jobtitle'].'';
    $json->rows[$key]['cell'][] = $row['users_tel'].'';
    $json->rows[$key]['cell'][] = $row['users_telint'].'';
    $json->rows[$key]['cell'][] = $row['users_telcell'].'';
    $json->rows[$key]['cell'][] = $row['users_email'].'';
    $json->rows[$key]['cell'][] = $row['users_note'].'';
  }
//  $ArrFields[$key+1]['id'] = 'TotalId';
//  $ArrFields[$key+1]['cell'][] = '';
//  $ArrFields[$key+1]['cell'][] = '';
//  $ArrFields[$key+1]['cell'][] = 'Всего: '. $VarDemandsCountTotal;
//  $ArrFields[$key+1]['cell'][] = '';
//  $ArrFields[$key+1]['cell'][] = '';
//  $ArrFields[$key+1]['cell'][] = '';
//  $ArrFields[$key+1]['cell'][] = '';
//  $ArrFields[$key+1]['cell'][] = '';
//  $ArrFields[$key+1]['cell'][] = '';
//  $ArrFields[$key+1]['cell'][] = '';

  $json->page = $page;
  $json->total = $total_pages;
  $json->records = $count;
  $json->userdata['users_fio'] = $MS_SP->Sp_LangMessage['script']['ScrTotal'];
  $json->userdata['demands_count'] = $VarDemandsCountTotal;
  echo json_encode($json);
}
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');
?>