<?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']) {
  if(true === isset($_GET['sidx']) && 'ip_name' == $_GET['sidx']){
    $_GET['sidx'] = 'ip_name_sort';
  }
  
  $ObjJqGridMod = new JqGridMod();
  $ObjJqGridMod->SetArrayIn($_GET);
  $VarWhere = '';
  if(true === isset($_GET['_search']) && 'true' == $_GET['_search']){
    if(true === isset($ObjJqGridMod->filters) && '' != $ObjJqGridMod->filters){
      $VarWhere .= $ObjJqGridMod->generateSearchStringFromFilter();
    }
    if(true === isset($ObjJqGridMod->searchField) && '' != $ObjJqGridMod->searchField){
      $VarWhere .= $ObjJqGridMod->generateSearchStringFromSearchField();
    }
  }
  $VarOrder = $ObjJqGridMod->GetOrder();
//  if(true === isset($_GET['sidx']) && 'ip_name' == $_GET['sidx']){
//    $VarOrder = str_replace('ip_name', '(REPLACE(ip_name, ".", ""))*1', $VarOrder);
//  }
  $VarLimit = $ObjJqGridMod->GetLimit();
  $ObjModIp = new IpMod($MS_SP);
  $count = $ObjModIp->GetFromDb('SqlCount', 'GetOne', $VarWhere);
  $total_pages = $ObjJqGridMod->GetTotalPages($count);
  $page = $ObjJqGridMod->GetPage();
  $ArrIp = $ObjModIp->GetFromDb('SqlAll', 'GetAll', $VarWhere, $VarOrder, $VarLimit);

  $ArrFields = array();
  foreach ($ArrIp AS $key => $row){
    $VarName = '';
    if(true === isset($row['svt_name']) && '' != $row['svt_name']){
      $VarName = '<a target="_blank" class="ui-state-default" href="/'. $MS_SP->Sp_PathTop .'/SvtInformation.php?svt_id='. $row['svt_id'] .'">'. $row['svt_name'] .'</a>';
    }
    $VarBuxName = '';
    if(true === isset($row['svt_buxname']) && '' != $row['svt_buxname']){
      $VarBuxName = '<a target="_blank" class="ui-state-default" href="/'. $MS_SP->Sp_PathTop .'/SvtInformation.php?svt_id='. $row['svt_id'] .'">'. $row['svt_buxname'] .'</a>';
    }
    $ArrFields[$key]['id'] = $row['ip_id'];
    $ArrFields[$key]['cell'][] = $row['ip_name'].'';
    $ArrFields[$key]['cell'][] = $VarName;
    $ArrFields[$key]['cell'][] = $VarBuxName;
    $ArrFields[$key]['cell'][] = $row['ip_note'].'';
  }
  $json = array("page" => $page, "total" => $total_pages, "records" => $count,"rows" => $ArrFields);
  echo json_encode($json);
}
include($_SERVER['DOCUMENT_ROOT'] .'/script/FooterInc.php');