<?php require_once('includes/check-login.php');
require_once('includes/config.php');
$tablename="amenities";
$viewurl="amenities.php";
if(isset($_POST['Add'])){
        $created=date("Y-m-d H:i:s");
        $service_id=$_POST['service_id'];
        $subservice_id=$_POST['subservice_id'];
        $status=$_POST['status'];
        $query="INSERT INTO ".$tablename." SET service_id='".$service_id."',subservice_id='".$subservice_id."',amenities_title='".$_POST['amenities_title']."',status='".$status."',created='".$created."'";
		$exe = mysqli_query($con, $query );	
		$amenities_id=mysqli_insert_id($con);
		$amenities_name=isset($_POST['amenities_name']) ? implode("*",$_POST['amenities_name']) : '';
		$explode=explode("*",$amenities_name);
		if(count($explode)>0){
		 for($i=0;$i<count($explode);$i++){
		     $result=mysqli_query($con,"INSERT INTO amenities_description SET amenities_id='".$amenities_id."',amenities_name='".$explode[$i]."'");
		 }   
		}
		if( $exe ) {				
			$_SESSION[$tablename.'succ_msg'] = 'Amenities added successfully';
		}else{				
			$_SESSION[$tablename.'err_msg'] = 'Error occured';
		}
        header("Location:".$base_url.$viewurl."");
}
if(isset($_POST['Update'])){	
$id=$_POST['id']; 
$modified=date("Y-m-d H:i:s");
$query = "update ".$tablename." SET service_id='".$_POST['service_id']."',subservice_id='".$_POST['subservice_id']."',amenities_title='".$_POST['amenities_title']."',status='".$_POST['status']."',modified='".$modified."' WHERE id='".$id."'";
	$exe = mysqli_query( $con, $query );
	$amenities_name=isset($_POST['amenities_name']) ? implode("*",$_POST['amenities_name']) : '';
	$explode=explode("*",$amenities_name);
	$row_id=isset($_POST['row_id']) ? implode("*",$_POST['row_id']) : '';
	$row_id1=explode("*",$row_id);
	if(count($row_id1)>0){
		 for($i=0;$i<count($row_id1);$i++){
		     if($row_id1[$i]==0){
		     $result=mysqli_query($con,"INSERT INTO amenities_description SET amenities_id='".$id."',amenities_name='".$explode[$i]."'");
		     }else{
		     $result=mysqli_query($con,"UPDATE amenities_description SET amenities_id='".$id."',amenities_name='".$explode[$i]."' WHERE id='$row_id1[$i]'");    
		     }
		 }   
	}
	if( $exe ) {				
			$_SESSION[$tablename.'succ_msg'] = 'Amenities updated successfully';
	}else{				
			$_SESSION[$tablename.'err_msg'] = 'Error occured';
	}
    header("Location:".$base_url.$viewurl."");
}	  
	  
if(isset($_GET['id'])){
$display       = "Update";
$sql="SELECT * from ".$tablename." WHERE id = '".$_GET['id']."'";    
$result = $con->query($sql);
$row=mysqli_fetch_array($result);
$id            = $row['id']; 
$service_id    = $row['service_id'];
$subservice_id = $row['subservice_id'];
$amenities_title = $row['amenities_title'];
$status        = $row['status'];     
}else{
$id            = ""; 
$service_id    = "";
$subservice_id    = "";
$amenities_title = "";
$status        = "";
$display = "Add";
}	  
$sql_rslt = "SELECT id,service_name from services WHERE type='enquiry' and status='1'";
$result_rslt = $con->query($sql_rslt);

$sql_child = "SELECT id,service_name from subservices WHERE service_id='$service_id' and status='1'";
$result_child = $con->query($sql_child);
?>
<?php require_once('includes/header.php'); ?>
<?php require_once('includes/menu.php'); ?>
<!------ Include the above in your HEAD tag ---------->
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
      <!-- Content Header (Page header) -->
            <section class="content-header">                               
                    <ol class="breadcrumb">
                        <li><a href="<?php echo $base_url;?>"><i class="fa fa-dashboard"></i>Home</a></li>                    
                        <li class="active"><a href="amenities.php">Amenities</a></li>           
                    </ol>                
            </section>
