<?php
/*
 * @Overview 설비 고장 조치 내역
 * @History 2024-04-15 / 최승연 / 최초작성
 */

$cate_id = "REG_FACLT_REPAIR";
include_once "{$_SERVER['DOCUMENT_ROOT']}/common.inc.php";
include_once _SVC_INC_PATH_ . "/auth_check.php";
include_once _HOME_INC_PATH_ . "/ajax_init.php";


/*
 * @Overview 설비고장조치등록
 * @History 2024-04-22 / 이원기 / 최초작성
 */

include_once "{$_SERVER['DOCUMENT_ROOT']}/common.inc.php";
include_once _SVC_INC_PATH_ . "/auth_check.php";
include_once _HOME_INC_PATH_ . "/ajax_init.php";

if (!_notAuth("R")) {


  switch ($_REQUEST['mode']) {

    // 작업자별 생산금액
    case 'getFacilityRep':

      $squl = "USP_P_FACILITY_REPAIR_HST_S10";

      $_REQUEST['RS_CODE'] = "";
      $_REQUEST['RS_MSG'] = "";

      $params = array(
        "FACLT_GRP" => $_REQUEST["FACLT_GRP_P"] ?: "",
        "FACLT_CD" => $_REQUEST["FACLT_CD"] ?: "",
        "FACLT_NM" => $_REQUEST["FACLT_NM"] ?: "",
        "P_START_DATE" => $_REQUEST["date_st"],
        "P_ED_DATE" => $_REQUEST["date_ed"],
        "RS_CODE" => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
        "RS_MSG" => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
      );
      $rst = $baseObj->DB->exec($squl, $params);

      $rows = array();
      if ($rst && $_REQUEST['RS_CODE'] !== 'E') {
        $retval['code'] = true;
        while ($row = $baseObj->DB->fetch_array($rst)) {
          $rows[] = $row;
        }
        ;
        $retval['data'] = $rows;
        $retval['mesg'] = '정상 동작 되었습니다.';
      } else {
        $retval['code'] = false;
        $retval['mesg'] = $_REQUEST['RS_MSG'];
      }

      break;
    case "putFacilityRep":
      if (_notAuth("W"))
        break; /************ 권한체크*/
      $_REQUEST['RS_CODE'] = "";
      $_REQUEST['RS_MSG'] = "";

      $_REQUEST['reqdata'] = json_decode($_REQUEST['reqdata']);
      $rst = true;
      $ok_cd = array();

      foreach ($_REQUEST['reqdata'] as $data) {
        $_REQUEST['RS_CODE'] = "";
        $_REQUEST['RS_MSG'] = "";

        $data = (array) $data;

        if ($data['FACLT_CD'] && $data['_STATUS_'] == 'D') {
          $sql = "USP_P_FACILITY_REPAIR_HST_D10";
          $params = array(
            "FACLT_CD" => $data['FACLT_CD'],
            "REPAIR_NO" => $data['REPAIR_NO'],
            "RS_CODE" => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
            "RS_MSG" => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
          );
        } else {
          //유효성 검사

          //공란 검사
          if (!$data['FACLT_CD']) {
            $_REQUEST['RS_MSG'] = cUtil\Mlang::getText("입력하지 않은 설비명이 존재합니다");
            $rst = false;
            break;
          }

          if ($data['FACLT_CD'] && $data['_STATUS_'] == 'U') {

            $sql = "USP_P_FACILITY_REPAIR_HST_U10";
            $params = array("REPAIR_NO" => $data['REPAIR_NO']);
          } else {
            $sql = "USP_P_FACILITY_REPAIR_HST_I10";
            $params = array();
          }

          $params = array_merge($params, array(
            "FACLT_CD" => $data['FACLT_CD'] ?: "",
            "REPAIR_CONTENT" => $data['REPAIR_CONTENT'] ?: "",
            "REPAIR_DT" => $data['REPAIR_DT'] ?: "",
            "REPAIR_BP_NM" => $data['REPAIR_BP_NM'] ?: "",
            "REPAIR_AMT" => floatval($data['REPAIR_AMT']) ?: null,
            "REPAIR_TEL_NO" => $data['REPAIR_TEL_NO'] ?: "",
            "MAKE_USER_ID" => $GLOBALS['_Umem']['member_id'],
            "RS_CODE" => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
            "RS_MSG" => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
          )
          );
        }

        $rst = $baseObj->DB->exec($sql, $params);
      }



      if ($rst && !$_REQUEST['RS_CODE']) {
        $retval['code'] = true;
      } else {
        $retval['code'] = false;
        $retval['mesg'] = $_REQUEST['RS_MSG'];
      }


      break;
  }
}



if (!$_REQUEST['no_echo']) {
  echo json_encode($retval);

}