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

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


if(isset($_POST['add']))
{
	$maincategory=mysql_real_escape_string(ucwords($_POST['maincategory']));
	
	
	if(isset($_FILES['maincategory_file']))
	{
		move_uploaded_file($_FILES['maincategory_file']['tmp_name'],"maincatimage/".$_FILES['maincategory_file']['name']);
		
		$maincategory_files=$_FILES['maincategory_file']['name'];
	}
	
	$sqlcheck=mysql_query("select * from tbl_maincategory where name='$maincategory'");
	if(mysql_num_rows($sqlcheck)==0)
	{
		
		if($maincategory_files=="")
		{
			$insert=mysql_query("insert into tbl_maincategory(name,status) values('$maincategory','Enable')");
		}
		else
		{
			$insert=mysql_query("insert into tbl_maincategory(name,maincategory_file,status) values('$maincategory','$maincategory_files','Enable')");
		}
		
		$msg="Main 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="maincategory.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 Main Category</h3>
            </div>
            <div class="panel-body container-fluid">
              <form method="post" enctype="multipart/form-data">
                <div class="form-group form-material">
                  <label class="control-label" for="inputText">Main Category Name</label>
                  <input type="text" class="form-control" id="inputText" name="maincategory" value="<?php echo $name;?>" placeholder="Enter Main Category Name"
                  />
                </div>
                
                <div class="form-group form-material">
                  <label class="control-label" for="inputText">Main Category File</label>
                  <?php if($maincategory_file!=="")
				  {
					  ?><img src="maincatimage/<?php echo $maincategory_file;?>" width="50" height="50"><?php
				  }?>
                  <input type="text" class="form-control" placeholder="Browse.." readonly="" />
                  <input type="file" name="maincategory_file" id="inputFile" <?php if($maincategory_file=="") { ?> required <?php } ?>/>
                </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">Main Category Details</h3>
        </header>
        <div class="panel-body">
          <table class="table table-hover dataTable table-striped width-full" data-plugin="dataTable">
            <thead>
              <tr>
                <th>Name</th>
                <th>Status</th>
                <th>Action</th>
               
              </tr>
            </thead>
           
            <tbody>
              
              <?php
			  
			  $sqlmaincat=mysql_query("select * from tbl_maincategory");
			  if(mysql_num_rows($sqlmaincat))
			  {
			  while($resmaincat=mysql_fetch_assoc($sqlmaincat))
			  {?>
              <tr>
                <td><?php echo $resmaincat['name']?></td>
                <td><?php echo $resmaincat['status']?></td>
                <td><a href="maincategory.php?id=<?php echo $resmaincat['mcid']?>" 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['mcid']?>" 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 } }?>
            
             
            </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_maincat.php",{id:id},function(data){
	//alert(data);
	
	});
});
</script>