@extends('admin.layouts.app')

@section('title', isset($user) ? 'Edit User' : 'Add User')

@section('breadcrumb')
    <a href="{{ route('admin.users.index') }}">Users</a>
    <span class="separator">/</span>
    <span class="current">{{ isset($user) ? 'Edit' : 'Add' }}</span>
@endsection

@section('content')

<div class="page-header">
    <h1 class="page-title">
        <span class="title-icon">
            <i data-lucide="{{ isset($user) ? 'user-cog' : 'user-plus' }}" style="width:20px;height:20px;"></i>
        </span>
        {{ isset($user) ? 'Edit User' : 'Add New User' }}
    </h1>
    <p class="page-subtitle">{{ isset($user) ? 'Update user information and permissions' : 'Create a new system user' }}</p>
</div>

<div style="max-width:640px;">
    <div class="glass-card" style="padding:24px;">

        @if($errors->any())
            <div class="alert alert-error">
                <i data-lucide="alert-circle" class="alert-icon" style="width:15px;height:15px;"></i>
                <div>
                    @foreach($errors->all() as $error)
                        <div>{{ $error }}</div>
                    @endforeach
                </div>
            </div>
        @endif

        <form method="POST"
              action="{{ isset($user) ? route('admin.users.update', $user) : route('admin.users.store') }}">
            @csrf
            @if(isset($user)) @method('PUT') @endif

            <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">

                <div class="form-group" style="grid-column:1/-1;">
                    <label class="form-label" for="name">Full Name</label>
                    <input type="text" id="name" name="name"
                           value="{{ old('name', $user->name ?? '') }}"
                           class="form-control {{ $errors->has('name') ? 'border-red-500' : '' }}"
                           placeholder="John Doe" required>
                </div>

                <div class="form-group">
                    <label class="form-label" for="email">Email Address</label>
                    <input type="email" id="email" name="email"
                           value="{{ old('email', $user->email ?? '') }}"
                           class="form-control {{ $errors->has('email') ? 'border-red-500' : '' }}"
                           placeholder="user@example.com" required>
                </div>

                <div class="form-group">
                    <label class="form-label" for="role_id">Role</label>
                    <select id="role_id" name="role_id" class="form-control">
                        <option value="">Select Role</option>
                        @foreach($roles ?? [] as $role)
                            <option value="{{ $role->id }}"
                                {{ old('role_id', $user->role_id ?? '') == $role->id ? 'selected' : '' }}>
                                {{ $role->name }}
                            </option>
                        @endforeach
                    </select>
                </div>

                <div class="form-group">
                    <label class="form-label" for="password">
                        Password {{ isset($user) ? '(leave blank to keep current)' : '' }}
                    </label>
                    <input type="password" id="password" name="password"
                           class="form-control {{ $errors->has('password') ? 'border-red-500' : '' }}"
                           placeholder="{{ isset($user) ? 'New password...' : '••••••••' }}"
                           {{ isset($user) ? '' : 'required' }}>
                </div>

                <div class="form-group">
                    <label class="form-label" for="password_confirmation">Confirm Password</label>
                    <input type="password" id="password_confirmation" name="password_confirmation"
                           class="form-control"
                           placeholder="Repeat password"
                           {{ isset($user) ? '' : 'required' }}>
                </div>

                @if(isset($user))
                    <div class="form-group" style="grid-column:1/-1;">
                        <label class="form-label">Status</label>
                        <div style="display:flex;gap:16px;align-items:center;">
                            <label style="display:flex;align-items:center;gap:8px;cursor:pointer;font-size:13px;color:var(--vsc-text-primary);">
                                <input type="radio" name="is_active" value="1"
                                       {{ old('is_active', $user->is_active ?? 1) == 1 ? 'checked' : '' }}
                                       style="accent-color:var(--vsc-blue);">
                                <span class="badge badge-green">Active</span>
                            </label>
                            <label style="display:flex;align-items:center;gap:8px;cursor:pointer;font-size:13px;color:var(--vsc-text-primary);">
                                <input type="radio" name="is_active" value="0"
                                       {{ old('is_active', $user->is_active ?? 1) == 0 ? 'checked' : '' }}
                                       style="accent-color:var(--vsc-blue);">
                                <span class="badge badge-gray">Inactive</span>
                            </label>
                        </div>
                    </div>
                @endif

            </div>

            <div class="divider"></div>

            <div style="display:flex;gap:10px;justify-content:flex-end;">
                <a href="{{ route('admin.users.index') }}" class="btn btn-secondary">
                    <i data-lucide="x" style="width:13px;height:13px;"></i>
                    Cancel
                </a>
                <button type="submit" class="btn btn-primary">
                    <i data-lucide="{{ isset($user) ? 'save' : 'user-plus' }}" style="width:13px;height:13px;"></i>
                    {{ isset($user) ? 'Save Changes' : 'Create User' }}
                </button>
            </div>

        </form>
    </div>
</div>

@endsection
