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

$custObj = new Cust();

if( !_notAuth("R") ){/************ 권한체크*/

  $cinfo = $custObj->getCust($_REQUEST['cust_id']);

  switch( $_REQUEST['mode'] ) {
    case "getCustList":
      $retval['data'] = $custObj->getCustPage($_REQUEST, $query, $where);
    break;



    case "putCust":
    case "setCust":

      if( !$_REQUEST['cust_oid'] ){
        if( !$_REQUEST['cust_id'] ){
          $retval['mesg'] = cUtil\Mlang::getText("소속코드를 입력하지 않았습니다."); break;
        }else if( strlen(preg_replace("/[0-9a-z]/im","",$_REQUEST['cust_id'])) > 0 || strlen(preg_replace("/[^0-9a-z]/im","",$_REQUEST['cust_id'])) < $GLOBALS['_varsBaseCode']['cust_id_len'] ){
          $retval['mesg'] = cUtil\Mlang::getText("소속코드는 영문자/숫자 만으로 구성되며, {$GLOBALS['_varsBaseCode']['cust_id_len']}자리 이상이어야 합니다."); break;
        }else if( $cinfo['cust_id'] ){
          $retval['mesg'] = cUtil\Mlang::getText("이미 존재하는 코드 입니다."); break;
        }
      }

      $sqlrow = Array(
        "cust_id"       => $_REQUEST['cust_id'],
        "cust_name"     => $_REQUEST['cust_name'],
        "cust_fullname" => $_REQUEST['cust_fullname'],
        "cust_charger"  => $_REQUEST['cust_charger'],
        "cust_phone"    => $_REQUEST['cust_phone'],
        "cust_mail"     => $_REQUEST['cust_mail'],
        "cust_status"   => $_REQUEST['cust_status'],
        "mod_date"      => $custObj->DB->sqleval("getdate()"),
      );
      if( !$_REQUEST['cust_oid'] ){
        if( _notAuth("W") ) break; /************ 권한체크*/

        $sqlrow['cust_active'] = 1;
        $sqlrow['reg_date'] = $custObj->DB->sqleval("getdate()");
        $rst = $custObj->putCust($sqlrow);
      }else{
        if( _notAuth("D") ) break; /************ 권한체크*/

        $_REQUEST['cust_id'] = $_REQUEST['cust_oid'];
        $rst = $custObj->setCust($sqlrow);
      }
      $retval['code'] = $rst;
      $retval['mesg'] = cUtil\Mlang::getText("등록에 실패하였습니다.");
    break;



    case "delCust":
    case "setCustState":
      if( _notAuth("D") ) break; /************ 권한체크*/

      if( !$_REQUEST['cust_status'] && $_REQUEST['cust_status'] !== "0" && $_REQUEST['cust_delete'] != "true" ){
        $retval['mesg'] = cUtil\Mlang::getText("상태를 선택하지 않았습니다."); break;
      }
      if( !$_REQUEST['delchk'] ){
        $retval['mesg'] = cUtil\Mlang::getText("대상을 선택하지 않았습니다."); break;
      }

      if( $_REQUEST['cust_delete'] == "true" || $_REQUEST['mode'] == "delCust" ){
        $sqlrow = Array(
          "cust_status" => 0,
          "cust_active" => 0,
        );
      }else{
        $sqlrow = Array(
          "cust_status" => $_REQUEST['cust_status'],
        );
      }

      $retval['code'] = $custObj->setCustParts($sqlrow, "cust_id", $_REQUEST['delchk']);
      $retval['mesg'] = cUtil\Mlang::getText("적용에 실패하였습니다.");
    break;
  }
}

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