<?php
/*
 * @Overview 부서 등록
 * @History 2024-03-25 / 최승연 / 최초작성
 */

$cate_id = "BASE_DEPT";
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 'getDeptList':
      $squl = "USP_B_DEPT_S10";
      $params = array(
        "BIZ_AREA_NM" => $_REQUEST["BIZ_AREA_NM"] ?: "",
        "DEPT_NM" => $_REQUEST["DEPT_NM"] ?: "",
        "USE_FLG" => $_REQUEST["USE_FLG"] ?: ""
      );
      $rst = $baseObj->DB->exec($squl, $params);

      $rows = array();
      if ($rst) {
        $retval['code'] = true;
        while ($row = $baseObj->DB->fetch_array($rst)) {
          $rows[] = $row;
        }
        ;
        $retval['data'] = $rows;
        $retval['mesg'] = '정상 동작 되었습니다.';
      } else {
        $retval['code'] = false;
      }
      break;
    case 'getTableLayout':
      $squl = "USP_ZB_COLUMN_STYLE_S10";
      $params = array(
        "menu_id" => $cate_id ?: "",
      );
      $rst = $baseObj->DB->exec($squl, $params);

      $rows = array();
      if ($rst) {
        $retval['code'] = true;
        while ($row = $baseObj->DB->fetch_array($rst)) {
          $rows[] = $row;
        }
        ;
        $retval['data'] = $rows;
        $retval['mesg'] = '정상 동작 되었습니다.';
      } else {
        $retval['code'] = false;
      }
      break;
    case 'putDept':
      if (_notAuth("W"))
        break;

      $_REQUEST["reqdata"] = json_decode($_REQUEST['reqdata']);

      $rst = true;

      foreach ($_REQUEST['reqdata'] as $data) {
        $data = (array) $data;

        $_REQUEST['RS_CODE'] = "";
        $_REQUEST['RS_MSG'] = "";
        $params = array(
          "DEPT_CD" => $data['DEPT_CD']?:"",
          "DEPT_NM" => $data['DEPT_NM']?:"",
          "BIZ_AREA_CD" => $data['BIZ_AREA_CD'] ?: "",
          'PAR_DEPT_CD' => $data['PAR_DEPT_CD']?:null,
          "MANAGER_EMP_NM" => $data['EMP_NM']?:"",
          "USE_FLG" => $data['USE_FLG'] ?: "Y",

          "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)
        );


        if ($data['_STATUS_'] == 'D') {
          $sql = "USP_B_DEPT_D10";

          $params = array(
            "DEPT_CD" => $data['DEPT_CD'],
          );

        } else {
          // 길이제한
          if (strlen($data['DEPT_CD']) > 20) { $_REQUEST['RS_MSG'] = cUtil\Mlang::getText("부서코드는 20자 내로 작성해주세요."); $rst = false; break; }
          if (mb_strlen($data['DEPT_NM']) > 50) { $_REQUEST['RS_MSG'] = cUtil\Mlang::getText("부서명은 50자 내로 작성해주세요."); $rst = false; break; }

          switch ($data['_STATUS_']) {
            case 'I':
              $sql = "USP_B_DEPT_I10";
              break;

            case 'U':
              $sql = "USP_B_DEPT_U10";
              break;
          };


        }

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


      }

      if ($rst && !$_REQUEST['RS_CODE']) {
        $retval['code'] = true;
      } else {
        $retval['code'] = false;
        $retval['mesg'] = $_REQUEST['RS_MSG'];
      }
      break;
    case 'changeTable':
      $_REQUEST["reqdata"] = json_decode($_REQUEST['reqdata']);
//      print_r($_REQUEST["reqdata"]);
      $retval['code'] = true;
      $sql = "USP_ZB_COLUMN_STYLE_UI10";

      foreach ($_REQUEST['reqdata'] as $data) {
        $data = (array)$data;

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

        $params = array(
          "menu_id" => $cate_id  ?: "",
          "field" => $data['field'] ?: "",
          "width" => $data['width'] ?: "",
          "RS_CODE" => array(&$_REQUEST['RS_CODE'], SQLSRV_PARAM_OUT),
          "RS_MSG" => array(&$_REQUEST['RS_MSG'], SQLSRV_PARAM_OUT)
        );

        $rst = $baseObj->DB->exec($sql, $params);
      }
      break;
      $retval['code'] = true;
  }
}

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