<?php
require_once("header.php");
require_once("menu.php");


if(isset($_REQUEST['edit']))
{
    $service_id = base64_decode($_REQUEST['edit']);
    $ex = $obj->get_service_id($service_id);
    $row = mysqli_fetch_array($ex);
}
if(isset($_POST['update']))
{   
    $update_service['service_id'] = $_POST['service_id'];
    $update_service['service_name'] = $_POST['service_name'];
	 
        if(!empty($_FILES['service_banner']['name']))
        {      
            $server_url = '//'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
            $upload_dir = '../uploads/service/';      
            $tempname = $_FILES['service_banner']['tmp_name'];
            $upload_name = $upload_dir.$_FILES['service_banner']['name'];
            move_uploaded_file($tempname,$upload_name);
            $update_service['service_banner'] = $server_url."/".$upload_name;
        }
        else
        {
            $update_service['service_banner'] =$_POST['service_banner1'];
        }
     
    $ex = $obj->update_service($update_service);
    echo '<script>window.location="service.php"</script>';
}

if(isset($_POST['submit']))
{
   
    $service_info['service_url'] = preg_replace('/[^a-z0-9-]+/', '-', trim(strtolower($_POST['service_name'])));
	$service_info['service_name'] = $_POST['service_name'];
//	$service_info['service_url'] = php_slug($_POST['service_name']);
	$server_url = '//'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
    $upload_dir = '../uploads/service/';
        
        if(!empty($_FILES['service_banner']['name']))
        {            
            $tempname = $_FILES['service_banner']['tmp_name'];
            $upload_name = $upload_dir.$_FILES['service_banner']['name'];
            move_uploaded_file($tempname,$upload_name);
            $service_info['service_banner'] = $server_url."/".$upload_name;
        }
	
	
  echo  $ex = $obj->insert_service($service_info);
    echo '<script>window.location="service.php"</script>';
}

?>

<div id="layoutSidenav_content">
<main class="mt-4">
    <div class="container-fluid">
        
        <div class="card mb-4">
            <div class="card-header">
                <i class="fas fa-check-square mr-1"></i>Service Menu
                <a href="service.php" class="btn btn-primary btn-sm float-right">Back</a>
            </div>
            <div class="card-body">
                <form method="post" name="frm" id="frm" enctype="multipart/form-data">
                   <input type="hidden" name="service_id" value="<?php echo $service_id;?>">
                    
                    
					
					<div class="form-group">
                        <label>Service Menu Name</label>
                        <input type="text" class="form-control" name="service_name" required value="<?php echo $row['service_name'];?>">
                    </div>
					
					 <div class="form-group">
                        <label>Banner </label><br>
						
                       <input type="file" class="" name="service_banner" value="<?php echo $row['service_banner'];?>">
                       <input type="hidden" name="service_banner1" value="<?php echo $row['service_banner'];?>">
                       <img src="<?php echo $row['service_banner'];?>"  style="width: 100px;">
                     </div> 
					 
					
					
                    <?php if(isset($_REQUEST['edit']))
                    { ?> 
                        <button type="submit" class="btn btn-primary" name="update">Update</button>
                    <?php } 
                    else
                    { ?>
                        <button type="submit" class="btn btn-primary" name="submit">Submit</button>
                    <?php } ?>
                </form> 
            </div>
        </div>
    </div>
</main>

<script>
    var url = "service.php";
</script>

<?php require_once("footer.php");?>

