<?php
/**
 * pages/agency-form.php — Add / Edit agency (super_admin only)
 */
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/csrf.php';

require_role('super_admin');

$is_edit = isset($_GET['id']) && ctype_digit($_GET['id']);
$ag      = [];

if ($is_edit) {
    $stmt = db()->prepare('SELECT * FROM agencies WHERE id = ?');
    $stmt->execute([(int)$_GET['id']]);
    $ag = $stmt->fetch();
    if (!$ag) {
        set_flash('error', 'Agency not found.');
        redirect(base_url('pages/agencies.php'));
    }
}

$page_title  = $is_edit ? 'Edit Agency' : 'Add Agency';
$active_page = 'agencies';
require_once __DIR__ . '/../templates/head.php';
?>

<?php require_once __DIR__ . '/../templates/header.php'; ?>
<?php require_once __DIR__ . '/../templates/sidebar.php'; ?>

<div class="nxl-everything">
    <div class="nxl-container">
        <div class="main-content" style="padding-top: 80px;">

            <!-- Breadcrumb -->
            <nav aria-label="breadcrumb" class="mb-3">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="<?= base_url('pages/agencies.php') ?>">Agencies</a></li>
                    <li class="breadcrumb-item active"><?= $is_edit ? 'Edit' : 'Add' ?></li>
                </ol>
            </nav>

            <div class="page-header mb-4">
                <h4 class="fw-bold mb-0"><?= $is_edit ? 'Edit Agency' : 'Add New Agency' ?></h4>
            </div>

            <?php render_flash(); ?>

            <div class="card border-0 shadow-sm" style="max-width:720px;">
                <div class="card-body p-4">
                    <form method="POST" action="<?= base_url('actions/agency-save.php') ?>" novalidate>
                        <?= csrf_field() ?>
                        <?php if ($is_edit): ?>
                        <input type="hidden" name="id" value="<?= (int)$ag['id'] ?>">
                        <?php endif; ?>

                        <!-- Agency Name -->
                        <div class="mb-3">
                            <label class="form-label fw-semibold">Agency Name <span class="text-danger">*</span></label>
                            <input type="text" name="name" class="form-control"
                                   value="<?= e($ag['name'] ?? '') ?>"
                                   placeholder="e.g. Hartford Home Care" required maxlength="200">
                        </div>

                        <div class="row g-3 mb-3">
                            <!-- Contact Email -->
                            <div class="col-md-6">
                                <label class="form-label fw-semibold">Contact Email</label>
                                <input type="email" name="contact_email" class="form-control"
                                       value="<?= e($ag['contact_email'] ?? '') ?>"
                                       placeholder="admin@agency.org" maxlength="200">
                            </div>
                            <!-- Phone -->
                            <div class="col-md-6">
                                <label class="form-label fw-semibold">Phone</label>
                                <input type="tel" name="phone" class="form-control"
                                       value="<?= e($ag['phone'] ?? '') ?>"
                                       placeholder="(860) 555-0100" maxlength="30">
                            </div>
                        </div>

                        <!-- Address -->
                        <div class="mb-3">
                            <label class="form-label fw-semibold">Address</label>
                            <input type="text" name="address" class="form-control"
                                   value="<?= e($ag['address'] ?? '') ?>"
                                   placeholder="123 Main St, Hartford, CT 06103" maxlength="500">
                        </div>

                        <!-- Notes -->
                        <div class="mb-3">
                            <label class="form-label fw-semibold">Notes</label>
                            <textarea name="notes" class="form-control" rows="3"
                                      placeholder="Internal notes about this agency…"><?= e($ag['notes'] ?? '') ?></textarea>
                        </div>

                        <!-- Active toggle (edit only) -->
                        <?php if ($is_edit): ?>
                        <div class="mb-4">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="activeSwitch"
                                       name="active" value="1" <?= ($ag['active'] ?? 1) ? 'checked' : '' ?>>
                                <label class="form-check-label fw-semibold" for="activeSwitch">
                                    Agency is Active
                                </label>
                            </div>
                            <div class="form-text">Inactive agencies are hidden from agency-user dropdowns.</div>
                        </div>
                        <?php endif; ?>

                        <!-- Buttons -->
                        <div class="d-flex gap-2">
                            <button type="submit" class="btn btn-primary">
                                <?= $is_edit ? 'Save Changes' : 'Create Agency' ?>
                            </button>
                            <a href="<?= base_url('pages/agencies.php') ?>" class="btn btn-outline-secondary">Cancel</a>
                        </div>
                    </form>
                </div>
            </div>

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