<?php 
// Delete Item....
if(isset($_REQUEST['del-ctgi'])){
  $DeCtgi = $_REQUEST['del-ctgi'];
  $check = $conn->prepare("SELECT * FROM `tbl_items` WHERE `icateg_id`='$DeCtgi' ");
  $check->execute();
  if($check->rowCount()==0){
  $d=$conn->prepare("DELETE from `tbl_ictgs` WHERE `ictg_id`='".$_REQUEST['del-ctgi']."' ");
  $d->execute();
    $msge="Data Removed";
  }else{
    $msge="Do not delete this data";  
  }
  
}

 if(isset($_REQUEST['btnCtg'])){
		try{
			$ctgName=$_REQUEST['ctgName'];
			$elem=$_REQUEST['elem'];
      $ctgDesc=$_REQUEST['ctgDesc'];
			$sql = $db->query("INSERT INTO `tbl_ictgs`(`ctgi_name`,`ctgdesc`,`elemi`,`ictg_sts`)
			VALUES ('$ctgName','$ctgDesc','$elem',1)");
			$msg="Successfully Added Category!";
			
		}catch(PDOException $e){
			$msg = $e->getMessage();
		}
		
	}

  ##### UPDATE #####endregion

  if(isset($_REQUEST['updateCtg'])){
		try{
			$ctgname_upd=$_REQUEST['ctgname_upd'];
			$ictg=$_REQUEST['ictg'];
      $ctgdesc_upd=$_REQUEST['ctgdesc_upd'];
      $elem=$_REQUEST['elem'];
			$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			
			$sql=$db->query("UPDATE `tbl_ictgs` SET `ctgi_name` = '$ctgname_upd',`ctgdesc` = '$ctgdesc_upd',`elemi` = '$elem' WHERE `ictg_id` = '$ictg'");
			$msg="Updated Successfully";
			
		}catch(PDOException $e){
			echo $e->getMessage();
		}  
	}
?>

<div class="subheader">
    <h1 class="subheader-title" style="font-weight: bold;">
        <i class='subheader-icon fa fa-folder-open text-primary'></i><?php echo $title;?> 
    </h1>
    <div class="d-flex mr-4">
    <li class="position-absolute pos-top pos-right d-none d-sm-block"><span class="js-get-date"></span></li>
    </div>
</div>

<?php if(isset($msg)){?>
    <div class="alert alert-success alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>Well Done!</strong> <?php echo htmlentities($msg); ?>
      </div>
    <?php 
    }elseif(isset($msge)){
    ?>
     
    <div class="alert alert-danger alert-dismissable">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
      <strong>Oooh Snap!</strong> <?php echo htmlentities($msge); ?>
    </div>
 <?php 
 }
?>

<div class="row">
    <div class="col-md-4">
        <?php 
        if(isset($_REQUEST['c'])){
        include 'ictg_upd.php';
        }else{
        include 'ictgfm.php';
        }
        ?> 
    </div>
  
    <div class="col-md-8">
      <?php include 'listCtgi.php';?>   
   </div>
</div>