<?php 		  					
if(isset($_GET['edit_id']))
{
	$id      = $_GET['edit_id'];
	$sql     = mysqli_query($cn, "select * from `tbl_sh` 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 SH";}else{ echo "Add SH";}?></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">State <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="state" placeholder="State" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['state'];}?>" required>
                        </div>
                    </div> 
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">SH Code <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="sh_code" placeholder="SH Code" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['sh_code'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="exampleInputCity1">SH Name <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="sh_name" placeholder="SH Name" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['sh_name'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">ASM Code <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="asm_code" placeholder="ASM Code" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['asm_code'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="exampleInputCity1">ASM Name <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="asm_name" placeholder="ASM Name" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['asm_name'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">DS Code <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="ds_code" placeholder="DS Code" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['ds_code'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="exampleInputCity1">DS Name <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="ds_name" placeholder="DS Name" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['ds_name'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="exampleInputCity1">Sales District <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="district" placeholder="Sales District" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['district'];}?>" 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']))
{ 
	$state	     = mysqli_escape_string($cn,trim($_POST['state'])); 
	$sh_code     = mysqli_escape_string($cn,trim($_POST['sh_code']));
	$sh_name     = mysqli_escape_string($cn,trim($_POST['sh_name']));
	$asm_code    = mysqli_escape_string($cn,trim($_POST['asm_code'])); 
	$asm_name    = mysqli_escape_string($cn,trim($_POST['asm_name'])); 
	$ds_code     = mysqli_escape_string($cn,trim($_POST['ds_code']));
	$ds_name     = mysqli_escape_string($cn,trim($_POST['ds_name'])); 																					
	$district    = mysqli_escape_string($cn,trim($_POST['district']));	
	$sqlSubject  = mysqli_query($cn, "insert into `tbl_sh` SET `state`='".$state."', `sh_code`='".$sh_code."', `sh_name`='".$sh_name."', `asm_code`='".$asm_code."', `asm_name`='".$asm_name."', `ds_code`='".$ds_code."', `ds_name`='".$ds_name."', `district`='".$district."'");
	if($sqlSubject)
	{			
		$_SESSION['errorMsg'] = '<div class="alert alert-success"><strong>Success!</strong> SH Added Successfully.</div>';
	}
	else
	{
		$_SESSION['errorMsg'] = '<div class="alert alert-danger"><strong>Danger!</strong> SH not Added. Try Again.</div>';
	}	
	header("location:home.php?pages=addSH");	
}
		
		  
if(isset($_POST['edit']))
{
	$edit_id     = $_POST['edit_id'];
	$state	     = mysqli_escape_string($cn,trim($_POST['state'])); 
	$sh_code     = mysqli_escape_string($cn,trim($_POST['sh_code']));
	$sh_name     = mysqli_escape_string($cn,trim($_POST['sh_name']));
	$asm_code    = mysqli_escape_string($cn,trim($_POST['asm_code'])); 
	$asm_name    = mysqli_escape_string($cn,trim($_POST['asm_name'])); 
	$ds_code     = mysqli_escape_string($cn,trim($_POST['ds_code']));
	$ds_name     = mysqli_escape_string($cn,trim($_POST['ds_name'])); 																					
	$district    = mysqli_escape_string($cn,trim($_POST['district']));	
	$sqlSubject  = mysqli_query($cn, "update `tbl_sh` SET `state`='".$state."', `sh_code`='".$sh_code."', `sh_name`='".$sh_name."', `asm_code`='".$asm_code."', `asm_name`='".$asm_name."', `ds_code`='".$ds_code."', `ds_name`='".$ds_name."', `district`='".$district."' where `id`='".$edit_id."'");
	if($sqlSubject)
	{
		$_SESSION['errorMsg'] = '<div class="alert alert-success"><strong>Success!</strong> SH Update Successfully.</div>';
	}
	else
	{
		$_SESSION['errorMsg'] = '<div class="alert alert-danger"><strong>Danger!</strong> SH Not Update. Try Again.</div>';
	}
	header("location:home.php?pages=addSH&edit_id=".$edit_id);				
}