<?php
$cate_id = "mnu_basecode"; //현재 메뉴 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";


if( !_notAuth("R") ){/************ 권한체크*/
  switch( $_REQUEST['mode'] ){
    case "getCodeList":
      $retval['code'] = 1;
      $retval['data'] = $baseObj->getAllBaseCodeList("","","",$where);
      break;

    case "putCode":

      if( $_REQUEST['Code_Uid'] === "" ) {
        $retval['mesg'] = cUtil\Mlang::getText("코드 ID를 입력하지 않았습니다."); break;
      }
      if( !$_REQUEST['Code_Val'] && !$_REQUEST['Code_Exp'] ) {
        $retval['mesg'] = cUtil\Mlang::getText("코드값과 코드명중 하나는 입력해야 합니다.");break;
      }

      if( !$_REQUEST['Code_Val'] ) {
        $_REQUEST['Code_Val'] = $_REQUEST['Code_Uid'];
      }
      if( !$_REQUEST['Code_Exp'] ) {
        $_REQUEST['Code_Exp'] = $_REQUEST['Code_Val'];
      }

      if( !$_REQUEST['Code_Idx'] ){ //REQUEST 값 변경전 순서 찾기
        $midx = $baseObj->getMaxBaseCodeIdx($_REQUEST['Bran_Uid'], $_REQUEST['Group_Uid']);
        $_REQUEST['Code_Idx'] = $midx+1;
      }
      $_REQUEST['Bran_Uid'] = $_REQUEST['Bran_Uid'] ?: $_REQUEST['Group_Uid'] ?: $_REQUEST['Code_Uid'];
      $_REQUEST['Group_Uid'] = $_REQUEST['Group_Uid'] ?: $_REQUEST['Code_Uid'];

      $where = new DB_where("and");
      $subwhere = $where->sub_where("or");
      $subwhere->add("Bran_Uid != ?",$_REQUEST['Ori_Bran_Uid']);
      $subwhere->add("Group_Uid != ?",$_REQUEST['Ori_Group_Uid']);
      $subwhere->add("Code_Uid != ?",$_REQUEST['Ori_Code_Uid']);
      $row = $baseObj->getBaseCode($_REQUEST['Bran_Uid'], $_REQUEST['Group_Uid'], $_REQUEST['Code_Uid'], $where);

      if( isset($row['Code_Uid']) ){
        $retval['mesg'] = cUtil\Mlang::getText("코드가 중복됩니다."); break;
      }

      if( $_REQUEST['Code_Active'] != "Y" ){ $_REQUEST['Code_Active'] = "N"; }
      $sqldata = Array(
        'Code_Val'    => isset($_REQUEST['Code_Val']) ? $_REQUEST['Code_Val'] : "",
        'Code_Exp'    => isset($_REQUEST['Code_Exp']) ? $_REQUEST['Code_Exp'] : "",
        'Code_Idx'    => isset($_REQUEST['Code_Idx']) ? $_REQUEST['Code_Idx'] : "",
        'Active'      => $_REQUEST['Code_Active'],
      );
      if( $_REQUEST['Ori_Code_Uid'] !== "" ){

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

        $sqldata = array_merge(Array(
          'Code_Uid'   => isset($_REQUEST['Code_Uid']) ? $_REQUEST['Code_Uid'] : "",
        ), $sqldata);
        $rst = $baseObj->setBaseCode($sqldata, $_REQUEST['Ori_Bran_Uid'], $_REQUEST['Ori_Group_Uid'], $_REQUEST['Ori_Code_Uid']);

        if( $_REQUEST['Ori_Code_Uid'] != $_REQUEST['Code_Uid'] && $_REQUEST['Ori_Group_Uid'] == $_REQUEST['Ori_Code_Uid'] ){ //Group Uid 로 사용되는 Code_Uid 가 변경될때
          $sqldata = Array(
            'Group_Uid'   => isset($_REQUEST['Code_Uid']) ? $_REQUEST['Code_Uid'] : "",
          );
          $baseObj->setBaseCode($sqldata, $_REQUEST['Ori_Bran_Uid'], $_REQUEST['Ori_Group_Uid']);
        }
        //if( $_REQUEST['Ori_Group_Uid'] != $_REQUEST['Group_Uid'] && $_REQUEST['Ori_Bran_Uid'] == $_REQUEST['Ori_Group_Uid'] ){ //Bran Uid 로 사용되는 Code_Uid 가 변경될때
        if( $_REQUEST['Ori_Code_Uid'] != $_REQUEST['Code_Uid'] && $_REQUEST['Ori_Bran_Uid'] == $_REQUEST['Ori_Code_Uid'] ){ //Bran Uid 로 사용되는 Code_Uid 가 변경될때
          $sqldata = Array(
            'Bran_Uid'   => isset($_REQUEST['Code_Uid']) ? $_REQUEST['Code_Uid'] : "",
          );
          $baseObj->setBaseCode($sqldata, $_REQUEST['Ori_Bran_Uid']);
        }
      }else{

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

        $sqldata = array_merge(Array(
          'Bran_Uid'    => isset($_REQUEST['Bran_Uid'])  ? $_REQUEST['Bran_Uid'] : "",
          'Group_Uid'   => isset($_REQUEST['Group_Uid']) ? $_REQUEST['Group_Uid'] : "",
          'Code_Uid'    => isset($_REQUEST['Code_Uid'])  ? $_REQUEST['Code_Uid'] : "",
        ), $sqldata);
        $rst = $baseObj->putBaseCode($sqldata);
      }

      if( $rst ){
        //$_REQUEST['mode'] = "sortCode";
        //include __FILE__;
        $retval['code'] = 1;
        $baseObj->sortBaseCode($_REQUEST['Bran_Uid'], $_REQUEST['Group_Uid'], $_REQUEST['Code_Uid'], $_REQUEST['Code_Idx']);
      }else{
        $retval['mesg'] = cUtil\Mlang::getText("등록시 오류가 발생하였습니다. 확인하여 주십시오.");
      }
      break;

    case "delCode":

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

      if( !isset($_REQUEST['Bran_Uid']) || !isset($_REQUEST['Group_Uid']) || !isset($_REQUEST['Code_Uid']) ) {
        $retval['mesg'] = cUtil\Mlang::getText("삭제할 코드를 선택하지 않았거나, 올바른 접근이 아닙니다."); break;
      }

      $row = $baseObj->getBaseCode($_REQUEST['Bran_Uid'], $_REQUEST['Group_Uid'], $_REQUEST['Code_Uid'],$where);

      if( $row['Bran_Uid'] == $row['Code_Uid'] ){
        $rst = $baseObj->delBaseCode($row['Bran_Uid']);
      }else if( $row['Group_Uid'] == $row['Code_Uid'] ){
        $rst = $baseObj->delBaseCode($row['Bran_Uid'], $row['Group_Uid']);
      }else{
        $rst = $baseObj->delBaseCode($row['Bran_Uid'], $row['Group_Uid'], $row['Code_Uid']);
      }

      if( !$rst ){
        $retval['mesg'] = cUtil\Mlang::getText("삭제시 오류가 발생하였습니다. 확인하여 주십시오.");
      }else{
        //$_REQUEST['mode'] = "sortCode";
        //$_REQUEST['Code_Idx'] = $_REQUEST['Code_Idx'] - 1;
        //$_REQUEST['adv'] = 0;
        //include __FILE__;
        $retval['code'] = $baseObj->sortBaseCode($_REQUEST['Bran_Uid'], $_REQUEST['Group_Uid'], $_REQUEST['Code_Uid'], $_REQUEST['Code_Idx'], -1);
      }
      break;

    /*
    case "sortCode":
      return $baseObj->sortBaseCode($_REQUEST['Bran_Uid'], $_REQUEST['Group_Uid'], $_REQUEST['Code_Uid'], $_REQUEST['Code_Idx'], $_REQUEST['adv']);
    break;
    */
  }
}

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