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

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

  $prgid = $_POST[""];

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

    $wseq   = "";
    $table  = "";
    $pid    = "";
    $chgcd  = "";
    $vend = "";
    $ktktcd   = "";
    $ini    = "";
    $args   = "";

    // JSON形式をparam=value形式にする
    if($data==null) {

    } else {

      foreach ($data as $key => $value) {

        if($key=="PRGID") {
          $pid = $value;
        } elseif($key=="WSEQ") {
          $wseq = $value;
        } elseif($key=="TABLE") {
          $table = $value;
        } elseif($key=="CHGCD") {
          $chgcd = $value;
        } elseif($key=="KTKTCD") {
          $ktktcd = $value;
        } elseif($key=="VEND") {
          $vend = $value;
        } elseif($key=="INI") {
          $ini = $value;
        } elseif($key=="DBNO") {
          $dbno = $value;
        } elseif($key=="PARAMS") {
          foreach ($value as $pkey => $pvalue) {
            if($args==""){
              $args = $pkey . "=" . $pvalue;
            } else {
              $args = $args . "&" . $pkey . "=" . $pvalue;
            }
          }
        } else {

        }

      }

    }
    $outputFileName = uniqid();
    $exec = $serverUtil->getProjectDir() . "/Servlet/bin/".$pid.".sh";
    $param = $wseq . ' ' . $outputFileName . ' ' . $table . ' "' . $chgcd . '" "' . $ktktcd . '" @"' . $vend . '" "' . $args . '" @"' . $ini . '"'. '" @"' . $dbno;

    $logger->info("exec", $exec . " " . $param);

    $output = shell_exec($exec . ' '. $param);

    $result["filename"] = $outputFileName;

    $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("=== execReport_end ===", $response);
?>
