<?php
session_start();
include 'include/config.php';

// Redirect if session id is not set
if(!isset($_SESSION['id'])) {
    echo "<script>window.location.href = 'index.php'</script>";
    exit;
}

$idddddd = isset($_GET['iddd']) ? $_GET['iddd'] : null;
$sell = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM `employee` WHERE `id`='$idddddd'"));

include 'include/head.php';
?>

<?php
if(isset($_POST['submittt'])){
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $skin_color = $_POST['skin_color'];
    $height = $_POST['height'];
    $weight = $_POST['weight'];
    $shop_id = $_POST['shop_id'];
    $unique_id = $_POST['unique_id'];
    $charges = $_POST['charges'];
    $about_us = $_POST['about_us'];
  
    $updateQuery = "UPDATE `employee` SET `name`='$name',`phone`='$phone',`skin_color`='$skin_color',`height`='$height',`weight`='$weight',`unique_id`='$unique_id',
            `shop_id`='$shop_id',`charges`='$charges', `about_us`='$about_us'";
            
    // Check if a new profile image is uploaded
    if($_FILES['profile']['name']) {
        $image = $_FILES['profile']['name'];
        $tmp_name = $_FILES['profile']['tmp_name'];
        $image_url = 'upload/image/' . $image;
        
        // Append profile update to the query
        $updateQuery .= ", `profile`='$image_url'";
    }
   
    $updateQuery .= " WHERE `id`='$idddddd'";
    
    $update = mysqli_query($conn, $updateQuery);
    
    if($update){
        // If a new image is uploaded, move it to the upload directory
        if($_FILES['profile']['name']) {
            move_uploaded_file($tmp_name, '../upload/image/'.$image);
        }
        
        echo "<h6> Employee Updated </h6>";
    } else {
        echo "<h6>Employee Not Updated</h6>";
    }
}
?>
<div class="container-fluid">
    <!-- Page title -->
    <div class="row">
        <div class="col-12">
            <div class="page-title-box">
                <div class="page-title-right">
                    <ol class="breadcrumb m-0">
                        <li class="breadcrumb-item"><a href="">Home</a></li>
                        <li class="breadcrumb-item active">Edit Employee</li>
                    </ol>
                </div>
                <h4 class="page-title">Edit Employee</h4>
            </div>
        </div>
    </div>
    <!-- End page title -->

    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <!-- Form start -->
                    <form action="" method="POST" enctype="multipart/form-data">
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="mb-3">
                                    <label for="example-select" class="form-label">Name</label>
                                    <input type="text" value="<?php echo $sell['name']; ?>" name="name" class="form-control" placeholder="Name">
                                </div>
                            </div>
                            <!-- Add more input fields for other employee details -->
                            
                             <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Phone</label>
                                                        <input type="text" value="<?php echo $sell['phone'];?>" name="phone" class="form-control" placeholder="category name">
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Skin Color</label>
                                                        <input type="text" value="<?php echo $sell['skin_color'];?>" name="skin_color" class="form-control" placeholder="category name">
                                                    </div>
                                                </div>
                                               
                                                     <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Height</label>
                                                        <input type="text" value="<?php echo $sell['height'];?>" name="height" class="form-control" placeholder="category name">
                                                    </div>
                                                </div>
                                                
                                               


                                                     <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Wight</label>
                                                        <input type="text" value="<?php echo $sell['weight'];?>" name="weight" class="form-control" placeholder="category name">
                                                    </div>
                                                </div>


                                                     <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Shop Id</label>
                                                        <input type="text" value="<?php echo $sell['shop_id'];?>" name="shop_id" class="form-control" placeholder="category name">
                                                    </div>
                                                </div>


                                                     <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Unique Id</label>
                                                        <input type="text" value="<?php echo $sell['unique_id'];?>" name="unique_id" class="form-control" placeholder="category name">
                                                    </div>
                                                </div>
                                                
                                                <div class="mb-3">
                                                        <label for="example-select" class="form-label">Charges</label>
                                                        <input type="text" value="<?php echo $sell['charges'];?>" name="charges" class="form-control" placeholder="category name">
                                                    </div>
                                                    
                                                     <div class="col-lg-12">
                                                    <!-- Date Range Picker With Times -->
                                                    <div class="mb-3">
                                                        <label for="example-select" class="form-label">Profile</label>
                                                        <input type="file" value="" name="profile" class="form-control" placeholder="category name">
                                                        <?php
                                                                 $fullImagePath = $sell['profile'];
                                                                 $prefixToRemove = 'upload/image/';

                                                                 $imageFilename = str_replace($prefixToRemove, '', $fullImagePath);
                                                                 $imageUrl = '../upload/image/' . $imageFilename;
                                                                 ?>
                                                                 <img src="<?= $imageUrl; ?>" style="width: 100px; height: 60px;">
                                                    </div>
                                                </div>
                                                
                                                <div class="mb-3">
                                                        <label for="example-select" class="form-label">About Us</label>
                                                        <input type="text" value="<?php echo $sell['about_us'];?>" name="about_us" class="form-control" placeholder="category name">
                                                    </div>
                                                </div>
                        </div>

                        <div class="row">
                            <div class="clearfix"></div>
                            <div class="col-lg-6">
                                <div class="mb-3">
                                    <input type="submit" name="submittt" class="btn btn-success" value="Submit">
                                </div>
                            </div>
                        </div>
                    </form>
                    <!-- Form end -->
                </div>
                <!-- end card-body -->
            </div>
            <!-- end card-->
        </div>
        <!-- end col -->
    </div>
    <!-- end row-->
</div>
<!-- container -->

<?php include 'include/footer.php'; ?>
