<?php
/**
 * Profil Saya
 * Koperasi Management System
 */

$pageTitle = 'Profil Saya';
$pageSubtitle = 'Lihat dan perbarui informasi profil Anda';

require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/config/auth.php';
requireLogin();

$db = getDB();
$userId = $_SESSION['user_id'];

// Handle Actions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $namaLengkap = sanitize($_POST['nama_lengkap']);
    $password = $_POST['password'] ?? '';
    
    // Update Name
    $stmt = $db->prepare("UPDATE users SET nama_lengkap = ? WHERE id = ?");
    $stmt->execute([$namaLengkap, $userId]);
    
    // Update Password if provided
    if (!empty($password)) {
        if (strlen($password) < 6) {
            setFlash('danger', 'Password minimal 6 karakter.');
        } else {
            $pwStmt = $db->prepare("UPDATE users SET password = ? WHERE id = ?");
            $pwStmt->execute([password_hash($password, PASSWORD_DEFAULT), $userId]);
            setFlash('success', 'Profil dan password berhasil diperbarui.');
        }
    } else {
        setFlash('success', 'Profil berhasil diperbarui.');
    }
    
    header('Location: profile.php');
    exit;
}

// Get current user data
$stmt = $db->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$userId]);
$userData = $stmt->fetch();

include __DIR__ . '/includes/header.php';
?>

<div class="card" style="max-width: 600px; margin: 0 auto;">
    <div class="card-header">
        <h2><i class="fas fa-user-circle" style="color: var(--primary-500); margin-right: 8px;"></i>Edit Profil</h2>
    </div>
    <form method="POST">
        <div class="card-body">
            <div class="form-group">
                <label>Username</label>
                <input type="text" class="form-control" value="<?= htmlspecialchars($userData['username']) ?>" disabled>
                <span class="form-text">Username tidak dapat diubah.</span>
            </div>
            
            <div class="form-group">
                <label>Nama Lengkap <span class="required">*</span></label>
                <input type="text" name="nama_lengkap" class="form-control" required value="<?= htmlspecialchars($userData['nama_lengkap']) ?>" placeholder="Masukkan nama lengkap">
            </div>
            
            <div class="form-group">
                <label>Role</label>
                <input type="text" class="form-control" value="<?= ucfirst($userData['role']) ?>" disabled>
            </div>
            
            <div class="divider" style="margin: 24px 0; border-top: 1px solid var(--gray-100);"></div>
            
            <div class="form-group">
                <label>Ganti Password</label>
                <input type="password" name="password" class="form-control" placeholder="Kosongkan jika tidak ingin mengubah" minlength="6" autocomplete="new-password">
                <span class="form-text">Hanya isi jika Anda ingin mengganti password login.</span>
            </div>
        </div>
        <div class="card-footer" style="display: flex; justify-content: flex-end; gap: 12px; background: var(--gray-50);">
            <a href="index.php" class="btn btn-outline">Batal</a>
            <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Simpan Perubahan</button>
        </div>
    </form>
</div>

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