<?php 

session_start();
include("../all_query.php");
include '../db.php';

if(isset($_POST['login'])){
	
	 $username=$_POST['username'];
     $password=$_POST['password'];
     $login_as=$_POST['login_as'];
     $apisipid=$_POST['apisipid'];
	 
	 
	 $is_check_login  = true;
	 
	 if($login_as == 'advisor' && trim($apisipid) == '')
	 {
		 $_SESSION['login_error']='Please enter SipID';
		 
		 $is_check_login = false;
	 }
      
	  
	  if($is_check_login)
	  {
	
	if($login_as == 'admin')
	{
	$query=mysqli_query($conn,"select * from admin where username='$username' and password='$password' and ( status ='Activate' or status='Activate' )  ");
	}
	if($login_as == 'advisor')
	{
	$query=mysqli_query($conn,"select * from advisors where username='$username' and password='$password' and ( status1 ='' or status1='Activated' ) ");
	}
	
	if($login_as == 'processing_user')
	{
	$query=mysqli_query($conn,"select * from 	process_lead_owner_master	where username='$username' and password='$password' and (  status1 ='' or status1='Activated' ) ");
	}
	
	
	if($data=mysqli_fetch_assoc($query)){
			
			/* if($data['is_logged_in'] == '0')
			{
			*/
			$user_id=$data['id'];
			
			if($login_as == 'admin')
			{
				$data['apisipid'] = '';
			}
			
			
			// check already login in another
			$advQ = mysqli_query($conn,"select *from `advisors` where apisipid = '$apisipid' and id <> $user_id ");
			
			
			
			
		if(mysqli_num_rows($advQ) == 0 ||  $login_as == 'admin' )
		{
			
		
			
	 	
		$_SESSION['loan_crm_username']=$username;
		$_SESSION['loan_crm_userid']=$user_id;
		$_SESSION['loan_crm_password']=$password;
		
		$_SESSION['login_as'] = $login_as;
		
		if($login_as == 'admin')
		{
		 $_SESSION['login_name'] = $data['username'];
		 
		  $role = $data['role'];
 		$rights = $data['rights'];
 		
		$_SESSION['apisipid'] = $apisipid;
		
		}
		else {
			
		$_SESSION['login_name'] = $data['name'];
		$_SESSION['apisipid'] = $apisipid;
		 $role = 'advisor';
		$rights = '';
		$lead_owner_ids = $data['id'];
		
		
		}
		
		
		
		 $_SESSION['role'] = $role;
 		$_SESSION['rights'] = $rights;
		$_SESSION['lead_owner_ids'] = $lead_owner_ids;
		
		$_SESSION['image'] = $data['image'];
		$_SESSION['mobile'] = $data['mobile'];
		
		if(isset($data['email']))
		{
		$_SESSION['call_evaluation_email'] = $data['email'];
		}
		
		
		if($login_as == 'advisor')
		{
			 mysqli_query($conn,"update advisors set apisipid = '$apisipid' where id = $user_id ");
			 
			 
			 
			 
			 
			   $LOGIN_DATA = array();
			   
			   $LOGIN_DATA['advisor_id'] = $_SESSION['loan_crm_userid'];
			   $LOGIN_DATA['advisor_name'] = $_SESSION['login_name'];
			   $LOGIN_DATA['apisipid'] = $_SESSION['apisipid'];
			   $LOGIN_DATA['status_date'] = date('Y-m-d');
			   $LOGIN_DATA['status_time'] = date('h:i A');
			   $LOGIN_DATA['login_status'] = 'logged_in';
			   $LOGIN_DATA['login_status_type'] = 'manual';
			   
			   insertTable($conn,'advisor_login_status',$LOGIN_DATA,'add_submit');
			
			 
		}
		
		
		
		
		// call dailer api for login 
		
		
		
		 
      $_SESSION['timestamp'] = time();  
		
		header('location:index.php');
		$description="$username is done login sucessfully ";

			  }
			else {
				
				
				$_SESSION['login_error']='Sorry, Using This SipID already logged in another user !';
				
			}   
	}
	else 
	{
		
		$_SESSION['login_error']='Invalid Username or Password !';
		
	}
	
	
	
	  }
	


}


 ?>
 <!DOCTYPE html>








 
