<?php
$cate_id = "BASE_CODE"; //현재 메뉴 ID 필수!!!
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 "getAllCodeList":
      $retval['code'] = 1;
      $where = new DB_where("and");
      $where->add("CD.Bran_Uid != ?", "mem_status");
      $where->add("CD.Bran_Uid != ?", "DATA_FORMAT");
      $where->add("CD.Bran_Uid != ?", "PROD_STATUS");
      $where->add("CD.Bran_Uid != ?", "WRK_TEAM");
      $where->add("CD.Bran_Uid != ?", "ORD_TYPE");
      $where->add("CD.Bran_Uid != ?", "DAY_TYPE");
      $where->add("CD.Bran_Uid != ?", "RUNSTOP_GB");
      $where->add("CD.Bran_Uid != ?", "PLN_STATUS");
      $where->add("CD.Bran_Uid != ?", "DAY_TYPE");

      $where->add("CD.Bran_Uid != ?", "WO_STATUS");
      $where->add("CD.Bran_Uid != ?", "currency");
      $where->add("CD.Bran_Uid != ?", "cm_var");
      $where->add("CD.Bran_Uid != ?", "login_result");
      $where->add("CD.Bran_Uid != ?", "menu_area");
      $where->add("CD.Bran_Uid != ?", "auth_area");
      $where->add("CD.Bran_Uid != ?", "auth_admin");
      $where->add("CD.Bran_Uid != ?", "auth_member");
      $where->add("CD.Bran_Uid != ?", "common_status");
      $where->add("CD.Bran_Uid != ?", "board_editor");
      $where->add("CD.Bran_Uid != ?", "SALES_GRP");
      $where->add("CD.Bran_Uid != ?", "SHAPE_TYPE");
      $where->add("CD.Bran_Uid != ?", "SHIFT_ID");
      $where->add("CD.Bran_Uid != ?", "STOP_CD");
      $where->add("CD.Bran_Uid != ?", "TASKTYPE");
      $where->add("CD.Bran_Uid != ?", "TLColor");
      $where->add("CD.Bran_Uid != ?", "TM_TYPE");
      $where->add("CD.Bran_Uid != ?", "nation_code");
      $where->add("CD.Bran_Uid != ?", "SO_DTL_REMARK");
      $where->add("CD.Bran_Uid != ?", "FACLT_TYPE");
      $where->add("CD.Bran_Uid != ?", "TASK_TYPE");


      $where->add("BR.Active = ?", "Y");
      $where->add("GR.Active = ?", "Y");
      $where->add("CD.Active = ?", "Y");
      //            print_r($where);
      $retval['data'] = $baseObj->getAllBaseCodeList("", "", "", $where);
      break;
    case "getCodeList":
      $retval['code'] = 1;
      if ($_REQUEST['Bran_Uid']) {
        $where = new DB_Where();
        //$where->add("CD.Code_Uid <> ?",$_REQUEST['Bran_Uid']);
        $where->add("BR.Active = ?", "Y");
        $where->add("GR.Active = ?", "Y");

        $retval['data'] = $baseObj->getAllBaseCodeList($_REQUEST['Bran_Uid'], $_REQUEST['Group_Uid'], "", $where);

        for ($a = 0; $a < count($retval['data']); $a++) {
          $retval['data'][$a]['Code_Uid_Old'] = $retval['data'][$a]['Code_Uid'];
        }
      }
      break;

    case "putCode":
      if( _notAuth("W") ) break;

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

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

        $data = (array)$data;

        if( !$data['Code_Idx'] ){
          $midx = $baseObj->getMaxBaseCodeIdx($data['Bran_Uid'], $data['Group_Uid']) + 1;
          $data['Code_Idx'] = $midx;
        }

        // $data['Bran_Uid']  = $_REQUEST['Bran_Uid']  ?: $data['Group_Uid'] ?: $data['Code_Uid'];
        // $data['Group_Uid'] = $_REQUEST['Group_Uid'] ?: $data['Code_Uid'];

        $params = Array(
          'Bran_Uid'    => isset($data['Bran_Uid']) ? $data['Bran_Uid'] : "",
          'Group_Uid'   => isset($data['Group_Uid']) ? $data['Group_Uid'] : "",
          'Code_Uid'    => isset($data['Code_Uid']) ? $data['Code_Uid'] : null,
          'Code_Val'    => isset($data['Code_Val']) ? $data['Code_Val'] : "",
          'Code_Exp'    => isset($data['Code_Exp']) ? $data['Code_Exp'] : $data['Code_Val'],
          'Code_Idx'    => isset($data['Code_Idx']) ? $data['Code_Idx'] : "",
          'Active'      => $data['Bran_Active'],
        );

        switch($data['_STATUS_']) {
          case 'D':
            $rst = $baseObj->delBaseCode($data['Bran_Uid'],$data['Group_Uid'],$data['Code_Uid_Old']);
            break;
          case 'I':
            $row = $baseObj->getBaseCode($data['Bran_Uid'],"",$data['Code_Uid']);
            if( $row['Code_Uid'] && $row['Code_Uid'] == $data['Code_Uid'] ){
              $retval['mesg'] = cUtil\Mlang::getText("{@1} 는 이미 사용중인 코드입니다.",array($data['Code_Uid'])); break;
            }elseif(!$data['Code_Uid']){
              $retval['mesg'] = cUtil\Mlang::getText("Code를 입력해주세요."); break;
            }
            
            $rst = $baseObj->putBaseCode($params);
            break;
          case 'U':
            $rst = $baseObj->setBaseCode($params, $data['Bran_Uid'],$data['Group_Uid'],$data['Code_Uid_Old']);
          break;
        }
      }
      if( $rst ){
        $retval['code'] = 1;
      }
      break;


  }
}

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