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

$nama_perusahaan = mysqli_real_escape_string($koneksi, $_POST['nama_perusahaan']);
$alamat = mysqli_real_escape_string($koneksi, $_POST['alamat']);
$telepon = mysqli_real_escape_string($koneksi, $_POST['telepon']);
$email = mysqli_real_escape_string($koneksi, $_POST['email']);
$website = mysqli_real_escape_string($koneksi, $_POST['website']);
$deskripsi = mysqli_real_escape_string($koneksi, $_POST['deskripsi']);

$rand = rand();
$allowed = array('jpg', 'jpeg', 'png', 'gif');
$filename = $_FILES['logo']['name'];

// Ambil data lama untuk logo
$data_lama = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT profil_logo FROM profil_perusahaan LIMIT 1"));
$logo_lama = $data_lama['profil_logo'] ?? '';

if($filename == ""){
    // Tidak ada file baru, update tanpa logo
    mysqli_query($koneksi, "UPDATE profil_perusahaan SET 
        profil_nama='$nama_perusahaan',
        profil_alamat='$alamat',
        profil_telepon='$telepon',
        profil_email='$email',
        profil_website='$website',
        profil_deskripsi='$deskripsi'
        WHERE profil_id=1") or die(mysqli_error($koneksi));
    
    $_SESSION['alert'] = 'success';
    $_SESSION['message'] = 'Profil perusahaan berhasil diperbarui!';
    header("location:setting_profil.php");
}else{
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    
    if(!in_array(strtolower($ext), $allowed)) {
        $_SESSION['alert'] = 'error';
        $_SESSION['message'] = 'Format file tidak diperbolehkan! Hanya file JPG, JPEG, PNG, dan GIF yang diizinkan.';
        header("location:setting_profil.php");
    }else{
        // Cek ukuran file (maksimal 2MB)
        if($_FILES['logo']['size'] > 2097152){
            $_SESSION['alert'] = 'error';
            $_SESSION['message'] = 'Ukuran file terlalu besar! Maksimal 2MB.';
            header("location:setting_profil.php");
        }else{
            // Buat folder logo jika belum ada
            if(!is_dir('../gambar/logo')){
                mkdir('../gambar/logo', 0777, true);
            }
            
            // Hapus logo lama jika ada
            if(!empty($logo_lama) && file_exists('../gambar/logo/'.$logo_lama)){
                unlink('../gambar/logo/'.$logo_lama);
            }
            
            // Upload logo baru
            move_uploaded_file($_FILES['logo']['tmp_name'], '../gambar/logo/'.$rand.'_'.$filename);
            $logo_baru = $rand.'_'.$filename;
            
            mysqli_query($koneksi, "UPDATE profil_perusahaan SET 
                profil_nama='$nama_perusahaan',
                profil_alamat='$alamat',
                profil_telepon='$telepon',
                profil_email='$email',
                profil_website='$website',
                profil_logo='$logo_baru',
                profil_deskripsi='$deskripsi'
                WHERE profil_id=1") or die(mysqli_error($koneksi));
            
            $_SESSION['alert'] = 'success';
            $_SESSION['message'] = 'Profil perusahaan berhasil diperbarui!';
            header("location:setting_profil.php");
        }
    }
}
?>
