<?php require_once('includes/check-login.php');
require_once('includes/config.php');
$tablename="services";
$viewurl="services.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");
        $query = "insert into ".$tablename." SET  service_name='".$_POST['service_name']."',service_hint='".$_POST['service_hint']."',image='".$image."',status='".$_POST['status']."',type='".$_POST['type']."',created='".$created."'";
		$exe = mysqli_query( $con, $query );		
		if( $exe ) {				
			$_SESSION[$tablename.'succ_msg'] = '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); 	
        $modified=date("Y-m-d H:i:s");
        $query = "update ".$tablename." SET  service_name='".$_POST['service_name']."',service_hint='".$_POST['service_hint']."',image='".$image."',status='".$_POST['status']."',type='".$_POST['type']."',modified='".$modified."' WHERE id='".$_POST['id']."'";
		$exe = mysqli_query( $con, $query );		
		if( $exe ) {				
			$_SESSION[$tablename.'succ_msg'] = 'Service updated successfully';
		}else{				
			$_SESSION[$tablename.'err_msg'] = 'Error occured';
		}
        header("Location:".$base_url.$viewurl."");
}else{
	$modified=date("Y-m-d H:i:s");
     $query = "update ".$tablename." SET  service_name='".$_POST['service_name']."',service_hint='".$_POST['service_hint']."',type='".$_POST['type']."',status='".$_POST['status']."',modified='".$modified."' WHERE id='".$_POST['id']."'";
	$exe = mysqli_query( $con, $query );		
	if( $exe ) {				
			$_SESSION[$tablename.'succ_msg'] = 'Service 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_hint,image,status,type from ".$tablename." WHERE id = '".$_GET['id']."'";
$result = $con->query($sql);
$row=mysqli_fetch_array($result);
$id            = $row['id']; 
$service_name  = $row['service_name'];
$service_hint  = $row['service_hint'];
$type          = $row['type'];
$image         = $row['image'];
$status        = $row['status'];     
}else{
$id            = ""; 
$service_name  = "";
$service_hint  = "";
$type          = "";
$image         = "";
$status        = "";
$display = "Add";
}	  
?>
<?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">Services</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>Services</h3></center>
              <div class="form-group">
                <label class="control-label">Service Name</label>
                <input type="hidden" name="id" value="<?php echo $id;?>">
                <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">Service Hint</label>
                <input id="service_hint" name="service_hint" value="<?php echo $service_hint; ?>" type="text" class="form-control" placeholder="Service Hint" />
              </div>
              <div class="form-group">
                <label class="control-label">Choose Type</label>
                 <select name="type" class="form-control select2" required>
				 <option value="" disabled selected>Choose</option>
				 <option value="addtocart" <?php if($type=='addtocart'){ echo "selected";}?>>Addtocart</option>
				 <option value="enquiry" <?php if($type=='enquiry'){ echo "selected";}?>>Enquiry Form</option>
				 </select>
              </div>  
              <div class="form-group">
                <label class="control-label">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="imageshow" class="uploadimage" src="<?php echo $base_url.UPLOAD_SERVICEIMG.$image;?>" alt="image" style="display:initial;"/>
				<?php }} else {?>
			    <img id="imageshow" class="uploadimage" alt="image"/>
				<?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>  
			   <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>
  </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: {
               service_name: 'required',
			   service_hint: 'required',
			   image: {extension: "jpg|jpeg|png"},
               status:'required',  
           },
            messages: {
              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);
});
</script>
<?php require_once('includes/footer.php'); ?>