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

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

$gInit_array = array( //初期値
  "iseq" => "",
);

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

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

  // SQL実行
  $header = $data["header"];
$logger->info("data",$data);

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

  if(!$stringUtil->isEmpty($header["vseq"])) {

    //vseq入力ありの場合、aur32からデータ表示
    $whereParams=array();
    $whereParams["ktktcd"] = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
    $whereParams["mcd"]   = $header["mcd"];
    $whereParams["vseq"]   = $header["vseq"];
    $whereParams["viseq"]  = $header["viseq"];
    $sql = file_get_contents("./sql/search.sql");
  $logger->info('search>>'.$sql, $whereParams);
    $result = $dbutil->select($sql, $whereParams);

    if ($result == null) { //該当データなければ新規
      $ret = "0";
      $cnt = 0;
      $ret_detail = $gInit_array;
      $ret_detail["cls"] = $header["cls"];
    } else {
      $ret = "0";

      $ret_detail = $result;

      $cnt = 0;
      foreach($ret_detail as $key => $val) {
        $cnt++;
      }
    }
  } else {
    //vseq入力なしの場合、新規
    //上でとったヘッダー部分だけそのまま渡し、明細は無し
    $ret = "0";
    $cnt = 0;
  }


  $ret_array["ret"] = $ret;
  $ret_array["ret_array"] = $ret_detail;
  $ret_array["ret_header"] = $ret_header;
  $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("=== af271x_getDetail_end ===", $response);

?>
