@if($errors->any())
<script>
document.addEventListener('DOMContentLoaded', function() {
    new bootstrap.Modal(document.getElementById('editAttendanceModal')).show();
});
</script>
@endif

<script>
function editAttendance(id) {
    fetch(`{{ url('admin/attendance') }}/${id}/edit`)
        .then(response => response.json())
        .then(data => {
            document.getElementById('edit_user_name').value = data.user_name;
            document.getElementById('edit_date').value = data.date;
            document.getElementById('edit_status').value = data.status;
            document.getElementById('edit_check_in_time').value = data.check_in_time;
            document.getElementById('edit_check_out_time').value = data.check_out_time;
            document.getElementById('edit_notes').value = data.notes || '';
            document.getElementById('editAttendanceForm').action = `{{ url('admin/attendance') }}/${id}/update`;
            
            toggleTimeFields();
            new bootstrap.Modal(document.getElementById('editAttendanceModal')).show();
        });
}

function confirmDeleteAttendance(id, name, date) {
    document.getElementById('deleteAttendanceName').textContent = name;
    document.getElementById('deleteAttendanceDate').textContent = date;
    document.getElementById('deleteAttendanceForm').action = `{{ url('admin/attendance') }}/${id}/delete`;
    new bootstrap.Modal(document.getElementById('deleteAttendanceModal')).show();
}

function toggleTimeFields() {
    const status = document.getElementById('edit_status').value;
    const timeFields = document.getElementById('timeFields');
    const checkInField = document.getElementById('edit_check_in_time');
    const checkOutField = document.getElementById('edit_check_out_time');
    
    if (status === 'hadir') {
        timeFields.style.display = 'block';
        checkInField.removeAttribute('disabled');
        checkOutField.removeAttribute('disabled');
    } else {
        timeFields.style.display = 'none';
        checkInField.value = '';
        checkOutField.value = '';
        checkInField.setAttribute('disabled', 'disabled');
        checkOutField.setAttribute('disabled', 'disabled');
    }
}
</script>