@extends('layouts.app')

@section('title', 'Edit User Roles')
    
@section('content')
    <!-- [ page-header ] start -->
    <form id="updateUserRolesForm" method="POST" action="{{ route('user-roles.update', ['user' => $user->id]) }}">
        @csrf
        @method('PUT')
        <div class="page-header">
            <div class="page-header-left d-flex align-items-center">
                <div class="page-header-title">
                    <h5 class="m-b-10">User Roles</h5>
                </div>
                <ul class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ route('index') }}">Home</a></li>
                    <li class="breadcrumb-item">Update</li>
                </ul>
            </div>
            <div class="page-header-right ms-auto">
                <div class="page-header-right-items">
                    <div class="d-flex d-md-none">
                        <a href="javascript:void(0)" class="page-header-right-close-toggle">
                            <i class="feather-arrow-left me-2"></i>
                            <span>Back</span>
                        </a>
                    </div>
                    <div class="d-flex align-items-center gap-2 page-header-right-items-wrapper">
                        <a href="javascript:void(0);" class="btn btn-primary successAlertMessage"
                            onclick="document.getElementById('updateUserRolesForm').submit();">
                            <i class="feather-user-plus me-2"></i>
                            <span>Update User Role</span>
                        </a>
                    </div>
                </div>
                <div class="d-md-none d-flex align-items-center">
                    <a href="javascript:void(0)" class="page-header-right-open-toggle">
                        <i class="feather-align-right fs-20"></i>
                    </a>
                </div>
            </div>
        </div>
        <!-- [ page-header ] end -->
        <!-- [ Main Content ] start -->
        <div class="main-content">
            <div class="row">
                <div class="col-lg-12">
                    <div class="card border-top-0">
                        <div class="tab-content">
                            <div class="tab-pane fade show active" id="profileTab" role="tabpanel">
                                <div class="card-body personal-info">
                                    <div class="mb-4 d-flex align-items-center justify-content-between">
                                        <h5 class="fw-bold mb-0 me-4">
                                            <span class="d-block mb-2">User Information:</span>
                                        </h5>
                                    </div>

                                    <div class="row">
                                        <!-- User Name -->
                                        <div class="col-md-6 mb-4 align-items-center">
                                            <div class="col-lg-4 mb-2">
                                                <label for="fullnameInput" class="fw-semibold">User Name:</label>
                                            </div>
                                            <div class="col-lg-12">
                                                <div class="input-group">
                                                    <div class="input-group-text"><i class="feather-user"></i></div>
                                                    <input type="text" class="form-control" name="user_name"
                                                        id="fullnameInput" value="{{ old('user_name', $user->name) }}" required>
                                                </div>
                                            </div>
                                        </div>

                                        <!-- Email -->
                                        <div class="col-md-6 mb-4 align-items-center">
                                            <div class="col-lg-4 mb-2">
                                                <label for="mailInput" class="fw-semibold">Email:</label>
                                            </div>
                                            <div class="col-lg-12">
                                                <div class="input-group">
                                                    <div class="input-group-text"><i class="feather-mail"></i></div>
                                                    <input type="email" name="email" class="form-control"
                                                        value="{{ old('email', $user->email) }}" required>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="col-md-6 mb-4 align-items-center">
                                            <div class="col-lg-4 mb-2">
                                                <label for="status" class="fw-semibold">Status:</label>
                                            </div>
                                            <div class="col-lg-12">
                                                <div class="input-group">
                                                    <div class="input-group-text"><i class="feather-user"></i></div>
                                                    <select class="form-control" name="status" id="status" required>
                                                        <option value="1" {{ old('status', $user->status) == 1 ? 'selected' : '' }}>Active</option>
                                                        <option value="0" {{ old('status', $user->status) == 0 ? 'selected' : '' }}>Inactive</option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>

                                        <!-- Roles -->
                                        <div class="col-md-6 mb-4 align-items-center">
                                            <label class="fw-semibold mb-2">Team:</label>
                                            <div class="input-group">
                                                <div class="input-group-text"><i class="feather-users"></i></div>
                                                <select name="team_id" class="form-control @error('team_id') is-invalid @enderror" data-select2-selector="default" data-placeholder="Select Team">
                                                    <option value="">Select Team</option>
                                                    @foreach ($teams as $team)
                                                        <option value="{{ $team->id }}" @selected((int) old('team_id', $selectedTeamId) === (int) $team->id)>
                                                            {{ $team->name }}{{ $team->code ? ' · '.$team->code : '' }}
                                                        </option>
                                                    @endforeach
                                                </select>
                                            </div>
                                            @error('team_id')<div class="invalid-feedback d-block">{{ $message }}</div>@enderror
                                            <small class="text-muted">Changing the Team updates the employee membership.</small>
                                        </div>
                                        <div class="col-md-6 mb-4 align-items-center">
                                            <label class="fw-semibold mb-2">Reporting Manager:</label>
                                            <select name="reporting_manager_id" class="form-control">
                                                <option value="">Select Reporting Manager</option>
                                                @foreach ($users as $manager)
                                                    <option value="{{ $manager->id }}"
                                                        @selected(old('reporting_manager_id', $user->reporting_manager_id) == $manager->id)>
                                                        {{ $manager->name }} — {{ $manager->email }}
                                                    </option>
                                                @endforeach
                                            </select>
                                        </div>

                                        <!-- Roles -->
                                        <div class="col-md-6 mb-4 align-items-center">
                                            <div class="col-lg-4 mb-2">
                                                <label class="fw-semibold">Roles:</label>
                                            </div>
                                            <div class="col-lg-12">
                                                @foreach($roles as $role)
                                                
                                                    <div class="col-md-6 mb-2">
                                                        <div class="form-check">
                                                            <input class="form-check-input" type="checkbox" name="roles[]" 
                                                                value="{{ $role->id }}" id="role{{ $role->id }}"
                                                                {{ in_array($role->id, $userRoles) ? 'checked' : '' }}>
                                                            <label class="form-check-label" for="role{{ $role->id }}">
                                                                {{ $role->name }}
                                                            </label>
                                                        </div>
                                                    </div>
                                                @endforeach
                                            </div>
                                        </div>

                                        <!-- Access Type -->
                                        <div class="col-md-6 mb-4 align-items-center">
                                            <div class="col-lg-4 mb-2">
                                                <label class="fw-semibold">Access Type: </label>
                                            </div>
                                            <div class="col-lg-12">
                                                @foreach($accessTypes as $type)
                                                    <div class="form-check">
                                                        <input class="form-check-input access-type-radio"
                                                            type="radio"
                                                            name="access_type"
                                                            id="accessType{{ $type->id }}"
                                                            value="{{ $type->id }}"
                                                            data-access-name="{{ strtolower($type->name) }}"
                                                            {{ $userAccess && $userAccess->access_type_id == $type->id ? 'checked' : '' }}>
                                                        <label class="form-check-label" for="accessType{{ $type->id }}">
                                                            {{ $type->name }}
                                                        </label>
                                                    </div>
                                                @endforeach
                                            </div>
                                        </div>

                                        <!-- Specific Users -->
                                        <div class="col-md-6 mb-4 align-items-center"
                                            id="specific-user-field"
                                            style="{{ $userAccess && $userAccess->access_type_id == 3 ? '' : 'display:none;' }}">
                                            <div class="col-lg-4 mb-2">
                                                <label class="fw-semibold">Specific Users: </label>
                                            </div>
                                            <div class="col-lg-12">
                                                <select class="form-control js-example-basic-multiple"
                                                        name="specific_user[]" multiple="multiple" id="specific_user">
                                                    @foreach ($users as $u)
                                                        <option value="{{ $u->id }}"
                                                            {{ in_array($u->id, $specificUsers ?? []) ? 'selected' : '' }}>
                                                            {{ $u->name }}
                                                        </option>
                                                    @endforeach
                                                </select>
                                            </div>
                                        </div>
                                    </div><!-- end row -->

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <!-- [ Main Content ] end -->
@endsection

