<?php

$page_title="Manage Categories"; 

include("includes/header.php");

require("includes/function.php");
require("language/language.php");

$tableName="tbl_category";   
$targetpage = "manage_category.php"; 
$limit = 12; 

$keyword='';

if(!isset($_GET['keyword'])){
  $query = "SELECT COUNT(*) as num FROM $tableName";
}
else{

  $keyword=addslashes(trim($_GET['keyword']));

  $query = "SELECT COUNT(*) as num FROM $tableName WHERE `category_name` LIKE '%$keyword%'";

  $targetpage = "manage_category.php?keyword=".$_GET['keyword'];

}

$total_pages = mysqli_fetch_array(mysqli_query($mysqli,$query));
$total_pages = $total_pages['num'];

$stages = 3;
$page=0;
if(isset($_GET['page'])){
  $page = mysqli_real_escape_string($mysqli,$_GET['page']);
}
if($page){
  $start = ($page - 1) * $limit; 
}else{
  $start = 0; 
} 

if(!isset($_GET['keyword'])){
  $sql_query="SELECT * FROM tbl_category ORDER BY tbl_category.`cid` DESC LIMIT $start, $limit"; 
}
else{

  $sql_query="SELECT * FROM tbl_category WHERE `category_name` LIKE '%$keyword%' ORDER BY tbl_category.`cid` DESC LIMIT $start, $limit"; 
}

$result=mysqli_query($mysqli,$sql_query) or die(mysqli_error($mysqli));

function get_total_songs($cat_id)
{ 

  global $mysqli;   
  $qry_songs="SELECT COUNT(*) as num FROM tbl_mp3 WHERE cat_id='".$cat_id."'";
  $total_songs = mysqli_fetch_array(mysqli_query($mysqli,$qry_songs));
  $total_songs = $total_songs['num'];
  return $total_songs;

}

?>

<div class="row">
  <div class="col-xs-12">
    <?php
        if(isset($_SERVER['HTTP_REFERER']))
        {
          echo '<a href="'.$_SERVER['HTTP_REFERER'].'"><h4 class="pull-left" style="font-size: 20px;color: #e91e63"><i class="fa fa-arrow-left"></i> Back</h4></a>';
       }
      ?>
    <div class="card mrg_bottom">
      <div class="page_title_block">
        <div class="col-md-5 col-xs-12">
          <div class="page_title"><?=$page_title?></div>
        </div>
        <div class="col-md-7 col-xs-12">
          <div class="search_list">
            <div class="search_block">
              <form method="get" action="">
                <input class="form-control input-sm" placeholder="Search here..." aria-controls="DataTables_Table_0" type="search" name="keyword" value="<?php if(isset($_GET['keyword'])){ echo $_GET['keyword'];} ?>" required="required">
                <button type="submit" class="btn-search"><i class="fa fa-search"></i></button>
              </form>  
            </div>
            <div class="add_btn_primary"> <a href="add_category.php?add=yes&redirect=<?=$redirectUrl?>">Add Category</a> </div>
          </div>
        </div>
        <div class="col-md-3 col-xs-12 text-right" style="float: right;">
          <div class="checkbox" style="width: 95px;margin-top: 5px;margin-left: 10px;right: 100px;position: absolute;">
            <input type="checkbox" id="checkall_input">
            <label for="checkall_input">
              Select All
            </label>
          </div>
          <div class="dropdown" style="float:right">
            <button class="btn btn-primary dropdown-toggle btn_cust" type="button" data-toggle="dropdown">Action
              <span class="caret"></span>
            </button>
            <ul class="dropdown-menu" style="right:0;left:auto;">
              <li><a href="javascript:void(0)" class="actions" data-action="enable" data-table="tbl_category" data-column="status">Enable</a></li>
              <li><a href="javascript:void(0)" class="actions" data-action="disable" data-table="tbl_category" data-column="status">Disable</a></li>
              <li><a href="javascript:void(0)" class="actions" data-action="delete" data-table="tbl_category" data-column="status">Delete !</a></li>
            </ul>
          </div>
        </div>
      </div>
      <div class="clearfix"></div>
      <div class="col-md-12 mrg-top">
        <div class="row">
          <?php 
          $i=0;
          while($row=mysqli_fetch_array($result))
          {         
            ?>
            <div class="col-lg-3 col-sm-6 col-xs-12">
              <div class="block_wallpaper">
                <div class="wall_category_block">
                  <h2>
                    <?php 
                    if(strlen($row['category_name']) > 17){
                      echo substr(stripslashes($row['category_name']), 0, 17).'...';  
                    }else{
                      echo $row['category_name'];
                    }
                  ?><span>(<?php echo get_total_songs($row['cid']);?>)</span>
                </h2>
                <div class="checkbox" style="float: right;z-index: 1">
                  <input type="checkbox" name="post_ids[]" id="checkbox<?php echo $i;?>" value="<?php echo $row['cid']; ?>" class="post_ids">
                  <label for="checkbox<?php echo $i;?>">
                  </label>
                </div>
              </div>           
              <div class="wall_image_title">
                <ul>                
                  <li><a href="add_category.php?cat_id=<?php echo $row['cid'];?>&redirect=<?=$redirectUrl?>" data-toggle="tooltip" data-tooltip="Edit"><i class="fa fa-edit"></i></a></li>               
                  <li>
                    <a href="javascript:void(0)" class="btn_delete_a" data-table="tbl_category" data-id="<?php echo $row['cid'];?>"  data-toggle="tooltip" data-tooltip="Delete"><i class="fa fa-trash"></i></a>
                  </li>

                  <li>
                    <div class="row toggle_btn">
                      <input type="checkbox" id="enable_disable_check_<?=$i?>" data-id="<?=$row['cid']?>" data-table="tbl_category" data-column="status" class="cbx hidden enable_disable" <?php if($row['status']==1){ echo 'checked';} ?>>
                      <label for="enable_disable_check_<?=$i?>" class="lbl"></label>
                    </div>
                  </li>

                </ul>
              </div>
              <span><img src="images/<?php echo $row['category_image'];?>" style="height: 100%"/></span>
            </div>
          </div>
          <?php

          $i++;
        }
        ?>     

      </div>
    </div>
    <div class="col-md-12 col-xs-12">
      <div class="pagination_item_block">
        <nav>
          <?php include("pagination.php"); ?>
        </nav>
      </div>
    </div>
    <div class="clearfix"></div>
  </div>
</div>
</div>

<?php include("includes/footer.php");?>