<?php include 'views/layout/header.php'; ?>
<nav class="navbar navbar-dark bg-dark mb-4">
  <div class="container-fluid">
    <span class="navbar-brand mb-0 h1">Admin Panel</span>
    <a href="<?php echo BASE_URL; ?>?page=auth&action=logout" class="btn btn-outline-light btn-sm">Logout</a>
  </div>
</nav>

<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">Edit User</h5>
                <a href="<?php echo BASE_URL; ?>?page=admin&action=dashboard" class="btn btn-secondary btn-sm">Back</a>
            </div>
            <div class="card-body">
                <form action="<?php echo BASE_URL; ?>?page=admin&action=edit_user&id=<?php echo $user['id']; ?>" method="POST" enctype="multipart/form-data">
                    <h6 class="border-bottom pb-2 mb-3">User Details</h6>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label>Full Name</label>
                            <input type="text" name="name" class="form-control" value="<?php echo htmlspecialchars($user['name']); ?>" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label>Email</label>
                            <input type="email" name="email" class="form-control" value="<?php echo htmlspecialchars($user['email']); ?>" required>
                        </div>
                    </div>
                    <div class="row">
                         <div class="col-md-6 mb-3">
                            <label>Mobile Number</label>
                            <input type="text" name="mobile" class="form-control" value="<?php echo htmlspecialchars($user['mobile']); ?>" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label>Password (Leave blank to keep current)</label>
                            <input type="password" name="password" class="form-control">
                        </div>
                    </div>

                    <h6 class="border-bottom pb-2 mb-3 mt-3">WhatsApp Configuration</h6>
                     <div class="row">
                        <div class="col-md-6 mb-3">
                            <label>WhatsApp API URL</label>
                            <input type="url" name="wa_url" class="form-control" value="<?php echo htmlspecialchars($api['wa_url']); ?>" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label>WhatsApp API Key</label>
                            <input type="text" name="wa_apikey" class="form-control" value="<?php echo htmlspecialchars($api['wa_apikey']); ?>" required>
                        </div>
                    </div>

                    <h6 class="border-bottom pb-2 mb-3 mt-3">SMS Configuration</h6>
                    <div class="row">
                         <div class="col-md-6 mb-3">
                            <label>SMS API URL (Get Method)</label>
                            <input type="url" name="sms_url" class="form-control" value="<?php echo htmlspecialchars($api['sms_api_url']); ?>" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label>SMS User/Username</label>
                            <input type="text" name="sms_user" class="form-control" value="<?php echo htmlspecialchars($api['sms_user']); ?>" required>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label>SMS Password</label>
                            <input type="text" name="sms_password" class="form-control" value="<?php echo htmlspecialchars($api['sms_password']); ?>" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label>SMS Sender ID</label>
                            <input type="text" name="sms_sender_id" class="form-control" value="<?php echo htmlspecialchars($api['sms_sender_id']); ?>" required>
                        </div>
                    </div>

                    <h6 class="border-bottom pb-2 mb-3 mt-3">Template Greeting & Style</h6>
                    <div class="mb-3">
                        <label>Default Greeting Text (Use {NAME} for placeholder)</label>
                        <input type="text" name="greeting_text" class="form-control" value="<?php echo htmlspecialchars($template['greeting_text'] ?? 'Happy Birthday {NAME}!'); ?>" required>
                    </div>

                    <div class="row">
                        <div class="col-md-3 mb-3">
                            <label>Position X (%)</label>
                            <input type="number" name="pos_x" class="form-control" value="<?php echo $template['pos_x'] ?? 50; ?>" min="0" max="100">
                            <small class="text-muted">Percentage from Left (0-100)</small>
                        </div>
                        <div class="col-md-3 mb-3">
                            <label>Position Y (%)</label>
                            <input type="number" name="pos_y" class="form-control" value="<?php echo $template['pos_y'] ?? 90; ?>" min="0" max="100">
                            <small class="text-muted">Percentage from Top (0-100)</small>
                        </div>
                        <div class="col-md-3 mb-3">
                            <label>Font Size</label>
                            <input type="number" name="font_size" class="form-control" value="<?php echo $template['font_size'] ?? 20; ?>">
                        </div>
                        <div class="col-md-3 mb-3">
                            <label>Font Color</label>
                            <input type="color" name="font_color" class="form-control" value="<?php echo $template['font_color'] ?? '#000000'; ?>">
                        </div>
                    </div>

                    <h6 class="border-bottom pb-2 mb-3 mt-3">Template Image</h6>
                    <div class="mb-3">
                        <label>Upload New Template (Leave blank to keep current)</label>
                        <input type="file" name="template_image" class="form-control" accept="image/*">
                        <?php if (!empty($template['image_path'])): ?>
                            <div class="mt-2">
                                <small>Current Template:</small><br>
                                <img src="<?php echo BASE_URL . $template['image_path']; ?>" style="max-height: 100px; border: 1px solid #ddd; padding: 2px;">
                            </div>
                        <?php endif; ?>
                    </div>

                    <button type="submit" class="btn btn-primary w-100">Update User</button>
                </form>
            </div>
        </div>
    </div>
</div>
<?php include 'views/layout/footer.php'; ?>
