<?php
//共通
require_once '../../base/common.php';
$logger = new logger(basename(__FILE__));
$logger->info("===af9031x_getDetail_start===");

$response["status"] = "NG";

try {
  global $dbutil;
  global $stringUtil;
  $valutil = new getValueUtil();

  $requestBody = file_get_contents('php://input');
  $data = json_decode($requestBody, true);

  $logger->info($data);

  // SQL実行
  $header = $data["header"];

  $ret_array = array(); //return用
  $ret_detail = array();  //データ用

  //cucdの取得
  if($stringUtil->isEmpty($header["cucd"])) {
    $ret03 = $valutil->getNmData("afm03",$header["cuedi"],"2");
    $header["cucd"] = $ret03["cucd"];
  }

  $whereParams=array();
  $whereParams["cucd"] = $header["cucd"];
  $sql = file_get_contents("./sql/searchreq.sql");
  $logger->info($sql,$whereParams);
  $result = $dbutil->select($sql, $whereParams);
  if ($result == null) {

    $whereParams=array();
    $whereParams["cucd"] = $header["cucd"];
    $sql = file_get_contents("./sql/search.sql");
    $logger->info($sql,$whereParams);
    $result = $dbutil->select($sql, $whereParams);

    if ($result == null) {
      $ret = "1";
      $cnt = 0;
    } else {
      $ret = "0";
      $ret_detail = $result;

      $cnt = 0;
      foreach($ret_detail as $key => $val) {
        $ret_detail[$key]["tdat"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]['SDDAY'];
        $cnt++;
      }
    }
  } else {
    $ret = "0";
    $ret_detail = $result;

    $cnt = 0;
    foreach($ret_detail as $key => $val) {
      $cnt++;
    }
  }

  if($cnt == 0) {
    $ret_array["tdat"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]['SDDAY'];
  }

  $ret_array["ret"] = $ret;
  $ret_array["ret_array"] = $ret_detail;
  $ret_array["cnt"] = $cnt;
  $result = $ret_array;

  $logger->info('getDetail_ret',$result );
  $response["status"] = "OK";
  $response["data"]   = json_encode($result, true);

} catch(userException $ue) {
  $retData["code"]  = $ue->getErrorCode();
  $retData["msg"]   = $ue->getErrorMessage();

  $response["status"] = "NG";
  $response["data"]   = $retData;

  $dbutil->rollback();
} catch(Exception $e) {
  $response["status"] = "NG";
  $response["data"]   = $e->getMessage();

  $dbutil->rollback();

} finally {
  print(json_encode($response));
}


$logger->info("=== af9031x_getDetail_end ===", $response);

?>