<html lang="en" class="light-style  customizer-hide" dir="ltr" data-theme="theme-bordered" data-assets-path="../assets/" data-template="vertical-menu-template-bordered">
 
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />

    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">



    <title>CRM Login</title>
    
    <meta name="description" content="Start your development with a Dashboard for Bootstrap 5" />
    <meta name="keywords" content="dashboard, bootstrap 5 dashboard, bootstrap 5 design, bootstrap 5">
    <!-- Canonical SEO -->
    <link rel="canonical" href="https://1.envato.market/frest_admin">
    
    <!-- Favicon -->
    <link rel="icon" type="image/x-icon" href="https://pixinvent.com/demo/frest-clean-bootstrap-admin-dashboard-template/assets/img/favicon/favicon.ico" />

    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com/">
    <link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&amp;family=Rubik:ital,wght@0,300;0,400;0,500;0,600;0,700;1,300;1,400;1,500;1,600;1,700&amp;display=swap" rel="stylesheet">

    <!-- Icons -->
    <link rel="stylesheet" href="../assets/vendor/fonts/boxicons.css" />
    <link rel="stylesheet" href="../assets/vendor/fonts/fontawesome.css" />
    <link rel="stylesheet" href="../assets/vendor/fonts/flag-icons.css" />

    <!-- Core CSS -->
    <link rel="stylesheet" href="../assets/vendor/css/rtl/core.css" class="template-customizer-core-css" />
    <link rel="stylesheet" href="../assets/vendor/css/rtl/theme-bordered.css" class="template-customizer-theme-css" />
    <link rel="stylesheet" href="../assets/css/demo.css" />

    <!-- Vendors CSS -->
    <link rel="stylesheet" href="../assets/vendor/libs/perfect-scrollbar/perfect-scrollbar.css" />
    <link rel="stylesheet" href="../assets/vendor/libs/typeahead-js/typeahead.css" />
    <!-- Vendor -->
	<link rel="stylesheet" href="../assets/vendor/libs/formvalidation/dist/css/formValidation.min.css" />

 <link rel="stylesheet" href="../assets/vendor/libs/toastr/toastr.css" />
	 <link rel="stylesheet" href="../assets/vendor/libs/animate-css/animate.css" />


    <!-- Page CSS -->
    <!-- Page -->
	<link rel="stylesheet" href="../assets/vendor/css/pages/page-auth.css">
    <!-- Helpers -->
    <script src="../assets/vendor/js/helpers.js"></script>

    <!--! Template customizer & Theme config files MUST be included after core stylesheets and helpers.js in the <head> section -->
    <!--? Template customizer: To hide customizer set displayCustomizer value false in config.js.  -->
    <script src="../assets/vendor/js/template-customizer.js"></script>
    <!--? Config:  Mandatory theme config file contain global vars & default theme options, Set your preferred theme option in this file.  -->
    <script src="../assets/js/config.js"></script>
    
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async="async" src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
    <script>
    window.dataLayer = window.dataLayer || [];

    function gtag() {
      dataLayer.push(arguments);
    }
    gtag('js', new Date());
    gtag('config', 'GA_MEASUREMENT_ID');
    </script>
    <!-- Custom notification for demo -->
    <!-- beautify ignore:end -->

</head>



 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   
   
   
   

<body>

  <!-- Content -->
  
  
  
  
  
  <script>
  
   function loginCall(obj)
   {
	   
	    var username = document.getElementById('username').value;
	    var password = document.getElementById('password').value;
	    var login_as = document.getElementById('login_as').value;
	   
	   
	   if(username.trim() == '')
	   {
		   toastr.warning('Please enter username');
		   return false;
	   }
	   
	   
	     if(password.trim() == '')
	   {
		   toastr.warning('Please enter password');
		   return false;
	   }
	   
	      if(login_as.trim() == '')
	   {
		   toastr.warning('Please select login as');
		   return false;
	   }
	   
	   
	   obj.disabled = true;
	   
	   
	     $.ajax({
      type: "POST",
      url: 'login_api_ajax.php',
      data: ({username:username,password:password,login_as:login_as}),
      success: function(data) {
		
		if(data.trim() == '0')
		{
			toastr.warning('Invalid username or password');
			
			 obj.disabled = false;
	   
		}
		
		else {
			
			if(login_as == 'advisor')
			{
				
				
				var info = JSON.parse(data);
				 
				 
				 
				 // call API HERE
				 
				 var empcode = info.username;
				 var password = info.password;
				 var apisipid = info.apisipid; 
				 
				 
							 $.ajax({
				  type: "GET",
				  url: 'http://192.168.1.112/Dialer/API/agentloginapi',
				  data: ({empcode:empcode,password:password,apisipid:apisipid,campname:'Callextreme'}),
				  success: function(data) {
					
						 alert(data);
						 
						 obj.disabled = false;
	   
						 window.location = 'index.php';
						 
						
					 }
					  // <- this turns it into synchronous

					  }
				   );
				
				  		 
								  window.location = 'index.php';
				 
		 
			}
			else
			{
				
				
			 obj.disabled = false;
	   
		 
		 window.location = 'index.php';
		 
			}
		 
		}	
			
			
	   
	   
		 }
		  // <- this turns it into synchronous

		  }
	   );
	   
   }
  
  </script>
  
  
  
  
  
  <script>
  
  function getLoginType(val)
  {
	  /*
	  
	  if(val == 'advisor')
	  {
		  document.getElementById('sipid_section').style.display = 'block';  
		    
	  }
	  else
	  {
		   document.getElementById('sipid_section').style.display = 'none';  
	  }
	  */
  }
  
  </script>
  
  
  
  

