<?php 
    $ip_add = getenv("REMOTE_ADDR");
    include("../../config/defines.php");


//Get Catalogue..
    if(isset($_POST["viewCatalogue"])){
        $product_query = mysqli_query($conn, "SELECT * FROM aalierp_catalogue");
        if(mysqli_num_rows($product_query) > 0){
            while($row = mysqli_fetch_array($product_query)){ 
                $cpro = mysqli_query($conn, "SELECT COUNT(*) AS count_products FROM aalierp_product WHERE catalogue_id='".$row["catalogue_id"]."'");
                $rec = mysqli_fetch_assoc($cpro);
            ?>
                <div class="brand-wrap">
                    <div class="brand-media">
                        <img src="<?php echo $AaliSYS; ?>/uploads/catalogues/<?php echo $row["catalogue_image"]; ?>" alt="<?php echo $row["catalogue_name"]; ?>">
                        <div class="brand-overlay"><a href="<?php echo $AaliLINK; ?>/products-by-catalogue/?cid=<?php echo $row["catalogue_id"]; ?>"><i class="fa fa-link"></i></a></div>
                    </div>
                    <div class="brand-meta">
                        <h4><?php echo $row["catalogue_name"]; ?></h4>
                    </div>
                </div>
<?php       } }else{echo "No Catalogue";} }
  

//Get Banner Small..
    if(isset($_POST["viewBannerSmall"])){$cat_menu_query = mysqli_query($conn, "SELECT * FROM aalierp_banner WHERE banner_size='Small' AND banner_status='Active' LIMIT 2"); 
        while($row = mysqli_fetch_array($cat_menu_query)){ ?>
            <div class="col-sm-6 col-lg-12">
                <div class="home-grid-promo"><a href=""><img src="<?php echo $AaliSYS; ?>/uploads/banners/<?php echo $row["banner_image"]; ?>" alt="Banner"></a></div>
            </div>
<?php   } }
    

//Get Banner Large..
    if(isset($_POST["viewBannerLarge"])){$cat_menu_query = mysqli_query($conn, "SELECT * FROM aalierp_banner WHERE banner_size='Large' AND banner_status='Active' LIMIT 3"); 
        while($row = mysqli_fetch_array($cat_menu_query)){ ?>
            <a href="#"><img src="<?php echo $AaliSYS; ?>/uploads/banners/<?php echo $row["banner_image"]; ?>" alt="Banner"></a>
<?php   } }


//Get Products..
    if(isset($_POST["viewProduct"])){
        $product_query = mysqli_query($conn, "SELECT * FROM aalierp_product");
        if(mysqli_num_rows($product_query) > 0){
            while($row = mysqli_fetch_array($product_query)){ ?>

                <div class="col">
                    <div class="product-card">
                        <div class="product-media">
                            <div class="product-label"><label class="label-text sale">Latest</label></div>
                            <a class="product-image" href="<?php echo $AaliLINK; ?>/product-detail?pid=<?php echo $row["product_id"]; ?>">
                                <img src="<?php echo $AaliSYS; ?>/uploads/products/<?php echo $row["product_image"]; ?>" alt="<?php echo $row["product_name"]; ?>">
                            </a>
                        </div>
                        <div class="product-content">
                            <h6 class="product-name"><a href="<?php echo $AaliLINK; ?>/product-detail?pid=<?php echo $row["product_id"]; ?>" style="text-transform: uppercase;"><?php echo $row["product_name"]; ?></a></h6>
                            <h6 class="product-price" style="float: right;font-weight: 700;"><span><?php echo $cur; ?><?php echo $row["product_price"]; ?></span></h6>
                            <a href="<?php echo $AaliLINK; ?>/product-detail?pid=<?php echo $row["product_id"]; ?>" style="background-color: #ED4B62;color: white;" class="product-add mb-1" title="View">
                                <i class="fas fa-clipboard"></i><span> Order Now</span>
                            </a>
                            <a href="https://web.whatsapp.com/send?phone=<?php echo $product_mobile; ?>&text=I want to buy this item <?php echo $AaliLINK; ?>/product-detail/?pid=<?php echo $row["product_id"] ?>" style="background-color: #00A313;color: white;" class="product-add" title="Order By WhatsApp">
                                <i class="fas fa-whatsapp"></i><span> Order By WhatsApp</span>
                            </a>
                        </div>
                    </div>
                </div>

<?php       }
        }else{echo "No Product";}
    }
    



