<?php 
    
    $page_title="Manage Sub Categories";

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

    $tableName="tbl_sub_category";   
    $targetpage = "manage_sub_category.php"; 
    $limit = 12; 

    $keyword='';

    if(!isset($_GET['keyword'])){
      $query = "SELECT COUNT(*) as num FROM tbl_sub_category
                LEFT JOIN tbl_category ON tbl_sub_category.`cat_id`=tbl_category.`cid`
                ORDER BY tbl_sub_category.`sid` DESC";
    }
    else{

      $keyword=real_escape_string($_GET['keyword']);

      $query = "SELECT COUNT(*) as num FROM tbl_sub_category
                LEFT JOIN tbl_category ON tbl_sub_category.`cat_id`=tbl_category.`cid`
                WHERE (tbl_category.`category_name` LIKE '%$keyword%' OR tbl_sub_category.`sub_cat_name` LIKE '%$keyword%')
                ORDER BY tbl_sub_category.`sid` DESC";

      $targetpage = "manage_sub_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_sub_category
                  LEFT JOIN tbl_category ON tbl_sub_category.`cat_id`=tbl_category.`cid`
                  ORDER BY tbl_sub_category.`sid` DESC LIMIT $start, $limit"; 
    }
    else{

      $sql_query="SELECT * FROM tbl_sub_category
                  LEFT JOIN tbl_category ON tbl_sub_category.`cat_id`=tbl_category.`cid`
                  WHERE (tbl_category.`category_name` LIKE '%$keyword%' OR tbl_sub_category.`sub_cat_name` LIKE '%$keyword%')
                  ORDER BY tbl_sub_category.`sid` DESC LIMIT $start, $limit"; 
    }

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

<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" id="searchForm" 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_sub_category.php?add=yes&redirect=<?=$redirectUrl?>">Add Sub Category</a> </div>
          </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 add_wall_category">           
                <div class="wall_category_block">
                  <h2><?php echo $row['category_name'];?></h2>              
                </div>
                <div class="wall_image_title">
                  <h2><a href="javascript:void(0)"><?php echo $row['sub_cat_name'];?> </a></h2>
                  <ul>                
                    <li><a href="add_sub_category.php?sub_cat_id=<?php echo $row['sid'];?>&redirect=<?=$redirectUrl?>" data-toggle="tooltip" data-tooltip="Edit"><i class="fa fa-edit"></i></a></li>

                     <li><a href="javascript:void(0)" data-id="<?php echo $row['sid'];?>" class="btn_delete_a" data-toggle="tooltip" data-tooltip="Delete"><i class="fa fa-trash"></i></a></li>
                  </ul>
                </div>
                <span><img src="images/<?php echo $row['sub_cat_image'];?>"/></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");?>       

<script type="text/javascript">

$(".btn_delete_a").click(function(e){
      e.preventDefault();

      var _id = $(this).data("id");
      var _table = 'tbl_sub_category';
    
        confirmDlg = duDialog('Are you sure?', 'All data will be removed which belong to this!', {
        init: true,
        dark: false, 
        buttons: duDialog.OK_CANCEL,
        okText: 'Proceed',
        callbacks: {
          okClick: function(e) {
            $(".dlg-actions").find("button").attr("disabled",true);
            $(".ok-action").html('<i class="fa fa-spinner fa-pulse"></i> Please wait..');
            $.ajax({
              type:'post',
              url:'processData.php',
              dataType:'json',
              data: {id: _id, for_action: 'delete', table: _table, 'action': 'multi_action'},
              success:function(res){
                location.reload();
              }
            });

          } 
        }
      });
      confirmDlg.show();
    });

</script>
