<?php $PageSecurity = 15;
$title = _('State Master');
echo '<p class="page_title_text"><h2 align="center"><title="' . _('Search') . '" alt="">' .$title.'</h2>';
$status=true;

if(isset($_POST['submit']) )
{
 	if(empty($_POST['state']))
	{
		$msgErr[]="Please Enter State Name";
	   $status=false;
	}
        if(empty($_POST['state_code']))
	{
		$msgErr[]="Please Enter State Code";
	   $status=false;
	}
	
	if($_POST['country']=='000')
	{
		$msgErr[]="Please Select Country ";
	   $status=false;
	}

	$check_sql=DB_query("Select * from state_master where state='".trim($_POST['state'])."'",$db);
	$num=DB_num_rows($check_sql);
	if($num>0)
	{
		$msgErr[]="This State already exists";
		$status=false;
	}
}

if(isset($_POST['submit']) && $status)
{ 
	$sql = "INSERT into state_master (id, state,state_code,country,union_territory) values ('','".trim($_POST['state'])."','".$_POST['state_code']."','".$_POST['country']."','".$_POST['union']."')";
	$result = DB_query($sql,$db);
	echo "<script>location.href='index.php?route=state_master&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}

if($_REQUEST['action'] == 'edit' && $_REQUEST['id'] != '')
{
	$get_sql = "select * from state_master where id='".$_REQUEST['id']."'";
	$get_result = DB_query($get_sql,$db);
	$get_row = DB_fetch_array($get_result);
 	$state = $get_row['state'];
	$country = $get_row['country'];
    $state_code = $get_row['state_code'];
    $union_territory=$get_row['union_territory'];
}

if(isset($_POST['edit']) && $_POST['state'] != '' && $_REQUEST['id'] != ''&& $status)
{
	$update_sql = "Update state_master SET state = '".trim($_POST['state'])."',state_code = '".$_POST['state_code']."',country = '".$_POST['country']."',union_territory='".$_POST['union']."' WHERE id = '".$_REQUEST['id']."'";
	$update_result = DB_query($update_sql,$db);
	echo "<script>location.href='index.php?route=state_master&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
 }

if($_REQUEST['action'] == 'del' && $_REQUEST['id'] != '')
{
	$delet_sql = "Delete from state_master WHERE id='".$_REQUEST['id']."'";
	$delete_result = DB_query($delet_sql,$db);
	echo"<div class='success'><strong>State Deleted Successfully!</strong></div>";
	echo "<script>location.href='index.php?route=state_master&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}

if($_REQUEST['daction'] )
{
echo"<div class='success'><strong>State ".$_REQUEST['daction']." Successfully!</strong></div>";
}
?>
<form method="post" action="">
   <table width="40%" cellpadding="3" border="0" align="center">
  
   <?php   
   if(isset($msgErr)&&$msgErr!='' )
   {?>
    <tr>
  
    <td colspan="4" ><div class="error"><?php 

for( $kp=0;$kp<count($msgErr);$kp++)
 {
		echo  '&nbsp;&nbsp; *'.$msgErr[$kp].'<br>';
 }
  ?></div></td>
  </tr>
  
   <?php }?>

    <tr>
    <td><strong>Country : </strong></td>
    <td>
	<select name="country">
	<option  value="000">--Please Select Country--</option>
		<?php
		
		$sql_country=DB_query("select * from country_master where 1=1",$db);
		while($row_country=DB_fetch_array($sql_country))
		{
		  $p='';
		  if($row_country['id']==$country)
		  $p="selected";
		 ?>
			<option  value="<?php echo $row_country['id'] ?>" <?php echo $p; ?>><?php echo $row_country['country'] ?></option>		 
	 <?php } ?>
	  </select>
	</td>
  </tr>
  <tr>
    <td><strong>State Name : </strong></td>
    <td>
	<input type="text" name="state" id="state" size="30" value="<?php if($state != '')echo $state; else echo $_POST['state'] ?>"/>
	</td>
  </tr>
<tr>
    <td><strong>State Code : </strong></td>
    <td>
	<input type="text" name="state_code" id="state_code" size="30" value="<?php if($state_code != '')echo $state_code; else echo $_POST['state_code'] ?>"/>
	</td>
  </tr>

  <tr>
    <td><strong>Union territory : </strong></td>
    <td>
	<input type="checkbox" name="union" id="union" value="1" <?php if($union_territory=='1') { ?> checked="checked" <?php } ?> />
	</td>
  </tr>

</table>
  <p>&nbsp;</p>
  <p align="center">
    <?php if($_REQUEST['action'] == 'edit') {?>
	<input type="submit" name="edit" value="Edit">
	<?php } else {?>
	<input type="submit" name="submit" value="Save">
	<?php } ?>
  </p>
  <p>&nbsp;</p>
</form>
<p>

</p>
<table width="40%" border="0" cellspacing="1" cellpadding="2" style="border: solid 1px #666666">
  <tr>
    <th scope="col">S.no</th>
	  <th scope="col">Country</th>
    <th scope="col">State</th>
    <th scope="col">State Code</th>
    <th scope="col">Union Territory</th>
	<th scope="col">Edit</th>
    <th scope="col">Delete</th>
  </tr>
 
    
	<?php 
   $state_sql = "select * from state_master order by state ASC";
   $state_result = DB_query($state_sql,$db);
   $i=1;
   while($state_row = DB_fetch_array($state_result))
   {?>
    <tr bgcolor="#FFFFFF">
	<td><div align="center"><?php echo $i; ?></div></td>
    <td><div align="left"><?php echo $s->getField('country_master','id',$state_row['country'],'country');
	  ?></div></td>
	 <td><div align="left"><?php echo $state_row['state']; ?></div></td>
    <td><div align="left"><?php echo $state_row['state_code']; ?></div></td>
    <td><?php if($state_row['union_territory']=='1') { echo "Yes"; } ?></td>
	<td><a href="index.php?route=state_master&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=edit&id=<?php echo $state_row['id']; ?>"><img src="images/icon_edit.gif" width="10" height="10" /></a></td>
	<td><a href="index.php?route=state_master&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=del&id=<?php echo $state_row['id']; ?>" onclick="return confirm('This state will be deleted. Are you sure?')"><img src="images/Icon_Delete.png"  width="10" height="10"  /></a></td>	
	  </tr>
<?php $i++;} ?>

</table>
<p>&nbsp;</p>
<?php include('includes/footer.inc');
?>
