<?php $PageSecurity = 15;
$title = _('Country Master');
echo '<p class="page_title_text"><h2 align="center"><title="' . _('Search') . '" alt="">' .$title.'</h2>';
$status=true;
if(isset($_POST['submit']) )
{
	if(empty($_POST['country']))
	{
		$msgerr="Please Enter Country Name";
	   $status=false;
	}
}
if(isset($_POST['submit']) && $status)
{
	 $checkQuery="select * from country_master where country='".$_REQUEST['country']."' and parent_id='".$_REQUEST['parent_id']."' and CompanyId='".$_SESSION['ACGCompanyId']."'";
        $checkExec = DB_query($checkQuery,$db);
        if(DB_num_rows($checkExec)==0)
	{
            $data['code']=$_REQUEST['code'];
            $data['country']=$_REQUEST['country'];
            $data['parent_id']=$_REQUEST['parent_id'];
            $data['type']=$_REQUEST['type'];
            $data['CompanyId']=$_SESSION['ACGCompanyId'];
            if($_REQUEST['parent_id']==0)
            {
                $data['type']='G';
            }
        else {
                 $type=$s->GetFieldValue('country_master',"id='".$_REQUEST['parent_id']."'",'type');
                if($type=='G')
                {
                $data['type']='A';
                }
                elseif($type=='A')
                  {
                    $data['type']='C';
                  }  
            }

           // echo '<pre>';
            //print_r($data);
            //exit();

            $insert= $s->insertRecord('country_master', $data);
            $row=DB_query($insert,$db);
            if(!$row)
            {
               die('Error'.DB_error());
            }
            echo"<div class='success'><strong>Country Inserted Successfully!</strong></div>";
            echo "<script>location.href='index.php?route=country_master&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
        }
}

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

if(isset($_POST['edit']) && $_POST['country'] != '' && $_REQUEST['id'] != '' && $status)
{
	$update_sql = "Update country_master SET country = '".$_POST['country']."' WHERE id = '".$_REQUEST['id']."'";
	$update_result = DB_query($update_sql,$db);
	echo"<div class='success'><strong>Country Updated Successfully!</strong></div>";
	echo "<script>location.href='index.php?route=country_master&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}

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

}
function GeoShow($parent_id)
{
    global $db;
    $herQuery="select * from country_master where parent_id='".$parent_id."' and type='A'";
     $her_result = DB_query($herQuery,$db);
    while($row1=DB_fetch_array($her_result))
             {?>
		<option value="<?php echo $row1['id'];?>"><?php echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--'.$row1['country'];?></option>
                <?php 
                GeoShow($row1['id']);
       } 
}
?>
<form method="post" action="">
  <table width="40%" cellpadding="3" border="0" align="center">
  <?php if(isset($msgerr)){?>
  <tr>
  <td colspan="2" ><div class="error"><?php  echo $msgerr;?></div></td>
  </tr>
  <?php } ?>
  <tr>
    <td><strong>Code</strong></td>
    <td>
	<input type="text" name="code" id="code" size="20" value="<?php echo $code?>"/>
	</td>
  </tr><tr>
    <td><strong>Name</strong></td>
    <td>
	<input type="text" name="country" id="country" size="20" value="<?php echo $country?>"/>
	</td>
  </tr>
  <tr>
    <td><strong>Under</strong></td>
    <td>
        <?php
        $herQuery="select * from country_master where parent_id='0'";
        $her_result = DB_query($herQuery,$db);
        
        ?>
        <select name="parent_id">
            <option value="0">Primary</option>
            <?php
            while($row1=DB_fetch_array($her_result))
             {?>
		<option value="<?php echo $row1['id'];?>"><?php echo $row1['country'];?></option>
                <?php 
                GeoShow($row1['id']);
       } ?>
	</select>
       </td>
  </tr>
</table>
  
   <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 } ?>
</form>
<table width="40%" border="1" cellspacing="1" cellpadding="2" style='border-collapse: collapse;'>
  <tr>
    <th scope="col"><strong>S. No.</strong></th>
    <th scope="col"><strong>Group</strong></th>
    <th scope="col"><strong>Area</strong></th>
    <th scope="col"><strong>Country</strong></th>
	<th scope="col"><strong>Edit</strong></th>
    <th scope="col"><strong>Delete</strong></th>
</tr>
<?php 
   $country_sql = "select * from country_master where CompanyId='".$_SESSION['ACGCompanyId']."' and parent_id='0'";
   $country_result = DB_query($country_sql,$db);
  	$i=1;
   while($country_row = DB_fetch_array($country_result))
   {?>
	<tr class='row'>
            <td ><?php echo $i; ?></td>
            <td colspan='3'><?php echo $country_row['country']; ?></td>
             <td><a href="index.php?route=country_master&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=edit&id=<?php echo $country_row['id']; ?>"> <img src="images/icon-edit.png"  width="10px" height="10"/></a></td>
             <?php 
         $area_sql = "select * from country_master where parent_id='".$country_row['id']."' and type='A'";
        $area_result = DB_query($area_sql,$db);
       
        ?>
            <td><a href="index.php?route=country_master&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=del&id=<?php echo $country_row['id']; ?>" onclick="return confirm('This Country will be deleted. Are you sure?')"><img src="images/Icon_Delete.png" width="10" height="10" /></a></td>
        </tr>
        <?php
        while($area_row = DB_fetch_array($area_result))
         {?>
        <tr class='row'>
            <td></td>
            <td></td>
            <td colspan='2'><div align="left"><?php echo $area_row['country']; ?></div></td>
            <td><a href="index.php?route=country_master&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=edit&id=<?php echo $country_row['id']; ?>"> <img src="images/icon-edit.png"  width="10px" height="10"/></a></td>
            <td><a href="index.php?route=country_master&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=del&id=<?php echo $country_row['id']; ?>" onclick="return confirm('This Country will be deleted. Are you sure?')"><img src="images/Icon_Delete.png" width="10" height="10" /></a></td>
        </tr>
         <?php 
         $country_sql = "select * from country_master where parent_id='".$area_row['id']."' and type='C'";
        $country_sql_result = DB_query($country_sql,$db);
        while($country_row = DB_fetch_array($country_sql_result))
         {?>
            <tr class='row'>
            <td></td>
            <td></td>
            <td></td>
            <td ><div align="left"><?php echo $country_row['country']; ?></div></td>
            <td><a href="index.php?route=country_master&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=edit&id=<?php echo $country_row['id']; ?>"> <img src="images/icon-edit.png"  width="10px" height="10"/></a></td>
            <td><a href="index.php?route=country_master&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=del&id=<?php echo $country_row['id']; ?>" onclick="return confirm('This Country will be deleted. Are you sure?')"><img src="images/Icon_Delete.png" width="10" height="10" /></a></td>
        </tr>
         
	<?php 
                } // Inner Country Loop
            } // Area Group Loop ?>
            <tr class='row'>
            <td colspan='7'></td>
        </tr>
        <?php  $i++; } // Outer gROUP lOOP
	?>
       
</table>
<p>&nbsp; </p>
<?php include('includes/footer.inc');?>