
<?php $__env->startSection('panel'); ?>
<section>
    <?php
        $jsonArray = json_encode($credentials);
    ?>
    <div class="container-fluid p-0">
		<div class="table_heading d-flex align--center justify--between">
			<nav  aria-label="breadcrumb">
				  <ol class="breadcrumb">
					<li class="breadcrumb-item"><a href="<?php echo e(route('admin.mail.list')); ?>"> <?php echo e(translate('Mail Configuration')); ?></a></li> 
				  </ol>
			</nav>
		</div>
		<div class="card ">
			<div class="card-header">
				<h4 class="card-title"><?php echo e(translate('Email Gateway List')); ?></h4>
                <div class="d-flex justify-content-end">
                    <form action="javascript:void(0)" class="testDefaultGateway" method="post">
                        <?php echo csrf_field(); ?>
                        <div class="input-group w-100">
                            <input type="email" name="email" class="form-control" placeholder="<?php echo e(translate('Type Mail Address')); ?>">
                            <button class="i-btn primary--btn btn--md" id="basic-addon2" type="submit"><?php echo e(translate('Test Default Gateway')); ?></button>
                        </div>
                    </form>
                </div>
			</div>

			

			<div class="card-body px-0">
				<div class="responsive-table">
					<table>
						<thead>
							<tr>
                                <th><?php echo e(translate('Sl No')); ?></th>
                                <th><?php echo e(translate('Gateway type')); ?></th>
								<th><?php echo e(translate('Name')); ?></th>
								<th><?php echo e(translate('Address')); ?></th>
                                <th><?php echo e(translate('Status')); ?></th>
								<th><?php echo e(translate('Default')); ?></th>
								<th><?php echo e(translate('Action')); ?></th>
							</tr>
						</thead>
						<?php $__empty_1 = true; $__currentLoopData = $gateways; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $log): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                            
							<tr class="<?php if($loop->even): ?><?php endif; ?>">
								<td class="d-none d-sm-flex align-items-center">
									
									<?php echo e($loop->iteration); ?>

                                   
								</td>

                                <td data-label="<?php echo e(translate('Name')); ?>" >
                                   <?php echo e(strToUpper($log?->type)); ?> 
                                 
								</td>

                                <td data-label="<?php echo e(translate('Name')); ?>">
                                    
                                    <?php echo e($log?->name); ?>

								
								</td>


								<td data-label="<?php echo e(translate('Address')); ?>">
                                    <?php echo e($log?->address); ?>

								</td>
                                <td data-label="<?php echo e(translate('Status')); ?>">
                                    <?php if($log?->status == 1): ?>
                                        <span class="badge badge--success"><?php echo e(translate('Active')); ?></span>
                                    <?php else: ?>
                                        <span class="badge badge--danger"><?php echo e(translate('Inactive')); ?></span>
                                    <?php endif; ?>
                                </td>

							
                                <td class="text-center" data-label="<?php echo e(translate('default')); ?>">
                                    <div class="d-flex justify-content-md-start justify-content-end">
                                        <?php if($log?->is_default == 1): ?>
                                            <i class="las la-check-double text--success" style="font-size:32px"></i>
                                        <?php else: ?>
                                            <label class="switch">
                                                <input type="checkbox" class="default_status" data-id="<?php echo e($log->id); ?>" value="1" name="default_value" id="default_gateway">
                                                <span class="slider"></span>
                                            </label>
                                        <?php endif; ?>
                                    </div>
                                </td>

								<td data-label=<?php echo e(translate('Action')); ?>>
									<div class="d-flex align-items-center justify-content-md-start justify-content-end gap-3">
										
                                        <a class="i-btn info--btn btn--sm gateway-details <?php echo e($log->primary_status == 1 ? "d-none" : ''); ?>"
                                            data-driver_information="<?php echo e(json_encode($log->mail_gateways)); ?>"
                                            data-bs-placement="top" title="Gateway Information"
                                            data-bs-toggle="modal"
                                            data-bs-target="#gatewayInfo">
                                            <i class="las la-info-circle"></i>
                                        </a>    
										<a href="javascript:void(0)" class="i-btn success--btn btn--sm edit-gateway" 
                                            data-bs-toggle="modal"
                                            data-bs-target="#editgateway"
                                            data-id="<?php echo e($log?->id); ?>"
                                            data-gateway_type="<?php echo e($log?->type); ?>"
                                            data-gateway_name="<?php echo e($log?->name); ?>"
                                            data-gateway_address="<?php echo e($log?->address); ?>"
                                            data-gateway_driver_information="<?php echo e(json_encode($log?->mail_gateways)); ?>"
                                            data-gateway_status="<?php echo e($log?->status); ?>">
                                            <i class="las la-pen"></i>
                                        </a>
	
										<a href="javascript:void(0)" class="i-btn danger--btn btn--sm delete"
											data-bs-toggle="modal"
											data-bs-target="#gatewayDelete"
											data-delete_id="<?php echo e($log->id); ?>"
											><i class="las la-trash"></i>
										</a>
                                     
									</div>
									
								</td>
							</tr>
						<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
							<tr>
								<td class="text-muted text-center" colspan="100%"><?php echo e(translate('No Data Found')); ?></td>
							</tr>
						<?php endif; ?>
					</table>
				</div>
				<div class="m-3">
					<?php echo e($gateways->appends(request()->all())->onEachSide(1)->links()); ?>

				</div>
			</div>
		</div>
        <a href="javascript:void(0);" class="support-ticket-float-btn" data-bs-toggle="modal" data-bs-target="#addgateway" title=" <?php echo e(translate('Add New Gateway')); ?>">
            <i class="fa fa-plus ticket-float"></i>
        </a>
	</div>
