<?php
require_once 'init.php';

$currentUser = $auth->getCurrentUser();
$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $company_name = $_POST['company_name'] ?? '';
    $address = $_POST['address'] ?? '';
    $city = $_POST['city'] ?? '';
    $province = $_POST['province'] ?? '';
    $zip_code = $_POST['zip_code'] ?? '';
    
    if (empty($name) || empty($company_name)) {
        $error = 'Nama dan perusahaan harus diisi';
    } else {
        $result = $database->update('users', [
            'name' => $name,
            'phone' => $phone,
            'company_name' => $company_name,
            'address' => $address,
            'city' => $city,
            'province' => $province,
            'zip_code' => $zip_code
        ], ['id' => $_SESSION['user_id']]);
        
        if ($result) {
            $_SESSION['name'] = $name;
            $_SESSION['company_name'] = $company_name;
            $success = 'Profile berhasil diupdate';
            $currentUser = $auth->getCurrentUser();
        } else {
            $error = 'Gagal mengupdate profile';
        }
    }
}

$pageTitle = 'Profile - KasirMurah.biz.id';
?>
<?php include 'layout/header.php'; ?>
    <?php include 'layout/navbar.php'; ?>
    
    <div class="container mt-4">
        <div class="row">
            <div class="col-md-8">
                <h1>Profile</h1>
                
                <?php if ($error): ?>
                <div class="alert alert-danger"><?php echo $error; ?></div>
                <?php endif; ?>
                
                <?php if ($success): ?>
                <div class="alert alert-success"><?php echo $success; ?></div>
                <?php endif; ?>
                
                <div class="card">
                    <div class="card-body">
                        <form method="POST">
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">Nama Lengkap *</label>
                                    <input type="text" class="form-control" name="name" value="<?php echo $currentUser['name']; ?>" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">Email</label>
                                    <input type="email" class="form-control" value="<?php echo $currentUser['email']; ?>" disabled>
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">Nama Perusahaan *</label>
                                    <input type="text" class="form-control" name="company_name" value="<?php echo $currentUser['company_name']; ?>" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">Telepon</label>
                                    <input type="text" class="form-control" name="phone" value="<?php echo $currentUser['phone']; ?>">
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">Alamat</label>
                                <input type="text" class="form-control" name="address" value="<?php echo $currentUser['address']; ?>">
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label">Kota</label>
                                    <input type="text" class="form-control" name="city" value="<?php echo $currentUser['city']; ?>">
                                </div>
                                <div class="col-md-3 mb-3">
                                    <label class="form-label">Provinsi</label>
                                    <input type="text" class="form-control" name="province" value="<?php echo $currentUser['province']; ?>">
                                </div>
                                <div class="col-md-3 mb-3">
                                    <label class="form-label">Kode Pos</label>
                                    <input type="text" class="form-control" name="zip_code" value="<?php echo $currentUser['zip_code']; ?>">
                                </div>
                            </div>
                            
                            <div class="d-flex gap-2">
                                <button type="submit" class="btn btn-primary">Simpan</button>
                                <a href="dashboard.php" class="btn btn-secondary">Batal</a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <?php include 'layout/footer.php'; ?>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
