@extends('layouts.app')

@section('title', __('vetClinic::lang.module_settings'))

@section('css')
@include('vetClinic::partials._mobile_responsive')
@endsection

@section('content')
<section class="content-header">
    <h1><i class="fas fa-cog"></i> @lang('vetClinic::lang.module_settings')</h1>
</section>

<section class="content">

    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            @component('components.widget', ['class' => 'box-primary'])
                <form id="settings_form">
                    @csrf

                    {{-- Doctors shared --}}
                    <div class="form-group">
                        <label class="control-label" style="font-size:15px;">
                            @lang('vetClinic::lang.doctors_shared_between_locations')
                        </label>
                        <p class="text-muted" style="margin-top:2px;">@lang('vetClinic::lang.doctors_shared_help')</p>
                        <div>
                            <label class="radio-inline">
                                <input type="radio" name="doctors_shared" value="1" {{ $settings['doctors_shared'] != '0' ? 'checked' : '' }}>
                                Yes
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="doctors_shared" value="0" {{ $settings['doctors_shared'] == '0' ? 'checked' : '' }}>
                                No
                            </label>
                        </div>
                    </div>

                    <hr>

                    {{-- Allow overlap --}}
                    <div class="form-group">
                        <label class="control-label" style="font-size:15px;">
                            @lang('vetClinic::lang.allow_overlap')
                        </label>
                        <p class="text-muted" style="margin-top:2px;">@lang('vetClinic::lang.allow_overlap_help')</p>
                        <div>
                            <label class="radio-inline">
                                <input type="radio" name="allow_overlap" value="1" {{ $settings['allow_overlap'] != '0' ? 'checked' : '' }}>
                                Yes
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="allow_overlap" value="0" {{ $settings['allow_overlap'] == '0' ? 'checked' : '' }}>
                                No
                            </label>
                        </div>
                    </div>

                    <hr>

                    {{-- Vaccination reminder days --}}
                    <div class="form-group">
                        <label class="control-label" style="font-size:15px;">
                            @lang('vetClinic::lang.reminder_days')
                        </label>
                        <div class="row">
                            <div class="col-md-3">
                                <input type="number" name="reminder_days" class="form-control"
                                       value="{{ $settings['reminder_days'] }}" min="1" max="365">
                            </div>
                        </div>
                    </div>

                    <hr>

                    {{-- SOAP chip limit --}}
                    <div class="form-group">
                        <label class="control-label" style="font-size:15px;">
                            <i class="fas fa-tag fa-fw"></i> Consultation Chip Limit
                        </label>
                        <p class="text-muted" style="margin-top:2px;">
                            How many quick-select chips to show by default in the S / I sections during consultation.
                            Options beyond this limit are found via the "+ Search more…" input.
                            Minimum 5, maximum 50.
                        </p>
                        <div class="row">
                            <div class="col-md-3">
                                <input type="number" name="soap_chip_limit" class="form-control"
                                       value="{{ $settings['soap_chip_limit'] }}" min="5" max="50">
                            </div>
                        </div>
                    </div>

                    <hr>

                    {{-- Action button target (new tab vs same tab) --}}
                    <div class="form-group">
                        <label class="control-label" style="font-size:15px;">
                            <i class="fas fa-external-link-alt fa-fw"></i> Open action buttons in
                        </label>
                        <p class="text-muted" style="margin-top:2px;">
                            Choose how Edit / View action buttons in list tables open the target page.
                            "Same tab" preserves filter state when you go back; "New tab" lets you compare records side-by-side.
                        </p>
                        <div>
                            <label class="radio-inline">
                                <input type="radio" name="action_target" value="_self" {{ ($settings['action_target'] ?? '_self') === '_self' ? 'checked' : '' }}>
                                Same tab
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="action_target" value="_blank" {{ ($settings['action_target'] ?? '_self') === '_blank' ? 'checked' : '' }}>
                                New tab
                            </label>
                        </div>
                    </div>

                    <hr>

                    <div class="form-group text-right">
                        <button type="button" class="btn btn-primary" id="btn_save_settings">
                            <i class="fa fa-save"></i> @lang('vetClinic::lang.save_settings')
                        </button>
                    </div>
                </form>
            @endcomponent
        </div>
    </div>
</section>
@endsection

@section('javascript')
<script>
$('#btn_save_settings').on('click', function () {
    var data = {
        _token:          '{{ csrf_token() }}',
        doctors_shared:  $('input[name="doctors_shared"]:checked').val(),
        allow_overlap:   $('input[name="allow_overlap"]:checked').val(),
        reminder_days:   $('input[name="reminder_days"]').val(),
        soap_chip_limit: $('input[name="soap_chip_limit"]').val(),
        action_target:   $('input[name="action_target"]:checked').val(),
    };

    $.ajax({
        url:    '{{ route('vc.settings.save') }}',
        method: 'POST',
        data:   data,
        success: function (res) {
            if (res.success) toastr.success(res.msg);
            else             toastr.error(res.msg ?? 'Error');
        },
        error: function (xhr) {
            var errors = xhr.responseJSON?.errors;
            if (errors) {
                $.each(errors, function (k, v) { toastr.error(v[0]); });
            } else {
                toastr.error(xhr.responseJSON?.message ?? 'Error saving settings.');
            }
        }
    });
});
</script>
@endsection
