<?php
if( !$cate_area ) return;

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

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

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

        <form method="get" name="sform" id="sform" class="sform form-inline" action="<?=$_SERVER['PHP_SELF']?>">
          <label> <span>가입 날짜 :</span>
            <input type="date" max="9999-12-31" name="sdate" class="dpicker form-control form-control-sm" value="<?=$_REQUEST['sdate']?>"> ~ <input type="date" name="fdate" class="dpicker form-control" value="<?=$_REQUEST['fdate']?>">
          </label>
          <?php if( $cate_area == "service" ){?>
          <label> <span>소속 :</span>
            <select name="cust_id" id="cust_id" class="form-select-sm form-control-sm" data-defval="<?=$_REQUEST['cust_id']?>">
              <?=getSelectOptions($cust_list,"- 선택 -")?>
            </select>
          </label>
          <?php }?>
          <label> <span>상태 :</span>
            <select name="member_status" id="member_status" class="form-select-sm form-control-sm" data-defval="<?=$_REQUEST['member_status']?>">
              <?=getSelectOptions($authDest,"- 전체 -")?>
            </select>
          </label>
          <label> <span>그룹 :</span>
            <select name="member_auth" id="member_auth" class="form-select-sm form-control-sm" data-defval="<?=$_REQUEST['member_auth']?>">
              <?=getSelectOptions($authDest,"- 전체 -")?>
            </select>
          </label>
          <label> <span>검색 내용 :</span> <input type="text" name="stxt" class="form-control form-control-sm" value="<?=$_REQUEST['stxt']?>"></label>
          <button type="submit" class="btn btn-primary btn-sm">검색</button>
          <label> <span>라인수 :</span>
            <select name="viewnum" id="viewnum" class="form-select-sm form-control-sm" data-defval="<?=$_REQUEST['viewnum']?:20?>" onchange="this.form.submit()">
              <option value="">라인수</option>
              <option value="10">10개</option>
              <option value="20">20개</option>
              <option value="30">30개</option>
              <option value="50">50개</option>
              <option value="100">100개</option>
            </select>
          </label>
          <?=hidden_input("ofld",$_REQUEST['ofld'])?>
          <?=hidden_input("oasc",$_REQUEST['oasc'])?>
        </form>

        <div id="cmd_bar" class="mb-2">
          <div class="h4"><i class="bi bi-files-alt"></i> <?=$cate_title?> 리스트</div>
          <div class="text-right">
            <a href="javascript:member_status_modal();" class="btn btn-secondary"> <i class="bi bi-caret-up-square"></i> 상태변경</a>
            <a href="./member_put.php" class="btn btn-primary"> <i class="bi bi-person-plus"></i> 회원 추가</a>
          </div>
        </div>

        <div class="container-fluid mt-3">
          <div class="row">
            <div class="col">
              <h5 class="d-flex justify-content-between">
                <span><i class="bi bi-subtract"></i> <?=$cate_title?> 정보</span>
              </h5>
              <div class="cont_hfix">
                <form method="post" name="listtableform" action="###" onsubmit="return false;">
                  <table class="table table-bordered mt-3">
                    <colgroup>
                      <col width="5%">
                      <col width="10%">
                      <col width="*">
                      <col width="10%">
                      <col width="10%">
                      <col width="10%">
                      <col width="10%">
                      <col width="10%">
                      <?php if( $cate_area == "service" ){?>
                      <col width="10%">
                      <?php }?>
                    </colgroup>
                    <thead class="sticky-top">
                      <tr class="text-center bg-dark text-light">
                        <th><label><input type="checkbox" class="allsel" data-tgt="delchk"> No</label></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'member_id'    );" class="text-decoration-none text-white">회원 ID</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'member_name'  );" class="text-decoration-none text-white">회원명</a>
                        <?php if( $cate_area == "service" ){?>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'cust_name'    );" class="text-decoration-none text-white">소속</a></th>
                        <?php }?>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'member_mail'  );" class="text-decoration-none text-white">메일</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'member_mobile');" class="text-decoration-none text-white">연락처</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'member_auth'  );" class="text-decoration-none text-white">그룹</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'member_status');" class="text-decoration-none text-white">Active</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'reg_date'     );" class="text-decoration-none text-white">등록일</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'mod_date'     );" class="text-decoration-none text-white">수정일</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'login_date'   );" class="text-decoration-none text-white">최종로그인</a></th>
                      </tr>
                    </thead>
                    <tbody>
                    <?php
                    $_REQUEST['no_echo'] = true;
                    $_REQUEST['mode'] = "getMemberList";
                    include "./member.ajax.php";
                    $mem_list = $retval['data'];

                    foreach( $mem_list->rows as $row ){
                      switch( $row['member_status'] ){ //기준정보로 대체
                        case "2" :
                          $row['member_status_txt'] = "<span class='text-success'>승인대기</span>"; break;
                        case "1" :
                          $row['member_status_txt'] = "활동중"; break;
                        default :
                          $row['member_status_txt'] = "<span class='text-danger'>정지됨</span>"; break;
                      }
                    ?>
                    <tr class="text-center">
                      <td><label><input type="checkbox" name="delchk[]" value="<?=$row['member_id']?>"> <?=$mem_list->line_cnt--?></label></td>
                      <td><a href="./member_log_list.php?stxt=<?=$row['member_id']?>"><?=$row['member_id']?></a></td>
                      <td><a href="./member_put.php?member_id=<?=$row['member_id']?>"><?=$row['member_name']?></a></td>
                      <?php if( $cate_area == "service" ){?>
                      <td><?=$row['cust_name']?></td>
                      <?php }?>
                      <td><?=$row['member_mail']?></td>
                      <td><?=$row['member_mobile']?></td>
                      <td><?=$authDest[$row['member_auth']]?></td>
                      <td><?=$row['member_status_txt']?></td>
                      <td><?=getDT_datetime($row['reg_date'])?></td>
                      <td><?=getDT_datetime($row['mod_date'])?></td>
                      <td><?=getDT_datetime($row['login_date'])?></td>
                    </tr>
                    <?php
                    }
                    if( count($mem_list->rows) < 1 ){
                    ?>
                    <tr>
                      <td colspan="20" class="text-center text-danger">※ <?=cUtil\Mlang::getText("결과가 존재하지 않습니다.");?></td>
                    </tr>
                    <?php
                    }
                    ?>
                    </tbody>
                  </table>
                </form>
                <nav aria-label="Page navigation example">
                  <ul class="pagination justify-content-center align-items-center">
                    <?php foreach( (array)$mem_list->pages as $page ){?>
                    <li><?=$page?></li>
                    <?php }?>
                  </ul>
                </nav>
              </div>
            </div>
          </div>
        </div>
<?php

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

<template id="member_status_modal" class="d-none">
  <form class="mform" action="###" onsubmit="return false;">
    <label class="form-inline"> <span>상태 선택 :</span>
      <select name="member_status" class="form-control">
        <?=getSelectOptions($GLOBALS['_varsMemStatus'],"- 선택 -")?>
      </select>
    </label>
  </form>
  <div><small class="text-info">※ 선택된 회원의 상태를 변경합니다.</small></div>
</template>

<script>
const member_status_modal = function(){
  kuls_confirm(
    "회원 활동상태 변경",
    jQuery("#member_status_modal").html(),
    function(){
      let formdata = new FormData(document.forms['listtableform']);
      let member_status = jQuery(this.el).find("select[name=member_status]").val();
      if( !member_status ){
        kuls_alert("<?=cUtil\Mlang::getText("상태를 선택하지 않았습니다.")?>");
        return;
      }

      formdata.append("member_status",member_status);
      formdata.append("mode","setMemberState");
      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.reload()});
        },
        error: function (request, status, error) {
          kuls_alert(error.message);
        }
      });
    },
    function(){},
    {icons:{enabled:false},dontclose:true}
  );
};
</script>
