<?php
require_once 'functions.php';

// Handle delete action
if (isset($_POST['delete_client'])) {
    $client_id = $_POST['client_id'];
    if (deleteClient($client_id)) {
        $success_message = "Клиентът беше изтрит успешно.";
    } else {
        $error_message = "Грешка при изтриване на клиента.";
    }
}

// Get all clients
$clients = getAllClients();
?>

<!DOCTYPE html>
<html lang="bg">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Клиенти - Протоколи за влагане на материали</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .nav {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
            flex-wrap: wrap;
        }
        .nav a {
            padding: 10px 15px;
            background-color: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            font-size: 14px;
        }
        .nav a:hover {
            background-color: #0056b3;
        }
        .nav a.active {
            background-color: #28a745;
        }
        .add-button {
            background-color: #28a745;
            color: white;
            padding: 10px 20px;
            text-decoration: none;
            border-radius: 4px;
            margin-bottom: 20px;
            display: inline-block;
        }
        .add-button:hover {
            background-color: #218838;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .action-buttons {
            display: flex;
            gap: 5px;
        }
        .btn {
            padding: 5px 10px;
            text-decoration: none;
            border-radius: 3px;
            font-size: 12px;
            border: none;
            cursor: pointer;
        }
        .btn-edit {
            background-color: #ffc107;
            color: #212529;
        }
        .btn-delete {
            background-color: #dc3545;
            color: white;
        }
        .btn:hover {
            opacity: 0.8;
        }
        .alert {
            padding: 15px;
            margin-bottom: 20px;
            border: 1px solid transparent;
            border-radius: 4px;
        }
        .alert-success {
            color: #155724;
            background-color: #d4edda;
            border-color: #c3e6cb;
        }
        .alert-error {
            color: #721c24;
            background-color: #f8d7da;
            border-color: #f5c6cb;
        }
        @media (max-width: 768px) {
            .nav {
                flex-direction: column;
            }
            table {
                font-size: 14px;
            }
            .action-buttons {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Клиенти</h1>
        
	   <?php
	   include_once 'header.php';
	   ?>

        <?php if (isset($success_message)): ?>
            <div class="alert alert-success"><?php echo $success_message; ?></div>
        <?php endif; ?>

        <?php if (isset($error_message)): ?>
            <div class="alert alert-error"><?php echo $error_message; ?></div>
        <?php endif; ?>

        <a href="add_client.php" class="add-button">Създай нов клиент</a>

        <?php if (empty($clients)): ?>
            <p>Няма създадени клиенти.</p>
        <?php else: ?>
  <div class="container-fluid">
    <div class="card mt-1">
        <h3 class="card-header bg-primary text-white"></h3>
        <div class="card-body">
            <div class="table-responsive">
                <table id="patientTable" class="table table-striped table-bordered table-hover w-100">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>Имена</th>
                        <th>Адрес</th>
                        <th>Телефон</th>
                        <th>Имейл</th>
						<th>Действия</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($clients as $client): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($client['id']); ?></td>
                            <td><?php echo htmlspecialchars($client['name']); ?></td>
                            <td><?php echo htmlspecialchars($client['address']); ?></td>
                            <td><?php echo htmlspecialchars($client['phone']); ?></td>
                            <td><?php echo htmlspecialchars($client['email']); ?></td>
                            <td>
                                <div class="action-buttons">
                                    <a href="edit_client.php?id=<?php echo $client['id']; ?>" class="btn btn-edit">Редактирай</a>
                                    <form method="post" style="display: inline;" onsubmit="return confirm('Сигурни ли сте, че искате да изтриете този клиент?');">
                                        <input type="hidden" name="client_id" value="<?php echo $client['id']; ?>">
                                        <button type="submit" name="delete_client" class="btn btn-delete">Изтрий</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>

</body>
</html>

