<?php
session_start(); 
include('config.php');
    $uname=trim($_REQUEST['uname']);
    $password=trim($_REQUEST['password']);
date_default_timezone_set("Asia/Kolkata");
$q=mysqli_query($conn,"select * from $user_table where login_id='$uname' and password='$password'");
//echo "select * from $user_table where login_id='$uname' and password='$password'";
if(mysqli_num_rows($q)>0)
{
	$_SESSION['user']= $uname; 
	while($r=mysqli_fetch_array($q))
	{
		$_SESSION['user_type']=$r["user_type"];
		$_SESSION['dealer_code']=$r["dealer_code"];
		$dealer_code=$r["dealer_code"];
		$xcodex = '0';
		$Ldate=date(DATE_RFC850);
		$cdate=date("Y-m-d");
		if($uname!="admin")
		{
	        $qN=mysqli_query($conn,"insert into $user_log (`dealer_code`, `login_id`, `user_type`, `Xcode`, `login_date`, `date`) values ('".$r["dealer_code"]."','$uname','".$r["user_type"]."','$xcodex','$Ldate','$Ldate')");		
			//echo "insert into $user_log values('".$r["dealer_code"]."','$uname','".$r["user_type"]."','$xcodex','$Ldate','$Ldate')";
			$sq = mysqli_query($conn,"SELECT * FROM `user_log` ORDER BY record DESC LIMIT 1");
			if(mysqli_num_rows($sq)>0)
			{
				while($r9=mysqli_fetch_array($sq))
				{
					$rec = $r9["record"];
				}
			}
		}
		if($r["user_type"]==999)
		{
		    
			if(($dealer_code=="E5222")||($dealer_code=="E5221")||($dealer_code=="E5223")||($dealer_code=="W5205")||($dealer_code=="W5209")||($dealer_code=="W5214")||($dealer_code=="W5217")||($dealer_code=="W5220")||($dealer_code=="E5224")||($dealer_code=="W5236")|($dealer_code=="E5A00")||($dealer_code=="W5243")||($dealer_code=="W2201")||($dealer_code=="W2214")||($dealer_code=="W2212")||($dealer_code=="W2261")||($dealer_code=="W2217")||($dealer_code=="W2247")||($dealer_code=="W2266")||($dealer_code=="W2256")||($dealer_code=="W2265"))
			{
				$q2=mysqli_query($conn,"select * from Q7 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");
				// echo "select * from Q7 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
			}
			else if (($dealer_code == "N3205") || ($dealer_code == "N3202") || ($dealer_code == "N3212") || ($dealer_code == "N3235") || ($dealer_code == "N3220") || ($dealer_code == "N3228") || ($dealer_code == "N3231") || ($dealer_code == "N3237") || ($dealer_code == "N3246") || ($dealer_code == "N3247") || ($dealer_code == "N3249") || ($dealer_code == "N3250") || ($dealer_code == "N3256") || ($dealer_code == "N3257") || ($dealer_code == "N3269") || ($dealer_code == "N3270") || ($dealer_code == "N3273") || ($dealer_code == "N3279") || ($dealer_code == "N3280") || ($dealer_code == "N3A02")) 
			{
                $q2=mysqli_query($conn,"select * from Q9 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");
            }
			else
			{
			    $q2=mysqli_query($conn,"select * from Q1 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");
    			//echo "select * from Q1 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q2 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q2 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			//echo mysqli_num_rows($q2);
    			}
    			else{}
    			if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q3 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q3 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			}
    			else{}
    			if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q4 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			}
    			else{}
    			if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q5 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q5 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			}
    			else{}
    			if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q6 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q5 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			}
    			else{}
    			if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q7 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q5 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			}
    			else{}
    			if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q8 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q5 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			}
    			else{}
    			if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q9 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q5 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			}
    			if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q10 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q5 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			}
    			else{}
    			if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q11 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q5 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			}
    			else{}
				if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q12 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q5 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			}
    			else{}
				if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q13 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q5 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			}
    			else{}
				if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q14 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q5 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			}
    			else{}
				
				if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q15 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q5 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			}
    			else{}
				if(mysqli_num_rows($q2)==0)
                {
    			$q2=mysqli_query($conn,"select * from Q16 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'");	
    			//echo "select * from Q5 where Main_Dealer_Code='$dealer_code' or Dealer_Code='$dealer_code'";
    			}
    			else{}
			}
	        
			
			
	        
	        if(mysqli_num_rows($q2)>0)
            {
            	while($r2=mysqli_fetch_array($q2))
            	{
            		$_SESSION['zone']=$r2["Zone"];
            		$_SESSION['Region']=$r2["Region"];
            		$_SESSION['Dealer_name']=$r2["Dealer_name"];
            		if($r2["Main_Dealer_Code"]==$dealer_code)
            		{
            		    $_SESSION['dealer_code_Wice']=0;
						$_SESSION['XXcodeXX'] = 1;
						$qN=mysqli_query($conn,"UPDATE `user_log` SET `dealer_code`='".$r["dealer_code"]."',`login_id`='$uname',`user_type`='".$r["user_type"]."',`Xcode`='1',`login_date`='$Ldate',`date`='$Ldate' WHERE `record` = '$rec'");
						//echo "hii";
            		}
            		else if($r2["Dealer_Code"]==$dealer_code)
            		{
                		//$_SESSION['dealer_code']=$r2["Main_Dealer_Code"];
						$_SESSION['dealer_code']=$dealer_code;
						//$_SESSION['dealer_code2']=;
                		$_SESSION['dealer_code_Wice']=$r2["Dealer_name"];
						$_SESSION['XXcodeXX'] = 2;
						$qN=mysqli_query($conn,"UPDATE `user_log` SET `dealer_code`='".$r["dealer_code"]."',`login_id`='$uname',`user_type`='".$r["user_type"]."',`Xcode`='2',`login_date`='$Ldate',`date`='$Ldate' WHERE `record` = '$rec'");
						//echo "hii2";
						//echo $_SESSION['dealer_code'];
            		}
            		else
            		{
            		    $_SESSION['dealer_code_Wice']=0;
            		}
            		$_SESSION['main_Dealer_name']=$r2["Main_dealer_name"];
            	}
            }
            else
            {
	            echo "Invalid Username and/or Password";
            }
		}
		echo ("success");
	}
}
else{
	 echo "Invalid Username and/or Password";
            session_destroy();
}
   
   
    
    
?>