@section('scripts')
<script>
$(document).ready(function() {
    $('.js-example-basic-multiple').select2();
    
    // Handle access type radio button changes
    $('.access-type-radio').change(function() {
        const selectedAccessType = $(this).data('access-name');
        const specificUserField = $('#specific-user-field');
        const specificUserSelect = $('#specific_user');
        
        // Show the specific user field only if "specific user" is selected
        if (selectedAccessType === 'specific user' || selectedAccessType === 'specific users') {
            specificUserField.show();
            // Make it required when showing
            specificUserSelect.prop('required', true);
        } else {
            specificUserField.hide();
            // Remove required attribute when hiding
            specificUserSelect.prop('required', false);
            // Clear selected values when hiding
            specificUserSelect.val(null).trigger('change');
        }
    });
    
    // Hide the field initially on page load and make it not required
    $('#specific-user-field').hide();
    $('#specific_user').prop('required', false);
    
    // Form validation to ensure specific users are selected when required
    $('#createUserForm').on('submit', function(e) {
        const specificUserField = $('#specific-user-field');
        const specificUserSelect = $('#specific_user');
        
        // If the field is visible and required, check if any users are selected
        if (specificUserField.is(':visible') && specificUserSelect.prop('required')) {
            const selectedUsers = specificUserSelect.val();
            if (!selectedUsers || selectedUsers.length === 0) {
                e.preventDefault();
                alert('Please select at least one specific user.');
                return false;
            }
        }
    });
});
</script>

@endsection
