<?php
require_once __DIR__ . '/../../config/session.php';
requireAdmin();
require_once __DIR__ . '/../../config/config.php';
require_once __DIR__ . '/../../functions/helper.php';
require_once __DIR__ . '/../../functions/csrf.php';

$db = getDB();
$id = $_GET['id'] ?? null;

if (!$id) {
    header('Location: ./');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && verifyCSRFToken($_POST['csrf_token'] ?? '')) {
    $username = $_POST['username'];
    $nama_lengkap = $_POST['nama_lengkap'];
    $no_hp = $_POST['no_hp'];
    $role = $_POST['role'];
    $is_active = $_POST['is_active'];
    
    try {
        if(!empty($_POST['password'])) {
            $password = $_POST['password'];
            $stmt = $db->prepare("UPDATE users SET username=?, password=?, nama_lengkap=?, no_hp=?, role=?, is_active=? WHERE id=?");
            $stmt->execute([$username, $password, $nama_lengkap, $no_hp, $role, $is_active, $id]);
        } else {
            $stmt = $db->prepare("UPDATE users SET username=?, nama_lengkap=?, no_hp=?, role=?, is_active=? WHERE id=?");
            $stmt->execute([$username, $nama_lengkap, $no_hp, $role, $is_active, $id]);
        }
        setFlash('success', 'User diperbarui.');
        header('Location: ./');
        exit;
    } catch(PDOException $e) {
        setFlash('error', 'Error: ' . $e->getMessage());
    }
}

$stmt = $db->prepare("SELECT * FROM users WHERE id=?");
$stmt->execute([$id]);
$edit_data = $stmt->fetch();

if (!$edit_data) {
    setFlash('error', 'Data pengguna tidak ditemukan.');
    header('Location: ./');
    exit;
}

require_once '../../includes/header_admin.php';
?>

<div class="flex items-center gap-4 mb-6">
    <a href="./" class="w-10 h-10 bg-white border border-gray-200 rounded-xl flex items-center justify-center text-gray-500 hover:bg-gray-50 hover:text-primary transition-colors shadow-sm">
        <i class="fas fa-arrow-left"></i>
    </a>
    <div>
        <h1 class="text-2xl font-bold text-gray-800">Edit Pengguna</h1>
        <p class="text-gray-500 text-sm mt-1">Perbarui data atau password pengguna.</p>
    </div>
</div>

<div class="bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden">
    <div class="px-6 py-4 border-b border-gray-100 bg-gray-50/50 flex items-center gap-2">
        <i class="fas fa-user-edit text-yellow-500"></i>
        <h3 class="font-bold text-gray-800">Edit Data User: <?= escape($edit_data['username']) ?></h3>
    </div>
    <div class="p-6">
        <form action="" method="POST" class="space-y-4">
            <?= csrfField() ?>
            
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Username (Bisa NIK) <span class="text-red-500">*</span></label>
                <input type="text" name="username" class="w-full px-4 py-2.5 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm" value="<?= escape($edit_data['username']) ?>" required>
            </div>
            
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Nama Lengkap <span class="text-red-500">*</span></label>
                <input type="text" name="nama_lengkap" class="w-full px-4 py-2.5 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm" value="<?= escape($edit_data['nama_lengkap']) ?>" required>
            </div>
            
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">No. HP / WA</label>
                <input type="text" name="no_hp" class="w-full px-4 py-2.5 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm" value="<?= escape($edit_data['no_hp']) ?>">
            </div>
            
            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Role Akses</label>
                    <?php if ($edit_data['role'] === 'user'): ?>
                        <input type="hidden" name="role" value="user">
                        <div class="w-full px-4 py-2.5 border border-gray-200 rounded-xl text-sm bg-gray-50 text-gray-500 flex items-center gap-2">
                            <i class="fas fa-lock text-xs text-gray-400"></i>
                            User / Pasien
                        </div>
                        <p class="text-xs text-gray-400 mt-1">Akun pasien tidak dapat diubah rolenya.</p>
                    <?php else: ?>
                        <select name="role" class="w-full px-4 py-2.5 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm bg-white">
                            <option value="admin" selected>Admin / Bidan</option>
                        </select>
                    <?php endif; ?>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Status</label>
                    <select name="is_active" class="w-full px-4 py-2.5 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm bg-white">
                        <option value="1" <?= $edit_data['is_active']=='1'?'selected':'' ?>>Aktif</option>
                        <option value="0" <?= $edit_data['is_active']=='0'?'selected':'' ?>>Non-Aktif</option>
                    </select>
                </div>
            </div>
            
            <div class="border-t border-gray-100 pt-4 mt-2">
                <label class="block text-sm font-medium text-gray-700 mb-1">Setel Ulang Password</label>
                <input type="password" name="password" class="w-full px-4 py-2.5 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-primary text-sm" placeholder="Kosongkan jika tak diubah">
                <p class="text-xs text-gray-500 mt-1">Abaikan jika tidak ingin mengganti password pengguna.</p>
            </div>
            
            <div class="pt-4 flex items-center justify-end gap-3 border-t border-gray-100 mt-6">
                <a href="./" class="px-6 py-2.5 bg-gray-100 hover:bg-gray-200 text-gray-700 font-medium rounded-xl transition-colors">Batal</a>
                <button type="submit" class="px-6 py-2.5 bg-yellow-500 hover:bg-yellow-600 text-white font-bold rounded-xl shadow-lg shadow-yellow-500/30 transition-all transform hover:-translate-y-0.5 focus:ring-2 focus:ring-offset-2 focus:ring-yellow-500 flex items-center gap-2">
                    <i class="fas fa-save"></i> Perbarui Data
                </button>
            </div>
        </form>
    </div>
</div>

<?php require_once '../../includes/footer_admin.php'; ?>
