<? 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);
            //  print_r($childexplode);exit();
		      $ch_id=$childexplode['0'];
		      $ch_name=$childexplode['1'];
		         // print_r($ch_id);exit();
		        //  print_r($ch_name);exit();
		      
		      
          $childquery="Select country_name from tbl_country where country_id='$ch_id'";
         
			  $childresult=mysqli_fetch_assoc(db_query($childquery));
			  $child_name=$childresult['country_name'];
			  //$child_name=strtolower(str_replace(" ","-",$childresult['country_name']));
			 //  print_r($child_name);exit();
        	
	
         	 $ptquery="Select country_name from tbl_country where country_id='$parent_childs'";
			  $ptresult=mysqli_fetch_assoc(db_query($ptquery));
			  $p_name=$ptresult['country_name'];
	
             
				
		     $sql = "update `tbl_faq` set parent='$parent_childs',parent_name='$p_name',child='$ch_id',child_name='$child_name', question1='$question1', answer1='$answer1', question2='$question2', answer2='$answer2', question3='$question3', answer3='$answer3',question4='$question4', answer4='$answer4', question5='$question5', answer5='$answer5' where id = $page_id ";
		      
		  //  print_r($sql);exit();
		      db_query($sql);
		      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_name from tbl_country where country_id='$ch_id'";
		 // print_r();exit($childquery);
			  $childresult=mysqli_fetch_assoc(db_query($childquery));
			  $child_name=$childresult['country_name'];
            //	print_r($child_name);exit();
		      
		      $ptquery="Select country_name from tbl_country where country_id='$parent_childs'";
			  $ptresult=mysqli_fetch_assoc(db_query($ptquery));
			  $p_name=$ptresult['country_name'];
	
             
			
			  $sql = "insert into `tbl_faq` set parent='$parent_childs',parent_name='$p_name',  child='$ch_id',child_name='$child_name',  question1='$question1', answer1='$answer1', question2='$question2', answer2='$answer2', question3='$question3', answer3='$answer3',question4='$question4', answer4='$answer4', question5='$question5', answer5='$answer5'";
			//  print_r($sql);exit();
              db_query($sql);
              set_session_msg("New Content added successfully.");
            }

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

  $page_id = $_REQUEST['page_id'];

  if($page_id!='') {
	$sql = "select * from `tbl_faq` where id = '$page_id'";
	$result = db_query($sql);
	if ($line_raw = mysqli_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 FAQ Content</div></td>
</tr>
</table>

<table width="80%" align="left">
<tr><td>
<script type="text/javascript" src="fckeditor/fckeditor.js"></script>
<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="faq_list.php?start=<?=$start?>" class="redcolor"><strong>Back To FAQ 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 = db_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(isset($parent_childs)=='0'){ echo 'selected="selected"'; } ?>>Select Parent Name</option>
<?php while($parentData = mysqli_fetch_assoc($pcqry)){ 
    $parent = $parent ?? '';

?>  
<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>

<? // $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>
		  <? //while($stateData = mysql_fetch_assoc($stateqry)){ ?> 
           <option value="<? //echo $stateData['state_id'];?>" <?// if($stateData['state_id']==$state){ echo 'selected="selected"'; } ?> ><? //echo $stateData['state_name']; ?></option>      		  
		  <?// }?>
          </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(isset($country_status)=='1'){ echo 'selected="selected"'; } ?>>Active</option>
<option value="0" <?php if(isset($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>Question1</b></td>
 <td class="feildstxt"><textarea style="width:300px;" name="question1" id="question1"  ><?=stripslashes($question1 ?? '')?></textarea></td>
  </tr>
  <tr><td>&nbsp;</td></tr>
        
        <tr>
		  <td class="lables"><b>Answer1 </b></td>
          <td class="feildstxt"><textarea style="width:300px;" name="answer1" id="answer1"  ><?= stripslashes($answer1 ?? ''); ?></textarea></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        
         <tr>
<td class="lables"><b>Question2</b></td>
 <td class="feildstxt"><textarea style="width:300px;" name="question2" id="question2"  ><?=stripslashes($question2 ?? '')?></textarea></td>
  </tr>
  <tr><td>&nbsp;</td></tr>
        
        <tr>
		  <td class="lables"><b>Answer2 </b></td>
          <td class="feildstxt"><textarea style="width:300px;" name="answer2" id="answer2"  ><?=stripslashes($answer2 ?? '')?></textarea></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        
     
      <tr>
<td class="lables"><b>Question3</b></td>
 <td class="feildstxt"><textarea style="width:300px;" name="question3" id="question3"  ><?=stripslashes($question3 ?? '')?></textarea></td>
  </tr>
  <tr><td>&nbsp;</td></tr>
        
        <tr>
		  <td class="lables"><b>Answer3 </b></td>
          <td class="feildstxt"><textarea style="width:300px;" name="answer3" id="answer3"  ><?=stripslashes($answer3 ?? '')?></textarea></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        
     
      <tr>
<td class="lables"><b>Question4</b></td>
 <td class="feildstxt"><textarea style="width:300px;" name="question4" id="question4"  ><?=stripslashes($question4 ?? '')?></textarea></td>
  </tr>
  <tr><td>&nbsp;</td></tr>
        
        <tr>
		  <td class="lables"><b>Answer4 </b></td>
          <td class="feildstxt"><textarea style="width:300px;" name="answer4" id="answer4"  ><?=stripslashes($answer4 ?? '')?></textarea></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        
     
      <tr>
<td class="lables"><b>Question5</b></td>
 <td class="feildstxt"><textarea style="width:300px;" name="question5" id="question5"  ><?=stripslashes($question5 ?? '')?></textarea></td>
  </tr>
  <tr><td>&nbsp;</td></tr>
        
        <tr>
		  <td class="lables"><b>Answer5 </b></td>
          <td class="feildstxt"><textarea style="width:300px;" name="answer5" id="answer5"  ><?=stripslashes($answer5 ?? '')?></textarea></td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        
     
     
        <tr><td>&nbsp;</td></tr>
        <tr>
		  <td class="lables" colspan="2"><hr/></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 type="text/javascript">
		 var oFCKeditor1=new FCKeditor('answer1','550','300')	;
		 oFCKeditor1.BasePath="fckeditor/";	
		 oFCKeditor1.ReplaceTextarea();
    </script>
    <script type="text/javascript">
		 var oFCKeditor2=new FCKeditor('answer2','550','300')	;
		 oFCKeditor2.BasePath="fckeditor/";	
		 oFCKeditor2.ReplaceTextarea();
    </script>
    <script type="text/javascript">
		 var oFCKeditor3=new FCKeditor('answer3','550','300')	;
		 oFCKeditor3.BasePath="fckeditor/";	
		 oFCKeditor3.ReplaceTextarea();
    </script>
    <script type="text/javascript">
		 var oFCKeditor4=new FCKeditor('answer4','550','300')	;
		 oFCKeditor4.BasePath="fckeditor/";	
		 oFCKeditor4.ReplaceTextarea();
		 
    </script>
    <script type="text/javascript">
		 var oFCKeditor5=new FCKeditor('answer5','550','300')	;
		 oFCKeditor5.BasePath="fckeditor/";	
		 oFCKeditor5.ReplaceTextarea();
	</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(){
 var initialPID = $('#parent_childs').val();
// alert(initialPID);
     /*   if (initialPID) {
            $('#parent_childs').trigger('change');
        }*/
        if(initialPID){
		$.ajax({
			type:'POST',
			url:'<?php echo SITE_WS_PATH; ?>/admin/getFaqChild.php',
			data:'parent_childs='+initialPID,
			success:function(html){
			$('#unique').html(html);
	
			}
		});
        }
});
</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");?>