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

//$response["status"] = "NG";
$valutil = new getValueUtil();

$header = Array();
foreach($_POST as $key => $val) {
  $header[$key] = $val;
}
if($header["p_mode"] == '1') {
  $header["cls"] = '000';
  $ret04 = $valutil->allMastData("", "afm04", $header["mcd"]);
  $header["mnm"] = $ret04["mnm"];
}
$logger->info("header",$header);

//POPUP
$listutil = new getListUtil();
$assign_var["hasPopCucd"] = "1";

$assign_var["hasError"] = false;

try {
  global $dbutil;
  global $stringUtil;

  $assign_var["isNoData"] = false;
  $assign_var["noeventClassHeader"] = "";
  $assign_var["noeventClassBody"]   = "";

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

  //表示件数の取得
  $retLcnt = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["PAGE_LINE"];
  if($retLcnt == null) {
    $lcnt = "100";
  } else {
    $lcnt = $retLcnt;
  }

  if($stringUtil->isEmpty($header["pagecnt"])) {
    $header["pagecnt"] = "1";
  }
  $pgcnt = $header["pagecnt"];  //今回のページNo  1

  $ret_array = array(); //return用
  $ret_pgctrl = array(); //ページコントロール用
  $ret_detail = array();  //データ用

  $whereParams=array();
  $whereParams["ktktcd"]  = $_SESSION[$GLOBALS['SYSTEM_NAME_EN']]["LGKTKTCD"];
  $whereParams["cudt"]    = $header["cudt"];

  //↓ここから固定　--------------------
  $whereParams["lcnt"] = $lcnt; //表示行数
  $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 = array();

    $lastpage = $result[count($result) - 1]["pgno"];    //最終ページ数
    $ret_detail = $result;

    //伝票金額合計算出
    $tamt = 0;
    $tamt2 = 0;
    $vcnt = 0;
    $header["ttpno"] = $result[0]["ttpno"];
    $header["tamt"]  = $result[0]["tamt"];
  }

  $dataList = $ret_detail;  //明細データ

  //ページコントロール用
  $header["lastpage"] = $lastpage;  //最終ページ

  $header["ret"] = $ret; //戻値
  $header["pgcnt"] = $ret_pgctrl;  //頁関連

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

  $assign_var["hasError"] = true;
  $assign_var["errorMsg"] = $ue->getErrorMessage();

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

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

  $assign_var["hasError"] = true;
  $assign_var["errorMsg"] = $e->getErrorMessage();

//  $dbutil->rollback();
}

$assign_var["header"] = $header;
//$assign_var["dataList"] = $dataList;
$assign_var["dataList"] = json_encode($dataList);
$assign_var["footer"] = $footer;

$logger->info('assign_var',$assign_var);

//Smartyへアサイン
$smarty->assign($assign_var);
$smarty->display("index");

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

?>
