<!DOCTYPE html>
<html>
<head>
<!-- FORM CSS CODE -->
<?php include"comman/code_css.php"; ?>
<!-- </copy> -->  
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

 <?php include"sidebar.php"; ?>


  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
       <?= $page_title ?>
      </h1>
      <ol class="breadcrumb">
        <li><a href="<?php echo $base_url; ?>dashboard"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active"><?= $page_title; ?></li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
      <!-- Demo Mode Alert for SMTP Settings -->
      <?php if(demo_app()){ ?>
      <div class="row">
        <div class="col-md-12">
          <div class="alert alert-warning text-center">
            <i class="fa fa-exclamation-triangle"></i>
            <strong>Demo Mode Active:</strong> SMTP Settings cannot be modified or saved in demo mode. 
            <a href="https://nanzige.gumroad.com/l/billingbook" target="_blank" class="btn btn-sm btn-danger" style="margin-left: 10px;">
              <i class="fa fa-shopping-cart"></i> Buy Now to Enable
            </a>
          </div>
        </div>
      </div>
      <?php } ?>

      <!-- Which store is being edited -->
      <?php
        $smtp_store_id = get_current_store_id();
        if ($smtp_store_id === null || $smtp_store_id === '' || (int)$smtp_store_id < 1) {
          $smtp_store_id = get_safe_store_id_for_query();
        }
        $smtp_store_name = get_store_details($smtp_store_id)->store_name;
      ?>
      <div class="row">
        <div class="col-md-12">
          <div class="alert alert-info">
            <i class="fa fa-info-circle"></i> <strong>Editing SMTP for store:</strong> <?= htmlspecialchars($smtp_store_name); ?> (ID: <?= (int)$smtp_store_id; ?>). Forgot-password and other emails use the <strong>recipient user’s store</strong>; ensure that store has SMTP configured if you have multiple stores. If mail still does not send, re-enter your SMTP password and click Update (passwords with special characters may have been saved incorrectly before).
          </div>
        </div>
      </div>
      
      <div class="row">
        <!-- right column -->
        <div class="col-md-12">
                     <!-- Horizontal Form -->
                     <div class="box box-primary ">
                        <!-- /.box-header -->
                        <!-- form start -->
                        <form class="form-horizontal" id="smtp-form" onkeypress="return event.keyCode != 13;">
                           <input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>">
                           <input type="hidden" id="base_url" value="<?php echo $base_url;; ?>">
                           <div class="box-body">
                              <!-- Store Code -->
                               <?php 
                                echo "<input type='hidden' name='store_id' id='store_id' value='".get_current_store_id()."'>";
                                ?>
                              <!-- Store Code end -->
                              <div class="form-group">
                                 <label for="smtp_status" class="col-sm-2 control-label"><?= $this->lang->line('smtp_status'); ?></label>
                                 <div class="col-sm-4">
                                    <select class='form-control select2' name='smtp_status' id="smtp_status" <?php echo demo_app() ? 'disabled' : ''; ?>>
                                      <option value="1">Enable</option>
                                      <option value="0">Disable</option>
                                    </select>
                                    <span id="smtp_host_msg" style="display:none" class="text-danger"></span>
                                 </div>
                              </div>
                              <div class="form-group">
                                 <label for="smtp_host" class="col-sm-2 control-label"><?= $this->lang->line('smtp_host'); ?></label>
                                 <div class="col-sm-4">
                                    <input type="text" class="form-control input-sm" id="smtp_host" name="smtp_host" placeholder=""  value="<?php print $smtp_host; ?>" autofocus <?php echo demo_app() ? 'disabled' : ''; ?>>
                                    <span id="smtp_host_msg" style="display:none" class="text-danger"></span>
                                 </div>
                              </div>
                              <div class="form-group">
                                 <label for="smtp_port" class="col-sm-2 control-label"><?= $this->lang->line('smtp_port'); ?></label>
                                 <div class="col-sm-4">
                                    <input type="text" class="form-control input-sm" id="smtp_port" name="smtp_port" placeholder=""  value="<?php print $smtp_port; ?>" autofocus <?php echo demo_app() ? 'disabled' : ''; ?>>
                                    <span id="smtp_port_msg" style="display:none" class="text-danger"></span>
                                 </div>
                              </div>
                              <div class="form-group">
                                 <label for="smtp_user" class="col-sm-2 control-label"><?= $this->lang->line('smtp_user'); ?></label>
                                 <div class="col-sm-4">
                                    <input type="text" class="form-control input-sm" id="smtp_user" name="smtp_user" placeholder="Email"  value="<?php print $smtp_user; ?>" autofocus <?php echo demo_app() ? 'disabled' : ''; ?>>
                                    <span id="smtp_user_msg" style="display:none" class="text-danger"></span>
                                 </div>
                              </div>
                              <div class="form-group">
                                 <label for="smtp_pass" class="col-sm-2 control-label"><?= $this->lang->line('smtp_pass'); ?></label>
                                 <div class="col-sm-4">
                                    <input type="password" class="form-control input-sm" id="smtp_pass" name="smtp_pass" placeholder="Leave blank to keep current password" value="" autocomplete="new-password" <?php echo demo_app() ? 'disabled' : ''; ?>>
                                    <span class="help-block small">Leave blank to keep existing password. Use App Password for Gmail/Office365 if 2FA is on.</span>
                                    <span id="smtp_pass_msg" style="display:none" class="text-danger"></span>
                                 </div>
                              </div>
                           </div>
                           <!-- /.box-body -->
                           <div class="box-footer">
                              <div class="col-sm-8 col-sm-offset-2 text-center">
                                 <div class="col-md-3 col-md-offset-3">
                                    <button type="button" id="update" class=" btn btn-block btn-success" title="Save Data" <?php echo demo_app() ? 'disabled' : ''; ?>>Update</button>
                                 </div>
                                 <div class="col-sm-3">
                                    <a href="<?=base_url('dashboard');?>">
                                    <button type="button" class="col-sm-3 btn btn-block btn-warning close_btn" title="Go Dashboard">Close</button>
                                    </a>
                                 </div>
                              </div>
                           </div>
                           <!-- /.box-footer -->
                        </form>
                     </div>
                     <!-- /.box -->
                  </div>
        <!--/.col (right) -->
      </div>

      <!-- Send Test Email -->
      <div class="row">
        <div class="col-md-12">
          <div class="box box-info">
            <div class="box-header with-border">
              <h3 class="box-title"><i class="fa fa-envelope"></i> Send Test Email</h3>
            </div>
            <div class="box-body">
              <p class="text-muted">Send a test email to verify SMTP. Uses the same store settings as above. Check inbox and spam.</p>
              <div class="form-inline">
                <div class="form-group">
                  <label for="test_email" class="sr-only">Email</label>
                  <input type="email" class="form-control" id="test_email" placeholder="your@email.com" style="min-width:220px;">
                </div>
                <button type="button" id="send-test-email" class="btn btn-primary" <?php echo demo_app() ? 'disabled' : ''; ?>>
                  <i class="fa fa-paper-plane"></i> Send Test Email
                </button>
              </div>
              <div id="test_email_result" style="display:none; margin-top:10px;"></div>
            </div>
          </div>
        </div>
      </div>
      <!-- /.row -->

    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->

 <?php include"footer.php"; ?>


  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->
