<?php
if( !$cate_area ) return;

include_once "{$_SERVER['DOCUMENT_ROOT']}/common.inc.php";
include_once _ADM_INC_PATH_ ."/auth_check.php";
include_once _HOME_INC_PATH_ ."/ajax_init.php";

switch( $cate_area ){
  case "admin":
    $authDest = $GLOBALS['_varsAuthAdmin'];
   break;

  case "service":
    $authDest = $GLOBALS['_varsAuthMember'];
  break;
}

$minfo = $baseObj->getMember($_REQUEST['member_id']);

if( !_notAuth("R") ){/************ 권한체크*/
  switch ($_REQUEST['mode']) {

    case "getMemberList" :
      $where = new DB_where();
      $subwhere = $where->sub_where("or");
      foreach( (array)$authDest as $key => $val ){
        $subwhere->add("member_auth = ?",$key);
      }

      $addquery = Array(
        "member_status" => $_REQUEST['member_status']?:"",
        "member_auth" => $_REQUEST['member_auth']?:"",
        "cust_id" => $_REQUEST['cust_id']?:"",
      );
      foreach( $addquery as $key => $val ){
        if( $val ) $where->add("{$key} = ?",$val);
      }

      $retval['data'] = $baseObj->getMemberPage($_REQUEST,$addquery,$where,"",1);
    break;











    case "getMemberLogList" :
      $where = new DB_where();
      $where->add("login_page = ?", $cate_area);

      $addquery = Array(
        "login_success" => hasval($_REQUEST['login_success'])?$_REQUEST['login_success']:"",
      );
      foreach( $addquery as $key => $val ){
        if( hasval($val) ) $where->add("{$key} = ?",$val);
      }

      $retval['data'] = $baseObj->getMemberLogPage($_REQUEST,$addquery,$where);
    break;











    case "putMember" :

      if( csrf_check('putMember') ){
        $retval['mesg'] = cUtil\Mlang::getText("정상적인 접근이 아닙니다."); break;
      }

      if( !$_REQUEST['member_oid'] ){
        if( !$_REQUEST['member_id'] ){
          $retval['mesg'] = cUtil\Mlang::getText("아이디를 입력하지 않았습니다."); break;
        }else if( strlen(preg_replace("/[0-9a-z]/im","",$_REQUEST['member_id'])) > 0 || strlen(preg_replace("/[^0-9a-z]/im","",$_REQUEST['member_id'])) < $GLOBALS['_varsBaseCode']['id_len'] ){
          $retval['mesg'] = cUtil\Mlang::getText("아이디는 영문자/숫자 만으로 구성되며, {$GLOBALS['_varsBaseCode']['id_len']}자리 이상이어야 합니다."); break;
        }else if( $minfo['member_id'] ){
          $retval['mesg'] = cUtil\Mlang::getText("이미 존재하거나 사용 금지된 아이디 입니다."); break;
        }
      }

      if( !$_REQUEST['member_oid'] || $_REQUEST['member_pw'] || $_REQUEST['member_pwf'] ){
        if( !$_REQUEST['member_pw'] ){
          $retval['mesg'] = cUtil\Mlang::getText("비밀번호를 입력하지 않았습니다."); break;
        }else if( !$_REQUEST['member_pwf'] ){
          $retval['mesg'] = cUtil\Mlang::getText("비밀번호 확인을 입력하지 않았습니다."); break;
        }else if( strlen($_REQUEST['member_pw']) < $GLOBALS['_varsBaseCode']['pw_len'] ){
          $retval['mesg'] = cUtil\Mlang::getText("비밀번호는 {$GLOBALS['_varsBaseCode']['pw_len']}자 이상이어야 합니다."); break;
        }else if( $_REQUEST['member_pw'] != $_REQUEST['member_pwf'] ){
          $retval['mesg'] = cUtil\Mlang::getText("비밀번호가 일치하지 않습니다."); break;
        }
      }
      if( $cate_id == "mnu_adm_list" ){
        if( !$_REQUEST['member_name'] ){
          $retval['mesg'] = cUtil\Mlang::getText("회원명을 입력하지 않았습니다."); break;
        }
      }else{
//        if( !$_REQUEST['emp_no'] ){
//          $retval['mesg'] = cUtil\Mlang::getText("사원을 선택하지 않았습니다."); break;
//        }
//        $_REQUEST['member_name'] = array_reduce($cmv_obj->getEmployeeList("",""), function($row, $item){ if($item['EMP_NO'] == $_REQUEST['emp_no']){ $row[] = $item; } return $row; })[0]['EMP_SHORT_NM'];
//        if( !$_REQUEST['member_name'] ){
//          $retval['mesg'] = cUtil\Mlang::getText("사원명이 정확하지 않습니다."); break;
//        }
      }
      if( !chkValiMail($_REQUEST['member_mail']) ){
        $retval['mesg'] = cUtil\Mlang::getText("메일주소를 확인해 주세요."); break;
      }
      if( !chkValiPhone($_REQUEST['member_mobile']) ){
        $retval['mesg'] = cUtil\Mlang::getText("연락처를 확인해 주세요."); break;
      }

      $ins_row = Array(
        "member_name"   => $_REQUEST['member_name'],
        "member_mail"   => $_REQUEST['member_mail'],
        "member_mobile" => $_REQUEST['member_mobile'],
        "member_auth"   => $_REQUEST['member_auth'],
        "member_status" => $_REQUEST['member_status']?:2,
      );

      if( $_REQUEST['cust_id'] && !$_REQUEST['cust_name'] ){
        $custObj = new Cust();
        $cinfo = $custObj->getCust($_REQUEST['cust_id']);
        $_REQUEST['cust_name'] = $cinfo['cust_name'];
      }

      if( !$_REQUEST['member_oid'] ){

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

        $ins_row['member_id'] = $_REQUEST['member_id'];
        $ins_row['member_pw'] = $_REQUEST['member_pw'];

        $retval['code'] = $baseObj->putMember($ins_row);
      }else{

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

        $_REQUEST['member_id'] = $_REQUEST['member_oid'];
        $ins_row['member_id'] = $_REQUEST['member_oid'];
        if( $_REQUEST['member_pw'] ){
          $ins_row['member_pw'] = $_REQUEST['member_pw'];
        }

        $retval['code'] = $baseObj->setMember($ins_row);
      }
      if( $_REQUEST['emp_no'] ){
        $baseObj->setMemberParts(Array(
          "member_id"     => $_REQUEST['member_id'],
          "emp_no"        => $_REQUEST['emp_no']?:'',
        ));
      }
      if( $retval['code'] ){
        $baseObj->setMemberUnion($_REQUEST);
      }

      $retval['mesg'] = cUtil\Mlang::getText("등록에 실패하였습니다.");
    break;











    case "setMemberState" :

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

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

      $ins_row = Array(
        "member_id"  => $_REQUEST['delchk'],
        "member_status" => $_REQUEST['member_status'],
      );
      $retval['code'] = $baseObj->setMemberParts($ins_row);
      $retval['mesg'] = cUtil\Mlang::getText("등록에 실패하였습니다.");
    break;
  }
}

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