<div class="container-xxl">
  <div class="authentication-wrapper authentication-basic container-p-y">
    <div class="authentication-inner py-4">

      <!-- Register -->
      <div class="card">
        <div class="card-body">
          <!-- Logo -->
          <div class="app-brand justify-content-center">
            <a href="index.php" class="app-brand-link gap-2">
              
              <span class="app-brand-text demo h3 mb-0 fw-bold">FinMan</span>
            </a>
          </div>
          <!-- /Logo -->
          <h4 class="mb-2">Welcome to FinMan! </h4>
          <p class="mb-4">Please sign-in to your account</p>

	<?php if(isset($_SESSION['login_error'])) { ?>
								 <div  class="alert alert-danger">
								<?php   echo $_SESSION['login_error'];  unset($_SESSION['login_error']); ?>    </div><?php } ?>
								
								
          <form   class="mb-3" action="" method="POST">
            <div class="mb-3">
              <label for="email" class="form-label">Username</label>
              <input type="text" class="form-control" id="username" name="username" placeholder="Enter your   username" autofocus required >
            </div>
            <div class="mb-3 form-password-toggle">
              <div class="d-flex justify-content-between">
                <label class="form-label" for="password">Password</label>
               
              </div>
              <div class="input-group input-group-merge">
                <input type="password" id="password" class="form-control" name="password" placeholder="&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;&#xb7;" aria-describedby="password" required />
                <span class="input-group-text cursor-pointer"><i class="bx bx-hide"></i></span>
              </div>
            </div>
			
			  <div class="mb-3">
              <label for="email" class="form-label">Login As</label>
			  <select onchange="getLoginType(this.value);" name="login_as" id="login_as" class="form-control" required>
			  <option value="">Login As</option>
			  <option value="admin">Admin</option>
			  <option value="advisor">Advisor</option>
			  </select> 
			  </div>
			
			
			 <div   id="sipid_section" class="mb-3">
              <label for="email" class="form-label">SipID</label>
              <input type="text" class="form-control" id="apisipid" name="apisipid" placeholder="Enter SipID" >
            </div>
			
			
             
            <div class="mb-3">
              
              <button  name="login" class="btn btn-primary d-grid w-100" type="submit">Log in</button>
            </div>
			
			<br/>
			
			<label style="float:right;">Powered By <a href="">SugarCode</a></label>
          </form>

          
         
        </div>
      </div>
      <!-- /Register -->
    </div>
  </div>
</div>

<!-- / Content -->

   
  

  <!-- Core JS -->
  <!-- build:js assets/vendor/js/core.js -->
  <script src="../assets/vendor/libs/jquery/jquery.js"></script>
  <script src="../assets/vendor/libs/popper/popper.js"></script>
  <script src="../assets/vendor/js/bootstrap.js"></script>
  <script src="../assets/vendor/libs/perfect-scrollbar/perfect-scrollbar.js"></script>
  <script src="../assets/vendor/libs/hammer/hammer.js"></script>
  <script src="../assets/vendor/libs/i18n/i18n.js"></script>
  <script src="../assets/vendor/libs/typeahead-js/typeahead.js"></script>
  
  
      <script src="../assets/vendor/libs/toastr/toastr.js"></script>

     <script src="../assets/js/ui-toasts.js"></script>
	
	
	
  <script src="../assets/vendor/js/menu.js"></script>
  <!-- endbuild -->

  <!-- Vendors JS -->
  <script src="../assets/vendor/libs/formvalidation/dist/js/FormValidation.min.js"></script>
<script src="../assets/vendor/libs/formvalidation/dist/js/plugins/Bootstrap5.min.js"></script>
<script src="../assets/vendor/libs/formvalidation/dist/js/plugins/AutoFocus.min.js"></script>

  <!-- Main JS -->
  <script src="../assets/js/main.js"></script>

  <!-- Page JS -->
  <script src="../assets/js/pages-auth.js"></script>
</body>

 
</html>
