<?php
if( !$cate_area ) return;

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

//회원 <-> 관리자 변경.. 하지말자.
//switch( $GLOBALS['_Umem']['member_auth'] ){
//  case "SA":
//    $authDest = array_merge($GLOBALS['_varsAuthAdmin'], $GLOBALS['_varsAuthMember']);
//  break;
//
//  default:
//    $authDest = $GLOBALS['_varsAuthMember'];
//  break;
//}
switch( $cate_area ){
  case "admin":
    $authDest = $GLOBALS['_varsAuthAdmin'];
   break;

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

$custObj = new Cust();
$where = new DB_where("and");
$where->add("cust_status = ?",1);
$where->add("cust_active = ?",1);
$cust_list = $custObj->getCustSelect("cust_id", "cust_name", $where);

$memObj = new Member();

$minfo = array();
$smnu_txt = "추가";
if( isset($_REQUEST['member_id']) ){
  $minfo = $memObj->getMember($_REQUEST['member_id']);
  $smnu_txt = "수정";
}

// unset($authDest['Y']); 
//비회원제거

include _ADM_INC_PATH_ ."/header.html";
?>

        <div id="cmd_bar" class="mb-2">
          <div class="h4"><i class="bi bi-files-alt"></i> <?=$cate_title?> <?=$smnu_txt?></div>
          <div class="text-right">
          </div>
        </div>

        <form method="post" enctype="multipart/form-data" id="aform" class="fadmin" action="###" onsubmit="return false;">
          <div class="fld-block">
            <label for="member_id">회원 ID </label>
            <div>
              <input type="text" name="member_id" id="member_id" value="<?=$minfo['member_id']?>" class="form-control inline" <?=($minfo)?"readonly":""?>>
              <small class="text-info"><?=cUtil\Mlang::getText("※ 아이디는 {$GLOBALS['_varsBaseCode']['id_len']}자리 이상이어야 합니다.")?></small>
            </div>
          </div>
          <div class="fld-block">
            <label for="member_pw">회원 비밀번호 </label>
            <div>
              <input type="password" name="member_pw" id="member_pw" class="form-control inline">
              <small class="text-info"><?=cUtil\Mlang::getText("※ 비밀번호는 대소문자, 숫자, 특수문자 포함 {$GLOBALS['_varsBaseCode']['pw_len']}자리 이상이어야 합니다.")?></small>
            </div>
          </div>
          <div class="fld-block">
            <label for="member_pwf">회원 비밀번호 확인</label>
            <div>
              <input type="password" name="member_pwf" id="member_pwf" class="form-control inline">
            </div>
          </div>
          <div class="fld-block">
            <label>회원명 </label>
            <div>
              <input type="text" name="member_name" value="<?=$minfo['member_name']?>" class="form-control inline">
            </div>
          </div>
          <!--
          <?php if( $cate_area == "service" ){?>
          <div class="fld-block">
            <label>소속 </label>
            <div>
              <select name="cust_id" data-defval="<?=$minfo['cust_id']?>" class="form-control form-select">
                <?=getSelectOptions($cust_list,"- 선택 -")?>
              </select>
            </div>
          </div>
          <?php }?>
          -->
          <div class="fld-block">
            <label for="member_mail">메일주소</label>
            <div>
              <input type="text" name="member_mail" data-defval="<?=$minfo['member_mail']?>" id="member_mail" class="form-control inline">
            </div>
          </div>
          <div class="fld-block">
            <label for="member_mobile">연락처</label>
            <div>
              <input type="text" name="member_mobile" data-defval="<?=$minfo['member_mobile']?>" id="member_mobile" class="form-control inline">
            </div>
          </div>
          <div class="fld-block">
            <label>승인여부 </label>
            <div class="form-inline">
              <select name="member_status" data-defval="<?=$minfo['member_status']?>" class="form-control form-select">
                <?=getSelectOptions($GLOBALS['_varsMemStatus'],"",$minfo['member_status'])?>
              </select>
            </div>
          </div>
          <div class="fld-block">
            <label>그룹 </label>
            <div class="form-inline">
              <select name="member_auth" data-defval="<?=$minfo['member_auth']?>" class="form-control form-select">
                <?=getSelectOptions($authDest,"",$minfo['member_auth'])?>
              </select>
            </div>
          </div>
          <?php if( $minfo ){?>
          <div class="fld-block">
            <label>로그인 횟수 </label>
            <div class="form-inline"><?=$minfo['login_count']?></div>
          </div>
          <div class="fld-block">
            <label>최종 방문 </label>
            <div class="form-inline"><?=getDT_datetime($minfo['login_date'])?></div>
          </div>
          <div class="fld-block">
            <label>최종 방문 IP </label>
            <div class="form-inline"><?=$minfo['login_ip']?></div>
          </div>
          <div class="fld-block">
            <label>가입일 </label>
            <div class="form-inline"><?=getDT_datetime($minfo['reg_date'])?></div>
          </div>
          <div class="fld-block">
            <label>수정일 </label>
            <div class="form-inline"><?=getDT_datetime($minfo['mod_date'])?></div>
          </div>
          <?php }?>
          <div class="text-center mt-4">
            <a href="./member_list.php" class="btn btn-success px-5">리스트</a>
            <button type="submit" class="btn btn-primary px-5"><?=$cate_title?> <?=$smnu_txt?></button>
          </div>
          <?=hidden_input('member_oid', $minfo['member_id'])?>
          <?=csrf_input('putMember')?>
        </form>

<?php
include _ADM_INC_PATH_ ."/footer.html";
?>

<script>
  jQuery("#aform").on("submit",function(){
    let formdata = new FormData(document.forms['aform']);
    formdata.append("mode","putMember");

    ajax_form({
      url: "./member.ajax.php",
      type: "POST",
      data: formdata,
      success: function (data) {
        if (!data.code) {
          kuls_alert(data.mesg);
          return;
        }
        kuls_alert('<?=cUtil\Mlang::getText("등록 되었습니다.")?>',function(){
          location.href = "<?=$list_url?>";
        });
      },
      error: function (request, status, error) {
        kuls_alert(error.message);
      }
    });
  });
</script>