<?php
require_once 'config.php';
require_once 'functions.php';

// Get database connection
$pdo = getDBConnection();

// Обработка на формата
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $company_name = $_POST['company_name'];
	$mol = $_POST['mol'];
    $bulstat = $_POST['bulstat'];
    $bank_account = $_POST['bank_account'];
    $address = $_POST['address'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    
    // Проверяваме дали има съществуващи данни
    $stmt = $pdo->query("SELECT COUNT(*) FROM company_data");
    $count = $stmt->fetchColumn();
    
    if ($count > 0) {
        // Актуализираме съществуващите данни
        $stmt = $pdo->prepare("UPDATE company_data SET 
            company_name = ?, mol = ?, bulstat = ?, bank_account = ?, 
            address = ?, email = ?, phone = ? 
            WHERE id = (SELECT id FROM (SELECT id FROM company_data LIMIT 1) as temp)");
        $stmt->execute([$company_name, $mol, $bulstat, $bank_account, $address, $email, $phone]);
    } else {
        // Вмъкваме нови данни
        $stmt = $pdo->prepare("INSERT INTO company_data 
            (company_name, mol, bulstat, bank_account, address, email, phone) 
            VALUES (?, ?, ?, ?, ?, ?, ?)");
        $stmt->execute([$company_name, $mol, $bulstat, $bank_account, $address, $email, $phone]);
    }
    
    $success_message = "Данните за фирмата са запазени успешно!";
}

// Зареждаме съществуващите данни
$company_data = null;
$stmt = $pdo->query("SELECT * FROM company_data LIMIT 1");
$company_data = $stmt->fetch(PDO::FETCH_ASSOC);
?>

<!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: 800px;
            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';
   ?>

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

            <h2 style="text-color: red;">Данни за фирмата</h2>
            <p style="margin-bottom: 2rem; color: red;">
                Въведете данните за вашата фирма. Тези данни ще се използват автоматично като "Изпълнител" във всички протоколи и оферти.
            </p>

            <form method="POST">
               <div class="form-group">
                    <label for="company_name">Наименование на фирмата <span style="color: red;">*</span></label>
                    <input type="text" id="company_name" name="company_name"  value="<?php echo htmlspecialchars($company_data['company_name']); ?>" required>
                </div>

			 <div class="form-group">
                    <label for="mol">Управител на фирмата <span style="color: red;">*</span></label>
                    <input type="text" id="mol" name="mol"  value="<?php echo htmlspecialchars($company_data['mol']); ?>" required>
                </div>

                <div class="form-group">
                    <div class="form-group">
                        <label for="bulstat">БУЛСТАТ</label>
                        <input type="text" id="bulstat" name="bulstat" value="<?php echo htmlspecialchars($company_data['bulstat']); ?>">
                    </div>

                    <div class="form-group">
                        <label for="bank_account">Банкова сметка</label>
                        <input type="text" id="bank_account" name="bank_account"  value="<?php echo htmlspecialchars($company_data['bank_account']); ?>">
                    </div>
                </div>

               <div class="form-group">
                    <label for="address">Седалище (адрес) <span style="color: red;">*</span></label>
                    <textarea id="address" name="address"><?php echo htmlspecialchars($company_data['address']); ?></textarea>
                </div>

                <div class="form-row">
                    <div class="form-group">
                        <label for="email">Имейл адрес <span style="color: red;">*</span></label>
                        <input type="email" id="email" name="email"  value="<?php echo htmlspecialchars($company_data['email']); ?>">
                    </div>

                    <div class="form-group">
                        <label for="phone">Телефон за контакт <span style="color: red;">*</span></label>
                        <input type="text" id="phone" name="phone"  value="<?php echo htmlspecialchars($company_data['phone']); ?>">
                    </div>
                </div>

                <button type="submit" class="btn">Запази данните</button>
            </form>
        </div>
    </div>
</body>
</html>

