{{-- <input
    type="file"
    id="folderPicker"
    webkitdirectory
    directory
    multiple
    style="display:none"
/>

<script>
function openFolderPicker() {
    const input = document.getElementById('folderPicker');
    input.value = ''; // reset
    input.click();    // open system folder dialog

    input.onchange = function(event) {
        const files = event.target.files;
        console.log("Files selected:", files);
        alert(`${files.length} files selected`);
    };
}
</script> --}}


<!-- Hidden input for folder picker -->
{{-- <input
    type="file"
    id="folderPicker"
    webkitdirectory
    directory
    multiple
    accept=".txt"
    style="display:none"
/>

<script>
function openFolderPicker() {
    const input = document.getElementById('folderPicker');
    input.value = '';
    input.click();

    input.onchange = function(event) {
        const files = Array.from(event.target.files);
        const txtFiles = files.filter(f => f.name.endsWith('.txt'));

        if (txtFiles.length === 0) {
            alert("No .txt files found in selected folder!");
            return;
        }

        txtFiles.forEach(file => {
            const reader = new FileReader();
            reader.onload = function(e) {
                const content = e.target.result;
                console.log("File:", file.name, "Content:", content);
                alert(`File: ${file.name}\nContent:\n${content}`);
            };
            reader.readAsText(file);
        });
    };
}
</script> --}}


<!-- Hidden file input -->
{{-- <input type="file" id="fileInput" style="display:none" />

<script>
function openFileDialog() {
    const input = document.getElementById('fileInput');
    input.value = ''; // reset
    input.click();    // open system file dialog

    input.onchange = function(event) {
        const file = event.target.files[0]; // get selected file
        if (!file) {
            alert("No file selected!");
            return;
        }

        const reader = new FileReader();

        reader.onload = function(e) {
            const content = e.target.result;
            console.log("File content:", content);

            // Show content in page (or Filament modal)
            alert(`File: ${file.name}\n\nContent:\n${content}`);
        };

        reader.onerror = function() {
            alert("Failed to read file!");
        };

        reader.readAsText(file); // read as text
    };
}
</script> --}}


<!-- Hidden file input -->
<input type="file" id="fileInput" style="display:none" />

<script>
function openFileDialog() {
    const input = document.getElementById('fileInput');
    input.value = '';
    input.click();

    input.onchange = function(event) {
        const file = event.target.files[0];
        if (!file) {
            alert("No file selected!");
            return;
        }

        const reader = new FileReader();

        reader.onload = function(e) {
            const content = e.target.result;

            // Split file lines into array
            const lines = content.split(/\r?\n/).map(l => l.trim());

            const payload = {
                name: lines[0] ?? '',
                identification1: lines[1] ?? '',
                identification2: lines[2] ?? '',
                identification3: lines[3] ?? '',
                contact_number: lines[4] ?? '',
                alternate_number: lines[5] ?? '',
            };

            console.log("Payload to send:", payload);

            // Send to backend
            fetch("{{ route('file.store') }}", {
                method: "POST",
                headers: {
                    "X-CSRF-TOKEN": "{{ csrf_token() }}",
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(payload)
            })
            .then(res => res.json())
            .then(data => {
                if (data.success) {
                    alert("DriverMaster record saved successfully!");
                } else {
                    alert("Failed to save record.");
                }
            })
            .catch(err => {
                console.error(err);
                alert("Error saving record.");
            });
        };

        reader.onerror = function() {
            alert("Failed to read file!");
        };

        reader.readAsText(file);
    };
}
</script>