</section>

<div class="modal fade" id="gatewayInfo" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog nafiz">
        <div class="modal-content">
            <div class="modal-body">
                <div class="card">
                    <div class="card-header bg--lite--violet">
                        <div class="card-title text-center text--light"><?php echo e(translate('Gateway Information')); ?></div>
                    </div>
                    <div class="card-body">
                        <div class="driver-info"></div>
                    </div>
                </div>
            </div>

            <div class="modal_button2 modal-footer">
                <div class="d-flex align-items-center justify-content-center gap-3">
                    <button type="button" class="i-btn primary--btn btn--md" data-bs-dismiss="modal"><?php echo e(translate('Close')); ?></button>
                </div>
            </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="gatewayDelete" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
        	<form action="<?php echo e(route('admin.mail.delete')); ?>" method="GET">
        		<?php echo csrf_field(); ?>
        		<input type="hidden" name="id" value="">
	            <div class="modal_body2">
	                <div class="modal_icon2">
	                    <i class="las la-trash"></i>
	                </div>
	                <div class="modal_text2 mt-4">
	                    <h5><?php echo e(translate('Are you sure to delete this gateway')); ?></h5>
	                </div>
	            </div>

				<div class="modal_button2 modal-footer">
					<div class="d-flex align-items-center justify-content-center gap-3">
						<button type="button" class="i-btn danger--btn btn--md" data-bs-dismiss="modal"><?php echo e(translate('Cancel')); ?></button>
						<button type="submit" class="i-btn primary--btn btn--md"><?php echo e(translate('Delete')); ?></button>
					</div>
				</div>
	        </form>
        </div>
    </div>
</div>

