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

$baseObj->DB->tableExists(TB_numbering_rule);

if( !_notAuth("R") ){/************ 권한체크*/
  switch( $_REQUEST['mode'] ){
    case "getSeqList":
      $where = new DB_where("and");

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

      $sql = "SELECT NUM_ID, count(*) as NUM_SEQS, stuff(( select tnrs.FORMAT_STR from ".TB_numbering_rule." as tnrs where tnrs.NUM_ID = tnr.NUM_ID order by DISP_SEQ for xml path(''), type).value('.','varchar(max)'),1,0,'') as NUM_FORMAT from ".TB_numbering_rule." as tnr where {$whereqry} group by NUM_ID order by NUM_ID";
      $num_row = $baseObj->DB->query($sql, $whererow);
      if( $num_row ){
        $retval['code'] = 1;
        $retval['data'] = $num_row;
      }
    break;

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

      $where = new DB_where("and");
      $where->add("NUM_ID = ?", $_REQUEST['NUM_ID']);

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

      $sql = "SELECT * from ".TB_numbering_rule." where {$whereqry} order by DISP_SEQ";
      $num_row = $baseObj->DB->query($sql, $whererow);
      if( $num_row ){
        $retval['code'] = 1;
        $retval['data'] = Array(
          "NUM_OID"  => $_REQUEST['NUM_ID'],
          "NUM_ID"   => $_REQUEST['NUM_ID'],
          "NUM_RULE" => $num_row,
        );
      }
    break;

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

      $where = new DB_where("and");
      $where->add("NUM_ID = ?", $_REQUEST['NUM_ID']);

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

      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;
      }

      if( $_REQUEST['NUM_OID'] ){
        if( _notAuth("D") ) break; /************ 권한체크*/
        $baseObj->DB->delete(TB_numbering_rule, $whereqry, $whererow);
        $adrow = Array(
          "EDIT_TM"      => $baseObj->DB->sqleval("getdate()"),
          "EDIT_USER_ID" => $GLOBALS['_Umem']['member_id'],
        );
      }else{
        if( _notAuth("W") ) break; /************ 권한체크*/
        $adrow = Array(
          "MAKE_TM"      => $baseObj->DB->sqleval("getdate()"),
          "MAKE_USER_ID" => $GLOBALS['_Umem']['member_id'],
        );
      }

      foreach( (array)$_REQUEST['NUM_RULE'] as $key => $rule ){
        $ins_row = array_merge(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'],
        ),$adrow);
        $rst = $baseObj->DB->insert(TB_numbering_rule, $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;
      }

      $where = new DB_where("and");
      $where->add("NUM_ID = ?", $_REQUEST['NUM_ID']);

      $wqry = $where->get_where();
      $whereqry = $wqry['qry'];
      $whererow = $wqry['val'];
      $rst = $baseObj->DB->delete(TB_numbering_rule, $whereqry, $whererow);
      if( $rst ){
        $retval['code'] = 1;
      }

    break;
  }
}

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