<? include_once("../includes/main.inc.php");

$page_id = $_REQUEST['page_id'];
$country_id = '1';

if(is_post_back()) {

                    $qs= explode("-", $unique );
			        $qs1 =$qs[0];
					$qs2 =$qs[1]; 
				//	print_r($unique);exit();

	     if($page_id!='') {	
              $childexplode=explode("-",$unique);
		      $ch_id=$childexplode['0'];
		      $ch_name=$childexplode['1'];
		      
		      
          $childquery="Select country_url from tbl_country where country_id='$ch_id'";
         	//  print_r($childquery);exit();
			  $childresult=mysql_fetch_assoc(mysql_query($childquery));
			  $child_name=$childresult['country_url'];
        	
	
         	 $ptquery="Select country_name from tbl_country where country_id='$parent_childs'";
			  $ptresult=mysql_fetch_assoc(mysql_query($ptquery));
			  $p_name=$ptresult['country_name'];
	
              $stquery="Select state_name from tbl_state where state_id='$country'";
			  $stresult=mysql_fetch_assoc(mysql_query($stquery));
			  $s_name=$stresult['state_name'];
			
			  $ctquery="Select county_name from tbl_county where county_id='$state'";
			  $ctresult=mysql_fetch_assoc(mysql_query($ctquery));
			  $c_name=$ctresult['county_name'];
				
		      $sql = "update `tbl_statecounty` set parent='$parent_childs',parent_name='$p_name',child='$ch_id',child_name='$child_name', state='$country',state_name='$s_name',county='$state',county_name='$c_name', country_status='$status', title='$title', description='$description', keyword='$keyword', country_title='$country_title', country_descriptions='$country_descriptions' where id = $page_id ";
		     // $sql1 = "update `tbl_country` set updated_on=CURRENT_TIMESTAMP where id = $page_id";
		     // print_r($sql1);exit();
		      db_query($sql);
		    //  db_query($sql1);
		      set_session_msg("Content updated successfully.");		
	      
		    } else{	
	          $childexplode=explode("-",$unique);
		      $ch_id=$childexplode['0'];
		      $ch_name=$childexplode['1'];
		    // 	print_r($ch_id);exit();
		  $childquery="Select country_url from tbl_country where country_id='$ch_id'";
		 // print_r();exit($childquery);
			  $childresult=mysql_fetch_assoc(mysql_query($childquery));
			  $child_name=$childresult['country_url'];
            //	print_r($child_name);exit();
		      
		      $ptquery="Select country_name from tbl_country where country_id='$parent_childs'";
			  $ptresult=mysql_fetch_assoc(mysql_query($ptquery));
			  $p_name=$ptresult['country_name'];
	
              $stquery="Select state_name from tbl_state where state_id='$country'";
			  $stresult=mysql_fetch_assoc(mysql_query($stquery));
			  $s_name=$stresult['state_name'];
			
			  $ctquery="Select county_name from tbl_county where county_id='$state'";
			  $ctresult=mysql_fetch_assoc(mysql_query($ctquery));
			  $c_name=$ctresult['county_name'];
			
			  $sql = "insert into `tbl_statecounty` set parent='$parent_childs',parent_name='$p_name',  child='$ch_id',child_name='$child_name', state='$country',state_name='$s_name',county='$state',county_name='$c_name', country_status='$status', title='$title', description='$description', keyword='$keyword', country_title='$country_title', country_descriptions='$country_descriptions'";
			 // print_r($sql);exit();
              //  $sql1 = "insert into `tbl_country` set updated_on=CURRENT_TIMESTAMP";
              //  print_r($sql1);exit();
             db_query($sql);
          //   db_query($sql1);
              set_session_msg("New Content added successfully.");
            }

	    header("Location: content_location_add.php?start=$start");
	    exit;
    }

  $page_id = $_REQUEST['page_id'];

  if($page_id!='') {
	$sql = "select * from `tbl_statecounty` where id = '$page_id'";
	$result = db_query($sql);
	if ($line_raw = mysql_fetch_array($result)) {
	@extract($line_raw);

	}
}
?>

<link href="styles.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="fckeditor/fckeditor.js"></script>
<? include("top.inc.php");?>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td id="pageHead"><div id="txtPageHead">
Add/Edit Location Content</div></td>
</tr>
</table>

<table width="80%" align="left">
<tr><td>

<form method="post" action="" enctype="multipart/form-data" name="form1" id="form1" onSubmit="return validate(this);">
<table width="80%" align="center" border=0 cellpadding="0"  cellspacing="0" class="text">

<tr>
<td  colspan="2"height="30"  align="right"><a href="content_location.php?start=<?=$start?>" class="redcolor"><strong>Back To Location Content List</strong></a></td>
</tr>    
<tr><td height="15"><div align="center"><span class="msg"><?=display_sess_msg();?></span></div><br /></td></tr>  

<?php  $pcqry = mysql_query("select country_id,country_name from `tbl_country` where parent_childs='0' ");	?>
<tr>
<td class="lables"><b>Parent Name</b></td>
<td class="feildstxt"><select name="parent_childs" id="parent_childs" style="width:300px;">
<option value="0" <?php if($parent_childs=='0'){ echo 'selected="selected"'; } ?>>Select Parent Name</option>
<?php while($parentData = mysql_fetch_assoc($pcqry)){ 
?>  
<option value="<?php echo $parentData['country_id']; ?>" <?php //if($parentData['country_id']==$parent){ echo 'selected="selected"'; } ?>><?php echo $parentData['country_name']; ?></option>      
<?php } ?>    
</select></td>
</tr>

