<?php
session_start();
require_once "config/database.php";

if (!isset($_SESSION['user_id']) || ($_SESSION['role'] ?? '') !== 'admin') {
    header("Location: /dms/login.php");
    exit;
}

$database = new Database();
$db = $database->getConnection();

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $company_name = trim($_POST['company_name'] ?? '');
    $company_code = trim($_POST['company_code'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $phone = trim($_POST['phone'] ?? '');

    if ($company_name === '') {
        $error = "Company name is required.";
    } else {
        $stmt = $db->prepare("
            INSERT INTO companies (company_name, company_code, email, phone)
            VALUES (:company_name, :company_code, :email, :phone)
        ");

        $stmt->bindValue(':company_name', $company_name, PDO::PARAM_STR);
        $stmt->bindValue(':company_code', $company_code, PDO::PARAM_STR);
        $stmt->bindValue(':email', $email, PDO::PARAM_STR);
        $stmt->bindValue(':phone', $phone, PDO::PARAM_STR);
        $stmt->execute();

        header("Location: /dms/companies.php");
        exit;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create Company</title>
    <link rel="stylesheet" href="/dms/assets/css/app.css">
</head>
<body>
<div class="layout">
    <?php include 'nav.php'; ?>

    <main class="main">
        <div class="hero">
            <div class="hero-top">
                <div>
                    <h1>Create Company</h1>
                    <p>Add a new company workspace.</p>
                </div>
                <div class="welcome-badge">
                    <?php echo htmlspecialchars($_SESSION['full_name'] ?? 'User'); ?>
                </div>
            </div>
        </div>

        <div class="card">
            <?php if ($error !== ''): ?>
                <div class="card" style="background:#fff1f1;color:#b42318;margin-bottom:12px;">
                    <?php echo htmlspecialchars($error); ?>
                </div>
            <?php endif; ?>

            <form method="POST" class="form-grid">
                <div class="form-group">
                    <label>Company Name</label>
                    <input type="text" name="company_name" required>
                </div>

                <div class="form-group">
                    <label>Company Code</label>
                    <input type="text" name="company_code">
                </div>

                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email">
                </div>

                <div class="form-group">
                    <label>Phone</label>
                    <input type="text" name="phone">
                </div>

                <div style="display:flex; gap:10px; flex-wrap:wrap; margin-top:12px;">
                    <button type="submit" class="btn btn-primary">Create Company</button>
                    <a href="/dms/companies.php" class="btn btn-secondary">Cancel</a>
                </div>
            </form>
        </div>
    </main>
</div>
</body>
</html>