@extends('layouts.user-layout')
@section('title') User-Profile
@endsection
@section('content')
    <style>
        .active {
            background-color: #007BFF !important;
            color: white !important;
        }
    </style>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <section class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-12">
                        <div class="card card-box-shadow mt-5">

                            <div class="card-header">
                                <h3 class="card-title">User Profile Update</h3>
                            </div>
                            <!-- /.card-header -->
                            <div class="card-body">
                                <form action="{{route('user.update-profile')}}" method="POST"
                                      enctype="multipart/form-data">
                                    @csrf
                                    <div class="col-md-9 offset-md-3 col-sm-10 offset-sm-2">
                                        <input type='hidden' value='{{Auth::user()->id }}' name='user_id'>
                                        <div class="col-md-8 col-sm-9">
                                            <div class="form-group">
                                                <label for="Name">First Name:</label>
                                                <input type="text" class="form-control form__field" name="first_name"
                                                       value="{{Auth::user()->first_name}}"></div>
                                        </div>

                                        <!-- <div class="col-md-8 col-sm-9">
                                            <div class="form-group">
                                                <label for="Name">Last Name:</label>
                                                <input type="text" class="form-control" name="lname" placeholder="Enter Name" id="Name" value=""> </div>
                                        </div> -->

                                        <div class="col-md-8 col-sm-9">
                                            <div class="form-group">
                                                <label for="Name">Email:</label>
                                                <input type="email" class="form-control" name="email"
                                                       placeholder="Enter Name" id="Name"
                                                       value="{{ Auth::user()->email }}" readonly></div>
                                        </div>

                                        <div class="col-md-8 col-sm-9">
                                            <div class="form-group">
                                                <label for="userProfileImageInput">Choose Profile:</label>
                                                <input type="file" class="form-control form__field visually-hidden" name="user_profile"
                                                       id="userProfileImageInput">
                                                <label for="userProfileImageInput" class="file-input-label form__field form-control">
                                                    <i class="fas fa-camera"></i> Choose file
                                                </label>

                                            </div>
                                        </div>
                                        <div class="col-md-8 col-sm-9">
                                            <div class="form-group">
                                                <input type="submit" class="form-control btn btn-primary btn-sm "
                                                       value="Update Profile"></div>
                                        </div>
                                        <div class="col-md-8 col-sm-9">
                                            <div class="form-group">
                                                <img id="userProfileImagePreview"
                                                     src="{{(Auth::user()->userImage('user_profile'))}}"
                                                     alt="Profile Preview"
                                                     style="width: 200px; height: 150px; margin-top: 10px;">
                                            </div>
                                        </div>
                                    </div>
                                </form>

                            </div>
                            <!-- /.card-body -->
                        </div>

                    </div>
                    <!-- /.col -->
                </div>
                <!-- /.row -->
            </div>
            <!-- /.container-fluid -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
@endsection
@section('script')
    <script>

        function showImagePreview(input, previewId) {
            const preview = document.getElementById(previewId);

            if (input.files && input.files[0]) {
                const reader = new FileReader();

                reader.onload = function (e) {
                    preview.src = e.target.result;
                    preview.style.display = 'block';
                };

                reader.readAsDataURL(input.files[0]);
            }
        }

        const imageInput = document.getElementById('userProfileImageInput');
        imageInput.addEventListener('change', function () {
            showImagePreview(this, 'userProfileImagePreview');
        });
    </script>
@endsection
