<?php require_once('includes/check-login.php'); ?>
<?php require_once('includes/config.php');
$tablename="amenities_service";
$viewurl="amenitiesservice.php";
if(isset($_POST['Add'])){
        if(!file_exists(UPLOAD_SERVICEIMG)) {
          mkdir(UPLOAD_SERVICEIMG, 0777, true);
        }			
        $file_name=$_FILES["image"]["name"];
		$file_tmp=$_FILES["image"]["tmp_name"];
		$actual_name = pathinfo($file_name,PATHINFO_FILENAME);
        $original_name = $actual_name;
        $extension = pathinfo($file_name, PATHINFO_EXTENSION);
        $j = 1;
        while(file_exists(UPLOAD_SERVICEIMG.$actual_name.".".$extension)){           
        $actual_name =$original_name.$j;
        $file_name = $actual_name.".".$extension;
        $j++;
        }         		
	    $d =move_uploaded_file($file_tmp,UPLOAD_SERVICEIMG.str_replace(' ','',$file_name));
		$image=str_replace(' ','',$file_name); 
		
        $created=date("Y-m-d H:i:s");
        $vendor_id=$_SESSION['userid'];
        $service_name=implode("*",$_POST['service_name']);
        $query = "insert into ".$tablename." SET vendor_id='".$vendor_id."',service_id='".$_POST['service_id']."',subservice_id='".$_POST['subservice_id']."',service_name='".$service_name."',title='".$_POST['title']."',month_price='".$_POST['month_price']."',deposit='".$_POST['deposit']."',image='".$image."',description='".$_POST['description']."',status='".$_POST['status']."',created='".$created."'";
		$exe = mysqli_query( $con, $query );
		$amenities_id=mysqli_insert_id($con);
		$title=isset($_POST['title1']) ? implode("*",$_POST['title1']) : '';
		$explode=explode("*",$title);
		$description=isset($_POST['description1']) ? implode("*",$_POST['description1']) : '';
		$description1=explode("*",$description);
		if(count($explode)>0){
		 for($i=0;$i<count($explode);$i++){
		     $result=mysqli_query($con,"INSERT INTO amenities_service_description SET amen_service_id='".$amenities_id."',title='".$explode[$i]."',description='".$description1[$i]."'");
		 }   
		}
		if( $exe ) {				
			$_SESSION[$tablename.'succ_msg'] = 'Amenities Service added successfully';
		}else{				
			$_SESSION[$tablename.'err_msg'] = 'Error occured';
		}
        header("Location:".$base_url.$viewurl."");
}
if(isset($_POST['Update'])){	
$id=$_POST['id']; 
if(isset($_FILES['image']["name"]) && $_FILES['image']["name"]!=null && $_FILES['image']["name"] !=""){
	    if(!file_exists(UPLOAD_SERVICEIMG)) {
          mkdir(UPLOAD_SERVICEIMG, 0777, true);
        }
		$file_name=$_FILES["image"]["name"]; 
		$file_tmp=$_FILES["image"]["tmp_name"];
		$actual_name = pathinfo($file_name,PATHINFO_FILENAME);
        $original_name = $actual_name;
        $extension = pathinfo($file_name, PATHINFO_EXTENSION);
        $j = 1;
        while(file_exists(UPLOAD_SERVICEIMG.$actual_name.".".$extension)){           
        $actual_name =$original_name.$j;
        $file_name = $actual_name.".".$extension;
        $j++;
        }         		
	    $d =move_uploaded_file($file_tmp,UPLOAD_SERVICEIMG.str_replace(' ','',$file_name));
		$image=str_replace(' ','',$file_name); 	
        $query = "update ".$tablename." SET image='".$image."' WHERE id='".$id."'";
		$exe = mysqli_query( $con, $query );		
}
$service_name=implode("*",$_POST['service_name']);
$modified=date("Y-m-d H:i:s");
$query = "update ".$tablename." SET service_id='".$_POST['service_id']."',subservice_id='".$_POST['subservice_id']."',service_name='".$service_name."',title='".$_POST['title']."',month_price='".$_POST['month_price']."',deposit='".$_POST['deposit']."',description='".$_POST['description']."',status='".$_POST['status']."',modified='".$modified."' WHERE id='".$id."'";
	$exe = mysqli_query( $con, $query );
	
	$title=isset($_POST['title1']) ? implode("*",$_POST['title1']) : '';
	$explode=explode("*",$title);
	$row_id=isset($_POST['row_id']) ? implode("*",$_POST['row_id']) : '';
	$row_id1=explode("*",$row_id);
	$description=isset($_POST['description1']) ? implode("*",$_POST['description1']) : '';
	$description1=explode("*",$description);
	if(count($row_id1)>0){
		 for($i=0;$i<count($row_id1);$i++){
		     if($row_id1[$i]==0 && $explode[$i]!=""){
		     $result=mysqli_query($con,"INSERT INTO amenities_service_description SET amen_service_id='".$id."',title='".$explode[$i]."',description='".$description1[$i]."'");
		     }else{
		     $result=mysqli_query($con,"UPDATE amenities_service_description SET amen_service_id='".$id."',title='".$explode[$i]."',description='".$description1[$i]."' WHERE id='$row_id1[$i]'");    
		     }
		 }   
	}
	if( $exe ) {				
			$_SESSION[$tablename.'succ_msg'] = 'Amenities Service updated successfully';
	}else{				
			$_SESSION[$tablename.'err_msg'] = 'Error occured';
	}
    header("Location:".$base_url.$viewurl."");
}	  
	  
