<?php
$_REQUEST['cate_area'] = $_REQUEST['cate_area']?:"service";
$cate_id = "mnu_menu_{$_REQUEST['cate_area']}"; //현재 메뉴 ID 필수!!!
include_once "{$_SERVER['DOCUMENT_ROOT']}/common.inc.php";
include_once _ADM_INC_PATH_ ."/auth_check.php";
include_once _HOME_INC_PATH_ ."/ajax_init.php";

$mnuObj = new Menu(TB_menu_list);
$mnuObj->cate_area = $_REQUEST['cate_area'];

if( !_notAuth("R") ){/************ 권한체크*/
  switch( $_REQUEST['mode'] ) {
    case "getMenuList":
      $rows = $mnuObj->getMenuAuthList("");
      for( $a=0 ; $a<count($rows) ; $a++ ){
  //      foreach( $GLOBALS['_varsAuthArea'] as $auth_area => $name ){
  //        $rows[$a]['cate_auth_read'][$auth_area] = $mnuObj->AUTH->getHasAuthRow($rows[$a]['auth'][$auth_area], "READ");
  //        $rows[$a]['cate_auth_write'][$auth_area] = $mnuObj->AUTH->getHasAuthRow($rows[$a]['auth'][$auth_area], "WRITE");
  //        $rows[$a]['cate_auth_delete'][$auth_area] = $mnuObj->AUTH->getHasAuthRow($rows[$a]['auth'][$auth_area], "DELETE");
  //      }
        $rows[$a]['data']['cate_auth_read'] = $mnuObj->AUTH->getHasAuthRow($rows[$a]['data']['auth']['G'], "READ");
        $rows[$a]['data']['cate_auth_write'] = $mnuObj->AUTH->getHasAuthRow($rows[$a]['data']['auth']['G'], "WRITE");
        $rows[$a]['data']['cate_auth_delete'] = $mnuObj->AUTH->getHasAuthRow($rows[$a]['data']['auth']['G'], "DELETE");
        $rows[$a]['icon'] = $rows[$a]['data']['cate_use']!="Y"?"jstree-notuse text-danger bi bi-bookmark-x-fill":"";
        //$rows[$a]['state']['disabled'] = $rows[$a]['data']['cate_use']!="Y"?"1":"";
      }
      if( $rows ){
        $retval['code'] = 1;
        $retval['data'] = $rows;
      }else{
        $retval['mesg'] = "불러오지 못하였습니다.";
      }
    break;

    case "putMenu":

      if( !$_REQUEST['cate_id'] ){
        $retval['mesg'] = cUtil\Mlang::getText("메뉴 ID를 입력하지 않았습니다."); break;
      }
      if( !$_REQUEST['cate_name'] ){
        $retval['mesg'] = cUtil\Mlang::getText("메뉴명을 입력하지 않았습니다."); break;
      }

      if( $_REQUEST['cate_mode'] == "N" ){

        if( _notAuth("W") ) break; /************ 권한체크*/

        $caterow = $mnuObj->cate_info_id($_REQUEST['cate_id']);
        if( $caterow['cate_id'] == $_REQUEST['cate_id'] ){
          $retval['mesg'] = cUtil\Mlang::getText("같은 메뉴 ID를 사용하는 항목이 존재합니다."); break;
        }

        $rst = $mnuObj->putMenu($_REQUEST, "G");
      }else{

        if( _notAuth("D") ) break; /************ 권한체크*/

        $where = new DB_where("and");
        $where->add("cate_id != ?",$_REQUEST['parent_id']);
        $caterow = $mnuObj->cate_info_id($_REQUEST['cate_id'], $where);
        if( $caterow['cate_id'] == $_REQUEST['cate_id'] ){
          $retval['mesg'] = cUtil\Mlang::getText("같은 메뉴 ID를 사용하는 항목이 존재합니다."); break;
        }

        $rst = $mnuObj->setMenu($_REQUEST, "G");
      }

      $retval['code'] = $rst;
      $retval['mesg'] = cUtil\Mlang::getText("등록에 실패하였습니다.");
    break;

    case "delMenu":

      if( _notAuth("D") ) break; /************ 권한체크*/

      if( !$_REQUEST['parent_id'] ){
        $retval['mesg'] = cUtil\Mlang::getText("삭제할 메뉴를 선택하지 않았습니다."); break;
      }

      $rst = $mnuObj->delMenu($_REQUEST['cate_id']);

      $retval['code'] = $rst;
      $retval['mesg'] = cUtil\Mlang::getText("삭제되지 않았습니다. 다시 확인해 주세요.");
    break;

    case "sortMenu":
      $retval['code'] = $mnuObj->move_target_cate_id($_REQUEST['cate_id'],$_REQUEST['target_id'],$_REQUEST['pos_num']);
    break;

  }
}

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