<?php
require_once 'functions.php';

$error_message = '';
$success_message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = sanitizeInput($_POST['name']);
    $address = sanitizeInput($_POST['address']);
    $phone = sanitizeInput($_POST['phone']);
    $email = sanitizeInput($_POST['email']);
    
    // Validation
    $validation_error = validateRequired([
        'Наименование/Име' => $name
    ]);
    
    if ($validation_error) {
        $error_message = $validation_error;
    } elseif (!empty($email) && !validateEmail($email)) {
        $error_message = "Невалиден имейл адрес.";
    } elseif (!empty($phone) && !validatePhone($phone)) {
        $error_message = "Невалиден телефонен номер.";
    } else {
        // Add client
        if (addClient($name, $address, $phone, $email)) {
            $success_message = "Клиентът беше добавен успешно.";
			
			header('Location: clients.php'); 
			
            // Clear form
            $name = $address = $phone = $email = '';
        } else {
            $error_message = "Грешка при добавяне на клиента.";
        }
    }
}
?>

<!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;
        }
 
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #333;
        }
        input[type="text"], input[type="email"], textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            box-sizing: border-box;
        }
        textarea {
            height: 80px;
            resize: vertical;
        }
        .required {
            color: red;
        }
        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            text-decoration: none;
            display: inline-block;
            margin-right: 10px;
        }
        .btn-primary {
            background-color: #007bff;
            color: white;
        }
        .btn-primary:hover {
            background-color: #0056b3;
        }
        .btn-secondary {
            background-color: #6c757d;
            color: white;
        }
        .btn-secondary:hover {
            background-color: #545b62;
        }
        .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;
            }
            .container {
                padding: 15px;
            }
        }
    </style>
</head>
<body>
    <div class="container">

  <?php
  include_once 'header.php';
?>


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

        <?php if ($error_message): ?>
            <div class="alert alert-error"><?php echo $error_message; ?></div>
        <?php endif; ?>
</br>
</br>
</br>
        <form method="post">
            <div class="form-group">
                <label for="name">Имена: <span class="required">*</span></label>
                <input type="text" id="name" name="name" value="<?php echo isset($name) ? htmlspecialchars($name) : ''; ?>" required>
            </div>

            <div class="form-group">
                <label for="address">Адрес: <span class="required">*</span></label>
                <textarea id="address" name="address"><?php echo isset($address) ? htmlspecialchars($address) : ''; ?></textarea>
            </div>

            <div class="form-group">
                <label for="phone">Телефон: <span class="required">*</span></label>
                <input type="text" id="phone" name="phone" value="<?php echo isset($phone) ? htmlspecialchars($phone) : ''; ?>" placeholder="+359 888 123 456" required>
            </div>

            <div class="form-group">
                <label for="email">Имейл:</label>
                <input type="email" id="email" name="email" value="<?php echo isset($email) ? htmlspecialchars($email) : ''; ?>" placeholder="example@email.com">
            </div>

            <button type="submit" class="btn btn-primary">Добави</button>
            <a href="clients.php" class="btn btn-secondary">Отказ</a>
        </form>
    </div>
</body>
</html>