<!-- Demo Mode Styling for SMTP Settings -->
<?php if(demo_app()){ ?>
<style>
  .form-control:disabled {
    background-color: #f5f5f5 !important;
    cursor: not-allowed !important;
    opacity: 0.6 !important;
  }
  
  .btn:disabled {
    cursor: not-allowed !important;
    opacity: 0.6 !important;
  }
  
  .box {
    opacity: 0.8;
  }
  
  .box::before {
    content: "DEMO MODE - READ ONLY";
    position: absolute;
    top: -10px;
    right: 20px;
    background: #f39c12;
    color: white;
    padding: 5px 15px;
    border-radius: 15px;
    font-size: 11px;
    font-weight: bold;
    z-index: 1000;
  }
</style>
<?php } ?>

<!-- SOUND CODE -->
<?php include"comman/code_js_sound.php"; ?>
<!-- TABLES CODE -->
<?php include"comman/code_js.php"; ?>

<script>
$("#smtp_status").val(<?=$smtp_status?>).select2();
/*Email validation code end*/

$('#update').on("click",function (e) {
  if($(this).prop('disabled')) {
    alert("SMTP Settings cannot be modified in demo mode. Please purchase the full version to enable this feature.");
    return false;
  }
  var base_url=$("#base_url").val();
  var this_id=this.id;
  $(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');
  $("#"+this_id).attr('disabled',true);
  e.preventDefault();
  data = new FormData($('#smtp-form')[0]);
  $.ajax({
    type: 'POST',
    url: base_url+'smtp/update_smtp',
    data: data,
    cache: false,
    contentType: false,
    processData: false,
    success: function(result){
      if(result=="success") {
        toastr["success"]("Success! Record Saved Successfully!!");
      } else if(result=="failed") {
        toastr['error']("Sorry! Failed to save Record. Try again.");
      } else {
        toastr['warning'](result);
      }
      $("#"+this_id).attr('disabled',false);
      $(".overlay").remove();
    }
  });
});

// Send Test Email
$('#send-test-email').on('click', function() {
  var base_url = $("#base_url").val();
  var email = $('#test_email').val().trim();
  var $btn = $(this);
  var $result = $('#test_email_result');
  if (!email) {
    $result.removeClass('alert-success alert-danger').addClass('alert alert-warning').html('<i class="fa fa-exclamation-triangle"></i> Please enter an email address.').show();
    return;
  }
  $btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> Sending...');
  $result.hide();
  $.ajax({
    type: 'POST',
    url: base_url + 'smtp/send_test_email',
    data: { test_email: email, '<?= $this->security->get_csrf_token_name(); ?>': '<?= $this->security->get_csrf_hash(); ?>' },
    dataType: 'json',
    success: function(res) {
      $btn.prop('disabled', false).html('<i class="fa fa-paper-plane"></i> Send Test Email');
      $result.removeClass('alert-success alert-danger alert-warning').addClass('alert');
      if (res.success) {
        $result.addClass('alert-success').html('<i class="fa fa-check-circle"></i> ' + (res.message || 'Sent.')).show();
      } else {
        $result.addClass('alert-danger').html('<i class="fa fa-times-circle"></i> ' + (res.message || 'Failed.')).show();
      }
    },
    error: function() {
      $btn.prop('disabled', false).html('<i class="fa fa-paper-plane"></i> Send Test Email');
      $result.removeClass('alert-success alert-warning').addClass('alert alert-danger').html('<i class="fa fa-times-circle"></i> Request failed. Check console.').show();
    }
  });
});
</script>
<!-- Make sidebar menu hughlighter/selector -->
<script>$(".<?php echo basename(__FILE__,'.php');?>-active-li").addClass("active");</script>

</body>
</html>