<tr><td>&nbsp;</td></tr>

<tr>
<td class="lables"><b>Services ( Childs Name )</b></td>
<td class="feildstxt">
<select name="unique" id="unique" style="width:300px;">
<option value="">Select Services Name</option>    
</select>
</td>
</tr>

<tr><td>&nbsp;</td></tr>

<?php  $stateqry = mysql_query("select state_id,state_name from `tbl_state` where country_id='1' order by state_name asc");	?>
          <tr>
		  <td class="lables"><b>State</b></td>
          <td class="feildstxt"><select name="country" id="country" class="" style="width:300px;">
		  <option value="0">Select</option>
		  <?php while($stateData = mysql_fetch_assoc($stateqry)){ ?> 
           <option value="<?php echo $stateData['state_id'];?>" <?php// if($stateData['state_id']==$state){ echo 'selected="selected"'; } ?> ><?php echo $stateData['state_name']; ?></option>      		  
		  <?php }?>
          </select></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
          <tr>
		  <td class="lables"><b>County</b></td>
          <td class="feildstxt"><select name="state" id="state" class="" style="width:300px;">
		  <option value="0">Select</option>
          </select></td>
        </tr>
        <tr><td>&nbsp;</td></tr>

<tr>
<td class="lables"><b>Status</b></td>
<td class="feildstxt"><select name="status" id="status">
<option value="1" <?php if($country_status=='1'){ echo 'selected="selected"'; } ?>>Active</option>
<option value="0" <?php if($country_status=='0'){ echo 'selected="selected"'; } ?>>InActive</option>          
</select></td>
</tr>

<tr><td>&nbsp;</td></tr>

<tr><td class="lables" colspan="2"><hr/></td> </tr>

<tr><td>&nbsp;</td></tr>
       
 <tr>
<td class="lables"><b>Title </b></td>
 <td class="feildstxt"><textarea style="width:300px;" name="title" id="title"  ><?=stripslashes($title)?></textarea></td>
  </tr>
  <tr><td>&nbsp;</td></tr>
        
        <tr>
		  <td class="lables"><b>Description </b></td>
          <td class="feildstxt"><textarea style="width:300px;" name="description" id="description"  ><?=stripslashes($description)?></textarea></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        
        <tr>
		  <td class="lables"><b>Keyword</b></td>
          <td class="feildstxt"><textarea style="width:300px;" name="keyword" id="keyword"  ><?=stripslashes($keyword)?></textarea></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
		  <td class="lables" colspan="2"><hr/></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
		<tr>
		  <td class="lables"><b>Services Title</b></td>
          <td class="feildstxt"><textarea style="width:300px;" name="country_title" id="country_title"  ><?=stripslashes($country_title)?></textarea></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        
        <tr>
		  <td class="lables"><b>Services Descriptions</b></td>
          <td class="feildstxt"><textarea style="width:300px;" name="country_descriptions" id="country_descriptions"  ><?=stripslashes($country_descriptions)?></textarea></td>
        </tr>
        <tr><td>&nbsp;</td></tr>

<tr>
<td align="left" class="lables">&nbsp;</td>
<td align="left" class="feildstxt"><input class="button" type="submit" name="submit" value="Add"></td>
</tr>

<tr><td height="25">&nbsp;</td></tr>
</table>
</form>
</td></tr></table> 
 <script type="text/javascript">
		 var oFCKeditor=new FCKeditor('country_descriptions','650','400')	;
		 oFCKeditor.BasePath="fckeditor/";	
		 oFCKeditor.ReplaceTextarea();
    </script>   
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


<script>
$(document).ready(function(){
    // Attach the change event handler
$('#parent_childs').on('change',function(){
	var PID = $(this).val();
	/*alert(PID) */
	if(PID){
		$.ajax({
			type:'POST',
			url:'<?php echo SITE_WS_PATH; ?>/admin/getCounty.php',
			data:'parent_childs='+PID,
			success:function(html){
			$('#unique').html(html);
	
			}
		}); 
	}else{
		$('#unique').html('<option value="">Select Services</option>');
	 
	}
});
});
</script> 

	<script>
    $(document).ready(function () {
        // If the form is in edit mode and a country has been previously selected, pre-select the previously selected country and state
        var selectedStateId = '<?php echo $state; ?>';
        var selectedCountyId = '<?php echo $county; ?>';
        
        var selectedStateName = '<?php echo $state_name; ?>';
        var selectedCountyName = '<?php echo $county_name; ?>';
      

        if (selectedStateId) {
            $('#country').val(selectedStateId);
            //$('#state').removeAttr('disabled');
            //if ($('#state').is(':disabled')) {
            //    console.log('State dropdown is still disabled.');
            //}
        }
        if (selectedCountyId) {
            $('#state').val(selectedCountyId); // Use selectedCountyId to pre-select the county dropdown
        }

        // When the user selects a country, dynamically load the list of states for the selected country using Ajax
        $('#country').on('change', function () {
            var countryId = $(this).val();
            if (countryId) {
                $.ajax({
                    type: 'POST',
                    url: '<?php echo SITE_WS_PATH; ?>/admin/getCounty.php',
                    data: {
                        'state_id': countryId,
                        'selected_county': selectedCountyId
                    },
                    success: function (counties) { // Change 'states' to 'counties' for clarity
                        $('#state').html(counties).removeAttr('disabled');
                    }
                });
            } else {
                $('#state').html('<option value="">Select a state</option>').attr('disabled', 'disabled');
            }
        });
    });

</script>
	
<? include("bottom.inc.php");?>