@extends('layouts.app')
@section('title', __('clinicCore::lang.reminder_settings'))

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

@section('content')
<section class="content-header">
    <h1><i class="fa fa-bell"></i> @lang('clinicCore::lang.reminder_settings')</h1>
</section>

<section class="content">
    <div class="row">
        <div class="col-md-8">
            @component('components.widget', ['class' => 'box-primary', 'title' => __('clinicCore::lang.reminder_settings')])

                @if(session('status'))
                    <div class="alert alert-{{ session('status.success') ? 'success' : 'danger' }}">
                        {{ session('status.msg') }}
                    </div>
                @endif

                <form method="POST" action="{{ route('clinic.reminders.settings.save') }}">
                    @csrf

                    {{-- Master toggle --}}
                    <div class="form-group">
                        <div class="checkbox">
                            <label style="font-size:15px;font-weight:600;">
                                <input type="checkbox" name="is_enabled" value="1"
                                    {{ $settings->is_enabled ? 'checked' : '' }}>
                                @lang('clinicCore::lang.enable_reminders')
                            </label>
                            <p class="text-muted" style="margin-left:20px;margin-top:4px;">
                                @lang('clinicCore::lang.enable_reminders_help')
                            </p>
                        </div>
                    </div>

                    <hr>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>@lang('clinicCore::lang.days_before_due')</label>
                                <div class="input-group" style="max-width:160px;">
                                    <input type="number" name="days_before_due" class="form-control"
                                        value="{{ $settings->days_before_due }}" min="1" max="90" required>
                                    <span class="input-group-addon">@lang('clinicCore::lang.days')</span>
                                </div>
                                <p class="help-block">@lang('clinicCore::lang.days_before_due_help')</p>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>@lang('clinicCore::lang.max_reminders_per_vaccine')</label>
                                <input type="number" name="max_reminders_per_vaccine" class="form-control"
                                    style="max-width:120px;"
                                    value="{{ $settings->max_reminders_per_vaccine }}" min="1" max="10" required>
                                <p class="help-block">@lang('clinicCore::lang.max_reminders_help')</p>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" name="send_overdue" value="1"
                                    {{ $settings->send_overdue ? 'checked' : '' }}>
                                @lang('clinicCore::lang.send_overdue_reminders')
                            </label>
                        </div>
                    </div>

                    <div class="form-group" id="overdue_interval_wrap">
                        <label>@lang('clinicCore::lang.overdue_reminder_interval')</label>
                        <div class="input-group" style="max-width:160px;">
                            <input type="number" name="overdue_reminder_interval" class="form-control"
                                value="{{ $settings->overdue_reminder_interval }}" min="1" max="30" required>
                            <span class="input-group-addon">@lang('clinicCore::lang.days')</span>
                        </div>
                        <p class="help-block">@lang('clinicCore::lang.overdue_interval_help')</p>
                    </div>

                    <hr>
                    <h4>@lang('clinicCore::lang.channels')</h4>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="email_enabled" value="1"
                                            {{ $settings->email_enabled ? 'checked' : '' }}>
                                        <i class="fa fa-envelope text-primary"></i>
                                        @lang('clinicCore::lang.email_reminders')
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="wa_enabled" value="1"
                                            {{ $settings->wa_enabled ? 'checked' : '' }}>
                                        <i class="fab fa-whatsapp text-success"></i>
                                        @lang('clinicCore::lang.wa_reminders')
                                        <span class="label label-default" style="font-size:10px;">Manual only</span>
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="sms_enabled" value="1"
                                            {{ ($settings->sms_enabled ?? false) ? 'checked' : '' }}>
                                        <i class="fa fa-sms text-info"></i>
                                        SMS reminders
                                        <span class="label label-default" style="font-size:10px;">
                                            Uses business SMS settings
                                        </span>
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label>@lang('clinicCore::lang.schedule_time')</label>
                        <input type="time" name="schedule_time" class="form-control"
                            style="max-width:140px;"
                            value="{{ $settings->schedule_time }}" required>
                        <p class="help-block">@lang('clinicCore::lang.schedule_time_help')</p>
                    </div>

                    <div class="form-group">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-save"></i> @lang('clinicCore::lang.save')
                        </button>
                    </div>
                </form>

            @endcomponent
        </div>

        <div class="col-md-4">
            {{-- Stats & Manual trigger --}}
            @component('components.widget', ['class' => 'box-info', 'title' => 'Reminder Status'])
                <dl class="dl-horizontal">
                    <dt>Today sent:</dt>
                    <dd><strong>{{ $sentToday }}</strong></dd>
                    <dt>Last sent:</dt>
                    <dd>
                        @if($lastLog)
                            {{ \Carbon\Carbon::parse($lastLog->sent_at)->format(session('business.date_format','d M Y') . ' H:i') }}
                            <br><small class="text-muted">to {{ $lastLog->sent_to }}</small>
                        @else
                            <span class="text-muted">Never</span>
                        @endif
                    </dd>
                </dl>
                <hr>
                <p class="text-muted" style="font-size:12px;">
                    Send all pending reminders right now (ignores schedule time).
                    Only emails confirmed patient owners.
                </p>
                <button id="btn_send_now" class="btn btn-warning btn-block">
                    <i class="fa fa-paper-plane"></i> Send Now
                </button>
                <div id="send_now_result" style="margin-top:10px;display:none;"></div>
            @endcomponent

            @component('components.widget', ['class' => 'box-default', 'title' => 'Templates'])
                <p class="text-muted" style="font-size:12px;">
                    Customise the email and WhatsApp message templates sent to patient owners.
                </p>
                <a href="{{ route('clinic.reminders.templates') }}" class="btn btn-default btn-block">
                    <i class="fa fa-file-text"></i> Manage Templates
                </a>
            @endcomponent
        </div>
    </div>
</section>
@endsection

@section('javascript')
<script>
$(document).ready(function () {

    $('input[name="send_overdue"]').on('change', function () {
        $('#overdue_interval_wrap').toggle(this.checked);
    }).trigger('change');

    $('#btn_send_now').on('click', function () {
        var $btn = $(this);
        $btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> Sending...');
        $('#send_now_result').hide();

        $.post('{{ route("clinic.reminders.send-now") }}', {_token: '{{ csrf_token() }}'})
            .done(function (res) {
                $('#send_now_result')
                    .removeClass('alert-danger').addClass('alert alert-success')
                    .html('<i class="fa fa-check"></i> ' + res.msg)
                    .show();
            })
            .fail(function () {
                $('#send_now_result')
                    .removeClass('alert-success').addClass('alert alert-danger')
                    .html('<i class="fa fa-times"></i> Failed to send reminders.')
                    .show();
            })
            .always(function () {
                $btn.prop('disabled', false).html('<i class="fa fa-paper-plane"></i> Send Now');
            });
    });
});
</script>
@endsection
