<?php 		  					
if(isset($_GET['edit_id']))
{
	$id      = $_GET['edit_id'];
	$sql     = mysqli_query($cn, "select * from `tbl_product` where id='$id'");
	$sqlData = mysqli_fetch_array ($sql); 
}
?>    
<div class="col-md-12">
<?php if(isset($_SESSION['errorMsg'])){ echo $_SESSION['errorMsg']; unset($_SESSION['errorMsg']);}?>
    <div class="card">
        <div class="card-body">
            <h4 class="card-title"><?php if(isset($_GET['edit_id'])){ echo "Edit Product";}else{ echo "Add Product";}?></h4>        
            <form class="forms-sample" method="post" enctype="multipart/form-data">													                
                <div class="row">                
                	<div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">PRODUCT NAME <span style="color:#F00;">*</span></label>
                            <input type="hidden" name="edit_id" value="<?php echo $_GET['edit_id'];?>"/>
                            <input type="text" class="form-control" name="product_name" placeholder="PRODUCT NAME" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['product_name'];}?>" required>
                        </div>
                    </div> 
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">SKU <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="sku" placeholder="SKU" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['sku'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">CASE SIZE <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="case_size" placeholder="CASE SIZE" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['case_size'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">SMALL UNIT TYPE <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="unit1" placeholder="SMALL UNIT TYPE" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['unit1'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">LARGE UNIT TYPE <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="unit2" placeholder="LARGE UNIT TYPE" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['unit2'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">RETAIL PRICE <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="price" placeholder="RETAIL PRICE" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['price'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">RETAIL PER PCS/SET PRICE <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="set_price" placeholder="RETAIL PER PCS/SET PRICE" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['set_price'];}?>" required>
                        </div>
                    </div>                                        
                                        
                    <div class="col-md-1">
                        <div class="form-group">
                            <label for="exampleInputCity1">&nbsp;</label>
                            <button type="submit" class="btn btn-success mr-2 form-control" name="<?php if(isset($_GET['edit_id'])) {echo "edit";}else{ echo "add";}?>"><?php if(isset($_GET['edit_id'])) {echo "UPDATE";}else{ echo "ADD NEW";}?></button>
                        </div>
                    </div>                                                                                                                                                                                                                                                                                	                                      
                </div>                                                                                                                                                                                                                                                                       
            </form>
        </div>
    </div>
</div>

<?php 		  
if(isset($_POST['add']))
{ 
	$product_name = mysqli_escape_string($cn,trim($_POST['product_name'])); 
	$sku          = mysqli_escape_string($cn,trim($_POST['sku']));
	$case_size    = mysqli_escape_string($cn,trim($_POST['case_size']));
	$unit1        = mysqli_escape_string($cn,trim($_POST['unit1'])); 
	$unit2        = mysqli_escape_string($cn,trim($_POST['unit2'])); 
	$price        = mysqli_escape_string($cn,trim($_POST['price'])); 
	$set_price    = mysqli_escape_string($cn,trim($_POST['set_price']));	
	$sqlSubject   = mysqli_query($cn, "insert into `tbl_product` SET `product_name`='".$product_name."', `sku`='".$sku."', `case_size`='".$case_size."', `unit1`='".$unit1."', `unit2`='".$unit2."', `price`='".$price."', `set_price`='".$set_price."'");
	if($sqlSubject)
	{			
		$_SESSION['errorMsg'] = '<div class="alert alert-success"><strong>Success!</strong> Product Added Successfully.</div>';
	}
	else
	{
		$_SESSION['errorMsg'] = '<div class="alert alert-danger"><strong>Danger!</strong> Product not Added. Try Again.</div>';
	}	
	header("location:home.php?pages=addProduct");	
}
		
		  
if(isset($_POST['edit']))
{
	$edit_id      = $_POST['edit_id'];
	$product_name = mysqli_escape_string($cn,trim($_POST['product_name'])); 
	$sku          = mysqli_escape_string($cn,trim($_POST['sku']));
	$case_size    = mysqli_escape_string($cn,trim($_POST['case_size']));
	$unit1        = mysqli_escape_string($cn,trim($_POST['unit1'])); 
	$unit2        = mysqli_escape_string($cn,trim($_POST['unit2'])); 
	$price        = mysqli_escape_string($cn,trim($_POST['price'])); 
	$set_price    = mysqli_escape_string($cn,trim($_POST['set_price']));	
	$sqlSubject  = mysqli_query($cn, "update `tbl_product` SET `product_name`='".$product_name."', `sku`='".$sku."', `case_size`='".$case_size."', `unit1`='".$unit1."', `unit2`='".$unit2."', `price`='".$price."', `set_price`='".$set_price."' where `id`='".$edit_id."'");
	if($sqlSubject)
	{
		$_SESSION['errorMsg'] = '<div class="alert alert-success"><strong>Success!</strong> Product Update Successfully.</div>';
	}
	else
	{
		$_SESSION['errorMsg'] = '<div class="alert alert-danger"><strong>Danger!</strong> Product Not Update. Try Again.</div>';
	}
	header("location:home.php?pages=addProduct&edit_id=".$edit_id);				
}