@extends('admin.setting.index')

@section('sub_content')
    <div class="setting-layout-body">
        <div class="add-company">
            <div class="add-company profile-settings mx-auto">
                <div class="d-flex justify-content-end mt-2">
                    <button type="button" data-bs-target="#testMailModal" data-bs-toggle="modal" class="btn btn--base btn--sm "><i class="las la-paper-plane"></i> @lang('Send Test Mail')</button>
                </div>
                <form action="{{ route('admin.notification.setting.config.save') }}" method="POST" class="add-company-form" enctype="multipart/form-data">
                    @csrf
                    <div class="card style-two w-100">
                        <div class="card-header">
                            <p class="card-title">{{ __($pageTitle) }}</p>
                        </div>
                        <div class="card-body">
                            <div class="row c-gx-4 gy-3 gy-md-4">
                                <div class="col-12">
                                    <label class="form--label style-two">@lang('Email Send Method')</label>
                                    <select name="email_method" class="select2 form--control md-style" data-minimum-results-for-search="-1">
                                        <option value="php" @if ($mailConfig->name == 'php') selected @endif>@lang('PHP Mail')</option>
                                        <option value="smtp" @if ($mailConfig->name == 'smtp') selected @endif>@lang('SMTP')</option>
                                        <option value="sendgrid" @if ($mailConfig->name == 'sendgrid') selected @endif>@lang('SendGrid API')</option>
                                        <option value="mailjet" @if ($mailConfig->name == 'mailjet') selected @endif>@lang('Mailjet API')</option>
                                    </select>
                                </div>

                            </div>
                            <div class="row c-gx-4 gy-3 gy-md-4 mt-1  d-none configForm" id="smtp">
                                <div class="col-md-12">
                                    <h6 class="mb-2">@lang('SMTP Configuration')</h6>
                                </div>
                                <div class="col-md-4">
                                    <label class="form--label style-two">@lang('Host') </label>
                                    <input type="text" class="form--control md-style" placeholder="e.g. @lang('smtp.googlemail.com')" name="host" value="{{ $mailConfig->host ?? '' }}">
                                </div>
                                <div class="col-md-4">
                                    <label class="form--label style-two">@lang('Port') </label>
                                    <input type="text" class="form--control md-style" placeholder="@lang('Available port')" name="port" value="{{ $mailConfig->port ?? '' }}">
                                </div>
                                <div class="col-md-4">
                                    <label class="form--label style-two">@lang('Encryption')</label>
                                    <select class="form--control md-style select2" data-minimum-results-for-search="-1" name="enc">
                                        <option value="ssl" @selected(isset($mailConfig->enc) && $mailConfig->enc == 'ssl')>@lang('SSL')</option>
                                        <option value="tls" @selected(isset($mailConfig->enc) && $mailConfig->enc == 'tls')>@lang('TLS')</option>
                                    </select>
                                </div>
                                <div class="col-md-6">
                                    <label class="form--label style-two">@lang('Username') </label>
                                    <input type="text" class="form--control md-style" placeholder="@lang('Normally your email') address" name="username" value="{{ $mailConfig->username ?? '' }}">
                                </div>
                                <div class="col-md-6">
                                    <label class="form--label style-two">@lang('Password') </label>
                                    <input type="text" class="form--control md-style" placeholder="@lang('Normally your email password')" name="password" value="{{ $mailConfig->password ?? '' }}">
                                </div>
                            </div>
                            <div class="row c-gx-4 gy-3 gy-md-4 mt-1 d-none configForm" id="sendgrid">
                                <div class="col-md-12">
                                    <h6 class="mb-2">@lang('SendGrid API Configuration')</h6>
                                </div>
                                <div class="col-md-12">
                                    <label class="form--label style-two">@lang('App Key') </label>
                                    <input type="text" class="form--control md-style" placeholder="@lang('SendGrid App key')" name="appkey" value="{{ $mailConfig->appkey ?? '' }}">
                                </div>
                            </div>
                            <div class="row c-gx-4 gy-3 gy-md-4 mt-1 d-none configForm" id="mailjet">
                                <div class="col-md-12">
                                    <h6 class="mb-2">@lang('Mailjet API Configuration')</h6>
                                </div>
                                <div class="col-md-6">
                                    <label class="form--label style-two">@lang('Api Public Key') </label>
                                    <input type="text" class="form--control md-style" placeholder="@lang('Mailjet Api Public Key')" name="public_key" value="{{ $mailConfig->public_key ?? '' }}">
                                </div>
                                <div class="col-md-6">
                                    <label class="form--label style-two">@lang('Api Secret Key') </label>
                                    <input type="text" class="form--control md-style" placeholder="@lang('Mailjet Api Secret Key')" name="secret_key" value="{{ $mailConfig->secret_key ?? '' }}">
                                </div>
                            </div>
                            <div class="mt-3 mt-md-4">
                                <button type="submit" class="btn btn--base btn--md w-100">@lang('Submit')</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>


    {{-- TEST MAIL MODAL --}}
    <div id="testMailModal" class="modal fade" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">@lang('Test Mail Setup')</h5>
                    <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                        <i class="las la-times"></i>
                    </button>
                </div>
                <form action="{{ route('admin.notification.setting.email.test') }}" method="POST">
                    @csrf
                    <input type="hidden" name="id">
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <label class="form--label style-two">@lang('Sent to') </label>
                                <input type="text" name="email" class="form--control md-style" placeholder="@lang('Email Address')">
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn--primary w-100 btn--md">@lang('Submit')</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection


@push('script')
    <script>
        (function($) {
            "use strict";

            var method = '{{ $mailConfig->name }}';
            emailMethod(method);
            $('select[name=email_method]').on('change', function() {
                var method = $(this).val();
                emailMethod(method);
            });

            function emailMethod(method) {
                $('.configForm').addClass('d-none');
                if (method != 'php') {
                    $(`#${method}`).removeClass('d-none');
                }
            }

        })(jQuery);
    </script>
@endpush