if(isset($_GET['id'])){
$display       = "Update";
if($_SESSION['role_id']==1){
$sql = "SELECT * from ".$tablename." WHERE id = '".$_GET['id']."'";    
}else{
$sql = "SELECT * from ".$tablename." WHERE id = '".$_GET['id']."' and vendor_id = '".$_SESSION['userid']."'";
}
$result = $con->query($sql);
$row=mysqli_fetch_array($result);
$id            = $row['id']; 
$service_id    = $row['service_id'];
$subservice_id = $row['subservice_id'];
$service_name  = $row['service_name'];
$title         = $row['title'];
$month_price   = $row['month_price'];
$deposit       = $row['deposit'];
$image         = $row['image'];
$description   = $row['description'];
$status        = $row['status'];     
}else{
$id            = ""; 
$service_id    = "";
$subservice_id = "";
$service_name  = "";
$title         = "";
$month_price   = "";
$deposit       = "";
$image         = "";
$description   = "";
$status        = "";
$display = "Add";
if($_SESSION['role_id']==1){
header("Location:".$base_url.$viewurl."");    
}
}	  
$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'); ?>
<style>
.checkbox * {
  cursor: pointer;
}
.checkbox {
  padding-left: 20px;
  width: 20px;
  height: 20px;
  margin-top: 1px;
}

.checkbox label {
  display: inline-block;
  vertical-align: middle;
  position: relative;
  padding-left: 5px;
  white-space: nowrap;
}
.checkbox label::before {
  content: "";
  display: inline-block;
  position: absolute;
  width: 20px;
  height: 20px;
  left: 0;
  margin-left: -20px;
  border: 1px solid #555;
  border-radius: 3px;
  background-color: #fff;
  -webkit-transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
  -o-transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
  transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
}

.checkbox label::after {
  display: inline-block;
  position: absolute;
  width: 20px;
  height: 20px;
  left: 0;
  top: 2px;
  margin-left: -21px;
  padding-left: 3px;
  padding-top: 1px;
  font-size: 11px;
  color: #555555;
}

.checkbox input[type="checkbox"]{
  opacity: 0;
  z-index: 1;
  width: 150px;
  height: 20px;
  margin-left: -20px !important;
}


.checkbox input[type="checkbox"]:checked + label::after{
  font-family: "FontAwesome";
  content: "\f00c";
}


.checkbox.checkbox-inline {
  margin-top: 0;
}

