<?php
include("access.php");
include("include/admin.class.php");
include("include/conf.class.php");
$userinfo=$cartCore->get_user_info($CARTUSERNAME);
$cartAdminMain->check_grant($userinfo['USER_ID'],$page_name);
$cartCore->get_headder();
$cartCore->view_welcome($userinfo['USERNAME'],$userinfo['IMAGE']);
$cartCore->view_sidebar($userinfo['USER_ID'],$TOKEN_ID);
$cartCore->view_credit($COMPANY_ID,$bk);
if(isset($_POST['check_update'])){
	$cartAdminMain->add_edit_smtp_config($userinfo['USERNAME'],$page_name,$_GET['token_id'],$_GET['menu_name'],$_GET['step']);	
	exit;
}
?>
        <!-- top navigation -->
        <div class="top_nav">
          <div class="nav_menu">
            <nav>
              <div class="nav toggle">
                <a id="menu_toggle"><i class="fa fa-bars"></i></a>
              </div>

              <ul class="nav navbar-nav navbar-right">
              
              
              <?=$cartCore->view_userprofile($userinfo['USER_ID'],$userinfo['USERNAME'],$userinfo['IMAGE'],$TOKEN_ID,$COMPANY_ID,$bk)?>

                <li role="presentation" class="dropdown">
                  <a href="javascript:;" class="dropdown-toggle info-number" data-toggle="dropdown" aria-expanded="false">
                    <i class="fa fa-envelope-o"></i>
                    <span class="badge bg-green"><?=$cartAdminMain->getMessageCount($userinfo['USERNAME'])?></span>
                  </a>
                  <ul id="menu1" class="dropdown-menu list-unstyled msg_list" role="menu">
                  <?=$cartAdminMain->getUserMessage($userinfo['USERNAME'])?>
                    <li>
                      <div class="text-center">
                        <a href="message.php?token_id=<?=$TOKEN_ID?>&pid=<?=$cartCore->md5_base64_encode($userinfo['USERNAME'])?>">
                          <strong>See All Alerts</strong>
                          <i class="fa fa-angle-right"></i>
                        </a>
                      </div>
                    </li>
                  </ul>
                </li>
              </ul>
            </nav>
          </div>
        </div>
        <!-- /top navigation -->

        <!-- page content -->
        <div class="right_col" role="main">
          <div class="">
            <div class="page-title">
              <div class="title_left">
                <h3>
				<?php
                if(isset($_GET['menu_name']) && $_GET['menu_name'] != "")
				{
					echo $cartCore->md5_base64_decode($_GET['menu_name']);
				}
				else
				{
					echo $cartCore->getPageHeading($page_name);
				}
				?>
			</h3>
              </div>
            </div>
            <div class="clearfix"></div>

            <div class="row">
              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2><?=$cartCore->getMainPageHeading($page_name)?> 
                    <small><?php
                if(isset($_GET['menu_name']) && $_GET['menu_name'] != "")
				{
					echo $cartCore->md5_base64_decode($_GET['menu_name']);
				}
				else
				{
					echo $cartCore->getPageHeading($page_name);
				}
				?></small></h2>
                
                    <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                      </li> 
                    </ul>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">

<?php
	


		if(isset($_GET['success_msg']) && $_GET['success_msg']!="")
		{	
			echo $cartCore->display_sucess_msg($cartCore->md5_base64_decode($_GET['success_msg']));		  
		}
		if(isset($_GET['warning_msg']) && $_GET['warning_msg']!="")
		{	
			echo $cartCore->display_warning_msg($cartCore->md5_base64_decode($_GET['warning_msg']));		  
		}
		
		
		
