<?php 
include("header.php");
include("sidebar.php");

$msg="";$name="";$status="";$caption="Add"; $botton="add";$mcid="";
//echo'<pre>';print_r($_REQUEST);exit;
if(isset($_GET['id']))
{
	$sqlcheck1=mysql_query("select * from tbl_category where cid='".$_GET['id']."'");
	$rescheck1=mysql_fetch_assoc($sqlcheck1);
	
	$mcid=$rescheck1['mcid'];
	$name=$rescheck1['name'];
	$status=$rescheck1['status'];
	
	$caption="Update"; $botton="update";
	
	if(isset($_POST['update']))
	{
		$maincategory=$_POST['maincategory'];
		$category=mysql_real_escape_string(ucwords($_POST['category']));
		
		$update=mysql_query("update tbl_category set status='".$_POST['status']."',mcid='$maincategory',name='$category' where cid='".$_GET['id']."'");
		
		
		$msg="Category update successfully";
		
		?>
		<script>
        setTimeout(function(){
			window.location="category.php";
			},2000);
        </script>
		<?php
	}
	
}


if(isset($_POST['add']))
{
	$maincategory=mysql_real_escape_string(ucwords($_POST['maincategory']));
	$category=mysql_real_escape_string(ucwords($_POST['category']));
	
	$sqlcheck=mysql_query("select * from tbl_category where name='$category' and mcid='$maincategory'");
	if(mysql_num_rows($sqlcheck)==0)
	{
		$insert=mysql_query("insert into tbl_category(mcid,name,status) values('$maincategory','$category','Enable')");
		
		$msg="Category add successfully";
		?>
		<script>
        setTimeout(function(){
			window.location.href=window.location.href;
			},2000);
        </script>
		<?php
	}
}

?>


  <!-- Page -->
  <div class="page animsition">
    <div class="page-header">
      <h1 class="page-title">Category</h1>
      <ol class="breadcrumb">
        <li><a href="dashboard.php">Home</a></li>
        <li><a href="category.php">Category</a></li>
      </ol>
     <!-- <div class="page-header-actions">
        
        <button type="button" class="btn btn-sm btn-icon btn-default btn-outline btn-round"
        data-toggle="tooltip" data-original-title="Refresh">
          <i class="icon wb-refresh" aria-hidden="true"></i>
        </button>
        <button type="button" class="btn btn-sm btn-icon btn-default btn-outline btn-round"
        data-toggle="tooltip" data-original-title="Setting">
          <i class="icon wb-settings" aria-hidden="true"></i>
        </button>
      </div>-->
    </div>
    <?php if($msg==!"")
	{
		
		?>
			<div class="alert alert-info alert-dismissible" role="alert">
                  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                  </button>
                  <?php echo $msg;?>
                </div>
		<?php
	}
    ?>
    <div class="page-content container-fluid">
      <div class="row">
        <div class="col-sm-12">
          <!-- Panel Static Lables -->
          <div class="panel">
            <div class="panel-heading">
              <h3 class="panel-title">Add Category</h3>
            </div>
            <div class="panel-body container-fluid">
              <form method="post">
                
                <div class="form-group form-material">
                      <label class="control-label" for="inputText">Main Category</label>
                      	<select name="maincategory" class="form-control">
                        	<option value="">Please Select</option>
                          <?php
							$sqlmainc=mysql_query("select * from tbl_maincategory where status='Enable' order by name asc");
							while($resmainc=mysql_fetch_assoc($sqlmainc))
							{?>
                            
                            <option <?php if($mcid==$resmainc['mcid']) { echo "selected";}?> value="<?php echo $resmainc['mcid'];?>"><?php echo $resmainc['name'];?></option>
                      	    <?php } ?>
                      	</select>
                    </div>
                
                <div class="form-group form-material">
                  <label class="control-label" for="inputText">Category Name</label>
                  <input type="text" class="form-control" id="inputText" name="category" value="<?php echo $name;?>" placeholder="Enter Category Name"
                  />
                </div>
                <?php if($status==!"")
				{
					?>
					<div class="form-group form-material">
                      <label class="control-label" for="inputText">Status</label>
                      	<select name="status" class="form-control">
                        	<option value="">Please Select</option>
                            <option <?php if($status=="Enable") { echo "selected";}?> value="Enable">Enable</option>
                            <option <?php if($status=="Disable") { echo "selected";}?> value="Disable">Disable</option>
                      	
                      	</select>
                    </div>
					<?php
				}
			    ?>
               
                
                <div class="form-group form-material row">
                  <div class="col-xs-12 col-sm-4 col-lg-2">
                  <button type="submit" name="<?php echo $botton?>" class="btn btn-block btn-primary"><?php echo $caption?></button>
                  </div>
                </div>
              </form>
            </div>
          </div>
          <!-- End Panel Static Lables -->
        </div>

        
      </div>
    </div>
    
    <div class="page-content">
      <!-- Panel Basic -->
      <div class="panel">
        <header class="panel-heading">
          <div class="panel-actions"></div>
          <h3 class="panel-title">Category Details</h3>
        </header>
        <div class="panel-body">
          <table class="table table-hover dataTable table-striped width-full" data-plugin="dataTable">
            <thead>
              <tr>
                <th>Sr</th>
                <th>Main Category</th>
                <th>Category Name</th>
                <th>Status</th>
                <th>Action</th>
               
              </tr>
            </thead>
           
            <tbody>
              
              <?php
			  $i=1;
			  
			  $sqlmaincat=mysql_query("select * from tbl_category");
			  if(mysql_num_rows($sqlmaincat))
			  {
			  while($resmaincat=mysql_fetch_assoc($sqlmaincat))
			  {?>
              <tr>
                <td><?php echo $i;?></td>
                <td><?php 
				$sqlmain=mysql_query("select * from tbl_maincategory where mcid='".$resmaincat['mcid']."'");
				$resmain=mysql_fetch_assoc($sqlmain);
				echo $resmain['name']?></td>
                <td><?php echo $resmaincat['name']?></td>
                <td><?php echo $resmaincat['status']?></td>
                <td><a href="category.php?id=<?php echo $resmaincat['cid']?>" type="button" class="btn btn-sm btn-icon btn-default btn-outline btn-round" data-toggle="tooltip"  data-original-title="Edit"><i class="icon wb-pencil" aria-hidden="true"></i></a>
               <a id="<?php echo $resmaincat['cid']?>" type="button" class="btn btn-sm btn-icon btn-default btn-outline btn-round delete" data-toggle="tooltip"  data-original-title="Delete"><i class="icon wb-trash" aria-hidden="true"></i></a></td>
              </tr>
              <?php 
			  $i++;
			  } }?>
            
             
            </tbody>
          </table>
        </div>
      </div>
    
    </div>
    
    
  </div>
  
 
  <!-- End Page -->


  <!-- Footer -->
<?php 
include("footer.php");
?>

<script>
$(".delete").click(function(){
var id=$(this).attr('id');

$(this).closest('tr').fadeOut();

$.post("ajax/ajax_delete_cat.php",{id:id},function(data){
	//alert(data);
	
	});
});
</script>