.checkbox-red    input[type="checkbox"]:checked + label::after,
.checkbox-yellow input[type="checkbox"]:checked + label::after,
.checkbox-green  input[type="checkbox"]:checked + label::after,
.checkbox-blue   input[type="checkbox"]:checked + label::after,
.checkbox-black  input[type="checkbox"]:checked + label::after
{color: #fff;}

.checkbox-red input[type="checkbox"] + label::before
{background-color: #990000;  border-color: #990000;}

.checkbox-yellow input[type="checkbox"] + label::before
{background-color: #face00;  border-color: #face00;}

.checkbox-green input[type="checkbox"] + label::before
{background-color: #008000;  border-color: #008000;}

.checkbox-blue input[type="checkbox"] + label::before
{background-color: #428bca;  border-color: #428bca;}

.checkbox-black input[type="checkbox"] + label::before
{background-color: #000000;  border-color: #000000;}

input[type="checkbox"].styled:checked + label:after {
  font-family: 'FontAwesome';
  content: "\f00c";
}
</style>
<!------ 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="amenitiesservice.php">Service Price</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 Service Price</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" id="title" name="title" value="<?php echo $title; ?>" class="form-control"/>
              </div>
              <div class="form-group">
                <label class="control-label">Per Month price</label>
                <input  type="number" id="month_price" name="month_price" value="<?php echo $month_price; ?>" class="form-control"/>
              </div>
              <div class="form-group">
                <label class="control-label">Deposit</label>
                <input  type="number" id="deposit" name="deposit" value="<?php echo $deposit;?>" class="form-control"/>
              </div>
              <div class="form-group">
                <label class="control-label">Banner Image</label>
                <input  type="file" id="image" name="image" value="<?php echo $image; ?>" class="form-control"/>
				<?php if(isset($_GET['id'])){if(@is_array(getimagesize(UPLOAD_SERVICEIMG.$image))){?>
				<img id="imageshow1" class="uploadimage" src="<?php echo $base_url.UPLOAD_SERVICEIMG.$image;?>" alt="image" style="display:initial;"/>
				<?php }} else {?>
			    <img id="imageshow1" class="uploadimage" alt="image"/>
				<?php }?>
				<small style="color:red">[Image Size 300px x 100px]</small>
              </div>
              <div class="form-group">
                <label class="control-label">Service Type</label> 
                <div id="service_name">
                <?php if(isset($_GET['id'])){
                $explode=explode("*",$service_name);
                $sql1=$con->query("SELECT amenities_description.*,amenities.amenities_title FROM amenities INNER JOIN amenities_description ON  amenities.id=amenities_description.amenities_id WHERE amenities.service_id='$service_id' and amenities.subservice_id='$subservice_id' and amenities.status='1' order by amenities_description.amenities_id asc");
                $amen_id="";
                while($fetchrow1=mysqli_fetch_array($sql1)){
                $checked="";    
                if(in_array($fetchrow1['id'],$explode)){
                  $checked="checked";
                }
                $title="";
                if($amen_id!=$fetchrow1['amenities_id']){
                $title=$fetchrow1['amenities_title'];   
                }?>    
                <p><?php echo $title;?></p>
                <div class="col-xs-12 text-center">
                <div class="checkbox checkbox-circle checkbox-blue">
                <input type="checkbox" name="service_name[]" value="<?php echo $fetchrow1['id'];?>" <?php echo $checked;?>>
                <label><?php echo $fetchrow1['amenities_name'];?></label>
                </div>
                </div>
                <?php $amen_id=$fetchrow1['amenities_id'];}}?>
                </div>
              </div> 
               <div class="add_more">
               <p>Additional Requirement</p>       
              <?php if(isset($_GET['id'])){
              $amenities=$con->query("SELECT * from amenities_service_description WHERE amen_service_id='$id'");
              $s=0;
              if(mysqli_num_rows($amenities)>0){
              while($row=mysqli_fetch_array($amenities)){?>
			  <div class="add_more1">
              <div class="form-group">
                <input type="hidden" name="row_id[]" value="<?php echo $row['id'];?>">
                 <div class="row">
                 <div class="col-lg-5 col-md-5">     
                <input  id="title1" name="title1[]" value="<?php echo $row['title']; ?>" type="text" class="form-control" placeholder="Title"  required/>
                </div>
                <div class="col-lg-5 col-md-5">     
                <input  id="description1" name="description1[]" value="<?php echo $row['description']; ?>" type="text" class="form-control" placeholder="Description"  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">
                   <input type="hidden" name="row_id[]" value="0">
                 <div class="row">
                 <div class="col-lg-5 col-md-5">     
                <input  id="title1" name="title1[]" value="" type="text" class="form-control" placeholder="Ex : parking"/>
                </div>
                <div class="col-lg-5 col-md-5">     
                <input  id="description1" name="description1[]" value="" type="text" class="form-control" placeholder="1"/>
                </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 }}else{?>
              <div class="add_more1">
              <div class="form-group">
                 <div class="row">
                 <div class="col-lg-5 col-md-5">     
                <input  id="title1" name="title1[]" value="" type="text" class="form-control" placeholder="Ex : parking"/>
                </div>
                <div class="col-lg-5 col-md-5">     
                <input  id="description1" name="description1[]" value="" type="text" class="form-control" placeholder="1"/>
                </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>Description</label><br/>                        
                     <textarea id="description" name="description" rows="20" cols="200" class="form-control tinymce"><?php echo $description; ?></textarea>
               </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/tinymce/tinymce.min.js"></script>
<script>
    tinymce.init({ 
        selector:'.tinymce',
        branding: false 
    });
</script>    
<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: {
			   service_id: 'required',
			   subservice_id: 'required',
			   title: 'required',
               status:'required',  
           },
            messages: {
              
            },
            submitHandler: function(form) {
              form.submit();
           }
    });        
});
function readURL(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function(e) {
      $('#imageshow').attr('src', e.target.result);
	  $('#imageshow').css('display','initial');
    }
    reader.readAsDataURL(input.files[0]);
  }
}
$("#image").change(function() {
  readURL(this);
});
$(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() {		
		$("#subservice_id").on('change',function(e) {
		  e.preventDefault();
		  var service_id=$("#service_id").val();
		  var subservice_id=$(this).val();
           $.ajax({
           type: "POST",
           url: "amenities-ajax.php",
           data:{service_id:service_id,subservice_id:subservice_id},
           cache: false,
           success: function(data){
            $("#service_name").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"><input type="hidden" name="row_id[]" value="0"><div class="row"><div class="col-lg-5 col-md-5"><input id="title1" name="title1[]" value="" type="text" class="form-control" placeholder="Title"  /></div><div class="col-lg-5 col-md-5"><input  id="description1" name="description1[]" value="" type="text" class="form-control" placeholder="Description"/></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_service_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'); ?>