<?php if(isset($_SESSION['msg']) ){?>
	<div class="alert alert-info">                
	<h4>Info!</h4>
	<?php echo $_SESSION['msg']; unset($_SESSION['msg']); ?>
	</div> <?php }  ?> 
<?php
 if(isset($_REQUEST['submit']))
 {
	$name	                = ucwords(strtolower(mysql_real_escape_string($_POST['name'])));
	$p_code			        = mysql_real_escape_string($_POST['p_code']);
	
	mysql_query("insert into `tbl_product` set `name`='$name',`p_code`='$p_code' ")or die(mysql_error());
	 $_SESSION['msg'] = 'Product Added Sucessfully!!';
	 header("location:home.php?page=product_master");	 
 }
 if(isset($_REQUEST['update']))
 {
	$id		                = $_POST['id'];
	$name	                = ucwords(strtolower(mysql_real_escape_string($_POST['name'])));
	$p_code			        = mysql_real_escape_string($_POST['p_code']);
	
	mysql_query("update `tbl_product` set `name`='$name',`p_code`='$p_code' where `id`='$id' ")or die(mysql_error());
	 $_SESSION['msg'] = 'Client Added Sucessfully!!';
	 header("location:home.php?page=product_master");	 
 }
 ?>           
<div class="row-fluid" >
    <div class="span12">
        <div class="head clearfix">
            <div class="isw-ok"></div>
            <h1><?php if(isset($_REQUEST['page_id'])) { echo "Modify";}else {echo "Add";}?> Product</h1>
            
        </div>
        <div class="block-fluid">
            <form class="form-horizontal" onsubmit="return get_validation();" action="" method="post" enctype="multipart/form-data">
              <fieldset>
              <?php
			   if(isset($_REQUEST['page_id']))
			   {
				   $id            =       $_REQUEST['page_id'];
				   $modify_menu   =       mysql_query("SELECT * FROM `tbl_product` WHERE `id` = '$id' ");
				   $show_modify   =       mysql_fetch_array($modify_menu);
			   }
			  ?>
              <input type="hidden" name="id" placeholder="Page Name" <?php if(isset($_REQUEST['page_id'])){?> required value="<?php echo $show_modify['id'];  ?>"<?php } ?> id="txt_name" autocomplete="off" />
                
                     <div class="row-form clearfix">
                        <div class="span3">
                        	<label>Product Name</label>
                        	<input type="text" name="name" placeholder="Product Name" <?php if(isset($_REQUEST['page_id'])){?>value="<?php echo $show_modify['name'];  ?>"<?php } ?> id="name" onkeyup="capital_letter('name');" required/>
                        </div>
                        <div class="span3">
                        	<label>Product Code</label>
                        	<input type="text" name="p_code" placeholder="Product Code" <?php if(isset($_REQUEST['page_id'])){?>value="<?php echo $show_modify['p_code'];  ?>"<?php } ?> id="p_code" />
                        </div>
                    <div class="span2">
                    		<label>&nbsp;</label>
                            <input type="submit" name="<?php if(isset($_REQUEST['page_id'])){echo "update"; }else{ echo "submit";}?>" class="btn btn-block" value="<?php if(isset($_REQUEST['page_id'])){ echo "Update"; }else{ echo "Save";}?>"/></div>
                </div>
              </fieldset>
            </form>  
        </div> 
    </div>
</div> 
<script>
function capital_letter(id)
{
    var arr 		  = $('#'+id).val().split(' ');
    var result 		  = "";
    for (var x=0; x<arr.length; x++)
        result+=arr[x].substring(0,1).toUpperCase()+arr[x].substring(1)+' ';
    $('#'+id).val(result.substring(0, result.length-1));
}
</script>   
<?php

	$menu		= mysql_query("SELECT * FROM `tbl_product` WHERE 1 order by `id` ASC")or die(mysql_error());
	$count		= mysql_num_rows($menu);
?>     
<div class="row-fluid" >
<div class="span12">    
<div class="head clearfix">
<div class="isw-ok"></div>
<h1>View Product List</h1>
</div>   
<div class="block-fluid">
<table cellpadding="0" cellspacing="0" width="100%" class="table" id="example">
    <thead>
        <tr>
            <th>S.No.</th>
            <th>Name</th>
            <th>Product Code</th>
            <th>Modify</th>
        </tr>
    </thead>                 
    <tbody>
        <?php
        $i=1;
        while($show_menu = mysql_fetch_array($menu)) 
        {
        ?>	
            <tr>
            <td><?php echo $i++; ?></td>
            <td><?php echo $show_menu['name']; ?></td>
            <td><?php echo $show_menu['p_code']; ?></td>
            <td class="center" style="text-align:center">
                <a href="home.php?page=product_master&page_id=<?php echo $show_menu['id'];?>"><img src="images/edit_new.png"  border="0" style="width: 20px;"/></a>
            </td>     
             
        </tr>
       <?php } ?>                
    </tbody>            
</table> 
</div>    
</div>
</div>            
<script>
$(document).ready(function() {
var table = $('#example').DataTable( {
"order": [[ 2, "asc" ]],
"pageLength": 50,stateSave: true,
fixedHeader: {
header: true,
footer: true
}
} );
table.on( 'order.dt search.dt', function () {
table.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
cell.innerHTML = i+1;
} );
} ).draw(); } );
</script>