<?php
$cate_id = "mnu_cust"; //현재 메뉴 ID 필수!!!
include_once "{$_SERVER['DOCUMENT_ROOT']}/common.inc.php";
include_once _ADM_INC_PATH_ ."/auth_check.php";

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

        <form method="get" name="sform" id="sform" class="sform form-inline" action="<?=$_SERVER['PHP_SELF']?>">
          <label> <span>상태 :</span>
            <select name="cust_status" id="cust_status" class="form-control form-control-sm" data-defval="<?=$_REQUEST['cust_status']?>">
              <?=getSelectOptions($GLOBALS['_varsCommonStatus'],"- 전체 -")?>
            </select>
          </label>
          <label>검색 내용 : <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="10">10</option>
              <option value="20">20</option>
              <option value="50">50</option>
              <option value="100">100</option>
              <option value="500">500</option>
            </select>
          </label>
        </form>

        <div id="cmd_bar" class="mb-2">
          <div class="h4"><i class="bi bi-files-alt"></i> 소속 리스트</div>
          <div class="text-right">
            <a href="javascript:cust_status_modal();" class="btn btn-secondary"> <i class="bi bi-caret-up-square"></i> 상태변경</a>
            <a href="./cust_put.php" class="btn btn-primary"> <i class="bi bi-building"></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> 소속 정보</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%">
                      <col width="10%">
                    </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),'cust_id'     );" class="text-decoration-none text-white">소속 ID</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'cust_name'   );" class="text-decoration-none text-white">소속명</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'cust_charger');" class="text-decoration-none text-white">담당자</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'cust_phone'  );" class="text-decoration-none text-white">연락처</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'cust_mail'   );" class="text-decoration-none text-white">메일</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'cust_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>
                      </tr>
                    </thead>
                    <tbody>
                    <?php
                    $_REQUEST['no_echo'] = true;
                    $_REQUEST['mode'] = "getCustList";
                    include "./cust.ajax.php";
                    $cust_list = $retval['data'];

                    foreach( $cust_list->rows as $row ){
                      switch( $row['cust_status'] ){ //기준정보로 대체
                        case "1" :
                          $row['cust_status_txt'] = "사용중"; break;
                        default :
                          $row['cust_status_txt'] = "<span class='text-danger'>정지</span>"; break;
                      }
                    ?>
                    <tr class="text-center">
                      <td><label><input type="checkbox" name="delchk[]" value="<?=$row['cust_id']?>"> <?=$cust_list->line_cnt--?></label></td>
                      <td><a href="./cust_put.php?cust_id=<?=$row['cust_id']?>"><?=$row['cust_id']?></a></td>
                      <td><a href="./cust_put.php?cust_id=<?=$row['cust_id']?>"><?=$row['cust_name']?></a></td>
                      <td><?=$row['cust_charger']?></td>
                      <td><?=$row['cust_phone']?></td>
                      <td><?=$row['cust_mail']?></td>
                      <td><?=$row['cust_status_txt']?></td>
                      <td><?=getDT_datetime($row['reg_date'])?></td>
                      <td><?=getDT_datetime($row['mod_date'])?></td>
                    </tr>
                    <?php }?>
                    </tbody>
                  </table>
                </form>
                <nav aria-label="Page navigation example">
                  <ul class="pagination justify-content-center align-items-center">
                    <?php foreach( (array)$cust_list->pages as $page ){?>
                    <li><?=$page?></li>
                    <?php }?>
                  </ul>
                </nav>
              </div>
            </div>
          </div>
        </div>
<?php

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

<template id="cust_status_modal" class="d-none">
  <form class="mform" action="###" onsubmit="return false;">
    <label class="form-inline text-center"> <span>상태 선택 :</span>
      <select name="cust_status" data-defval="<?=$_REQUEST['cust_status']?>" class="form-control">
        <?=getSelectOptions($GLOBALS['_varsCommonStatus'],"- 선택 -")?>
      </select>
    </label>
    <label class="text-danger mt-3 text-center">
      <input type="checkbox" name="cust_delete"> 삭제처리
    </label>
  </form>
  <div><small class="text-info">※ 선택된 소속의 상태를 변경합니다.</small></div>
</template>

<script>
const cust_status_modal = function(){
  kuls_confirm(
    "소속 상태 변경",
    jQuery("#cust_status_modal").html(),
    function(){
      let formdata = new FormData(document.forms['listtableform']);
      let cust_status = jQuery(this.el).find("select[name=cust_status]").val();
      let cust_delete = jQuery(this.el).find("input[name=cust_delete]").is(":checked");
      if( !cust_status && !cust_delete ){
        kuls_alert("<?=cUtil\Mlang::getText("상태를 선택하지 않았습니다.")?>");
        return;
      }

      formdata.append("cust_status",cust_status);
      formdata.append("cust_delete",cust_delete);
      formdata.append("mode","setCustState");
      ajax_form({
        url: "./cust.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>
