<?php
$cate_id = "mnu_auto_seq"; //현재 메뉴 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 "getSeqList":
      $where = new DB_where("and");

      $wqry = $where->get_where();
      $whereqry = $wqry['qry'];
      $whererow = $wqry['val'];

      $sql = "USP_Z_NUMBERING_RULE_S10";
      $num_row = $baseObj->DB->exec_all($sql);
      if( $num_row ){
        $retval['code'] = 1;
        $retval['data'] = $num_row;
      }
    break;

    case "getSeq":
      if( !$_REQUEST['NUM_ID'] ){
        $retval['mesg'] = cUtil\Mlang::getText("채번 ID를 입력하지 않았습니다."); break;
      }

      $sql = "USP_Z_NUMBERING_RULE_S20";
      $params = Array(
        "NUM_ID" => $_REQUEST['NUM_ID']?:"",
      );
      $num_row = $baseObj->DB->exec_all($sql, $params);
      if( $num_row ){
        $retval['code'] = 1;
        $retval['data'] = Array(
          "NUM_OID"  => $_REQUEST['NUM_ID'],
          "NUM_ID"   => $_REQUEST['NUM_ID'],
          "REMK"     => $num_row[0]['REMK'],
          "NUM_RULE" => $num_row,
        );
      }
    break;

    case "putSeq":
      if( !$_REQUEST['NUM_ID'] ){
        $retval['mesg'] = cUtil\Mlang::getText("채번 ID를 입력하지 않았습니다."); break;
      }

      if( !$_REQUEST['NUM_OID'] || $_REQUEST['NUM_OID'] != $_REQUEST['NUM_ID'] ){

        /* 채번 읽기 호출 */
        $_REQUEST['mode'] = "getSeq";
        $_REQUEST['no_echo'] = true;
        include __DIR__."/auto_seq.ajax.php";
        $_REQUEST['no_echo'] = false;

        if( $retval['code'] ){
          $retval['code'] = 0;
          $retval['mesg'] = cUtil\Mlang::getText("이미 존재하는 채번 ID입니다."); break;
        }
        $retval['code'] = 0;
      }

      $duprow = array();
      foreach( (array)$_REQUEST['NUM_RULE'] as $key => $rule ){
        $duprow[] = $rule['COLUMN_ID'];
      }

      if( count($duprow) != count(array_unique($duprow)) ){
        $retval['mesg'] = cUtil\Mlang::getText("중복되는 컬럼ID 존재합니다."); break;
      }

      if( $_REQUEST['NUM_OID'] ){
        if( _notAuth("D") ) break; /************ 권한체크*/

        /* 채번 삭제 호출 */
        $_REQUEST['mode'] = "delSeq";
        $_REQUEST['no_echo'] = true;
        include __DIR__."/auto_seq.ajax.php";
        $_REQUEST['no_echo'] = false;

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

      foreach( (array)$_REQUEST['NUM_RULE'] as $key => $rule ){
        $ins_row = Array(
          "NUM_ID"       => $_REQUEST['NUM_ID'],
          "NUM_SEQNO"    => $key+1,
          "DISP_SEQ"     => $key+1,
          "COLUMN_ID"    => $rule['COLUMN_ID'],
          "FORMAT_STR"   => $rule['FORMAT_STR'],
          "DISP_TYPE"    => $rule['DISP_TYPE'],
          "START_POS"    => $rule['START_POS'],
          "SEL_LEN"      => $rule['SEL_LEN'],
          "INCRE_STEP"   => $rule['INCRE_STEP'],
          "INCRE_SEED"   => $rule['INCRE_SEED'],
          "CONV_FUNC"    => $rule['CONV_FUNC'],
          "REMK"         => $_REQUEST['REMK'],
          "MAKE_TM"      => "",
          "MAKE_USER_ID" => $GLOBALS['_Umem']['member_id'],
        );
        $sql = "USP_Z_NUMBERING_RULE_I10";
        $rst = $baseObj->DB->exec($sql, $ins_row);
        if( $rst ){
          $retval['code'] = 1;
        }
      }
    break;


    case "delSeq" :
      if( _notAuth("D") ) break; /************ 권한체크*/
      if( !$_REQUEST['NUM_ID'] ){
        $retval['mesg'] = cUtil\Mlang::getText("채번 ID를 입력하지 않았습니다."); break;
      }

      $sql = "USP_Z_NUMBERING_RULE_D10";
      $params = Array(
        "NUM_ID" => $_REQUEST['NUM_ID']?:"",
      );
      $rst = $baseObj->DB->exec($sql, $params);
      if( $rst ){
        $retval['code'] = 1;
      }

    break;
  }
}

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