<?php
include '../includes/db.php';
include 'header.php';

/* DELETE */
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];

    $old = $conn->query("SELECT logo FROM clients WHERE id=$id")->fetch_assoc();
    if ($old && file_exists("Images/Clients/".$old['logo'])) {
        unlink("Images/Clients/".$old['logo']);
    }

    $conn->query("DELETE FROM clients WHERE id=$id");
    header("Location: clientadminpage.php");
    exit;
}

/* ADD */
if (isset($_POST['upload'])) {
    $name = $_POST['name'];
    $logo = time().'_'.$_FILES['logo']['name'];
    $target = "Images/Clients/".$logo;

    if (move_uploaded_file($_FILES['logo']['tmp_name'], $target)) {
        $conn->query("INSERT INTO clients (name, logo) VALUES ('$name','$logo')");
    }
    header("Location: clientadminpage.php");
    exit;
}

/* UPDATE */
if (isset($_POST['update'])) {
    $id = (int)$_POST['id'];
    $name = $_POST['name'];

    if (!empty($_FILES['logo']['name'])) {
        $old = $conn->query("SELECT logo FROM clients WHERE id=$id")->fetch_assoc();
        if ($old && file_exists("Images/Clients/".$old['logo'])) {
            unlink("Images/Clients/".$old['logo']);
        }

        $logo = time().'_'.$_FILES['logo']['name'];
        move_uploaded_file($_FILES['logo']['tmp_name'], "Images/Clients/".$logo);
        $conn->query("UPDATE clients SET name='$name', logo='$logo' WHERE id=$id");
    } else {
        $conn->query("UPDATE clients SET name='$name' WHERE id=$id");
    }

    header("Location: clientadminpage.php");
    exit;
}

/* EDIT FETCH */
$edit = null;
if (isset($_GET['edit'])) {
    $id = (int)$_GET['edit'];
    $edit = $conn->query("SELECT * FROM clients WHERE id=$id")->fetch_assoc();
}
?>

<style>
.container { max-width: 700px; margin:auto; background:#fff; padding:25px; border-radius:10px; }
input, button { width:100%; padding:10px; margin:10px 0; }
button { background:#007bff; color:white; border:none; border-radius:6px; }
button:hover { background:#0056b3; }
table { width:100%; border-collapse:collapse; margin-top:30px; }
th, td { padding:10px; border-bottom:1px solid #ddd; }
th { background:#1a237e; color:white; }
.actions a { margin-right:10px; text-decoration:none; font-weight:bold; }
</style>

<div class="container">

<h2><?= $edit ? 'Edit Client' : 'Add Client' ?></h2>

<form method="POST" enctype="multipart/form-data">
    <?php if ($edit): ?>
        <input type="hidden" name="id" value="<?= $edit['id'] ?>">
    <?php endif; ?>

    <input type="text" name="name" placeholder="Client Name" required
           value="<?= $edit['name'] ?? '' ?>">

    <input type="file" name="logo">

    <button type="submit" name="<?= $edit ? 'update' : 'upload' ?>">
        <?= $edit ? 'Update Client' : 'Upload Client' ?>
    </button>

    <?php if ($edit): ?>
        <a href="clientadminpage.php">Cancel Edit</a>
    <?php endif; ?>
</form>

<table>
<tr>
    <th>Logo</th>
    <th>Name</th>
    <th>Actions</th>
</tr>

<?php
$result = $conn->query("SELECT * FROM clients ORDER BY id DESC");
while ($row = $result->fetch_assoc()):
?>
<tr>
    <td>
        <img src="Images/Clients/<?= $row['logo'] ?>" width="70">
    </td>
    <td><?= htmlspecialchars($row['name']) ?></td>
    <td class="actions">
        <a href="?edit=<?= $row['id'] ?>">✏️ Edit</a>
        <a href="?delete=<?= $row['id'] ?>"
           onclick="return confirm('Delete this client?')">🗑 Delete</a>
    </td>
</tr>
<?php endwhile; ?>
</table>

</div>

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