@php
    $showSubject = in_array($channel, ['in_app', 'email']);
@endphp
<div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
        {!! Form::open(['route' => 'mednotify.templates.update', 'class' => 'mednotify-form']) !!}
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">
                {{ $event->label }} — @lang('mednotify::lang.channel_' . $channel)
            </h4>
        </div>
        <div class="modal-body">
            {!! Form::hidden('event_key', $event->event_key) !!}
            {!! Form::hidden('channel', $channel) !!}

            @if(! $isOverride)
                <p class="help-block"><i class="fa fa-info-circle"></i> @lang('mednotify::lang.showing_system_default')</p>
            @else
                <p class="help-block text-primary"><i class="fa fa-check-circle"></i> @lang('mednotify::lang.showing_business_override')</p>
            @endif

            @if($showSubject)
                <div class="form-group">
                    {!! Form::label('subject', __('mednotify::lang.subject') . ':') !!}
                    {!! Form::text('subject', $template->subject ?? null, ['class' => 'form-control']) !!}
                </div>
            @endif

            <div class="form-group">
                {!! Form::label('body', __('mednotify::lang.body') . ':*') !!}
                {!! Form::textarea('body', $template->body ?? null, ['class' => 'form-control', 'rows' => 6, 'required']) !!}
            </div>

            <div class="checkbox">
                <label>
                    {!! Form::checkbox('enabled', 1, $template ? (bool) $template->enabled : true) !!}
                    @lang('mednotify::lang.enabled')
                </label>
            </div>

            @if(!empty($variables))
                <div class="well well-sm">
                    <strong>@lang('mednotify::lang.available_variables'):</strong>
                    <div>
                        @foreach($variables as $var)
                            @php
                                $vname = is_array($var) ? ($var['name'] ?? '') : $var;
                                $vlabel = is_array($var) ? ($var['label'] ?? $vname) : $var;
                            @endphp
                            <code title="{{ $vlabel }}">{{ '{' . $vname . '}' }}</code>&nbsp;
                        @endforeach
                        <code>{business_name}</code> <code>{user_name}</code> <code>{contact_name}</code>
                    </div>
                </div>
            @endif
        </div>
        <div class="modal-footer">
            <button type="submit" class="btn btn-primary">@lang('messages.save')</button>
            @if($isOverride)
                <button type="button" class="btn btn-warning mednotify-reset-btn"
                    data-event="{{ $event->event_key }}" data-channel="{{ $channel }}">
                    <i class="fa fa-undo"></i> @lang('mednotify::lang.reset_to_default')
                </button>
            @endif
            <button type="button" class="btn btn-default" data-dismiss="modal">@lang('messages.close')</button>
        </div>
        {!! Form::close() !!}
    </div>
</div>
<script>
$(function () {
    $(document).off('click.mnReset').on('click.mnReset', '.mednotify-reset-btn', function () {
        if (!window.confirm('@lang('mednotify::lang.confirm_reset')')) return;
        var $btn = $(this).prop('disabled', true);
        $.ajax({
            url: '{{ route('mednotify.templates.reset') }}',
            method: 'POST',
            data: {
                _token: $('meta[name=csrf-token]').attr('content'),
                event_key: $btn.data('event'),
                channel: $btn.data('channel')
            },
            success: function (resp) {
                if (resp.success) {
                    toastr.success(resp.msg);
                    $btn.closest('.modal').modal('hide');
                } else { toastr.error(resp.msg || 'Something went wrong.'); }
                $btn.prop('disabled', false);
            },
            error: function () { $btn.prop('disabled', false); toastr.error('Something went wrong.'); }
        });
    });
});
</script>