//Add Product To Cart..
    if(isset($_POST["addToCart"])){
        $p_id = $_POST["proId"];
        $qty = $_POST["qty"];
        if(isset($_SESSION["user_id"])){
            $user_id = $_SESSION["user_id"];
            $sql = "SELECT * FROM `aalierp_cart` WHERE p_id = '$p_id' AND user_id = '$user_id'";
            $run_query = mysqli_query($conn,$sql);
            $count = mysqli_num_rows($run_query);
            if($count > 0){
                echo "<div class='alert alert-warning alert-block' style='padding:10px;'><b>Product is already added into the cart Continue Shopping..!</b></div>";
            } else {
                $sql = "INSERT INTO `aalierp_cart` (`p_id`, `ip_add`, `user_id`, `qty`) VALUES ('$p_id','$ip_add','$user_id','$qty')";
                if(mysqli_query($conn,$sql)){

$contt = mysqli_query($conn,"SELECT * FROM aalierp_contents ORDER BY company_id DESC LIMIT 1"); $roc = mysqli_fetch_assoc($contt);
$sql = mysqli_query($conn,"SELECT * FROM aalierp_user WHERE user_email = '".$_SESSION["user_email"]."' AND user_status='Approved'");
$row = mysqli_fetch_assoc($sql);
$date = date("Y-m-d H:i:s");
$subjectt = "Notification of login at eTewish Portal"; 
$fromm = "noreply@tewish.mraalionline.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://tewish.mraalionline.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://tewish.mraalionline.com'><img src='$logo' style='width: 100px; height: auto;' alt='tewish.mraalionline Logo' /></a><br><br>
            | <a href='https://tewish.mraalionline.com/products' style='text-decoration: none; color: blue;'> Products</a> | 
            <a href='https://tewish.mraalionline.com/about' style='text-decoration: none; color: blue;'> About</a> | 
            <a href='https://tewish.mraalionline.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 using ".$roc["company_name"]." Portal!<br><br>
This is to inform you that you have added products into your cart. Details are below:<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>
";
$n=1;$sum=0;
$proquery = mysqli_query($conn, "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://tewish.mraalionline.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://tewish.mraalionline.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>
tewish.mraalionline 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://tewish.mraalionline.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://tewish.mraalionline.com'>Home</a> | <a href='https://tewish.mraalionline.com/products'>Products</a> | <a href='https://tewish.mraalionline.com/about'>About</a> | <a href='https://tewish.mraalionline.com/contact'>Contact</a> |
        <br><br><br>
</body></html>";
    mail($row["user_email"], $subjectt, $messag, $header);
                    echo "<div class='alert alert-success alert-block' style='padding:10px;'>Product is Added..!<b> Go to Cart to proceed!</b></div>";
                }
            }
        }else{
            $sql = "SELECT id FROM aalierp_cart WHERE ip_add = '$ip_add' AND p_id = '$p_id' AND user_id = -1";
            $query = mysqli_query($conn,$sql);
            if (mysqli_num_rows($query) > 0) {
                echo "
                    <div class='alert alert-warning alert-block' style='padding:10px;'><b>Product is already added into the cart. Continue Shopping..!</b></div>
                ";
                exit();
            }
            $sql = "INSERT INTO `aalierp_cart` (`p_id`, `ip_add`, `user_id`, `qty`) VALUES ('$p_id','$ip_add','-1','$qty')";
            if (mysqli_query($conn,$sql)) {
                echo "
                    <div class='alert alert-success' style='padding:10px;'><b>Product is Added Successfully..!</b></div>
                ";
                exit();
            }
        }
    }
   
//Add Product To Cart From Detail..
    if(isset($_POST["qty"])){
        $p_id = $_POST["p_id"];
        $qty = $_POST["qty"];
        if(isset($_SESSION["user_id"])){
            $user_id = $_SESSION["user_id"];

            $sql = "SELECT * FROM `aalierp_cart` WHERE p_id = '$p_id' AND user_id = '$user_id'";
            $run_query = mysqli_query($conn,$sql);
            $count = mysqli_num_rows($run_query);
            if($count > 0){
                echo "
                    <div class='alert alert-warning alert-block ' style='padding:10px;'>
                        <b>Product is already added into the cart Continue Shopping..!</b>
                    </div>
                ";
            } else {
                $sql = "INSERT INTO `aalierp_cart` (`p_id`, `ip_add`, `user_id`, `qty`) VALUES ('$p_id','$ip_add','$user_id','$qty')";
                if(mysqli_query($conn,$sql)){

$contt = mysqli_query($conn,"SELECT * FROM aalierp_contents ORDER BY company_id DESC LIMIT 1"); $roc = mysqli_fetch_assoc($contt);

                    
$sql = mysqli_query($conn,"SELECT * FROM aalierp_user WHERE user_email = '".$_SESSION["user_email"]."' AND user_status='Approved'");
$row = mysqli_fetch_assoc($sql);
$date = date("Y-m-d H:i:s");
$subjectt = "Products Added To Cart Notification at ".$roc["company_name"]." Portal"; 
$fromm = "noreply@tewish.mraalionline.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://tewish.mraalionline.com/cms/uploads/logos/".$roc["company_logo"];
$messag = "
<html>
    <head>
        <title>Products Added To Cart 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://tewish.mraalionline.com'><img src='$logo' style='width: 100px; height: auto;' alt='tewish.mraalionline Logo' /></a><br><br>
            | <a href='https://tewish.mraalionline.com/products' style='text-decoration: none; color: blue;'> Products</a> | 
            <a href='https://tewish.mraalionline.com/about' style='text-decoration: none; color: blue;'> About</a> | 
            <a href='https://tewish.mraalionline.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 using ".$roc["company_name"]." Portal!<br><br>
This is to inform you that you have added products into your cart. Details are below:<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>
";


$n=1;$sum=0;
$proquery = mysqli_query($conn, "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://tewish.mraalionline.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://tewish.mraalionline.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>

tewish.mraalionline 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://tewish.mraalionline.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://tewish.mraalionline.com'>Home</a> | <a href='https://tewish.mraalionline.com/products'>Products</a> | <a href='https://tewish.mraalionline.com/about'>About</a> | <a href='https://tewish.mraalionline.com/contact'>Contact</a> |
        <br><br><br>


</body></html>";
                                  
    
    mail($row["user_email"], $subjectt, $messag, $header);
                    
                    
                    
                    
                    echo "
                        <div class='alert alert-success alert-block ' style='padding:10px;'>
                            Product is Added..!<b> Go to Cart to proceed!</b>
                        </div>
                    ";
                }
            }
        }else{
            $sql = "SELECT id FROM aalierp_cart WHERE ip_add = '$ip_add' AND p_id = '$p_id' AND user_id = -1";
            $query = mysqli_query($conn,$sql);
            if (mysqli_num_rows($query) > 0) {
                echo "
                    <div class='alert alert-warning alert-block ' style='padding:10px;'>
                        <b>Product is already added into the cart. Continue Shopping..!</b>
                    </div>";
                    exit();
            }
            $sql = "INSERT INTO `aalierp_cart` (`p_id`, `ip_add`, `user_id`, `qty`) VALUES ('$p_id','$ip_add','-1','$qty')";
            if (mysqli_query($conn,$sql)) {
                echo "
                    <div class='alert alert-success alert-block ' style='padding:10px;'>
                        <b>Product is Added Successfully..!</b>
                    </div>
                ";
                exit();
            }
        }
    }
    



//Add Post To Cart..
    if(isset($_POST["orderCart"])){
        $userId = $_POST["userId"];
        if(isset($_SESSION["user_id"])){
            $user_id = $_SESSION["user_id"];
            $sql = "SELECT * FROM `aalierp_cart` WHERE user_id = '$user_id'";
            $run_query = mysqli_query($conn,$sql);
            $count = mysqli_num_rows($run_query);
            if($count > 0){
                $sql = "UPDATE aalierp_cart SET status='Processing' WHERE user_id='$user_id' AND status='Chosen'";
                if(mysqli_query($conn,$sql)){
$contt = mysqli_query($conn,"SELECT * FROM aalierp_contents ORDER BY company_id DESC LIMIT 1"); $roc = mysqli_fetch_assoc($contt);
$sql = mysqli_query($conn,"SELECT * FROM aalierp_user WHERE user_email = '".$_SESSION["user_email"]."' AND user_status='Approved'");
$row = mysqli_fetch_assoc($sql);
$date = date("Y-m-d H:i:s");
$subjectt = "Yout Order Notification at ".$roc["company_name"]." Portal"; 
$fromm = "noreply@tewish.mraalionline.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://tewish.mraalionline.com/cms/uploads/logos/".$roc["company_logo"];
$messag = "
<html>
    <head><title>Your Order 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://tewish.mraalionline.com'><img src='$logo' style='width: 100px; height: auto;' alt='tewish.mraalionline Logo' /></a><br><br>
            | <a href='https://tewish.mraalionline.com/products' style='text-decoration: none; color: blue;'> Products</a> | 
            <a href='https://tewish.mraalionline.com/about' style='text-decoration: none; color: blue;'> About</a> | 
            <a href='https://tewish.mraalionline.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 using ".$roc["company_name"]." Portal!<br><br>
This is to inform you that you have added products into your cart. Details are below:<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>
";
$n=1;$sum=0;
$proquery = mysqli_query($conn, "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://tewish.mraalionline.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://tewish.mraalionline.com/invoice/?usr=".$row["user_id"]."' style='text-decoration: none; color: white; font-size: 2.0em; text-align: center;'> 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>
eTewish 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://tewish.mraalionline.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://tewish.mraalionline.com'>Home</a> | <a href='https://tewish.mraalionline.com/products'>Products</a> | <a href='https://tewish.mraalionline.com/about'>About</a> | <a href='https://tewish.mraalionline.com/contact'>Contact</a> |
        <br><br><br>
</body></html>";
    mail($row["user_email"], $subjectt, $messag, $header);     
                    echo "
                        <div class='alert alert-success' style='padding:10px;'>
                            Your Order Placed & Processing..!<b> Check detail in profile page! <a href='".$AaliLINK."/profile' class='btn btn-success btn-sm'> Go To Profile </a></b>
                        </div>
                    ";
                }
            }
        }
    }
    


//Count User cart item
    if (isset($_POST["count_item"])) {
        if (isset($_SESSION["user_id"])) {
            $sql = "SELECT COUNT(*) AS count_item FROM aalierp_cart WHERE user_id = '".$_SESSION["user_id"]."' AND status = 'Chosen'";
        }else{
            $sql = "SELECT COUNT(*) AS count_item FROM aalierp_cart WHERE ip_add = '$ip_add' AND user_id < 0";
        }
        
        $query = mysqli_query($conn,$sql);
        $row = mysqli_fetch_array($query);
        echo $row["count_item"];
        exit();
    }   
    
    if (isset($_POST["count_processing"])) {
        if (isset($_SESSION["user_id"])) {
            $sql = "SELECT COUNT(*) AS count_processing FROM aalierp_cart WHERE user_id = '".$_SESSION["user_id"]."' AND status = 'Processing'";
        }
        
        $query = mysqli_query($conn,$sql);
        $row = mysqli_fetch_array($query);
        echo $row["count_processing"];
        exit();
    }
    
    if (isset($_POST["count_processed"])) {
        if (isset($_SESSION["user_id"])) {
            $sql = "SELECT COUNT(*) AS count_processed FROM aalierp_cart WHERE user_id = '".$_SESSION["user_id"]."' AND status = 'Processed'";
        }
        
        $query = mysqli_query($conn,$sql);
        $row = mysqli_fetch_array($query);
        echo $row["count_processed"];
        exit();
    }





//Count Item prices
    if (isset($_POST["chosen_item_price"])) { $tot=0;
        if (isset($_SESSION["user_id"])) {
            $sql = "SELECT * FROM aalierp_cart WHERE user_id = '".$_SESSION["user_id"]."' AND status = 'Chosen'";
        }else{
            $sql = "SELECT * FROM aalierp_cart WHERE ip_add = '$ip_add' AND user_id < 0";
        }
        $query = mysqli_query($conn,$sql);
        while($row = mysqli_fetch_array($query)){
            $priceq=mysqli_query($conn, "SELECT product_price FROM aalierp_product WHERE product_id='".$row["p_id"]."'"); 
            while($rec=mysqli_fetch_array($priceq)){ $tp=0;
                $tp=$row["qty"]*$rec["product_price"];
                $tot = $tot + $tp;
            }
        }
        echo $tot;
        exit();
    }  
    if (isset($_POST["processing_item_price"])) { $tot=0;
        if (isset($_SESSION["user_id"])) {
            $sql = "SELECT * FROM aalierp_cart WHERE user_id = '".$_SESSION["user_id"]."' AND status = 'Processing'";
        }else{
            $sql = "SELECT * FROM aalierp_cart WHERE ip_add = '$ip_add' AND user_id < 0";
        }
        $query = mysqli_query($conn,$sql);
        while($row = mysqli_fetch_array($query)){
            $priceq=mysqli_query($conn, "SELECT product_price FROM aalierp_product WHERE product_id='".$row["p_id"]."'"); 
            while($rec=mysqli_fetch_array($priceq)){ $tp=0;
                $tp=$row["qty"]*$rec["product_price"];
                $tot = $tot + $tp;
            }
        }
        echo $tot;
        exit();
    } 
    if (isset($_POST["processed_item_price"])) { $tot=0;
        if (isset($_SESSION["user_id"])) {
            $sql = "SELECT * FROM aalierp_cart WHERE user_id = '".$_SESSION["user_id"]."' AND status = 'Processed'";
        }else{
            $sql = "SELECT * FROM aalierp_cart WHERE ip_add = '$ip_add' AND user_id < 0";
        }
        $query = mysqli_query($conn,$sql);
        while($row = mysqli_fetch_array($query)){
            $priceq=mysqli_query($conn, "SELECT product_price FROM aalierp_product WHERE product_id='".$row["p_id"]."'"); 
            while($rec=mysqli_fetch_array($priceq)){ $tp=0;
                $tp=$row["qty"]*$rec["product_price"];
                $tot = $tot + $tp;
            }
        }
        echo $tot;
        exit();
    }  





//Items in side cart
    if(isset($_POST["itemInCart"])){
        if (isset($_SESSION["user_id"])) {
            $sql = "SELECT * FROM aalierp_cart WHERE user_id = '".$_SESSION["user_id"]."' AND status = 'Chosen'";
        }else{$sql = "SELECT * FROM aalierp_cart WHERE ip_add = '$ip_add' AND user_id < 0";}
        $query = mysqli_query($conn,$sql);
        while($row = mysqli_fetch_array($query)){
            $priceq=mysqli_query($conn, "SELECT * FROM aalierp_product WHERE product_id='".$row["p_id"]."'"); 
            while($rec=mysqli_fetch_array($priceq)){
?>
                <li class="cart-item" style="border-radius:20px; padding:10px;background:#f7f7f7;margin-top:10px;border:1px solid #ED4B62;">
                    <div class="cart-media"><a href="#"><img src="<?php echo $AaliSYS; ?>/uploads/products/<?php echo $rec["product_image"]; ?>" style="width: 50px;height:50px;border: 1px solid lightgrey;border-radius: 7px;" alt="product"></a></div>
                    <div class="cart-info-group">
                        <button remove_id="<?php echo $rec["product_id"]; ?>" class="cart-delete remove" style="float: right;color: red;padding-right: 31px;"><i class="far fa-trash-alt"></i></button>
                        <div class="cart-info">
                            <h6><a href="#"><?php echo $rec["product_name"]; ?></a></h6>
                            <p style="font-size:12px"><?php echo $cur."".$rec["product_price"]; ?></p>
                            <p style="font-size:12px">Qty: <?php echo $row["qty"]; ?></p>
                        </div>
                        <div class="cart-action-group">
                            <div class="product-action"></div>
                            <h6><?php echo $cur."".$rec["product_price"]*$row["qty"]; ?></h6>
                        </div>
                    </div>
                </li>
<?php
            }
        }
    }
    
    


//Items in checkout
    if(isset($_POST["itemInCheckout"])){
        if (isset($_SESSION["user_id"])) {
            $sql = "SELECT * FROM aalierp_cart WHERE user_id = '".$_SESSION["user_id"]."' AND status = 'Chosen'";
        }else{$sql = "SELECT * FROM aalierp_cart WHERE ip_add = '$ip_add' AND user_id < 0";}
        $query = mysqli_query($conn,$sql);
        while($row = mysqli_fetch_array($query)){
            $priceq=mysqli_query($conn, "SELECT * FROM aalierp_product WHERE product_id='".$row["p_id"]."'"); 
            while($rec=mysqli_fetch_array($priceq)){
?>
                <tr>
                    <td>
                        <img src="<?php echo $AaliSYS; ?>/uploads/products/<?php echo $rec["product_image"]; ?>" style="width: 75px;height:75px;border: 1px solid lightgrey;border-radius: 7px;" alt="product">
                    </td>
                    <td><?php echo $rec["product_name"]."<br> Quantity: ".$row["qty"]." x ".$cur."".$rec["product_price"]."<br>Price: ".$cur."".$row["qty"]*$rec["product_price"]; ?></td>
                    <td><button remove_id="<?php echo $rec["product_id"]; ?>" class="cart-delete remove" style="float: right;color: red;padding-right: 31px;"><i class="far fa-trash-alt"></i></button></td>
                </tr>
<?php
            }
        }
    }

    
    

//Coupon view..
    if(isset($_POST["viewCoupon"])){
        $query = mysqli_query($conn, "SELECT * FROM aalierp_coupon WHERE user_id='".$_SESSION["user_id"]."'"); 
        while($row = mysqli_fetch_assoc($query)){ ?>
            <div class="col-md-4">
                <div class="card" style="border: 2px solid #E25A00;">
                    <div class="card-body">
                        <h5 class="text-center" style='color: #E25A00;'>Silver Coupon</h5>
                        <h6 class="text-center">CouponID-<?php echo $row["coupon_cid"]." -> Discount: ".$row["coupon_discount"]."%"; ?></h6>
                        <h6 class="text-center">Valid: <?php echo $row["coupon_valid_from"]." - ".$row["coupon_valid_to"]; ?></h6>
                        <h6 class="text-center">Order qty.: <?php echo $row["coupon_minimum"]; ?></h6>
                    </div>
                </div>
            </div>
        <?php }
    }
    
    
    
    
    
    
//Get Cart Item From Database
    if(isset($_POST["Common"])){
        if(isset($_SESSION["user_id"])){
            $sql = "SELECT p.product_id,p.product_name,p.product_type,p.product_image,p.product_price,c.id,c.user_id,c.qty,c.ip_add,c.ship,c.mail,c.coupon,c.cdiscount,c.status FROM aalierp_product p,aalierp_cart c WHERE p.product_id=c.p_id AND c.user_id='".$_SESSION["user_id"]."'";
        }else{
            $sql = "SELECT p.product_id,p.product_name,p.product_type,p.product_image,p.product_price,c.id,c.user_id,c.qty,c.ip_add,c.ship,c.mail,c.status FROM aalierp_product p,aalierp_cart c WHERE p.product_id=c.p_id AND c.ip_add='$ip_add' AND c.user_id < 0";
        }
        $query = mysqli_query($conn,$sql);
        if(isset($_POST["getCartItem"])){
            if(mysqli_num_rows($query) > 0){ $n=1; $tot=0; $tprice=0; $tship=0; $cdis=0; ?>
                
                <div class='col-md-9'>
                    <h4 class="mb-3">Cart Detail</h4>
                    <div class="cart_msg"></div>
                    <table width="100%" class="table table-sm py-3" style="color: #777;">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Product</th>
                                <th>Quantity</th>
                                <th>Unit Price</th>
                                <th>Shipment</th>
                                <?php if(isset($_SESSION["user_id"])){ ?>
                                    <th>Discount (CID)</th>
                                <?php } ?>
                                <th>Total</th>
                            </tr>
                        </thead>
                        <tbody>
                
                        <?php while ($row=mysqli_fetch_array($query)){ 
                            if($row["status"]=="Chosen"){
                            $tot = $tot + $row["product_price"]*$row["qty"]+$row["ship"]-$row["cdiscount"]; $ip_add=$row["ip_add"]; 
                            $tprice = $tprice + $row["product_price"]*$row["qty"];
                            $tship = $tship + $row["ship"]; $cdis = $cdis + $row["cdiscount"];
                        ?>
                            <tr>
                                <td><?php echo $n; ?></td>
                                <td><?php echo $row["product_name"]; ?></td>
                                <td><?php echo $row["qty"]; ?></td>
                                <td><?php echo $cur."".$row["product_price"]; ?></td>
                                <td><?php echo $cur."".round($row["ship"],2); ?></td>
                                <?php if(isset($_SESSION["user_id"])){ ?>
                                    <td><?php echo $cur."".$row["cdiscount"]; ?></td>
                                <?php } ?>
                                <td><?php echo $cur." ".round($row["product_price"]*$row["qty"]-$row["cdiscount"]+$row["ship"],2); ?></td>
                            </tr>
                        <?php $n++; } } ?>
                        </tbody>
                    </table>
                </div>
                <div class='col-md-3'>
                    <div class="card shadow-lg">
                        <div class="card_body p-3">
                            <div class="row">
                                <div class='col-md-6'>Items Price: </div>
                                <div class='col-md-6' style="text-align: right;"><?php echo $cur."".round($tprice,2); ?></div>
                                <div class='col-md-6'>Shipment: </div>
                                <div class='col-md-6' style="text-align: right;"><?php echo $cur."".round($tship,2); ?></div>
                                <div class='col-md-6'>Dicount: </div>
                                <div class='col-md-6' style="text-align: right;"><?php echo $cur."".round($cdis,2); ?></div>
                                
                                <div class='col-md-12'><hr></div>
                                <div class='col-md-6'>Total Payable: </div>
                                <div class='col-md-6' style="text-align: right;"><?php echo $cur."".round($tot,2); ?></div>
                                <div class='col-md-12'><hr></div>
                                
                                <div class='col-md-12'>
                                    <?php if(isset($_SESSION["user_id"])){ ?>
                                        <a style="float:right;" href="<?php echo $AaliLINK; ?>/invoice/?usr=<?php echo $_SESSION["user_id"]; ?>" class="btn btnn w-100" target="_blank"><i class="fa fa-clipboard"></i> Generate Invoice </a>
                                    <?php }else{ ?>
                                        <a style="float:right;" href="<?php echo $AaliLINK; ?>/invoice/?ip=<?php echo $ip_add; ?>&id=-1" class="btn btnn w-100" target="_blank"><i class="fa fa-clipboard"></i> Generate Invoice </a>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class='col-md-6'></div>
                <div class='col-md-3'>
                    <a style="float:right;" href="<?php echo $AaliLINK; ?>/checkout" class="btn btn-info w-100"><i class="fa fa-send"></i> Update & Checkout </a>
                </div>
                
                <?php exit();
            }
        }
        
        if(isset($_POST["getOrderItem"])){
            if(mysqli_num_rows($query) > 0){ $n=1; $tot=0; $tprice=0; $tship=0; $cdis=0; ?>
                <div class='col-md-12'><hr></div>
                <div class='col-md-9'>
                    <h4 class="mb-3">Orders Processing</h4>
                    <div class="msg"></div>
                    <table width="100%" class="table table-sm py-3" style="color: #777;">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Product</th>
                                <th>Quantity</th>
                                <th>Unit Price</th>
                                <th>Shipment</th>
                                <th>Discount (CID)</th>
                                <th>Total</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                
                        <?php while ($row=mysqli_fetch_array($query)) { 
                            if($row["status"]=="Processing"){
                            $tot = $tot + $row["product_price"]*$row["qty"]+$row["ship"]-$row["cdiscount"]; $ip_add=$row["ip_add"]; 
                            $tprice = $tprice + $row["product_price"]*$row["qty"];
                            $tship = $tship + $row["ship"]; $cdis = $cdis + $row["cdiscount"];
                        ?>
                            <tr>
                                <td><?php echo $n; ?></td>
                                <td><?php echo $row["product_name"]; ?></td>
                                <td><?php echo $row["qty"]; ?></td>
                                <td><?php echo $cur."".$row["product_price"]; ?></td>
                                <td><?php echo $cur."".round($row["ship"],2); ?></td>
                                <td><?php echo $cur."".$row["cdiscount"]; ?></td>
                                <td><?php echo $cur."".round($row["product_price"]*$row["qty"]-$row["cdiscount"]+$row["ship"],2); ?></td>
                                <td><?php echo $row["status"]; ?></td>
                                <td>
                                    <?php if($row["status"] == "Processing"){ ?>
                                        <a href="#" remove_id="<?php echo $row["product_id"]; ?>" class="btn btn-danger btn-sm remove">Cancel</a>
                                    <?php } ?>
                                </td>
                            </tr>
                        <?php } $n++; } ?>
                        </tbody>
                    </table>
                </div>
                <div class='col-md-3'>
                    <div class="card shadow-lg">
                        <div class="card_body p-3">
                            <div class="row">
                                <div class='col-md-6'>Items Price: </div>
                                <div class='col-md-6' style="text-align: right;"><?php echo $cur."".round($tprice,2); ?></div>
                                <div class='col-md-6'>Shipment: </div>
                                <div class='col-md-6' style="text-align: right;"><?php echo $cur."".round($tship,2); ?></div>
                                <div class='col-md-6'>Dicount: </div>
                                <div class='col-md-6' style="text-align: right;"><?php echo $cur."".round($cdis,2); ?></div>
                                
                                <div class='col-md-12'><hr></div>
                                <div class='col-md-6'>Total Payable: </div>
                                <div class='col-md-6' style="text-align: right;"><?php echo $cur."".round($tot,2); ?></div>
                                <div class='col-md-12'><hr></div>
                                
                                <div class='col-md-12'>
                                    <?php if(isset($_SESSION["user_id"])){ ?>
                                        <a style="float:right;" href="<?php echo $AaliLINK; ?>/invoice/?usr=<?php echo $_SESSION["user_id"]; ?>" class="btn btnn w-100" target="_blank"><i class="fa fa-clipboard"></i> Generate Invoice </a>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <?php exit();
            }
        }
        
        if(isset($_POST["getOrderedItem"])){
            if(mysqli_num_rows($query) > 0){ $n=1; $tot=0; $tprice=0; $tship=0; $cdis=0; ?>
                <div class='col-md-12'><hr></div>
                <div class='col-md-12'>
                    <h4 class="mb-3">History Orders Processed (All Orders Purchases Done)</h4>
                    <div class="msg"></div>
                    <table width="100%" class="table table-sm py-3" style="color: #777;">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Product</th>
                                <th>Quantity</th>
                                <th>Unit Price</th>
                                <th>Shipment</th>
                                <th>Discount (CID)</th>
                                <th>Total</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                
                        <?php while ($row=mysqli_fetch_array($query)) { 
                            if($row["status"]=="Processed"){
                            $tot = $tot + $row["product_price"]*$row["qty"]+$row["ship"]-$row["cdiscount"]; $ip_add=$row["ip_add"]; 
                            $tprice = $tprice + $row["product_price"]*$row["qty"];
                            $tship = $tship + $row["ship"]; $cdis = $cdis + $row["cdiscount"];
                        ?>
                            <tr>
                                <td><?php echo $n; ?></td>
                                <td><?php echo $row["product_name"]; ?></td>
                                <td><?php echo $row["qty"]; ?></td>
                                <td><?php echo $cur."".$row["product_price"]; ?></td>
                                <td><?php echo $cur."".round($row["ship"],2); ?></td>
                                <td><?php echo $cur."".$row["cdiscount"]; ?></td>
                                <td><?php echo $cur."".round($row["product_price"]*$row["qty"]-$row["cdiscount"]+$row["ship"],2); ?></td>
                                <td><?php echo $row["status"]; ?></td>
                                <td>
                                    <?php if($row["status"] == "Processing"){ ?>
                                        <a href="#" remove_id="<?php echo $row["product_id"]; ?>" class="btn btn-danger btn-sm remove">Cancel</a>
                                    <?php } ?>
                                </td>
                            </tr>
                        <?php } $n++; } ?>
                        </tbody>
                    </table>
                </div>
                <?php exit();
            }
        }
    
        if(isset($_POST["checkOutDetails"])){
            if(mysqli_num_rows($query) > 0){ ?>
                <form method='post'>
            <?php $n=0;
                while($row=mysqli_fetch_array($query)){
                    if($row["status"]=="Chosen"){
                    $n++;
                    $product_id = $row["product_id"];
                    $product_name = $row["product_name"];
                    $product_image = $row["product_image"];
                    $product_price = $row["product_price"];
                    $product_type = $row["product_type"];
                    $cart_item_id = $row["id"];
                    $qty = $row["qty"]; ?>
                    
                    <tr>
                        <input type="hidden" name="product_id[]" value="<?php echo $product_id; ?>"/>
                        <input type="hidden" name="id" value="<?php echo $cart_item_id; ?>"/>
                        <td width="3%"><?php echo $n; ?></td>
                        <td width="12%"><?php echo $product_name; ?></td>
                        <td width="7%"><input type="text" class="form-control qty" value="<?php echo $qty; ?>" ></td>
                        <td width="9%"><input type="text" class="form-control price" value="<?php echo $product_price; ?>" readonly="readonly"></td>
                        <td width="9%"><input type="text" class="form-control ship" value="<?php echo round($row["ship"],2); ?>" readonly="readonly"></td>
                        <td width="9%"><input type="text" class="form-control cdis" value="<?php echo round($row["cdiscount"],2); ?>" readonly="readonly"></td>
                        <td width="14%"><input type="text" class="form-control total" value="<?php echo round($product_price,2); ?>" readonly="readonly"></td>
                        <td width="8%">
                            <div class="btn-group float-right">
                                <a href="#" update_id="<?php echo $product_id; ?>" class="btn btn-success btn-sm update"><i class="fa fa-thumbs-up"></i></a>
                                <a href="#" remove_id="<?php echo $product_id; ?>" class="btn btn-danger btn-sm remove"><i class="fa fa-trash"></i></a>        
                            </div>
                        </td>
                    </tr>
                        
                <?php } } ?>
                </form>
                <?php if(isset($_SESSION['user_id'])){ ?>
                    <tr>
                        <td colspan="7"></td>
                        <td colspan="4"><span style="font-size: 1.3em; float: right;">Total: <?php echo $cur; ?><b class="net_total"></b></span></td>
                    </tr>
                    <tr>
                        <td colspan="7"></td>
                        <td colspan="4"><a href="#" onclick="Do you want to order product (Y/N)?" pid="<?php echo $_SESSION["user_id"]; ?>" id="order_cart" class="btn btnn w-100" style="background:red;"> Confirm Order </a></td>
                    </tr> 
                <?php }else{ ?>
                    <tr>
                        <td colspan="7"></td>
                        <td colspan="3"><span style="font-size: 1.3em; float: right;">Total: <?php echo $cur; ?><b class="net_total"></b></span></td>
                    </tr>
                <?php } ?>
                
         	<?php  
            }
        }
    }
    
    
    




//Update Item From cart
    if (isset($_POST["updateCartItem"])){
        $update_id = $_POST["update_id"];
        $qty = $_POST["qty"];
        $mail = $_POST["mail"];
        $coupon = $_POST["coupon"];
        
        $mtot = 0; $ctot = 0;
        
        if (isset($_SESSION["user_id"])) {
            $sql = "UPDATE aalierp_cart SET qty='$qty' WHERE p_id = '$update_id' AND user_id = '".$_SESSION["user_id"]."'";
            if(mysqli_query($conn,$sql)){
            
                    
$sql = mysqli_query($conn,"SELECT * FROM aalierp_user WHERE user_email = '".$_SESSION["user_email"]."' AND user_status='Approved'");
$row = mysqli_fetch_assoc($sql);
$date = date("Y-m-d H:i:s");
$subjectt = "Your Cart Update at tewish.mraalionline Portal"; 
$fromm = "noreply@tewish.mraalionline.org";
$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://tewish.mraalionline.com/assets/images/logo.png";
$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://tewish.mraalionline.com'><img src='$logo' style='width: 100px; height: auto;' alt='tewish.mraalionline Logo' /></a><br><br>
            | <a href='https://tewish.mraalionline.com/products' style='text-decoration: none; color: blue;'> Products</a> | 
            <a href='https://tewish.mraalionline.com/library' style='text-decoration: none; color: blue;'> Post Material</a> | 
            <a href='https://tewish.mraalionline.com/forum' style='text-decoration: none; color: blue;'> Forum</a> | 
            <a href='https://tewish.mraalionline.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 using tewish.mraalionline Portal!<br><br>
This is to inform you that you have updated your cart. Details are below:<br>

----------------------------------------------------------<br>
    Name: ".$row["user_name"]."<br>
    Email: ".$row["user_email"]."<br>
    Update Cart On: ".$date."<br>
    City: ".$city."<br>
    Country: ".$country."<br>
----------------------------------------------------------<br><br>

</p>

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


$n=1;$sum=0;
$proquery = mysqli_query($conn, "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["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://tewish.mraalionline.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;'>
<a href='https://tewish.mraalionline.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>

tewish.mraalionline Portal<br><br><br>

| <a href='mailto:info@tewish.mraalionline.org'>info@tewish.mraalionline.org</a> | <a href='tel:<?php echo $company_phone; ?>'><?php echo $company_phone; ?></a> | <br><br>
            </p>
        </div>
        <br><br><br>
        <a href='https://tewish.mraalionline.com'><img src='$logo' style='width: 100px; height: auto;' alt='tewish.mraalionline logo' /></a><br><br>
        A108 Adam Street | Las Vegas, NY 535022 | United States<br><br>
        
        | <a href='https://tewish.mraalionline.com'>Home</a> | <a href='https://tewish.mraalionline.com/products'>Products</a> | <a href='https://tewish.mraalionline.com/about'>About</a> | <a href='https://tewish.mraalionline.com/forum'>Forum</a> | <a href='https://tewish.mraalionline.com/contact'>Contact</a> |
        <br><br><br>


</body></html>";
                                  
    
    mail($row["user_email"], $subjectt, $messag, $header);
                            
            }          
        }else{
            $sql = "UPDATE aalierp_cart SET qty='$qty', mail='$mail', ship='$mtot' WHERE p_id = '$update_id' AND ip_add = '$ip_add'";
            if(mysqli_query($conn,$sql)){
                echo "<div class='alert alert-success' style='padding:10px;'><b>Cart Updated</b></div>".$ctot;
                exit();
            }
        }
    }   
    
    
    
    
    
    
    
//Remove Item From cart
    if (isset($_POST["removeItemFromCart"])) {
        $remove_id = $_POST["rid"];
        if (isset($_SESSION["user_id"])) {
            $sql = "DELETE FROM aalierp_cart WHERE p_id = '$remove_id' AND user_id = '$_SESSION[user_id]'";
        }else{
            $sql = "DELETE FROM aalierp_cart WHERE p_id = '$remove_id' AND ip_add = '$ip_add'";
        }
        if(mysqli_query($conn,$sql)){ ?>
            <div class='alert alert-danger' style="padding:10;"><b>Product removed from cart</b></div>
            <?php exit();
        }
    }
    
//Total Deposit
    if (isset($_POST["deposit"])) {
        if (isset($_SESSION["user_id"])) {
            $sql = "SELECT * FROM aalierp_deposit WHERE user_id = '".$_SESSION["user_id"]."' AND dep_status = 'Approved'";
        }
        $total = 0;
        $query = mysqli_query($conn,$sql);
        while($row = mysqli_fetch_array($query)){ $total = $total + $row["dep_amount"]; }
        echo $cur."".$total;
        exit();
    } 
//Latest Deposit
    if (isset($_POST["latest_deposit"])) {
        if (isset($_SESSION["user_id"])) {
            $sql = "SELECT * FROM aalierp_deposit WHERE user_id = '".$_SESSION["user_id"]."' AND dep_status = 'Approved' ORDER BY dep_id DESC LIMIT 1";
        }
        $total = 0;
        $query = mysqli_query($conn,$sql);
        $row = mysqli_fetch_assoc($query);
        echo $cur."".$row["dep_amount"];
        exit();
    } 
    


//Add To Cart..
    if (isset($_POST["user_name"])) {
        $p_id = $_POST["p_id"];
        

    }

    
    
    
    
    
    


?>