<?php 		  					
if(isset($_GET['edit_id']))
{
	$id      = $_GET['edit_id'];
	$sql     = mysqli_query($cn, "select * from `tbl_retailer` 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 Retailer";}else{ echo "Add Retailer";}?></h4>        
            <form class="forms-sample" method="post" enctype="multipart/form-data">													                
                <div class="row">                
                	<div class="col-md-3">
                        <div class="form-group">
                            <label for="exampleInputCity1">Shop 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="shop_name" placeholder="Shop Name" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['shop_name'];}?>" required>
                        </div>
                    </div> 
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">Contact Name <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="name" placeholder="Name" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['name'];}?>" required>
                        </div>
                    </div> 
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">Mobile<span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="mobile" placeholder="Mobile" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['mobile'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">State <span style="color:#F00;">*</span></label>
							<select name="state" class="form-control" onchange="getDistrict(this.value);" required> 
                            	<option value="">Select State</option>
                                <?php
								$sqlState = mysqli_query($cn, "select `state` from `tbl_sh` group by `state` order by `state`");
								while($sqlStateData = mysqli_fetch_array($sqlState)){?>
                                <option value="<?php echo $sqlStateData['state'];?>" <?php if(isset($_GET['edit_id'])){if($sqlStateData['state']==$sqlData['state']){ echo "selected";}}?>><?php echo $sqlStateData['state'];?></option>
                                <?php }?>
                            </select>                           
                        </div>
                    </div>
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">District <span style="color:#F00;">*</span></label>
							<select name="district" class="form-control" id="district" required> 
                            	<option value="">Select District</option>
                                <?php
								if(isset($_GET['edit_id'])){
								$sqlState = mysqli_query($cn, "select `district` from `tbl_retailer` where `state`='".$sqlData['state']."' group by `district` order by `district`");
								while($sqlStateData = mysqli_fetch_array($sqlState)){?>
                                <option value="<?php echo $sqlStateData['district'];?>" <?php if($sqlStateData['district']==$sqlData['district']){ echo "selected";}?>><?php echo $sqlStateData['district'];?></option>
                                <?php }}?>
                            </select>                           
                        </div>
                    </div>
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">Sales District <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="sale_district" placeholder="Sales District" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['sale_district'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-3">
                        <div class="form-group">
                            <label for="exampleInputCity1">Beat <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="deat" placeholder="Beat" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['beat'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="exampleInputCity1">Full Address <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="address" placeholder="Full Address" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['address'];}?>" required>
                        </div>
                    </div>
                    
                    <div class="col-md-2">
                        <div class="form-group">
                            <label for="exampleInputCity1">Pincode <span style="color:#F00;">*</span></label>
                            <input type="text" class="form-control" name="pincode" placeholder="Pincode" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['pincode'];}?>" 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="Distributor Code" value="<?php if(isset($_GET['edit_id'])){echo $sqlData['ds_code'];}?>" 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']))
{ 
	$shop_name	   = mysqli_escape_string($cn,trim($_POST['shop_name'])); 
	$name          = mysqli_escape_string($cn,trim($_POST['name']));
	$mobile        = mysqli_escape_string($cn,trim($_POST['mobile']));
	$state         = mysqli_escape_string($cn,trim($_POST['state'])); 
	$district      = mysqli_escape_string($cn,trim($_POST['district'])); 
	$sale_district = mysqli_escape_string($cn,trim($_POST['sale_district']));
	$deat          = mysqli_escape_string($cn,trim($_POST['deat'])); 																					
	$address       = mysqli_escape_string($cn,trim($_POST['address']));
	$pincode       = mysqli_escape_string($cn,trim($_POST['pincode']));	
	$ds_code       = mysqli_escape_string($cn,trim($_POST['ds_code']));	
	$sqlSubject    = mysqli_query($cn, "INSERT INTO `tbl_retailer` SET `ds_code`='".$ds_code."', `state`='".$state."', `district`='".$district."', `sale_district`='".$sale_district."', `beat`='".$beat."', `shop_name`='".$shop_name."', `mobile`='".$mobile."', `address`='".$address."', `pincode`='".$pincode."', `status`=1");
	if($sqlSubject)
	{			
		$_SESSION['errorMsg'] = '<div class="alert alert-success"><strong>Success!</strong> Shop Added Successfully.</div>';
	}
	else
	{
		$_SESSION['errorMsg'] = '<div class="alert alert-danger"><strong>Danger!</strong> Shop not Added. Try Again.</div>';
	}	
	header("location:home.php?pages=addRetailer");	
}
		
		  
if(isset($_POST['edit']))
{
	$edit_id     = $_POST['edit_id'];
	$shop_name	   = mysqli_escape_string($cn,trim($_POST['shop_name'])); 
	$name          = mysqli_escape_string($cn,trim($_POST['name']));
	$mobile        = mysqli_escape_string($cn,trim($_POST['mobile']));
	$state         = mysqli_escape_string($cn,trim($_POST['state'])); 
	$district      = mysqli_escape_string($cn,trim($_POST['district'])); 
	$sale_district = mysqli_escape_string($cn,trim($_POST['sale_district']));
	$deat          = mysqli_escape_string($cn,trim($_POST['deat'])); 																					
	$address       = mysqli_escape_string($cn,trim($_POST['address']));
	$pincode       = mysqli_escape_string($cn,trim($_POST['pincode']));	
	$ds_code       = mysqli_escape_string($cn,trim($_POST['ds_code']));	
	$sqlSubject  = mysqli_query($cn, "update `tbl_retailer` SET `ds_code`='".$ds_code."', `state`='".$state."', `district`='".$district."', `sale_district`='".$sale_district."', `beat`='".$beat."', `shop_name`='".$shop_name."', `mobile`='".$mobile."', `address`='".$address."', `pincode`='".$pincode."' where `id`='".$edit_id."'");
	if($sqlSubject)
	{
		$_SESSION['errorMsg'] = '<div class="alert alert-success"><strong>Success!</strong> Shop Update Successfully.</div>';
	}
	else
	{
		$_SESSION['errorMsg'] = '<div class="alert alert-danger"><strong>Danger!</strong> Shop Not Update. Try Again.</div>';
	}
	header("location:home.php?pages=addRetailer&edit_id=".$edit_id);				
}