<?php
/**
 * Edit Deployed From Entity
 * Dozar & Tripper Management System
 */

require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../includes/auth.php';

if (!hasRole('admin')) {
    header('Location: ' . SITE_URL . '/deployed/');
    exit;
}

$pdo = getPDO();
$error = '';

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if (!$id) {
    $_SESSION['error_message'] = 'Invalid entity ID';
    header('Location: ' . SITE_URL . '/deployed/');
    exit;
}

$stmt = $pdo->prepare("SELECT * FROM deployed_from WHERE id = ?");
$stmt->execute([$id]);
$entity = $stmt->fetch();

if (!$entity) {
    $_SESSION['error_message'] = 'Entity not found';
    header('Location: ' . SITE_URL . '/deployed/');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $entity_name  = sanitize($_POST['entity_name']  ?? '');
    $entity_type  = sanitize($_POST['entity_type']  ?? '');
    $contact_info = sanitize($_POST['contact_info'] ?? '');
    $is_active    = isset($_POST['is_active']) ? 1 : 0;

    if (!$entity_name) {
        $error = 'Entity Name is required.';
    } else {
        $stmt = $pdo->prepare(
            "UPDATE deployed_from SET entity_name = ?, entity_type = ?, contact_info = ?, is_active = ? WHERE id = ?"
        );
        $stmt->execute([$entity_name, $entity_type, $contact_info, $is_active, $id]);
        $_SESSION['success_message'] = 'Entity updated successfully!';
        header('Location: ' . SITE_URL . '/deployed/');
        exit;
    }
}

$pageTitle = 'Edit Deployed Entity';
?>
<?php include __DIR__ . '/../includes/header.php'; ?>

<div class="row mb-4">
    <div class="col-12 d-flex justify-content-between align-items-center">
        <h2><i class="fas fa-building me-2"></i> Edit Deployed Entity</h2>
        <a href="index.php" class="btn btn-outline-secondary rounded-pill">
            <i class="fas fa-arrow-left me-1"></i> Back
        </a>
    </div>
</div>

<?php if ($error): ?>
<div class="alert alert-danger rounded-3"><?php echo $error; ?></div>
<?php endif; ?>

<div class="card shadow-sm" style="border-radius:16px; border:none;">
    <div class="card-body p-4">
        <form method="POST">
            <div class="row g-3">
                <div class="col-md-6">
                    <label class="form-label fw-bold">Entity Name *</label>
                    <input type="text" name="entity_name" class="form-control"
                           value="<?php echo htmlspecialchars($entity['entity_name']); ?>" required>
                </div>

                <div class="col-md-3">
                    <label class="form-label fw-bold">Type</label>
                    <select name="entity_type" class="form-select">
                        <option value="kmc"     <?php echo $entity['entity_type'] === 'kmc'     ? 'selected' : ''; ?>>KMC</option>
                        <option value="private" <?php echo $entity['entity_type'] === 'private' ? 'selected' : ''; ?>>Private</option>
                        <option value="other"   <?php echo $entity['entity_type'] === 'other'   ? 'selected' : ''; ?>>Other</option>
                    </select>
                </div>

                <div class="col-md-3 d-flex align-items-end">
                    <div class="form-check form-switch ps-0 w-100">
                        <label class="form-label fw-bold d-block">Active</label>
                        <input class="form-check-input ms-0" type="checkbox" name="is_active" id="is_active"
                               style="width:3rem;height:1.5rem;"
                               <?php echo $entity['is_active'] ? 'checked' : ''; ?>>
                    </div>
                </div>

                <div class="col-12">
                    <label class="form-label fw-bold">Contact Info</label>
                    <input type="text" name="contact_info" class="form-control"
                           value="<?php echo htmlspecialchars($entity['contact_info'] ?? ''); ?>"
                           placeholder="Phone, email, or other contact details">
                </div>
            </div>

            <div class="d-flex gap-2 mt-4">
                <button type="submit" class="btn btn-primary px-4 rounded-pill shadow-sm">
                    <i class="fas fa-save me-1"></i> Save Changes
                </button>
                <a href="index.php" class="btn btn-light px-4 rounded-pill border">Cancel</a>
            </div>
        </form>
    </div>
</div>

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