<?php 
	
    include("../../config/defines.php");
    

    
    if(isset($_POST["user_email"]) && isset($_POST["user_password"])){
        $user_email = mysqli_real_escape_string($conn,$_POST["user_email"]);
        $user_password = md5($_POST["user_password"]);
        $sql = "SELECT * FROM aalierp_user WHERE user_email = '$user_email' AND user_password = '$user_password' AND user_type = 'User' AND user_status='Approved'";
        $run_query = mysqli_query($conn,$sql);
        $count = mysqli_num_rows($run_query);
        $ip_add = getenv("REMOTE_ADDR");
        //if user record is available in database then $count will be equal to 1
        if($count == 1){
            
            $row = mysqli_fetch_array($run_query);
            $_SESSION["user_id"] = $row["user_id"];
            $_SESSION["user_name"] = $row["user_name"];
            $_SESSION["user_image"] = $row["user_image"];
            $_SESSION["user_email"] = $row["user_email"];
            $_SESSION["user_mobile"] = $row["user_mobile"];
            $_SESSION["user_type"] = $row["user_type"];
            //we have created a cookie in login_form.php page so if that cookie is available means user is not login
                if (isset($_COOKIE["pro_list"])){
                    $p_list = stripcslashes($_COOKIE["pro_list"]);
                    //here we are decoding stored json product list cookie to normal array
                    $pro_list = json_decode($p_list,true);
                    for ($i=0; $i < count($pro_list); $i++) { 
                        //After getting user id from database here we are checking user cart item if there is already product is listed or not
                        $verify_cart = "SELECT id FROM aalierp_cart WHERE user_id = '".$_SESSION["user_id"]."' AND p_id = ".$pro_list[$i];
                        $result  = mysqli_query($conn,$verify_cart);
                        if(mysqli_num_rows($result) < 1){
                            //if user is adding first time product into cart we will update user_id into database table with valid id
                            $update_cart = "UPDATE aalierp_cart SET user_id = '".$_SESSION["user_id"]."' WHERE ip_add = '$ip_add' AND user_id = -1";
                            mysqli_query($conn,$update_cart);
                        }else{
                            //if already that product is available into database table we will delete that record
                            $delete_existing_product = "DELETE FROM aalierp_cart WHERE user_id = -1 AND ip_add = '$ip_add' AND p_id = ".$pro_list[$i];
                            mysqli_query($conn,$delete_existing_product);
                        }
                    }
                    //here we are destroying user cookie
                    setcookie("pro_list","",strtotime("-2 day"),"/");
                    //if user is logging from after cart page we will send cart_login
                    echo "cart_login";
                    exit();
                }
 
$contt = mysqli_query($conn,"SELECT * FROM aalierp_contents ORDER BY company_id DESC LIMIT 1"); $roc = mysqli_fetch_assoc($contt);
          
$date = date("Y-m-d H:i:s");
$subjectt = "Notification of login at D101Sale Portal"; 
$fromm = "noreply@D101Sale.com";
$header  = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= 'From: '.$fromm."\r\n".'Reply-To: '.$fromm."\r\n" .'X-Mailer: PHP/' . phpversion();
$logo = "https://d101sale.com/cms/uploads/logos/".$roc["company_logo"];
$messag = "
<html>
    <head>
        <title>Login Notification</title>
    </head>
    <body style='background: #DFE9F5; padding: 70px; text-align: center;'>
        <div style='background: #f0f0f0; width: 100%; height: auto; text-align: center; border-top: 3px dotted #000;'>
            <a href='https://d101sale.com'><img src='".$roc["company_logo"]."' style='width: 100px; height: auto;' alt='".$roc["company_name"]." Logo' /></a><br><br>
            | <a href='https://d101sale.com/products' style='text-decoration: none; color: blue;'> Products</a> | 
            <a href='https://d101sale.com/about' style='text-decoration: none; color: blue;'> About</a> | 
            <a href='https://d101sale.com/contact' style='text-decoration: none; color: blue;'> Contact</a> | <br><br> 
        </div>
        <div style='background: #fff; width: 100%; height: auto; text-align: center;'><br><br>
            <h4 style='text-align: center;'>Welcome ".$row["user_name"]."!</h4>
            <p style='text-align: center; padding: 50px;'>

Thanks for loging into D101Sale Portal!<br><br>
This is to inform you that someone is logged into ".$roc["company_name"]." website using following credentials:<br>

----------------------------------------------------------<br>
    Name: ".$row["user_name"]."<br>
    Email: ".$row["user_email"]."<br>
    Login Date & Time: ".$date."<br>
    City: ".$row["user_city"]."<br>
    Country: ".$row["user_country"]."<br>
----------------------------------------------------------<br><br>

</p>

<div style='width: 100%; margin-top: 50px; margin-bottom: 50px;'>
            <h4>Your cart detail</h4>
            <table border='1' width='100%'>
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Product</th>
                        <th>Shipment Type</th>
                        <th>Coupon</th>
                        <th>Quantity</th>
                        <th>Unit Price</th>
                        <th>Shipment</th>
                        <th>Discount (CID)</th>
                        <th>Total</th>
                    </tr>
                </thead>
                <tbody>
";

$conne = mysqli_connect("localhost","d101sale_cart","d101sale_cart","d101sale_cart");   
$n=1;$sum=0;
$proquery = mysqli_query($conne, "SELECT p.product_id,p.product_name,p.product_image,p.product_price,c.id,c.p_id,c.user_id,c.qty,c.ship,c.mail,c.coupon,c.cdiscount FROM aalierp_product p, aalierp_cart c WHERE p.product_id=c.p_id AND c.user_id='".$_SESSION["user_id"]."'");
while($rec = mysqli_fetch_array($proquery)){ $sum = $sum + ($rec["qty"]*$rec["product_price"]+$rec["ship"]-$rec["cdiscount"]);
$messag .= "
    
        
                    <tr>
                        <td>".$n."</td>
                        <td>".$rec["product_name"]."</td>
                        <td>".$rec["mail"]."</td>
                        <td>".$rec["coupon"]."</td>
                        <td>".$rec["qty"]."</td>
                        <td>".$rec["product_price"]."</td>
                        <td>".$rec["ship"]."</td>
                        <td>".$rec["cdiscount"]."</td>
                        <td>".$sum."</td>
                    </tr>
                
    
    
    
    ";
$n++;}





$messag .= "
                </tbody>
            </table>
        </div>

<br><br>

<a href='https://d101sale.com/checkout' style='padding: 15px; background: cyan; width: 250px; text-decoration: none;'> Checkout By Clicking </a>

<br><br>
<div style='width: 300px; height: 60px; line-height: 60px; background: red; text-align: center;'>
<a href='https://d101sale.com/invoice/?usr=".$row["user_id"]."' style='text-decoration: none; color: white; font-size: 2.0em;'> Download Invoice </a>
</div>
<br><br>
            <p style='padding: 50px;'>
Feel free to reach out to our team with any questions or for more information on our best material for training.<br><br>

Sincerely,<br>

D101Sale Portal <br><br><br>

| <a href='mailto:".$roc["company_email"]."'>".$roc["company_email"]."</a> | <a href='tel:".$roc["company_phone"]."'>".$roc["company_phone"]."</a> | <br><br>
            </p>
        </div>
        <br><br><br>
        <a href='https://d101sale.com'><img src='".$roc["company_logo"]."' style='width: 100px; height: auto;' alt='".$roc["company_name"]." Logo' /></a><br><br>
        ".$roc["company_address"]." | ".$roc["company_city"].", ".$roc["company_pob"]." | ".$roc["company_country"]."<br><br>
        
        | <a href='https://d101sale.com'>Home</a> | <a href='https://d101sale.com/products'>Products</a> | <a href='https://d101sale.com/about'>About</a> | <a href='https://d101sale.com/contact'>Contact</a> |
        <br><br><br>


</body></html>";
                                  
    
    mail($user_email, $subjectt, $messag, $header);
                
                $update_cart = "UPDATE aalierp_cart SET user_id = '".$_SESSION["user_id"]."' WHERE ip_add = '$ip_add' AND user_id = -1";
                mysqli_query($conn,$update_cart);
                echo "login_success";
                exit();
            }else{
                echo "<div class='alert alert-danger'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>Verify your email if registered..!</b></div>";
                exit();
            }
    }
    
    
    
    
?>