<?php
/**
 * SalonPro X - User Profile View
 * @var object $profileUser User data with role_name and branch_name
 */
$user = $profileUser;
?>
<div class="row">
    <div class="col-lg-4">
        <div class="card glass-card mb-4">
            <div class="card-body text-center">
                <div class="profile-avatar mx-auto mb-3" style="width:100px;height:100px;">
                    <?php if ($user->avatar): ?>
                        <img src="<?= uploadUrl($user->avatar) ?>" alt="">
                    <?php else: ?>
                        <div class="avatar-placeholder" style="font-size:40px;">
                            <?= strtoupper(substr($user->first_name, 0, 1) . substr($user->last_name, 0, 1)) ?>
                        </div>
                    <?php endif; ?>
                </div>
                <h5><?= e($user->first_name . ' ' . $user->last_name) ?></h5>
                <p class="text-muted"><?= e($user->role_name ?? 'N/A') ?></p>
                <p class="text-muted small"><i class="fas fa-building me-1"></i><?= e($user->branch_name ?? 'All Branches') ?></p>
                
                <form method="POST" action="<?= url('profile/update') ?>" enctype="multipart/form-data" class="mt-3">
                    <?= csrf_field() ?>
                    <div class="mb-3">
                        <input type="file" name="avatar" class="form-control form-control-sm input-file-preview" 
                               accept="image/*" data-preview="#avatarPreview">
                    </div>
                    <img id="avatarPreview" style="display:none;max-width:100px;margin:0 auto 10px;border-radius:50%;">
                    <button type="submit" class="btn btn-gold btn-sm w-100">
                        <i class="fas fa-upload me-1"></i>Update Photo
                    </button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-8">
        <div class="card glass-card mb-4">
            <div class="card-header">
                <h5 class="card-title">Personal Information</h5>
            </div>
            <div class="card-body">
                <form method="POST" action="<?= url('profile/update') ?>" class="needs-validation" novalidate>
                    <?= csrf_field() ?>
                    <div class="row">
                        <div class="col-md-6">
                            <?php \core\View::component('form', [
                                'label' => 'First Name', 'name' => 'first_name',
                                'value' => old('first_name', $user->first_name),
                                'required' => true, 'icon' => 'fa-user',
                                'prepend' => true,
                            ]); ?>
                        </div>
                        <div class="col-md-6">
                            <?php \core\View::component('form', [
                                'label' => 'Last Name', 'name' => 'last_name',
                                'value' => old('last_name', $user->last_name),
                                'required' => true, 'icon' => 'fa-user',
                                'prepend' => true,
                            ]); ?>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Email</label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="fas fa-envelope"></i></span>
                            <input type="email" class="form-control" value="<?= e($user->email) ?>" disabled>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Username</label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="fas fa-at"></i></span>
                            <input type="text" class="form-control" value="<?= e($user->username) ?>" disabled>
                        </div>
                    </div>

                    <?php \core\View::component('form', [
                        'label' => 'Phone', 'name' => 'phone',
                        'value' => old('phone', $user->phone),
                        'icon' => 'fa-phone', 'prepend' => true,
                    ]); ?>

                    <button type="submit" class="btn btn-gold">
                        <i class="fas fa-save me-1"></i>Update Profile
                    </button>
                </form>
            </div>
        </div>

        <div class="card glass-card">
            <div class="card-header">
                <h5 class="card-title">Change Password</h5>
            </div>
            <div class="card-body">
                <form method="POST" action="<?= url('profile/password') ?>" class="needs-validation" novalidate>
                    <?= csrf_field() ?>
                    <?php \core\View::component('form', [
                        'label' => 'Current Password', 'name' => 'current_password',
                        'type' => 'password', 'required' => true, 'icon' => 'fa-lock', 'prepend' => true,
                    ]); ?>
                    <?php \core\View::component('form', [
                        'label' => 'New Password', 'name' => 'new_password',
                        'type' => 'password', 'required' => true, 'icon' => 'fa-lock', 'prepend' => true,
                        'help' => 'Minimum 8 characters.',
                    ]); ?>
                    <?php \core\View::component('form', [
                        'label' => 'Confirm New Password', 'name' => 'confirm_password',
                        'type' => 'password', 'required' => true, 'icon' => 'fa-check', 'prepend' => true,
                    ]); ?>
                    <button type="submit" class="btn btn-gold">
                        <i class="fas fa-key me-1"></i>Change Password
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>
