@php
    $segment = (request()->segment(2) == 'admin' ? request()->segment(3):request()->segment(2)); // supplier-groups
@endphp

<x-button type="submit" color="primary" id="submit_button" i="save" label="{{ __('dashboard.save') }}" />

<x-button
    type="link"
    color="danger"
    i="times"
    label="{{ __('dashboard.cancel') }}"
    href="{{ route($segment . '.index') }}"
/>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        let isProcessing = false;

        const submitButton = document.getElementById('submit_button');
        const form = document.querySelector('form');

        // عند تقديم الفورم
        if (form) {
            form.addEventListener('submit', function(e) {
                if (isProcessing) {
                    e.preventDefault();
                    return;
                }
                isProcessing = true;
                if (submitButton) submitButton.disabled = true;

                // تعطيل زر Cancel
                const cancelButtons = document.querySelectorAll('a.btn, button[type="button"]');
                cancelButtons.forEach(btn => {
                    btn.style.pointerEvents = 'none';
                    btn.style.opacity = '0.5';
                });
            });
        }

        // تعطيل الزرين عند الضغط على Cancel
        const cancelLinks = document.querySelectorAll('a.btn');
        cancelLinks.forEach(link => {
            link.addEventListener('click', function(e) {
                if (isProcessing) {
                    e.preventDefault();
                    return;
                }
                isProcessing = true;
                if (submitButton) submitButton.disabled = true;
                link.style.pointerEvents = 'none';
                link.style.opacity = '0.5';
            });
        });
    });
</script>
