<?php
$pageTitle = 'Add Service';
require_once dirname(__DIR__) . '/includes/header.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title  = trim($_POST['title'] ?? '');
    $icon   = trim($_POST['icon_class'] ?? '');
    $short  = trim($_POST['short_desc'] ?? '');
    $full   = $_POST['full_desc'] ?? '';
    $status = $_POST['status'] ?? 'active';
    $sort   = (int)($_POST['sort_order'] ?? 0);
    $img    = handleUpload('image', 'services') ?? trim($_POST['image_url'] ?? '');

    if ($title) {
        $st = db()->prepare("INSERT INTO services (title,icon_class,short_desc,full_desc,image,sort_order,status) VALUES (?,?,?,?,?,?,?)");
        $st->bind_param('ssssssi', $title, $icon, $short, $full, $img, $sort, $status);
        $st->execute();
        flash('Service added successfully!', 'success');
        redirect(ADMIN_URL . '/pages/services.php');
    } else { $error = 'Title is required.'; }
}
?>
<div class="page-header">
    <a href="services.php" class="btn btn-secondary btn-sm"><i class="fas fa-arrow-left"></i></a>
    <h1>Add Service</h1>
</div>
<div class="card">
    <div class="card-header"><i class="fas fa-concierge-bell" style="color:var(--brand)"></i><h2>New Service</h2></div>
    <div class="card-body">
        <?php if (!empty($error)): ?><div class="flash flash-error"><i class="fas fa-exclamation-circle"></i><?=e($error)?></div><?php endif; ?>
        <form method="POST" enctype="multipart/form-data">
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Service Title *</label>
                    <input type="text" name="title" class="form-control" placeholder="e.g. UK Student Visa" value="<?=e($_POST['title']??'')?>" required>
                </div>
                <div class="form-group">
                    <label class="form-label">Icon Class <span style="color:var(--muted);font-weight:400">(FontAwesome)</span></label>
                    <input type="text" name="icon_class" class="form-control" placeholder="fas fa-plane" value="<?=e($_POST['icon_class']??'')?>">
                    <div class="form-hint">e.g. fas fa-graduation-cap, fas fa-globe</div>
                </div>
            </div>
            <div class="form-group">
                <label class="form-label">Short Description</label>
                <textarea name="short_desc" class="form-control" rows="3" placeholder="Brief overview shown on cards..."><?=e($_POST['short_desc']??'')?></textarea>
            </div>
            <div class="form-group">
                <label class="form-label">Full Description</label>
                <textarea name="full_desc" id="editor" class="form-control" rows="8"><?=e($_POST['full_desc']??'')?></textarea>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Upload Image</label>
                    <input type="file" name="image" class="form-control" accept="image/*" onchange="previewImg(this)">
                    <img id="imgPreview" class="img-preview" style="margin-top:8px;display:none">
                </div>
                <div class="form-group">
                    <label class="form-label">Or Image URL</label>
                    <input type="text" name="image_url" class="form-control" placeholder="https://..." value="<?=e($_POST['image_url']??'')?>">
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Status</label>
                    <select name="status" class="form-select">
                        <option value="active">Active</option>
                        <option value="inactive">Inactive</option>
                    </select>
                </div>
                <div class="form-group">
                    <label class="form-label">Sort Order</label>
                    <input type="number" name="sort_order" class="form-control" value="0" min="0">
                </div>
            </div>
            <div style="display:flex;gap:10px;margin-top:8px">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Save Service</button>
                <a href="services.php" class="btn btn-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>
<script>
function previewImg(i){const r=new FileReader();r.onload=e=>{const p=document.getElementById('imgPreview');p.src=e.target.result;p.style.display='block'};r.readAsDataURL(i.files[0]);}
</script>
<?php require_once dirname(__DIR__) . '/includes/footer.php'; ?>