<div class="modal fade" id="editgateway" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><?php echo e(translate('Edit New Mail Gateway')); ?></h5>
                <button type="button" class="i-btn bg--lite--danger text--danger btn--sm" data-bs-dismiss="modal"> <i class="las la-times"></i></button>
            </div>

            <form action="<?php echo e(route('admin.mail.update')); ?>" method="post">
                <?php echo csrf_field(); ?>
                <input type="hidden" name="id" value="">
                <div class="modal-body">
                    <div class="card">
                        <div class="card-body">
                            <div class="form-item mb-3">
                                
                                <label for="gateway_type_edit" class="form-label"> <?php echo e(translate('Gateway Type')); ?> <sup class="text--danger">*</sup></label>
                                
                                <select class="form-select text-uppercase select-gateway-type gateway_type" name="type" required="" id="gateway_type_edit"></select> 
                            </div>
                            <div class="row mb-3 newdataadd"></div>
                            <div class="row mb-3 oldData"></div>
                            <div class="row">
                                <div class="form-item mb-3 col-lg-6">
                                    <label for="name" class="form-label"> <?php echo e(translate('From Name')); ?> <sup class="text--danger">*</sup></label>
                                    <input type="text" class="form-control" id="name" name="name" placeholder=" <?php echo e(translate('Enter From Name')); ?>" required>
                                </div>
                                <div class="form-item mb-3 col-lg-6">
                                    <label for="address" class="form-label"> <?php echo e(translate('From address')); ?> <sup class="text--danger">*</sup></label>
                                    <input type="email" class="form-control" id="address" name="address" placeholder=" <?php echo e(translate('Enter From Address')); ?>" required>
                                </div>
                            </div>
                    
                            <div class="form-item mb-3">
                                <label for="status" class="form-label"> <?php echo e(translate('Status')); ?> <sup class="text--danger">*</sup></label>
                                <select class="form-select" name="status" id="status" required>
                                    <option class="active" value="1"> <?php echo e(translate('Active')); ?></option>
                                    <option class="inactive" value="0"> <?php echo e(translate('Inactive')); ?></option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="modal-footer">
                    <div class="d-flex align-items-center gap-3">
                        <button type="button" class="i-btn danger--btn btn--md" data-bs-dismiss="modal"> <?php echo e(translate('Cancel')); ?></button>
                        <button type="submit" class="i-btn primary--btn btn--md"> <?php echo e(translate('Submit')); ?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="addgateway" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><?php echo e(translate('Add New Mail Gateway')); ?></h5>
                <button type="button" class="i-btn bg--lite--danger text--danger btn--sm" data-bs-dismiss="modal"> <i class="las la-times"></i></button>
            </div>

            <form action="<?php echo e(route('admin.mail.create')); ?>" method="POST">
                <?php echo csrf_field(); ?>
                <div class="modal-body">
                    <div class="card">
                        <div class="card-body">
                            <div class="form-item mb-3">
                                <label for="add_gateway_type" class="form-label"> <?php echo e(translate('Gateway Type')); ?> <sup class="text--danger">*</sup></label>
                                <select class="form-select gateway_type" name="type" required="" id="add_gateway_type">
                                    <option value=""selected disabled><?php echo e(translate("Select a gateway type")); ?></option>
                                    <?php $__currentLoopData = $credentials; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$credential): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e(strToLower($key)); ?>"><?php echo e(strtoupper($key)); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>
                          
                            <div class="row mb-3 newdataadd"></div>
                            <div class="row">
                                <div class="form-item mb-3 col-lg-6">
                                    <label for="name" class="form-label"> <?php echo e(translate('From Name')); ?> <sup class="text--danger">*</sup></label>
                                    <input type="text" class="form-control" id="name" name="name" placeholder=" <?php echo e(translate('Enter From Name')); ?>" required>
                                </div>
                                <div class="form-item mb-3 col-lg-6">
                                    <label for="address" class="form-label"> <?php echo e(translate('From address')); ?> <sup class="text--danger">*</sup></label>
                                    <input type="email" class="form-control" id="address" name="address" placeholder=" <?php echo e(translate('Enter From Address')); ?>" required>
                                </div>
                            </div>
                      
                        
                            <div class="form-item mb-3">
                                <label for="status" class="form-label"> <?php echo e(translate('Status')); ?> <sup class="text--danger">*</sup></label>
                                <select class="form-select" name="status" id="status" required>
                                    <option value="1"> <?php echo e(translate('Active')); ?></option>
                                    <option value="0"> <?php echo e(translate('Inactive')); ?></option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="modal-footer">
                    <div class="d-flex align-items-center gap-3">
                        <button type="button" class="i-btn danger--btn btn--md" data-bs-dismiss="modal"> <?php echo e(translate('Cancel')); ?></button>
                        <button type="submit" class="i-btn primary--btn btn--md"> <?php echo e(translate('Submit')); ?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<?php $__env->stopSection(); ?>

