<?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;
        }
        .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">

     <?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; ?>
</br>
</br>
</br>
        <a href="add_client.php" class="add-button">Нов клиент</a>

        <?php if (empty($clients)): ?>
            <p>Няма създадени клиенти.</p>
        <?php else: ?>
         </br>
		 </br>
		<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Търсене в клиенти..." class="search-input" autofocus>
</br>
		 <table id="myTable">
                <thead>
                    <tr>
                        <th>Клиент</th>
                        <th>Адрес</th>
                        <th>Телефон</th>
                         <th>Действия</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($clients as $client): ?>
                        <tr>
                          
                            <td><?php echo htmlspecialchars($client['name']); ?></td>
                            <td><?php echo htmlspecialchars($client['address']); ?></td>
                            <td><?php echo htmlspecialchars($client['phone']); ?></td>
                            <td>
                                <div class="action-buttons">
                                    <a href="client_dossier.php?id=<?php echo $client['id']; ?>"  onclick="window.open(this.href, '_blank', 'left=20,top=20,width=1000,height=600,toolbar=1,resizable=0'); return false;" class="btn" style="background-color: #17a2b8; color: white;">ДОСИЕ</a>
                                    <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>
	<script>
function myFunction() {
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }       
  }
}
</script>
</body>
</html>

