@extends('dashboard.main')

@section('main-content')
    <div class="container-fluid">

        @if (session('success'))
            <div class="alert alert-success">
                {{ session('success') }}
            </div>
        @endif

        <div class="card">

            <div class="card-header">
                <h3>Edit Profile</h3>
            </div>

            <div class="card-body">

                <form method="POST" action="{{ route('user.update', $data->id) }}" enctype="multipart/form-data">

                    @csrf
                    {{-- @method('PUT')  <!-- Uncomment if your route uses PUT --> --}}

                    <!-- Full Name -->
                    <div class="mb-3">
                        <label class="form-label">Full Name</label>

                        <input type="text" name="name" value="{{ old('name', $data->name) }}"
                            class="form-control @error('name') is-invalid @enderror">

                        @error('name')
                            <span class="text-danger">{{ $message }}</span>
                        @enderror
                    </div>

                    <!-- Email -->
                    <div class="mb-3">
                        <label class="form-label">Email Address</label>

                        <input type="email" name="email" value="{{ old('email', $data->email) }}"
                            class="form-control @error('email') is-invalid @enderror">

                        @error('email')
                            <span class="text-danger">{{ $message }}</span>
                        @enderror
                    </div>

                    <!-- Phone -->
                    <div class="mb-3">
                        <label class="form-label">Phone Number</label>

                        <input type="text" name="phone" value="{{ old('phone', $data->phone) }}"
                            class="form-control @error('phone') is-invalid @enderror">

                        @error('phone')
                            <span class="text-danger">{{ $message }}</span>
                        @enderror
                    </div>

                    <!-- Password Row -->
                    <div class="row">

                        <!-- New Password -->
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">
                                    New Password
                                    <small class="text-muted">(Optional)</small>
                                </label>

                                <input type="password" name="password"
                                    class="form-control @error('password') is-invalid @enderror"
                                    placeholder="Enter New Password">

                                @error('password')
                                    <span class="text-danger">{{ $message }}</span>
                                @enderror
                            </div>
                        </div>

                        <!-- Confirm Password -->
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">Confirm Password</label>

                                <input type="password" name="password_confirmation" class="form-control"
                                    placeholder="Confirm Password">
                            </div>
                        </div>

                    </div>

                    <!-- Current Profile Image -->
                    <div class="mb-3">

                        <label class="form-label">Current Profile Image</label>

                        <br>

                        @if ($data->profile_image)
                            <img src="{{ asset('uploads/profile/' . $data->profile_image) }}" width="120" height="120"
                                class="rounded border shadow-sm" style="object-fit:cover;">
                        @else
                            <img src="{{ asset('assets/images/default-user.png') }}" width="120" height="120"
                                class="rounded border shadow-sm">
                        @endif

                    </div>

                    <!-- Upload New Image -->
                    <div class="mb-3">

                        <label class="form-label">Change Profile Image</label>

                        <input type="file" name="profile_image"
                            class="form-control @error('profile_image') is-invalid @enderror" accept=".jpg,.jpeg,.png">

                        @error('profile_image')
                            <span class="text-danger">{{ $message }}</span>
                        @enderror

                    </div>

                    <!-- Buttons -->
                    <div class="d-flex gap-2">

                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save"></i> Update User
                        </button>

                        <a href="{{ route('users.show') }}" class="btn btn-secondary">
                            Cancel
                        </a>

                    </div>

                </form>
            </div>

        </div>

    </div>
@endsection