<?php $__env->startPush('script-push'); ?>
    <script>
        (function($){
            "use strict";
            var oldType = '';
            var oldInfo = [];
            $('.gateway-details').on('click', function(){
                $('.driver-info').empty();
                var modal = $('#gatewayInfo');
                var driver = $(this).data('driver_information');
                $.each(driver, function(key, value) {
                  var paragraph = $('<p class="d-flex justify-content-start align-items-center "><span class="fw-bold text-capitalize col-3">' + key + ' </span> <span class="col-9">: ' + value + ' </span></p>');
                  $('.driver-info').append(paragraph);
              });
                modal.modal('show');
            });

            $('.delete').on('click', function(){

                var modal = $('#gatewayDelete');
                modal.find('input[name=id]').val($(this).data('delete_id'));
                modal.modal('show');
            });




            $('.edit-gateway').on('click', function() {
                $('.newdataadd').empty();
                $('.oldData').empty();
                $('.select-gateway-type').empty();
                $('.active').attr("selected",false);
                $('.inactive').attr("selected",false);
                $('.gatewayType').attr("selected",false);
               
                var modal = $('#editgateway');
                modal.find('input[name=id]').val($(this).data('id'));
                modal.find('input[name=name]').val($(this).data('gateway_name'));
                modal.find('input[name=address]').val($(this).data('gateway_address'));
                
                
                $(this).data('gateway_status') == 1 ? $('.active').attr("selected",true) : $('.inactive').attr("selected",true);
                oldType = $(this).data('gateway_type');
                var data = Object.keys(<?php echo $jsonArray ?>);
                var creds = <?php echo $jsonArray ?>;
                
                $.each(data, function(key, value) {
                    var option = $('<option class="text-uppercase gatewayType" value="'+ value +'">'+ value +'</option>');
                    $('.select-gateway-type').append(option);
                    if(oldType == value){
                        $('.gatewayType').attr("selected",true)
                    }
                });

                oldInfo = $(this).data('gateway_driver_information');
               
                $.each(oldInfo, function(key, value) {
                    if(key != 'encryption') { 

                        var filterkey = key.replace("_", " ");
                        var div   = $('<div class="form-item mb-3 col-lg-6"></div>');
                        var label = $('<label for="' + key + '" class="form-label text-capitalize">' + filterkey + '<sup class="text--danger">*</sup></label>');
                        var input = $('<input type="text" class="form-control" id="' + key + '" value="' + value + '" name="driver_information[' + key + ']" placeholder="Enter ' + filterkey + '" required>');
                    
                        div.append(label, input);
                        $('.oldData').append(div);
                        
                    }
                    else{
                        
                        
                        $.each(creds, function(k, v) {
                            
                            $.each(v, function(cred_key, cred_value) {
                            
                                if(cred_key == key) {
                                    
                                    var filterkey = key.replace("_", " ");
                                    var div   = $('<div class="form-item mb-3 col-lg-6"></div>');
                                    var label  = $('<label for="' + key + '" class="form-label text-capitalize">' + filterkey + '<sup class="text--danger">*</sup></label>');
                                    var select = $('<select class="form-select" name="driver_information[' + key + ']" id="'+key+'"></select>')
                                    $.each(cred_value, function(name, method) {
                                        
                                        var option = $('<option class="encryptionType" value="'+method+'">'+name+'</option>');
                                        
                                        select.append(option);
                                        if(method == value){
                                            option.attr("selected",true)
                                        }
                                        
                                    }); 
                                    
                                    
                                    div.append(label,select);
                                    $('.oldData').append(div);

                                }
                            });
                        });
                      
                    }
                   
               
                  

                });

                modal.modal('show');
            });

            $('.default_status').on('change', function(){
                
                const default_value = $(this).val();
                const id = $(this).attr('data-id');
                $.ajax({
                    method:'get',
                    url: "<?php echo e(route('admin.mail.default.status')); ?>",
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                    data:{
                    'default_value' : default_value,
                    'id' :id
                    },
                    dataType: 'json'
                }).then(response => {
                    if(response.status){
                        notify('success', 'Recommended Status Updated Successfully');
                        window.location.reload()
                    }
                    else{
                        notify('error', 'Inactive gateway can not be the default gateway');
                        window.location.reload()
                    }
                })


		    });

            $('.testDefaultGateway').on('submit', function(){

                var button = $(this).find('button[type="submit"]');
                var email = $(this).find('input[name=email]').val();
                if (button.hasClass('disabled')) {
                    return;
                }

                button.addClass('disabled').append('<span class="loading-spinner spinner-border spinner-border-sm" aria-hidden="true"></span> ');

                $.ajax({
                    method:'post',
                    url: "<?php echo e(route('admin.mail.test')); ?>",
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                    data:{
                    'email' : email,
                    },
                    dataType: 'json'
                }).then(response => {
                    button.find('.loading-spinner').remove();
                    button.removeClass('disabled');
                    if(response.status){
                        notify('success', "Successfully sent mail to "+response.address+", please check your inbox or spam");
                       
                    }
                    else{
                        notify('error', "Mail Configuration Error, Please check your "+response.address+" configuration properly");
                       
                    }
                })


		    });

            $('.gateway_type').on('change', function(){
                
                $('.newdataadd').empty();
                var data = <?php echo $jsonArray ?>[this.value];
                var newType = this.value;
                
                if(newType != oldType){
                    
                    $.each(data, function(key, v) {
                        $('.oldData').empty();
                        var filterkey = key.replace("_", " ");
                        var div   = $('<div class="form-item mb-3 col-lg-6"></div>');
                        if(key != 'encryption'){
                            var label = $('<label for="' + key + '" class="form-label text-capitalize">' + filterkey + '<sup class="text--danger">*</sup></label>');
                            var input = $('<input type="text" class="form-control" id="' + key + '" name="driver_information[' + key + ']" placeholder="Enter ' + filterkey + '" required>');
                            div.append(label, input);
                            $('.newdataadd').append(div);
                        }
                        else{
                           
                            var label  = $('<label for="' + key + '" class="form-label text-capitalize">' + filterkey + '<sup class="text--danger">*</sup></label>');
                            var select = $('<select class="form-select" name="driver_information[' + key + ']" id="'+key+'"></select>')
                            $.each(v, function(name, method) {
                                var option = $('<option value="'+method+'">'+name+'</option>')
                                select.append(option);
                            }); 
                            div.append(label,select);
                            $('.newdataadd').append(div);
                        }
                    });
                }
                else{
              
                    $.each(oldInfo, function(key, value) {
                    var filterkey = key.replace("_", " ");
                    var div   = $('<div class="form-item mb-3 col-lg-6"></div>');
                    var label = $('<label for="' + key + '" class="form-label text-capitalize">' + filterkey + '<sup class="text--danger">*</sup></label>');
                    var input = $('<input type="text" class="form-control" id="' + key + '" value="' + value + '" name="driver_information[' + key + ']" placeholder="Enter ' + filterkey + '" required>');
                    div.append(label, input);
                    $('.oldData').append(div);
                });
                }
            });
        })(jQuery);
    </script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('admin.layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH E:\laragon\www\xsender\src\resources\views/admin/mail/index.blade.php ENDPATH**/ ?>