<form id="serviceData" method="POST" enctype="multipart/form-data">
  <center><div class="message"></div></center>
    <div class="row setup-content">
      <div class="col-xs-6 col-md-offset-3">
        <div class="col-md-12">
            <center><h3>Amenities</h3></center>
              <div class="form-group">
                <label class="control-label">Choose Service</label>
                <input type="hidden" name="id" value="<?php echo $id;?>">
               	<select name="service_id" id="service_id" class="form-control select2" required autofocus>
				<option value="" disabled selected>Choose</option>
				<?php if ($result_rslt->num_rows > 0) {
                while($row = $result_rslt->fetch_assoc()) {?>
				<option value="<?php echo $row['id'];?>" <?php if($service_id==$row['id']){ echo "selected";}?>><?php echo $row['service_name'];?></option>
				<?php }}?>
				</select>
              </div> 
			   <div class="form-group">
                <label class="control-label">Choose Child Service</label> 
               	<select name="subservice_id" id="subservice_id" class="form-control select2">
				<option value="" disabled selected>Choose</option>
				<?php if ($result_child->num_rows > 0) {
                while($row = $result_child->fetch_assoc()) {?>
				<option value="<?php echo $row['id'];?>" <?php if($subservice_id==$row['id']){ echo "selected";}?>><?php echo $row['service_name'];?></option>
				<?php }}?>
				</select>
              </div> 
              <div class="form-group">
                <label class="control-label">Title</label> 
               	<input type="text" name="amenities_title" class="form-control" value="<?php echo $amenities_title;?>">
              </div> 
              <div class="add_more">
              <?php if(isset($_GET['id'])){
              $amenities=$con->query("SELECT * from amenities_description WHERE amenities_id='$id'");
              $s=0;
              while($row=mysqli_fetch_array($amenities)){?>
			  <div class="add_more1">
              <div class="form-group">
                <label class="control-label">Amenities Name</label>   
                <input type="hidden" name="row_id[]" value="<?php echo $row['id'];?>">
                 <div class="row">
                 <div class="col-lg-10 col-md-10">     
                <input  id="amenities_name" name="amenities_name[]" value="<?php echo $row['amenities_name']; ?>" type="text" class="form-control" placeholder="Amenities Name"  required/>
                </div>
                <div class="col-lg-2 col-md-2">
                    <?php if($s==0){?>
					<a href="javascript:void(0)" id="amenities_data" class="btn btn-primary pull-right">Add</a>
					<?php } else {?>
					<a href="javascript:void(0)" id="<?php echo $row['id'];?>" class="btn btn-danger pull-right removerow">Remove</a>
					<?php }?>
				</div>
				</div>
              </div>  
              </div>
              <?php $s++;}}else{?>
              <div class="add_more1">
              <div class="form-group">
                <label class="control-label">Amenities Name</label>    
                 <div class="row">
                 <div class="col-lg-10 col-md-10">     
                <input  id="amenities_name" name="amenities_name[]" value="" type="text" class="form-control" placeholder="Amenities Name"  required/>
                </div>
                <div class="col-lg-2 col-md-2">
						<a href="javascript:void(0)" id="amenities_data" class="btn btn-primary pull-right">Add</a>
				</div>
				</div>
              </div>  
              </div>
              <?php }?>
              </div>
              <div class="form-group">
                <label class="control-label">Status</label>
                 <select name="status" class="form-control select2" required>
				 <option value="" disabled selected>Choose</option>
				 <option value="1" <?php if($status=='1'){ echo "selected";}?>>Active</option>
				 <option value="0" <?php if($status=='0'){ echo "selected";}?>>Inactive</option>
				 </select>
              </div>  
			  <div class="form-group">
			   <a href="<?php echo $base_url.$viewurl;?>" class="btn btn-primary btn-lg pull-left" >Back</a>
            <button class="btn btn-success btn-lg pull-right" type="submit" name="<?php echo $display;?>"><?php echo $display;?></button>
			</div>
        </div>
      </div>
    </div>
  </form>
</div>

<script src="js/common.js"></script>  
<script src="<?php echo $base_url;?>js/jquery-3.4.1.min.js"></script>
<script src="<?php echo $base_url;?>js/jquery.validate.min.js"></script>
<script src="<?php echo $base_url;?>js/additional-methods.min.js"></script>
<script type="text/javascript">
$(function(){
       $("#serviceData").validate({
          rules: {
               amenities_name: 'required',
               amenities_title: 'required',
			   service_id: 'required',
			   subservice_id: 'required',
               status:'required',  
           },
            messages: {
              
            },
            submitHandler: function(form) {
              form.submit();
           }
    });        
});
$(document).ready( function() {		
		$("#service_id").on('change',function(e) {
		  e.preventDefault();
		  var id=$(this).val();
           $.ajax({
           type: "POST",
           url: "subcategory.php",
           data:{id:id},
           cache: false,
           success: function(data){
            $("#subservice_id").html(data);
            }   
           });               
		    
		});
});
$(document).ready(function () {     
    $("#amenities_data").on("click", function () {
		var length=$(".add_more1").length+1;
        $(".add_more").append('<div class="add_more1"><div class="form-group"><label class="control-label">Amenities Name</label><input type="hidden" name="row_id[]" value="0"><div class="row"><div class="col-lg-10 col-md-10"><input id="amenities_name" name="amenities_name[]" value="" type="text" class="form-control" placeholder="Amenities Name"  required/></div><div class="col-lg-2 col-md-2"><a href="javascript:void(0)" class="btn btn-danger pull-right removerow">Remove</a></div></div></div></div>');  
    });
});
$(document).on('click', '.removerow', function() {
$(this).closest('.add_more1').remove(); 
});

$(document).on('click', '.removerow', function() {
		   var id=$(this).attr("id");
		   if(id!=""){
	       var table="amenities_description";		  
           $.ajax({
           type: "POST",
           url: "deleteamenities.php",
           data:{id:id,table:table},
           success: function(data){
          $(this).closest('.add_more1').remove(); 
            }   
           });  
		}
});
</script>
<?php require_once('includes/footer.php'); ?>