<?php
// Include DB connection
include 'ominterior_db.php';

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$member = null;

// Fetch team member
if ($id > 0) {
    $stmt = $conn->prepare("SELECT * FROM team_members WHERE id = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    $member = $result->fetch_assoc();
    $stmt->close();
}

// Delete only image
if (isset($_GET['delete_image']) && $member && !empty($member['photo'])) {
    $imagePath = 'uploads/team_members/' . $member['photo'];
    if (file_exists($imagePath)) {
        unlink($imagePath);
    }

    $stmt = $conn->prepare("UPDATE team_members SET photo = '' WHERE id = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $stmt->close();

    // Refresh page
    header("Location: edit-team-member.php?id=$id");
    exit;
}

// Form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'];
    $position = $_POST['position'];
    $uploadDir = 'uploads/team_members/';
    $photo = $member['photo'];

    // Handle image upload
    if (!empty($_FILES['photo']['name'])) {
        $imageName = basename($_FILES['photo']['name']);
        $targetPath = $uploadDir . $imageName;
        $imageFileType = strtolower(pathinfo($targetPath, PATHINFO_EXTENSION));
        $allowedTypes = ['jpg', 'jpeg', 'png', 'gif'];

        if (in_array($imageFileType, $allowedTypes)) {
            if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);
            if (!empty($photo) && file_exists($uploadDir . $photo)) {
                unlink($uploadDir . $photo);
            }
            if (move_uploaded_file($_FILES['photo']['tmp_name'], $targetPath)) {
                $photo = $imageName;
            }
        }
    }

    // Update DB
    $stmt = $conn->prepare("UPDATE team_members SET name = ?, position = ?, photo = ? WHERE id = ?");
    $stmt->bind_param("sssi", $name, $position, $photo, $id);
    $stmt->execute();
    $stmt->close();
    
    $member['name'] = $name;
    $member['position'] = $position;
    $member['photo'] = $photo;

    header("Location:gallery.php");
}

$conn->close();
?>



<!doctype html>
<html lang="en" dir="ltr">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

<link rel="icon" href="assets/images/black-logo.png" type="image/x-icon"/>

<title>OM INTERIOR - Dashborad</title>

<!-- Bootstrap Core and vandor -->
<link rel="stylesheet" href="assets/plugins/bootstrap/css/bootstrap.min.css" />

<!-- Plugins css -->
<link rel="stylesheet" href="assets/plugins/charts-c3/c3.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<!-- Core css -->
<link rel="stylesheet" href="assets/css/main2.css"/>
<link rel="stylesheet" href="assets/css/theme1.css"/>

<style type="text/css">
.img-wrapper {
    position: relative;
    display: inline-block;
}
.img-wrapper img {
    max-width: 200px;
    height: auto;
    border: 1px solid #ccc;
    border-radius: 5px;
}
.img-wrapper .delete-icon {
    position: absolute;
    top: -10px;
    right: -10px;
    background: red;
    color: white;
    font-weight: bold;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    text-align: center;
    line-height: 24px;
    cursor: pointer;
    text-decoration: none;
}
.img-wrapper .delete-icon:hover {
    background: darkred;
}
</style>

</head>

<body class="font-montserrat">
<!-- Page Loader -->
<div class="page-loader-wrapper">
    <div class="loader">
    </div>
</div>

<div id="main_content">

<?php include 'haderTop_logo.php'?>
    
<?php include 'rightside_menu.php'?>

<?php include 'leftside_menu.php' ?>


    <div class="page">
        <div id="page_top" class="section-body top_dark">
            <div class="container-fluid">
                <div class="page-header">
                    <div class="left">
                        <a href="javascript:void(0)" class="icon menu_toggle mr-3"><i class="fa  fa-align-left"></i></a>
                        <h1 class="page-title">Project</h1>                        
                    </div>
                    <div class="right">
                        <?php include 'dropdown_flex_menu.php' ?>
                    </div>
                </div>
            </div>
        </div>

       <div class="container mt-5">
            <h2>Edit Team Member</h2>

            <?php if ($member): ?>
                <form action="edit-team-member.php?id=<?= $member['id'] ?>" method="POST" enctype="multipart/form-data">
                    <div class="mb-3">
                        <label class="form-label">Full Name</label>
                        <input type="text" name="name" class="form-control" value="<?= htmlspecialchars($member['name']) ?>" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Position</label>
                        <input type="text" name="position" class="form-control" value="<?= htmlspecialchars($member['position']) ?>" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Change Image</label>
                        <input type="file" name="photo" class="form-control">
                        <small class="text-muted">Leave empty to keep current image.</small>
                    </div>

                    <?php if (!empty($member['photo']) && file_exists('uploads/team_members/' . $member['photo'])): ?>
                        <div class="mb-3">
                            <label class="form-label">Current Image</label><br>
                            <div class="img-wrapper">
                                <img src="uploads/team_members/<?= $member['photo'] ?>" alt="Current Image">
                                <a href="edit-team-member.php?id=<?= $member['id'] ?>&delete_image=1" class="delete-icon" onclick="return confirm('Are you sure you want to delete this image?')">×</a>
                            </div>
                        </div>
                    <?php endif; ?>

                    <button type="submit" class="btn btn-primary">Update</button>
                    <a href="team_list.php" class="btn btn-secondary">Cancel</a>
                </form>
            <?php else: ?>
                <div class="alert alert-warning">Team member not found.</div>
            <?php endif; ?>
        </div>

                        
        <div class="section-body">
            <footer class="footer">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-12 col-sm-12 text-md-right">
                            <ul class="list-inline mb-0">
                                <li class="list-inline-item"><a href="doc/index.html">OM INTERIOR</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </footer>
        </div>
    </div>    
</div>

<script src="assets/bundles/lib.vendor.bundle.js"></script>

<script src="assets/bundles/apexcharts.bundle.js"></script>
<script src="assets/bundles/counterup.bundle.js"></script>
<script src="assets/bundles/knobjs.bundle.js"></script>
<script src="assets/bundles/c3.bundle.js"></script>

<script src="assets/js/core.js"></script>
<script src="assets/js/page/project-index.js"></script>
</body>
</html>
