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

//전체게시판
$brdObj = new Board("");
$where = new DB_where("and");
$blist = $brdObj->getBoardList("board_id, board_name", $where, "board_name asc");
$board_list = array();
foreach( (array)$blist as $row ){
  $board_list[$row['board_id']] = $row['board_name'];
}

//검색필드
$sfields = array(
  "board_name" => "게시판명",
  "brd_id" => "게시판코드",
  "title" => "제목",
  "uname" => "작성자",
  "contents" => "내용",
);

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="board_id" id="board_id" class="form-select-sm form-control-sm" data-defval="<?=$_REQUEST['board_id']?>">
              <?=getSelectOptions($board_list,"- 전체게시판 -")?>
            </select>
          </label>
          <label> <span>검색필드 :</span>
            <select name="sfld" id="member_auth" class="form-select-sm form-control-sm" data-defval="<?=$_REQUEST['sfld']?>">
              <?=getSelectOptions($sfields,"- 전체 -")?>
            </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>
        </form>

        <div id="cmd_bar" class="mb-2">
          <div class="h4"><i class="bi bi-files-alt"></i> 게시물 리스트</div>
          <div class="text-right">
            <button class="btn btn-danger btnDelCont" data-defval="multiple"> <i class="bi bi-trash-fill"></i> 게시물 삭제</button>
          </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 name="frm_content_list" method="post" action="###">

                  <table class="table table-bordered mt-3">
                    <colgroup>
                      <col width="5%">
                      <col width="*">
                      <col width="*">
                      <col width="*">
                      <col width="*">
                      <col width="5%">
                      <col width="5%">
                      <col width="5%">
                    </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),'board_name');" class="text-decoration-none text-white">게시판 명</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'title'     );" class="text-decoration-none text-white">제목</a></th>
                        <th><a href="javascript:sort_page_move(form2qry(document.sform),'uname'     );" class="text-decoration-none text-white">작성자</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),'view'      );" class="text-decoration-none text-white">조회수</a></th>
                        <th>바로가기</th>
                        <th>삭제</th>
                      </tr>
                    </thead>
                    <tbody class="table-sm">
                      <?php
                      $_REQUEST['no_echo'] = true;
                      $_REQUEST['mode'] = "getContentsList";
                      include "./board.ajax.php";
                      $cont_list = $retval['data'];

                      foreach( $cont_list->rows as $cont ){
                      ?>
                      <tr class="cursor text-center">
                        <td><input type="checkbox" name="delchk[]" value="<?=$cont['board_id']?>|<?=$cont['cid']?>"> <?=$cont_list->line_cnt--?></td>
                        <td @click="contents.show = !contents.show"><?=$cont['board_name']?></td>
                        <td class="text-left" @click="contents.show = !contents.show"><?=$cont['title']?> <?=$cont['onum']>0 ? "({$cont['onum']})" : ""?></td>
                        <td @click="contents.show = !contents.show"><?=$cont['uname']?></td>
                        <td @click="contents.show = !contents.show"><?=getDT_datetime($cont['reg_date'])?></td>
                        <td @click="contents.show = !contents.show"><?=$cont['views']?></td>
                        <td><a href="//<?=_HOME_URL_?>/kbrd/kbrd_view.php?board_id=<?=$cont['board_id']?>&cid=<?=$cont['cid']?>" type="button" class="btn-sm btn-success" target="_blank">바로가기</a></td>
                        <td><button type="button" class="btn btn-sm btn-danger btnDelCont" data-defval="<?=$cont['board_id']?>|<?=$cont['cid']?>">삭제</button></td>
                      </tr>
                      <tr class="d-none">
                        <td colspan="1"></td>
                        <td colspan="5">
                          <div class="card">
                            <div class="card-body px-5 bg-secondary"><?=$cont['contents_txt']?></div>
                          </div>
                        </td>
                        <td colspan="2"></td>
                      </tr>
                      <?php
                      }
                      if( count($cont_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)$cont_list->pages as $page ){?>
                    <li><?=$page?></li>
                    <?php }?>
                  </ul>
                </nav>
              </div>
            </div>
          </div>
        </div>
  </div>

<script>
  /*
var vue_data = {
  contents_list: [],
  query: {
    page: "<?=$_REQUEST['page']?:"1"?>",
    brd_id: "<?=$_REQUEST['brd_id']?:""?>",
    bid: "<?=$_REQUEST['bid']?:""?>",
    gubun: "<?=$_REQUEST['gubun']?:""?>",
    viewnum: "<?=$_REQUEST['viewnum']?:"30"?>",
    sfld: "<?=$_REQUEST['sfld']?:""?>",
    stxt: "<?=$_REQUEST['stxt']?:""?>",
    ofld: "<?=$_REQUEST['ofld']?:""?>",
    oasc: "<?=$_REQUEST['oasc']?:""?>",
  }
}
*/
</script>
<script>
  /*
if( jQuery("#contents_list").length ){
  var V_contents_list = new Vue({
    data : vue_data,
    mounted: function(){
      var that = this;
      that.getContentsList();
    },
    computed:{},
    methods:{
      getContentsList: function( page, viewNum, search ){
        var that = this;
        var post_data = new FormData();
        post_data.append("mode","getContentsList");
        Object.keys(that.query).map( function(key){
          post_data.append(key,that.query[key]);
        });
        exec_ajax_common("./board.ajax.php",post_data,"json",function(resp){
          if( resp.code ){
            for( var a in resp.data.contents_list.rows ){
              resp.data.contents_list.rows[a].show = false;
              resp.data.contents_list.rows[a].hover = false;
            }
            that.contents_list = resp.data.contents_list;
          }else{
            palert(resp.mesg);
          }
        });
      },
      delContents: function(nid){
        if( confirm("정말 삭제하시겠습니까?") ){
          var that = this;
          var post_data = new FormData();
          post_data.append("mode","delContents");
          if(nid == 'all'){
            var del_chk = Array.from(document.querySelectorAll("[name^=del_chk]")).filter( function(e){ return e.checked } ).map( function(e){ return e.value } );
            for( var a=0 ; a<del_chk.length ; a++ ){
              post_data.append("del_chk[]", del_chk[a]);
            }
          }else{
            post_data.append("del_chk[]", nid);
          }
          exec_ajax_common("./board.ajax.php",post_data,"json",function(resp){
            if( resp.code ){
            //  location.reload();
            }else{
              palert(resp.mesg);
            }
          });
        }
      }
    },
  }).$mount("#contents_list");
}

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

<script>
const delContents = function(){
  let bcid = jQuery(this).data("defval");
  let formdata;

  if( bcid == "multiple" ){
    formdata = new FormData(document.forms['frm_content_list']);
  }else{
    formdata = new FormData();
    formdata.append("delchk[]",bcid);
  }
  kuls_confirm("게시물 삭제","정말 삭제하시겠습니까?",function(){
    formdata.append("mode","delContents");
    ajax_form({
      url: "./board.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 = "<?=_ADM_URL_."/board/contents_list.php"?>";
        });
      },
      error: function (request, status, error) {
        kuls_alert(error.message);
      }
    });
  },function(){return false;});
};

jQuery(".body").on("click",".btnDelCont",delContents);
</script>