<?php require_once('includes/check-login.php');
require_once('includes/config.php');
$tablename="servicename";
$viewurl="servicename.php";
if(isset($_POST['Add'])){
	    if(!file_exists(UPLOAD_SERVICEIMG)) {
          mkdir(UPLOAD_SERVICEIMG, 0777, true);
        }			
        $file_name=$_FILES["service_image"]["name"];
		$file_tmp=$_FILES["service_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); 
		
		$file_name=$_FILES["banner_image"]["name"];
		$file_tmp=$_FILES["banner_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));
		$banner_image=str_replace(' ','',$file_name); 
        $created=date("Y-m-d H:i:s");
        $query = "insert into ".$tablename." SET service_id='".$_POST['service_id']."',subservice_id='".$_POST['subservice_id']."',service_name='".$_POST['service_name']."',service_image='".$image."',banner_image='".$banner_image."',status='".$_POST['status']."',created='".$created."'";
		$exe = mysqli_query( $con, $query );		
		if( $exe ) {				
			$_SESSION[$tablename.'succ_msg'] = 'Service Name added successfully';
		}else{				
			$_SESSION[$tablename.'err_msg'] = 'Error occured';
		}
        header("Location:".$base_url.$viewurl."");
}
if(isset($_POST['Update'])){	
        $id=$_POST['id']; 
        $vendor_id=$_SESSION['userid'];
		if(isset($_FILES['service_image']["name"]) && $_FILES['service_image']["name"]!=null && $_FILES['service_image']["name"] !=""){
	    if(!file_exists(UPLOAD_SERVICEIMG)) {
          mkdir(UPLOAD_SERVICEIMG, 0777, true);
        }
		$file_name=$_FILES["service_image"]["name"];
		$file_tmp=$_FILES["service_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); 	
        $modified=date("Y-m-d H:i:s");
        $query = "update ".$tablename." SET service_image='".$image."' WHERE id='".$_POST['id']."'";
		$exe = mysqli_query( $con, $query );		
}
if(isset($_FILES['banner_image']["name"]) && $_FILES['banner_image']["name"]!=null && $_FILES['banner_image']["name"] !=""){
	    if(!file_exists(UPLOAD_SERVICEIMG)) {
          mkdir(UPLOAD_SERVICEIMG, 0777, true);
        }
		$file_name=$_FILES["banner_image"]["name"]; 
		$file_tmp=$_FILES["banner_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); 	
        $modified=date("Y-m-d H:i:s");
        $query = "update ".$tablename." SET banner_image='".$image."' WHERE id='".$_POST['id']."'";
		$exe = mysqli_query( $con, $query );		
}
$modified=date("Y-m-d H:i:s");
$query = "update ".$tablename." SET service_id='".$_POST['service_id']."',subservice_id='".$_POST['subservice_id']."',service_name='".$_POST['service_name']."',status='".$_POST['status']."',modified='".$modified."' WHERE id='".$_POST['id']."'";
	$exe = mysqli_query( $con, $query );		
	if( $exe ) {				
			$_SESSION[$tablename.'succ_msg'] = 'Service Name updated successfully';
	}else{				
			$_SESSION[$tablename.'err_msg'] = 'Error occured';
	}
    header("Location:".$base_url.$viewurl."");
}	  
	  
if(isset($_GET['id'])){
$display       = "Update";
$sql = "SELECT id,service_name,service_id,subservice_id,service_image,status,banner_image 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'];
$service_name  = $row['service_name'];
$service_image = $row['service_image'];
$banner_image = $row['banner_image'];
$status        = $row['status'];     
}else{
$id            = ""; 
$service_id    = "";
$subservice_id    = "";
$service_name  = "";
$service_image = "";
$banner_image = "";
$status        = "";
$display = "Add";
}	  
$sql_rslt = "SELECT id,service_name from services WHERE type='addtocart' 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="services.php">Service Name</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>Service Name</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">Service Name</label>               
                <input  id="service_name" name="service_name" value="<?php echo $service_name; ?>" type="text" class="form-control" placeholder="Service Name"  />
              </div>   
              <div class="form-group">
                <label class="control-label">Banner Image</label>
                <input  type="file" id="banner_image" name="banner_image" value="<?php echo $banner_image; ?>" class="form-control"/>
				<?php if(isset($_GET['id'])){if(@is_array(getimagesize(UPLOAD_SERVICEIMG.$banner_image))){?>
				<img id="imageshow1" class="uploadimage" src="<?php echo $base_url.UPLOAD_SERVICEIMG.$banner_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">Thumb Image</label>
                <input  type="file" id="image" name="service_image" value="<?php echo $service_image; ?>" class="form-control"/>
				<?php if(isset($_GET['id'])){if(@is_array(getimagesize(UPLOAD_SERVICEIMG.$service_image))){?>
				<img id="imageshow" class="uploadimage" src="<?php echo $base_url.UPLOAD_SERVICEIMG.$service_image;?>" alt="image" style="display:initial;"/>
				<?php }} else {?>
			    <img id="imageshow" class="uploadimage" alt="image"/>
				<?php }?>
		    	<small style="color:red">[Image Size 70px x 70px]</small>
              </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_name: 'required',
			   service_id: 'required',
			   subservice_id: 'required',
			   service_image: {extension: "jpg|jpeg|png"},
			   banner_image: {extension: "jpg|jpeg|png"},
               status:'required',  
           },
            messages: {
              service_image: {extension: "Please enter a value with a valid extension(jpg,jpeg,png)"},  
              banner_image: {extension: "Please enter a value with a valid extension(jpg,jpeg,png)"},
            },
            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);
});
function readURL1(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function(e) {
      $('#imageshow1').attr('src', e.target.result);
	  $('#imageshow1').css('display','initial');
    }
    reader.readAsDataURL(input.files[0]);
  }
}
$("#banner_image").change(function() {
  readURL1(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);
            }   
           });               
		    
		});
});
</script>
<?php require_once('includes/footer.php'); ?>