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

$msg="";$locality="";$city="";$cityname="";$status="";$caption="Add"; $botton="add";$pincode="";
//echo'<pre>';print_r($_POST);exit;
if(isset($_GET['id']))
{
	$sqlcheck1=mysql_query("select * from tbl_locality where id='".$_GET['id']."'");
	$rescheck1=mysql_fetch_assoc($sqlcheck1);
	
	
	
	$locality=$rescheck1['locality'];
	$city=$rescheck1['cityid'];
	$pincode=$rescheck1['pincode'];
	
	$sqlcity=mysql_query("select * from tbl_cities where city_id='".$city."'");
	$rescity=mysql_fetch_assoc($sqlcity);
	
	$cityname=$rescity['cityname'];
	
	$status=$rescheck1['status'];
}


if(isset($_POST['add']))
{
	
	$city=$_POST['city'];
	$locality=mysql_real_escape_string(ucwords($_POST['locality']));
	$pincode=mysql_real_escape_string($_POST['pincode']);
	$status=mysql_real_escape_string(ucwords($_POST['status']));
	
	
	$sqlcheck=mysql_query("select * from tbl_locality where cityid='$city' and locality='".$locality."' and pincode='".$pincode."'");
	if(mysql_num_rows($sqlcheck)==0)
	{
		$insert=mysql_query("insert into tbl_locality(cityid,locality,pincode,status) values('$city','$locality','$pincode','$status')");
		
		$msg="Locality add successfully";
		?>
		<script>
       setTimeout(function(){
			window.location.href=window.location.href;
			},2000);
        </script>
		<?php
	}
	else
	{
		if(isset($_GET['id']))
		{
			
			
			$update=mysql_query("update tbl_locality set locality='$locality',pincode='$pincode',cityid='$city',status='$status' where id='".$_GET['id']."'");
			
			$msg="Locality update successfully";
			
			?>
		<script>
       setTimeout(function(){
			window.location="localities.php";
			},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="localities.php">Locality</a></li>
      </ol>
    
    </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 Locality</h3>
            </div>
            <div class="panel-body container-fluid">
              
              <form method="post">
              
              
              	
                <div class="form-group form-material">
                  <label class="control-label" for="inputText">City</label>
                    <select name="city" class="form-control" required>
                        <option value="">Please Select</option>
                        
                        <?php $sqlcity=mysql_query("select * from tbl_cities where status='Enable' order by cityname asc");
							  while($rescity=mysql_fetch_assoc($sqlcity))
							  {
						?>
                        
                        <option <?php if($city==$rescity['city_id']) { echo "selected";}?> value="<?php echo $rescity['city_id'];?>"><?php echo $rescity['cityname'];?></option>
                       
                             <?php } ?>
                    </select>
                </div>
              
                <div class="form-group form-material">
                  <label class="control-label" for="inputText">Locality</label>
                  <input type="text" class="form-control" id="inputText" name="locality" required value="<?php echo htmlentities($locality);?>" placeholder="Enter Locality"
                  />
                </div>
                
                 <div class="form-group form-material">
                  <label class="control-label" for="inputText">Postcode</label>
                  <input type="text" class="form-control" id="inputText" name="pincode" required value="<?php echo $pincode;?>" placeholder="Enter pincode"
                  />
                </div>
                
                
                
                
                
              
                <div class="form-group form-material">
                  <label class="control-label" for="inputText">Status</label>
                    <select name="status" class="form-control" required>
                        <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>
				
               
                
                <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>
    
          <!--<link rel="stylesheet" type="text/css" href="datatable/css/jquery.dataTables.css">-->
      <script type="text/javascript" language="javascript" src="datatable/js/jquery.js"></script>
      <script type="text/javascript" language="javascript" src="datatable/js/jquery.dataTables.js"></script>
      <script type="text/javascript" language="javascript" >
          $(document).ready(function() {
              var dataTable = $('#employee-grid').DataTable( {
                  "processing": false,
                  "serverSide": true,
                  "ajax":{
                      url :"ajax/employee-grid-data.php", // json datasource
                      type: "post",  // method  , by default get
                      error: function(){  // error handling
                          $(".employee-grid-error").html("");
                          $("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
                         // $("#employee-grid_processing").css("display","none");
                          
                      }
                  }
              } );
          } );
      </script>
    
    
    <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" id="employee-grid" data-plugin="dataTable">-->
           <table id="employee-grid" class="table table-hover dataTable table-striped width-full"  cellpadding="0" cellspacing="0" border="0" <?php /*?>class="display"<?php */?> width="100%">
            <thead>
              <tr>
                <!--<th>Sr.</th>-->
                <th>Locality</th>
                <th>Cityname</th>
                <th>Postcode</th>
                <th>Status</th>
                <th>Action</th>
               
              </tr>
            </thead>
           
           <?php /*?> <tbody>
              
              <?php
			  
			  $sqlmaincat=mysql_query("select * from tbl_locality");
			  if(mysql_num_rows($sqlmaincat))
			  {
			  while($resmaincat=mysql_fetch_assoc($sqlmaincat))
			  {?>
              <tr>
                <td><?php $sqlc=mysql_query("select * from tbl_cities where city_id='".$resmaincat['city_id']."'");
				$resc=mysql_fetch_assoc($sqlc);
				echo $resc['cityname'];
				?></td>
                <td><?php echo $resmaincat['locality'];?></td>
                <td><?php echo $resmaincat['pincode'];?></td>
                <td><?php echo $resmaincat['status'];?></td>
                <td><a href="localities.php?id=<?php echo $resmaincat['id'];?>" 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['id']?>" 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><?php */?>
          </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_locality_delete.php",{id:id},function(data){
	//alert(data);
	
	});
});
</script>