<?php
session_start();
if (!isset($_SESSION['login_user'])) {
    header('Location: login.php');
    exit();
}

include 'config/koneksi.php'; // Pastikan koneksi ke database

if ($koneksi->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$user_id = $_SESSION['login_user'];
$sql = "SELECT * FROM tb_user WHERE user = '$user_id'";
$result = $koneksi->query($sql);
$user = $result->fetch_assoc();

if (!$user) {
    echo "User not found!";
    exit();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nama = $_POST['nama'];
    $email = $_POST['email'];
    $no_hp = $_POST['no_hp'];
    $jk = $_POST['jk'];
    $usia = $_POST['usia'];
    $alamat = $_POST['alamat'];

    $update_sql = "UPDATE tb_user SET nama = '$nama', email = '$email', no_hp = '$no_hp', jk = '$jk', usia = '$usia', alamat = '$alamat' WHERE user = '$user_id'";
    if ($conn->query($update_sql) === TRUE) {
        echo "Profile updated successfully!";
        $user['nama'] = $nama;
        $user['email'] = $email;
        $user['no_hp'] = $no_hp;
        $user['jk'] = $jk;
        $user['usia'] = $usia;
        $user['alamat'] = $alamat;
    } else {
        echo "Error: " . $update_sql . "<br>" . $conn->error;
    }
}

$koneksi->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>User Profile</title>
    <link href="assets/css/yeti-bootstrap.min.css" rel="stylesheet"/>
    <link href="assets/css/general.css" rel="stylesheet"/>
    <link href="assets/css/select2.min.css" rel="stylesheet"/>
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>    
    <script src="assets/js/select2.min.js"></script>   
    <style type="text/css">
    .hi {
      background-image: linear-gradient(rgba(0, 0, 0, 0.5),rgba(0, 0, 0, 0.5)), url('vitaminbg.png');
      background-size: cover;
      background-position: center center;
      background-repeat: no-repeat;
      background-attachment: fixed;
    }
    .dark {
      background-color: #000;
      color: #fff;
    }
    </style>      
</head>
<body class="dark hi">
  <div class="container">
    <div class="page-header">
      <h1>Profile</h1>
    </div>
    <div class="row">
      <div class="col-md-4">
        <form method="post">                        
          <div class="form-group">
            <label for="user">Username</label>
            <input type="text" class="form-control" value="<?= $user['user'] ?>" readonly />
          </div>
          <div class="form-group">
            <label for="pass">Password</label>
            <input type="text" class="form-control" value="<?= $user['pass'] ?>" readonly />
          </div>
          <div class="form-group">
            <label for="nama">Nama</label>
            <input type="text" class="form-control" name="nama" value="<?= $user['nama'] ?>" />
          </div>
          <div class="form-group">
            <label for="nama">Email</label>
            <input type="text" class="form-control" name="email" value="<?= $user['email'] ?>" />
          </div>
          <div class="form-group">
            <label for="no_hp">No. Hp</label>
            <input type="text" class="form-control" name="no_hp" value="<?= $user['no_hp'] ?>" />
          </div>
          <div class="form-group">
            <label for="jk">Jenis Kelamin</label><br>
            <input type="radio" name="jk" value="Laki - Laki" <?= $user['jk'] == 'Laki - Laki' ? 'checked' : '' ?>> Laki - Laki
            <input type="radio" name="jk" value="Perempuan" <?= $user['jk'] == 'Perempuan' ? 'checked' : '' ?>> Perempuan
          </div>
          <div class="form-group">
            <label for="usia">Usia</label>
            <input type="text" class="form-control" name="usia" value="<?= $user['usia'] ?>" />
          </div>
          <div class="form-group">
            <label for="alamat">Alamat</label>
            <input type="text" class="form-control" name="alamat" value="<?= $user['alamat'] ?>" />
          </div>
          <div class="form-group">                
            <button class="btn edit" type="submit">Update Profile</button>
            <a class="btn edit" href="?m=home"><span class="glyphicon glyphicon-arrow-left"></span> Back</a>
          </div>        
        </form>      
      </div>
    </div> 
  </div>
</body>
</html>
