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

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

function esc($v){ return htmlspecialchars((string)($v ?? ''), ENT_QUOTES, 'UTF-8'); }

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) die("Missing company id.");

$cStmt = $pdo->prepare("SELECT * FROM companies WHERE id=?");
$cStmt->execute([$id]);
$company = $cStmt->fetch(PDO::FETCH_ASSOC);
if (!$company) die("Company not found.");

$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 {
        $upd = $pdo->prepare("
            UPDATE companies
            SET company_name=?, company_type=?, contact_person=?, email=?, phone=?, province=?, city=?, status=?
            WHERE id=?
        ");
        $upd->execute([$company_name, $company_type, $contact_person, $email, $phone, $province, $city, $status, $id]);

        header("Location: view_company.php?id=" . $id);
        exit;
    }
}

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

<div class="main-content">

    <div class="page-header">
        <h2>Edit Company</h2>
        <p class="page-subtitle">Update company profile and operational status.</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;">Edit</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 value="<?= esc($company['company_name']) ?>"
                       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;">
                    <?php $ct = (string)($company['company_type'] ?? ''); ?>
                    <option value="INSTALLER" <?= $ct==='INSTALLER'?'selected':'' ?>>Installer</option>
                    <option value="DISTRIBUTOR" <?= $ct==='DISTRIBUTOR'?'selected':'' ?>>Distributor</option>
                    <option value="CLIENT" <?= $ct==='CLIENT'?'selected':'' ?>>Client</option>
                </select>
            </div>

            <div>
                <label style="font-size:12px;color:#6b7280;">Status</label>
                <?php $st = (string)($company['status'] ?? 'ACTIVE'); ?>
                <select name="status" style="width:100%;padding:10px;border:1px solid #e5e7eb;border-radius:10px;">
                    <option value="ACTIVE" <?= $st==='ACTIVE'?'selected':'' ?>>Active</option>
                    <option value="LEAD" <?= $st==='LEAD'?'selected':'' ?>>Lead</option>
                    <option value="SUSPENDED" <?= $st==='SUSPENDED'?'selected':'' ?>>Suspended</option>
                    <option value="ARCHIVED" <?= $st==='ARCHIVED'?'selected':'' ?>>Archived</option>
                </select>
            </div>

            <div style="grid-column:1/3;">
                <label style="font-size:12px;color:#6b7280;">Contact Person</label>
                <input name="contact_person" value="<?= esc($company['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" value="<?= esc($company['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" value="<?= esc($company['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" value="<?= esc($company['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" value="<?= esc($company['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="view_company.php?id=<?= (int)$company['id'] ?>" 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 Changes
                </button>
            </div>
        </form>
    </div>

</div>

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