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

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

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

  $logger->info("data", $data);

try {
  global $dbutil;
    //更新
    foreach($data["data"] as $detail) {
      //apat0は変更不可のでチェックしない
      if ($detail['apat1'] != $detail['orgapat1']
          || $detail['apat2'] != $detail['orgapat2']
          || $detail['apat3'] != $detail['orgapat3']
          || $detail['apat4'] != $detail['orgapat4']
          || $detail['apat5'] != $detail['orgapat5']
          || $detail['apat6'] != $detail['orgapat6']
          || $detail['apat7'] != $detail['orgapat7']
          || $detail['apat8'] != $detail['orgapat8']
          || $detail['apat9'] != $detail['orgapat9']) {
        updateAxm993a( $dbutil, $detail, $logger );
      }
    }

    $dbutil->commit();

} 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 {
  $logger->info("response", $response );
  print( json_encode($response) );
}

$logger->info("=== af9993x_updata_end ===");

function updateAxm993a($dbutil, $data, $logger) {
  global $stringUtil;
  $valueData["apid"] = $data["apid"];
  $valueData["apat0"] = $data["apat0"];
  $valueData["apat1"] = $data["apat1"];
  $valueData["apat2"] = $data["apat2"];
  $valueData["apat3"] = $data["apat3"];
  $valueData["apat4"] = $data["apat4"];
  $valueData["apat5"] = $data["apat5"];
  $valueData["apat6"] = $data["apat6"];
  $valueData["apat7"] = $data["apat7"];
  $valueData["apat8"] = $data["apat8"];
  $valueData["apat9"] = $data["apat9"];

  $sql = file_get_contents("./sql/update.sql");
  $result = $dbutil->update($sql, $valueData);

  return $reuslt;
}

?>