if(!isset($_GET['step']))
{
?>
                    <form class="form-horizontal form-label-left" novalidate action="<?=$page_name?>?token_id=<?=$TOKEN_ID?>&step=<?=$cartCore->md5_base64_encode('step02')?>&menu_name=<?=$_GET['menu_name']?>" method="post">
                      
                       <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Company Name <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <select class="form-control col-md-7 col-xs-12" name="company_id">
                 			      <?=$cartAdminMain->find_selected_company(); ?>
                          </select>
                        </div>
                      </div>
                      
                      <div class="ln_solid"></div>
                      <div class="form-group">
                        <div class="col-md-6 col-md-offset-3">
                          <button id="send" type="submit" class="btn btn-success">Next</button>
                        </div>
                      </div>
                    </form>
                  </div>
               <?php 
}
                 if(isset($_GET['step']))
					{
						if(isset($_POST['company_id'])){
						$get_company_details=$cartAdminMain->get_global_config_for_add($_POST['company_id']);
						}
						else{
							$get_company_details=$cartAdminMain->get_global_config_for_add($cartCore->md5_base64_decode($_GET['company_id']));
						}
						$get_row=$cartCore->get_smtp_info_by_company_id($get_company_details['GLOBAL_ID']);
						if(mysql_num_rows($get_row)){
							$get_config_info=mysql_fetch_assoc($get_row);
							$check_update=true;
						}
						else
						{
							$get_config_info=NULL;
							$check_update=false;
						}
						?>
<h6>Selected Company : <b><?=$get_company_details['COMPANY_NAME']?></b></h6>
                  <form class="form-horizontal form-label-left" novalidate action="<?=$page_name?>?token_id=<?=$TOKEN_ID?>&menu_name=<?=$_GET['menu_name']?>" method="post" name="myform">
                     <input type="hidden" name="check_update" value="<?=$check_update?>" />
                     <input type="hidden" name="company_id" value="<?=$cartCore->md5_base64_encode($get_company_details['GLOBAL_ID'])?>" />
                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Host Name <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input id="name" class="form-control col-md-7 col-xs-12" data-validate-length-range="6" name="HOST" placeholder="Domain Name or IP address" required type="text" value="<?=$get_config_info['HOST']?>">
                        </div>
                      </div>
                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Port <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input type="number" id="number" name="PORT" required data-validate-minmax="10,65524" class="form-control col-md-7 col-xs-12" value="<?=$get_config_info['PORT']?>">
                        </div>
                      </div>
                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Username<span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input id="name" class="form-control col-md-7 col-xs-12" data-validate-length-range="6" name="USERNAME" required type="text" value="<?=$get_config_info['USERNAME']?>">
                        </div>
                      </div>
                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Password <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input id="name" class="form-control col-md-7 col-xs-12" data-validate-length-range="6" name="PASSWORD" required type="password" value="<?=$get_config_info['PASSWORD']?>">
                        </div>
                      </div>
                      
                       <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">SMTP Debug <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <select class="form-control col-md-7 col-xs-12" name="SMTP_DEBUG">
                          <?=$cartAdminMain->get_smtp_debug_by_selected_option($get_config_info['SMTP_DEBUG']);?>
                          </select>
                        </div>
                      </div>
                      
                       <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">SMTP Authentication <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <select class="form-control col-md-7 col-xs-12" name="SMTPAUTH" required>
                          <?=$cartAdminMain->get_smtp_auth_by_selected_option($get_config_info['SMTPAUTH']);?>
                          </select>
                        </div>
                      </div>
                      
                       <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">SMTP Security <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input id="name" class="form-control col-md-7 col-xs-12" required data-validate-length-range="3,4" name="SMTPSECURE" placeholder="SSL/TLS, TLS, SSL, None" type="text" value="<?=$get_config_info['SMTPSECURE']?>">
                        </div>
                      </div>
                      
                      <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Set Email From <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input id="email" class="form-control col-md-7 col-xs-12" required name="SET_FROM_EMAIL" type="email" value="<?=$get_config_info['SET_FROM_EMAIL']?>">
                        </div>
                      </div>
                       
                        <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">From name<span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                          <input id="name" class="form-control col-md-7 col-xs-12" required name="FROM_NAME" type="text" value="<?=$get_config_info['FROM_NAME']?>">
                        </div>
                      </div>
                      <div class="form-group">
                        <div class="col-md-6 col-md-offset-3">
                          <button id="send" type="submit" class="btn btn-success">Submit</button>
                        </div>
                      </div>
                    </form>
                    
                    <div class="ln_solid"></div>
                <!-- Button to trigger modal -->
<button class="btn btn-info btn-lg" data-toggle="modal" data-target="#modalForm">
    Test SMTP Configuration
</button>

<!-- Modal -->
<div class="modal fade" id="modalForm" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">Connection Test</h4>
            </div>
            
            <!-- Modal Body -->
            <div class="modal-body">
                <p class="statusMsg"></p>
                <form role="form">
                    <div class="form-group">
                        <label for="inputEmail">Add Email address</label>
                        <input type="email" class="form-control" id="inputEmail" placeholder="test message will be sent to this mail"/>
                        <input type="hidden" class="form-control" id="inputCompany" value="<?=$get_company_details['GLOBAL_ID']?>"/>
                    </div>
                </form>
            </div>
            
            <!-- Modal Footer -->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary submitBtn" onclick="submitContactForm()">SUBMIT</button>
            </div>
        </div>
    </div>
</div>
        

        
                    <?php }?>
               
                
 </div>
              </div>
            </div>
          </div>
        </div>
        <!-- /page content -->

        <!-- footer content --><?=$cartCore->get_footer();?>
        <!-- /footer content -->
      </div>
    </div>
    <!-- jQuery -->
    <script src="../vendors/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="../vendors/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- FastClick -->
    <script src="../vendors/fastclick/lib/fastclick.js"></script>
    <!-- NProgress -->
    <script src="../vendors/nprogress/nprogress.js"></script>
    <!-- validator -->
    <script src="../vendors/validator/validator.js"></script>

    <!-- Custom Theme Scripts -->
    <script src="../build/js/custom.min.js"></script>
    
    <!-- Custom Scripts -->
    
    <script>
function submitContactForm(){
    var reg = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
    var email = $('#inputEmail').val();
	var company_id = $('#inputCompany').val();
	 if(email.trim() == '' ){
        alert('Please enter your email.');
        $('#inputEmail').focus();
        return false;
    }else if(email.trim() != '' && !reg.test(email)){
        alert('Please enter valid email.');
        $('#inputEmail').focus();
        return false;
    }else{
        $.ajax({
            type:'POST',
            url:'smtp_relay.php',
            data:'contactFrmSubmit=1&email='+email+'&company_id='+company_id,
            beforeSend: function () {
                $('.submitBtn').attr("disabled","disabled");
                $('.modal-body').css('opacity', '.5');
            },
            success:function(msg){
                if(msg == 'ok'){
                    $('#inputEmail').val('');
                    $('.statusMsg').html('<span style="color:green;">Test mail has been sent, Check Your Email</p>');
                }else{
                    $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                }
                $('.submitBtn').removeAttr("disabled");
                $('.modal-body').css('opacity', '');
            }
        });
    }
}
</script>
	
  </body>
</html>