<x-app-layout :assets="$assets ?? []">
    <div>
        <div class="row">
            <div class="col-xl-12 col-lg-12">
                <div class="card">
                    <div class="card-header d-flex justify-content-between">
                        <div class="header-title">
                            <h4 class="card-title">Create API Key</h4>
                        </div>
                        <div class="card-action">
                            <a href="{{ route("apikeys") }}" class="btn btn-sm btn-primary" role="button">Back</a>
                        </div>
                    </div>
                    <div class="card-body">
                        <form action="{{ route("storeapikeys") }}" method="POST" role="form text-left" class="needs-validation" enctype="multipart/form-data">
                            @csrf
                            <div class="new-user-info">
                                <div class="row">
                                    <div class="form-group col-md-6">
                                        <label class="form-label" for="name">Source Name: <span class="text-danger">*</span></label>
                                        <input type="text" id="source" class="form-control" name="source" value="" required>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label class="form-label" for="email">API Key: <span class="text-danger">*</span></label>
                                        <input type="text" id="apikey" class="form-control" name="apikey" value="" required readonly>
                                    </div>
                                </div>
                                <button type="submit" class="btn btn-sm btn-primary">Save</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        document.getElementById('source').addEventListener('change', function() {
            document.getElementById('apikey').value = generateRandomKey();
        });

        function generateRandomKey() {
            var length = 16;
            var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+[]{}|;:,.<>?';
            var charactersLength = characters.length;
            var result = '';
            for (var i = 0; i < length; i++) {
                result += characters.charAt(Math.floor(Math.random() * charactersLength));
            }
            return result;
        }
    </script>
</x-app-layout>
