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

// Pastikan customer login
if(!isset($_SESSION['kd_cs'])){
    header("Location: ../user_login.php");
    exit();
}

$kode_customer = $_POST['kode_customer'];
$nama          = $_POST['nama'];
$username      = $_POST['username'];
$email         = $_POST['email'];
$telp          = $_POST['telp'];
$password      = $_POST['password'];

// Jika password tidak diisi, jangan update password
if (!empty($password)) {
    // hash password baru
    $hash = password_hash($password, PASSWORD_DEFAULT);
    $sql = "UPDATE customer 
            SET nama     = '$nama',
                username = '$username',
                email    = '$email',
                telp     = '$telp',
                password = '$hash'
            WHERE kode_customer = '$kode_customer'";
} else {
    // update tanpa ubah password
    $sql = "UPDATE customer 
            SET nama     = '$nama',
                username = '$username',
                email    = '$email',
                telp     = '$telp'
            WHERE kode_customer = '$kode_customer'";
}

$result = mysqli_query($conn, $sql);

if ($result) {
    $_SESSION['user'] = $nama;

    echo "
    <script>
        alert('Profil berhasil diperbarui');
        window.location = '../profil.php';
    </script>
    ";
} else {
    echo "
    <script>
        alert('Gagal memperbarui profil: " . mysqli_error($conn) . "');
        window.location = '../profil.php';
    </script>
    ";
}
?>
