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

// Proteksi Halaman
if (!isset($_SESSION['status']) || $_SESSION['status'] != "login") {
    header("Location: login.php");
    exit();
}

// Ambil data profil
$query = mysqli_query($koneksi, "SELECT * FROM profil_perusahaan WHERE id=1");
$profil = mysqli_fetch_array($query);

// Variabel data (Email dipastikan terpanggil di sini)
$nama   = $profil['nama_perusahaan'] ?? '';
$npwp   = $profil['npwp'] ?? '';
$telp   = $profil['telp'] ?? '';
$alamat = $profil['alamat'] ?? '';
$email  = $profil['email'] ?? '';
$logo_db = (!empty($profil['logo'])) ? $profil['logo'] : "assets/logoprofil.png";
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Profil Perusahaan - BEST Procurement</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <style>
        :root {
            --sidebar-grad: linear-gradient(180deg, #e60000 0%, #000000 100%);
            --main-bg: #f4f7f6;
            --card-white: #ffffff;
        }

        body { 
            margin: 0; 
            font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; 
            background: var(--main-bg);
            display: flex;
            height: 100vh;
            overflow: hidden;
        }

        /* SIDEBAR FIXED */
        .sidebar {
            width: 260px;
            background: var(--sidebar-grad);
            color: white;
            display: flex;
            flex-direction: column;
            flex-shrink: 0;
        }

        .sidebar-brand {
            padding: 30px 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }

        .sidebar-brand img {
            width: 200px; /* Ukuran logo sidebar dikunci */
            height: auto;
        }

        .menu-section { padding: 15px 20px 5px; font-size: 11px; text-transform: uppercase; color: rgba(255,255,255,0.5); font-weight: bold; }

        .nav-link {
            padding: 12px 25px;
            display: flex;
            align-items: center;
            gap: 15px;
            color: #b0b0b0;
            text-decoration: none;
            font-size: 14px;
            transition: 0.3s;
        }

        .nav-link:hover, .nav-link.active {
            background: rgba(255,255,255,0.1);
            color: white;
            border-left: 4px solid white;
        }

        /* MAIN CONTENT */
        .main-content {
            flex-grow: 1;
            overflow-y: auto;
            display: flex;
            flex-direction: column;
        }

        header {
            background: white;
            padding: 15px 30px;
            font-weight: bold;
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
        }

        .container { padding: 40px; display: flex; justify-content: center; }

        .card-profile {
            background: var(--card-white);
            width: 100%;
            max-width: 900px;
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.05);
            display: grid;
            grid-template-columns: 250px 1fr;
            gap: 40px;
        }

        /* LOGO UPLOAD AREA */
        .logo-upload-section { text-align: center; }
        .preview-wrapper {
            width: 200px;
            height: 200px;
            border: 2px dashed #ddd;
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            background: #fafafa;
            margin: 0 auto 15px;
        }
        .preview-wrapper img {
            max-width: 100%;
            max-height: 100%;
            object-fit: contain; /* Mencegah gambar pecah/kacau */
        }

        /* FORM STYLING */
        .form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 15px; }
        .form-group { margin-bottom: 15px; }
        .form-group label { display: block; font-size: 12px; font-weight: bold; color: #666; margin-bottom: 8px; }
        .form-control {
            width: 100%;
            padding: 12px;
            border: 1px solid #ccc;
            border-radius: 8px;
            box-sizing: border-box;
            font-size: 14px;
        }

        .btn-submit {
            background: #b81d24;
            color: white;
            border: none;
            padding: 15px;
            border-radius: 8px;
            width: 100%;
            font-weight: bold;
            cursor: pointer;
            margin-top: 10px;
            transition: 0.3s;
        }
        .btn-submit:hover { background: #e60000; }

        .alert-success {
            background: #d4edda;
            color: #155724;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            border: 1px solid #c3e6cb;
        }
    </style>
</head>
<body>

    <div class="sidebar">
        <div class="sidebar-brand">
            <img src="assets/logosys.png" alt="Logo">
        </div>
        
        <div class="menu-section">Menu Utama</div>
        <a href="dashboard.php" class="nav-link"><i class="fa-solid fa-gauge"></i> Dashboard</a>
        <a href="supplier.php" class="nav-link"><i class="fa-solid fa-truck"></i> Supplier</a>
        
        <div class="menu-section">Laporan</div>
        <a href="daftar_po.php" class="nav-link"><i class="fa-solid fa-file-invoice"></i> Daftar PO</a>
        
        <div class="menu-section">Superadmin</div>
        <a href="profil_perusahaan.php" class="nav-link active"><i class="fa-solid fa-building"></i> Profil Perusahaan</a>

        <div style="margin-top: auto; padding-bottom: 20px;">
            <a href="logout.php" class="nav-link" style="color: #ff6b6b;"><i class="fa-solid fa-power-off"></i> Logout</a>
        </div>
    </div>

    <div class="main-content">
        <header>Pengaturan Profil Perusahaan</header>

        <div class="container">
            <div style="width: 100%; max-width: 900px;">
                
                <?php if(isset($_GET['status']) && $_GET['status'] == 'success'): ?>
                    <div class="alert-success">
                        <i class="fa-solid fa-check-circle"></i> Profil berhasil diperbarui!
                    </div>
                <?php endif; ?>

                <form action="proses_update_profil.php" method="POST" enctype="multipart/form-data" class="card-profile">
                    
                    <div class="logo-upload-section">
                        <div class="form-group">
                            <label>Logo Perusahaan</label>
                            <div class="preview-wrapper">
                                <img src="<?php echo $logo_db; ?>" alt="Preview">
                            </div>
                            <input type="file" name="logo" class="form-control" style="font-size: 11px;">
                        </div>
                    </div>

                    <div class="form-section">
                        <div class="form-group">
                            <label>Nama Perusahaan</label>
                            <input type="text" name="nama_perusahaan" class="form-control" value="<?php echo $nama; ?>" required>
                        </div>

                        <div class="form-row">
                            <div class="form-group">
                                <label>NPWP</label>
                                <input type="text" name="npwp" class="form-control" value="<?php echo $npwp; ?>">
                            </div>
                            <div class="form-group">
                                <label>Nomor Telepon</label>
                                <input type="text" name="telp" class="form-control" value="<?php echo $telp; ?>">
                            </div>
                        </div>

                        <div class="form-group">
                            <label>Alamat Lengkap</label>
                            <textarea name="alamat" class="form-control" rows="3"><?php echo $alamat; ?></textarea>
                        </div>

                        <div class="form-group">
                            <label>Email Perusahaan</label>
                            <input type="email" name="email" class="form-control" value="<?php echo $email; ?>">
                        </div>

                        <button type="submit" name="submit" class="btn-submit">
                            <i class="fa-solid fa-save"></i> Simpan Perubahan
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

</body>
</html>