<?php
require_once __DIR__ . '/../includes/auth.php';
requireLogin();
requirePermission('companies.manage');

require_once __DIR__ . '/../config/database.php';

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $company_name = trim($_POST['company_name'] ?? '');
    $company_type = trim($_POST['company_type'] ?? '');
    $contact_person = trim($_POST['contact_person'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $province = trim($_POST['province'] ?? '');
    $city = trim($_POST['city'] ?? '');
    $status = trim($_POST['status'] ?? 'ACTIVE');

    if ($company_name === '' || $company_type === '') {
        $error = "Company Name and Type are required.";
    } else {
        $stmt = $pdo->prepare("
            INSERT INTO companies
            (company_name, company_type, contact_person, email, phone, province, city, status)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?)
        ");
        $stmt->execute([$company_name, $company_type, $contact_person, $email, $phone, $province, $city, $status]);

        header("Location: companies.php");
        exit;
    }
}

require_once __DIR__ . '/../includes/header.php';
require_once __DIR__ . '/../includes/sidebar.php';
?>

<div class="main-content">

    <div class="page-header">
        <h2>Add Company</h2>
        <p class="page-subtitle">Create a new company record for installers, distributors or clients.</p>
    </div>

    <div class="breadcrumbs" style="font-size:13px;color:#6b7280;margin-bottom:14px;">
        Dashboard › Companies › <span style="color:var(--edm-orange);font-weight:600;">Add Company</span>
    </div>

    <?php if ($error): ?>
        <div class="table-wrap" style="padding:14px;border-left:4px solid #dc2626;color:#991b1b;margin-bottom:14px;">
            <?= htmlspecialchars($error) ?>
        </div>
    <?php endif; ?>

    <div class="table-wrap" style="padding:20px;max-width:720px;">
        <form method="POST" style="display:grid;grid-template-columns:1fr 1fr;gap:14px;">

            <div style="grid-column:1/3;">
                <label style="font-size:12px;color:#6b7280;">Company Name *</label>
                <input name="company_name" required style="width:100%;padding:10px;border:1px solid #e5e7eb;border-radius:10px;">
            </div>

            <div>
                <label style="font-size:12px;color:#6b7280;">Company Type *</label>
                <select name="company_type" required style="width:100%;padding:10px;border:1px solid #e5e7eb;border-radius:10px;">
                    <option value="">Select</option>
                    <option value="INSTALLER">Installer</option>
                    <option value="DISTRIBUTOR">Distributor</option>
                    <option value="CLIENT">Client</option>
                </select>
            </div>

            <div>
                <label style="font-size:12px;color:#6b7280;">Status</label>
                <select name="status" style="width:100%;padding:10px;border:1px solid #e5e7eb;border-radius:10px;">
                    <option value="ACTIVE">Active</option>
                    <option value="LEAD">Lead</option>
                    <option value="SUSPENDED">Suspended</option>
                    <option value="ARCHIVED">Archived</option>
                </select>
            </div>

            <div style="grid-column:1/3;">
                <label style="font-size:12px;color:#6b7280;">Contact Person</label>
                <input name="contact_person" style="width:100%;padding:10px;border:1px solid #e5e7eb;border-radius:10px;">
            </div>

            <div>
                <label style="font-size:12px;color:#6b7280;">Email</label>
                <input type="email" name="email" style="width:100%;padding:10px;border:1px solid #e5e7eb;border-radius:10px;">
            </div>

            <div>
                <label style="font-size:12px;color:#6b7280;">Phone</label>
                <input name="phone" style="width:100%;padding:10px;border:1px solid #e5e7eb;border-radius:10px;">
            </div>

            <div>
                <label style="font-size:12px;color:#6b7280;">Province</label>
                <input name="province" style="width:100%;padding:10px;border:1px solid #e5e7eb;border-radius:10px;">
            </div>

            <div>
                <label style="font-size:12px;color:#6b7280;">City</label>
                <input name="city" style="width:100%;padding:10px;border:1px solid #e5e7eb;border-radius:10px;">
            </div>

            <div style="grid-column:1/3;display:flex;gap:10px;justify-content:flex-end;margin-top:8px;">
                <a href="companies.php" class="action-btn gray" style="text-decoration:none;">Cancel</a>
                <button type="submit" class="action-btn" style="background:var(--edm-orange);color:#fff;border:none;padding:10px 14px;border-radius:10px;font-weight:700;cursor:pointer;">
                    Save Company
                </button>
            </div>
        </form>
    </div>

</div>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>