<?php
session_start();
include 'koneksi.php';

// Cek koneksi database
if (!$conn) {
    die("Database tidak terhubung!");
}

// Cek login
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['name'];
$user_email = $_SESSION['email'] ?? 'user@example.com'; // Asumsi ada email di session

// Ambil data lengkap user dari database (Asumsi tabel 'users')
$query_user = mysqli_query($conn, "SELECT * FROM users WHERE id = '$user_id'");
$user_data = mysqli_fetch_assoc($query_user);

// Hitung statistik user (sama seperti di index.php)
$query_stats = mysqli_query($conn, "SELECT * FROM transaksi WHERE user_id = '$user_id'");
$total_pemasukan = 0;
$total_pengeluaran = 0;
$total_transaksi = 0;

while ($row = mysqli_fetch_assoc($query_stats)) {
    $total_transaksi++;
    if ($row['jenis'] == 'pemasukan') {
        $total_pemasukan += $row['jumlah'];
    } else {
        $total_pengeluaran += $row['jumlah'];
    }
}
$total_saldo = $total_pemasukan - $total_pengeluaran;

?>
<?php

$message = "";

if(isset($_POST['update_profil'])){

$nama_baru = mysqli_real_escape_string($conn, $_POST['name']);
$email_baru = mysqli_real_escape_string($conn, $_POST['email']);

$update = mysqli_query($conn,"UPDATE users SET 
name='$nama_baru',
email='$email_baru'
WHERE id='$user_id'
");

if($update){

$_SESSION['name'] = $nama_baru;
$_SESSION['email'] = $email_baru;

$user_name = $nama_baru;
$user_email = $email_baru;

$message = "Profil berhasil diperbarui";

}else{

$message = "Gagal memperbarui profil";

}

}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Profil - MyBank</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        /* COPY CSS DARI INDEX.PHP AGAR SAMA */
        *{margin:0; padding:0; box-sizing:border-box; -webkit-tap-highlight-color:transparent;}
        body{font-family:'Inter',sans-serif; background:linear-gradient(180deg,#f1f5f9,#e2e8f0); height:100vh; overflow:hidden;}
        .app{max-width:480px; margin:auto; height:100vh; display:flex; flex-direction:column; background:white; border-radius:25px; overflow:hidden; box-shadow:0 20px 50px rgba(0,0,0,0.1);}
        
        /* HEADER */
        .header{background:linear-gradient(135deg,#4f46e5,#9333ea); color:white; padding:35px 20px 40px; border-bottom-left-radius:30px; border-bottom-right-radius:30px;}
        .header-top{display:flex; justify-content:space-between; align-items:center;}
        .user-info h1{font-size:20px; font-weight:700;}
        .user-info p{font-size:13px; opacity:.8;}
        .logout-btn{background:rgba(255,255,255,0.2); padding:8px 15px; border-radius:20px; text-decoration:none; color:white; font-size:12px;}

        /* CONTENT */
        .content{
flex:1;
overflow-y:auto;
padding:20px;
padding-bottom:90px;
}
        
        /* PROFILE CARD */
        .profile-card{background:white; padding:25px; border-radius:20px; text-align:center; box-shadow:0 5px 15px rgba(0,0,0,0.05); margin-bottom:20px;}
        .avatar{width:80px; height:80px; background:#e0e7ff; border-radius:50%; margin:0 auto 15px; display:flex; align-items:center; justify-content:center; font-size:30px; color:#4f46e5;}
        .profile-name{font-size:18px; font-weight:700; margin-bottom:5px;}
        .profile-email{font-size:13px; color:#666; margin-bottom:20px;}
        
        .form-group{margin-bottom:15px; text-align:left;}
        .form-group label{font-size:12px; color:#555; display:block; margin-bottom:5px;}
        .form-group input{width:100%; padding:12px; border-radius:10px; border:1px solid #ddd; background:#f8fafc;}
        
        .btn-edit{width:100%; padding:12px; border:none; border-radius:10px; background:#4f46e5; color:white; font-weight:600; cursor:pointer;}

        /* STATS BOX */
        .stats-grid{display:grid; grid-template-columns:1fr 1fr; gap:15px; margin-bottom:20px;}
        .stat-box{background:white; padding:15px; border-radius:15px; box-shadow:0 5px 15px rgba(0,0,0,0.05);}
        .stat-label{font-size:11px; color:#888; margin-bottom:5px;}
        .stat-val{font-size:16px; font-weight:700; color:#333;}

        /* BOTTOM NAV */
       .bottom-nav{
display:flex;
justify-content:space-around;
padding:12px;
border-top:1px solid #eee;
background:white;

position:fixed;
bottom:0;
left:50%;
transform:translateX(-50%);
width:100%;
max-width:480px;

z-index:100;
box-shadow:0 -5px 20px rgba(0,0,0,0.08);
}
        .nav-item{text-align:center; font-size:11px; color:#888; text-decoration:none;}
        .nav-item.active{color:#4f46e5;}
        .nav-icon{font-size:20px; display:block;}
        .nav-item{
text-align:center;
font-size:11px;
color:#888;
text-decoration:none;
transition:0.2s;
}

.nav-item:active{
transform:scale(0.92);
}
    </style>
</head>
<body>

<div class="app">
    <!-- Header -->
    <div class="header">
        <div class="header-top">
            <div class="user-info">
                <h1>Profil Saya</h1>
                <p>Kelola akun Anda</p>
            </div>
            <a href="logout.php" class="logout-btn">Logout</a>
        </div>
    </div>

    <!-- Content -->
    <div class="content">
        <?php if($message != ""){ ?>

<div style="
background:#dcfce7;
color:#166534;
padding:10px;
border-radius:10px;
margin-bottom:15px;
font-size:13px;
text-align:center;
">

<?php echo $message; ?>

</div>

<?php } ?>
        <!-- Kartu Profil -->
        <div class="profile-card">
            <div class="avatar">👤</div>
            <div class="profile-name"><?php echo htmlspecialchars($user_name); ?></div>
            <div class="profile-email"><?php echo htmlspecialchars($user_email); ?></div>
            
            <form method="POST" action="">
                <div class="form-group">
                    <label>Nama Lengkap</label>
                  <input type="text" name="name" value="<?php echo htmlspecialchars($user_name); ?>" required>  
                </div>
                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" value="<?php echo htmlspecialchars($user_email); ?>" required>
                </div>
                <button type="submit" name="update_profil" class="btn-edit">
Simpan Perubahan
</button>
            </form>
        </div>

        <!-- Statistik Singkat -->
        <div class="section-title" style="margin-bottom:15px; font-weight:700; font-size:16px;">Ringkasan Akun</div>
        
        <div class="stats-grid">
            <div class="stat-box">
                <div class="stat-label">Total Saldo</div>
                <div class="stat-val">Rp <?php echo number_format($total_saldo, 0, ',', '.'); ?></div>
            </div>
            <div class="stat-box">
                <div class="stat-label">Total Transaksi</div>
                <div class="stat-val"><?php echo $total_transaksi; ?> x</div>
            </div>
            <div class="stat-box">
                <div class="stat-label">Pemasukan</div>
                <div class="stat-val" style="color:#16a34a;">+ Rp <?php echo number_format($total_pemasukan, 0, ',', '.'); ?></div>
            </div>
            <div class="stat-box">
                <div class="stat-label">Pengeluaran</div>
                <div class="stat-val" style="color:#dc2626;">- Rp <?php echo number_format($total_pengeluaran, 0, ',', '.'); ?></div>
            </div>
        </div>

        <!-- Info Tambahan -->
        <div style="background:white; padding:20px; border-radius:15px; box-shadow:0 5px 15px rgba(0,0,0,0.05);">
            <div style="display:flex; justify-content:space-between; margin-bottom:15px; border-bottom:1px solid #eee; padding-bottom:10px;">
                <span style="color:#666; font-size:14px;">ID Pengguna</span>
                <span style="font-weight:600; font-size:14px;"><?php echo $user_id; ?></span>
            </div>
            <div style="display:flex; justify-content:space-between; margin-bottom:15px; border-bottom:1px solid #eee; padding-bottom:10px;">
                <span style="color:#666; font-size:14px;">Status Akun</span>
                <span style="font-weight:600; font-size:14px; color:#16a34a;">Aktif</span>
            </div>
            <div style="display:flex; justify-content:space-between;">
                <span style="color:#666; font-size:14px;">Versi Aplikasi</span>
                <span style="font-weight:600; font-size:14px;">1.0.0</span>
            </div>
        </div>

    </div>

    <!-- Bottom Nav -->
    <div class="bottom-nav">
        <a href="index.php" class="nav-item">
            <span class="nav-icon">🏠</span>
            Beranda
        </a>
        <a href="laporan.php" class="nav-item">
            <span class="nav-icon">📊</span>
            Laporan
        </a>
        <a href="profil.php" class="nav-item active">
            <span class="nav-icon">👤</span>
            Profil
        </a>
    </div>
</div>

</body>
</html>