@section('header')
	@include('common.header')
@stop

@section('content')
	<div class="row">
		<div class="col-sm-8">
			<div class="widget widget-classic">
				<div class="widget-content">
					<p class="lead">Add a new email address</p>

					<form method="POST" action="{{ route('admin.settings.emails.store') }}" class="form-horizontal form-edit">
						@csrf

						<!-- Include form fields -->
						@include('admin::settings.emails.partials.form-fields')

						<div class="text-right form-actions">
							<button type="submit" class="btn btn-primary">Create</button>
							<a href="{!! URL::route('admin.settings.emails.index') !!}" class="btn btn-default">Cancel</a>
						</div>
					</form>

				</div>
			</div>
		</div>
		<div class="col-sm-4">
			@include('admin::settings.emails.partials.info')
		</div>
	</div>
@stop

@section('js')
	<!-- Test Connection JavaScript -->
	<script>
	$(document).ready(function() {
		console.log('Test connection JavaScript loaded');
		
		// Test IMAP Connection
		$('#test-imap-btn').click(function() {
			console.log('IMAP test button clicked');
			var btn = $(this);
			var resultSpan = $('#imap-test-result');
			
			// Disable button and show loading
			btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> Testing...');
			resultSpan.hide();
			
			// Gather IMAP form data
			var data = {
				server: $('input[name="server"]').val(),
				port: $('input[name="port"]').val(),
				protocol: $('select[name="protocol"]').val(),
				username: $('input[name="username"]').val(),
				password: $('input[name="password"]').val(),
				_token: $('meta[name="csrf-token"]').attr('content')
			};
			
			console.log('IMAP test data:', data);
			
			// Make AJAX request
			$.post('/admin/settings/emails/test-imap', data)
				.done(function(response) {
					console.log('IMAP test response:', response);
					if (response.success) {
						resultSpan.removeClass('text-danger').addClass('text-success')
							.html('<i class="fa fa-check"></i> ' + response.message).show();
					} else {
						resultSpan.removeClass('text-success').addClass('text-danger')
							.html('<i class="fa fa-times"></i> ' + response.message).show();
					}
				})
				.fail(function(xhr) {
					console.log('IMAP test failed:', xhr);
					var message = 'Connection test failed';
					if (xhr.responseJSON && xhr.responseJSON.message) {
						message = xhr.responseJSON.message;
					}
					resultSpan.removeClass('text-success').addClass('text-danger')
						.html('<i class="fa fa-times"></i> ' + message).show();
				})
				.always(function() {
					// Re-enable button
					btn.prop('disabled', false).html('<i class="fa fa-download"></i> Test IMAP Connection');
				});
		});
		
		// Test SMTP Connection
		$('#test-smtp-btn').click(function() {
			console.log('SMTP test button clicked');
			var btn = $(this);
			var resultSpan = $('#smtp-test-result');
			
			// Disable button and show loading
			btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> Testing...');
			resultSpan.hide();
			
			// Gather SMTP form data
			var data = {
				smtp_host: $('input[name="smtp_host"]').val(),
				smtp_port: $('input[name="smtp_port"]').val(),
				smtp_encryption: $('select[name="smtp_encryption"]').val(),
				smtp_username: $('input[name="smtp_username"]').val(),
				smtp_password: $('input[name="smtp_password"]').val(),
				smtp_from_name: $('input[name="smtp_from_name"]').val(),
				smtp_from_address: $('input[name="smtp_from_address"]').val(),
				smtp_cc: $('input[name="smtp_cc"]').val(),
				smtp_bcc: $('input[name="smtp_bcc"]').val(),
				smtp_test_to: $('input[name="smtp_test_to"]').val(),
				email: $('input[name="email"]').val(),
				_token: $('meta[name="csrf-token"]').attr('content')
			};
			
			console.log('SMTP test data:', data);
			
			// Make AJAX request
			$.post('/admin/settings/emails/test-smtp', data)
				.done(function(response) {
					console.log('SMTP test response:', response);
					if (response.success) {
						resultSpan.removeClass('text-danger').addClass('text-success')
							.html('<i class="fa fa-check"></i> ' + response.message).show();
					} else {
						resultSpan.removeClass('text-success').addClass('text-danger')
							.html('<i class="fa fa-times"></i> ' + response.message).show();
					}
				})
				.fail(function(xhr) {
					console.log('SMTP test failed:', xhr);
					var message = 'Connection test failed';
					if (xhr.responseJSON && xhr.responseJSON.message) {
						message = xhr.responseJSON.message;
					}
					resultSpan.removeClass('text-success').addClass('text-danger')
						.html('<i class="fa fa-times"></i> ' + message).show();
				})
				.always(function() {
					// Re-enable button
					btn.prop('disabled', false).html('<i class="fa fa-send"></i> Test SMTP Connection');
				});
		});
	});
	</script>
@stop