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

if (isset($_POST['submit'])) {
    $nama   = mysqli_real_escape_string($koneksi, $_POST['nama_perusahaan']);
    $npwp   = mysqli_real_escape_string($koneksi, $_POST['npwp']);
    $telp   = mysqli_real_escape_string($koneksi, $_POST['telp']);
    $alamat = mysqli_real_escape_string($koneksi, $_POST['alamat']);
    $email  = mysqli_real_escape_string($koneksi, $_POST['email']);

    // Cek file logo
    if ($_FILES['logo']['name'] != "") {
        $ekstensi_diperbolehkan = array('png','jpg','jpeg');
        $x = explode('.', $_FILES['logo']['name']);
        $ekstensi = strtolower(end($x));
        $file_tmp = $_FILES['logo']['tmp_name'];
        $nama_file = "logo_" . time() . "." . $ekstensi;
        $path = "assets/" . $nama_file;

        if (in_array($ekstensi, $ekstensi_diperbolehkan)) {
            move_uploaded_file($file_tmp, $path);
            $query = "UPDATE profil_perusahaan SET 
                        nama_perusahaan='$nama', npwp='$npwp', telp='$telp', 
                        alamat='$alamat', email='$email', logo='$path' 
                      WHERE id=1";
        }
    } else {
        // Jika tidak ganti logo, pastikan email tetap di-update
        $query = "UPDATE profil_perusahaan SET 
                    nama_perusahaan='$nama', npwp='$npwp', telp='$telp', 
                    alamat='$alamat', email='$email' 
                  WHERE id=1";
    }

    if (mysqli_query($koneksi, $query)) {
        header("Location: profil_perusahaan.php?status=success");
    } else {
        echo "Gagal memperbarui data: " . mysqli_error($koneksi);
    }
}
?>