<?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="col-lg-2 col-md-6 item float-left">
                    <div class='mb-2' id="msg"></div>
                    <div class="card shadow-lg">
                        <div class="card-header p-0 position-relative">
                            <a href="<?php echo $AaliLINK; ?>/products-by-catalogue?cid=<?php echo $row["catalogue_id"]; ?>" class="zoom d-block products_by_catalogue">
                                <img class="card-img-bottom d-block" src="<?php echo $AaliSYS; ?>/uploads/catalogues/<?php echo $row["catalogue_image"]; ?>" alt="<?php echo $row["catalogue_name"]; ?>">
                            </a>
                            <div class="post-pos">
                                <a href="<?php echo $AaliLINK; ?>/products-by-catalogue?cid=<?php echo $row["catalogue_id"]; ?>" class="receipe blue bg-dark text-light products_by_catalogue"><i class="fa fa-eye"></i> View Products</a>
                            </div>
                        </div>
                        <div class="card-body course-details" style="margin-top: -15px;">
                            <a href="<?php echo $AaliLINK; ?>/products-by-catalogue?cid=<?php echo $row["catalogue_id"]; ?>" class="course-desc text-center products_by_catalogue" style="margin-top: -20px; font-size: 0.8em;"><?php echo $row["catalogue_name"]; ?></a>
                            <div class="course-meta mt-2 text-center">
                                <span class="meta-value float-left" style="font-size: 0.7em;"> Stock : <b><?php echo $rec["count_products"]; ?></b> </span>
                                <span class="meta-value float-right" style="font-size: 0.7em;"> Views: 12</span>
                            </div>
                        </div>
                    </div>
                </div>
<?php       }
        }else{echo "No Catalogue";}
    }
    
    
    



















//Get Products..
    if(isset($_POST["viewProductByCatalogue"])){
        ?><div class="row"><?php
        $cat_id = $_POST["catId"];
        $product_query = mysqli_query($conn, "SELECT * FROM aalierp_product WHERE catalogue_id='".$cat_id."'");
        if(mysqli_num_rows($product_query) > 0){
            while($row = mysqli_fetch_array($product_query)){ ?>
                <div class="col-lg-3 col-md-6 item float-left">
                    <div class='mb-2' id="msg"></div>
                    <div class="card shadow-lg">
                        <div class="card-header p-0 position-relative">
                            <a href="<?php echo $AaliLINK; ?>/product-detail?pid=<?php echo $row["product_id"]; ?>" class="zoom d-block">
                                <img class="card-img-bottom d-block" src="<?php echo $AaliSYS; ?>/uploads/products/<?php echo $row["product_image"]; ?>" alt="<?php echo $row["product_name"]; ?>">
                            </a>
                            <div class="post-pos">
                                <a href="<?php echo $AaliLINK; ?>/product-detail?pid=<?php echo $row["product_id"]; ?>" class="receipe blue bg-dark text-light"><i class="fa fa-eye"></i> View Detail</a>
                            </div>
                        </div>
                        <div class="card-body course-details" style="margin-top: -15px;">
                            <div class="price-review d-flex justify-content-between mb-1align-items-center">
                                <p style="color: #D84F00;">Price: $<?php echo $row["product_price"]; ?></p>
                                <ul class="rating-star">
                                    <li><del>$<?php echo $row["product_old_price"]; ?></del></li>
                                </ul>
                            </div>
                            <a href="<?php echo $AaliLINK; ?>/product-detail?pid=<?php echo $row["product_id"]; ?>" class="course-desc text-center"><?php echo $row["product_name"]; ?></a>
                            <div class="course-meta mt-2 text-center">
                                <div class="meta-item course-">
                                    <span class="fa fa-user-o"></span>
                                    <span class="meta-value"> : 128 </span>
                                </div>
                                <a href="#" pid="<?php echo $row["product_id"]; ?>" id="add_to_cart" class="meta-item course- products">
                                    <span class="fa fa-shopping-cart"></span>
                                    <span class="meta-value"> Add To Cart</span>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
<?php       }
        }else{echo "No Product";} ?>
            </div>
            <div class="row py-5">
                <h4 class="col-lg-12 col-md-12">Some Other Catalogues</h4>
<?php   $cat_query = mysqli_query($conn, "SELECT * FROM aalierp_catalogue WHERE catalogue_id = '".$cat_id."'");
        while($rr = mysqli_fetch_array($cat_query)){ 
            $query = mysqli_query($conn, "SELECT COUNT(*) AS count_product FROM aalierp_product WHERE catalogue_id='".$rr["catalogue_id"]."'");
            $ss = mysqli_fetch_assoc($query);
        ?>
            <div class="col-lg-2 col-md-6 item float-left">
                <div class='mb-2' id="msg"></div>
                <div class="card shadow-lg">
                    <div class="card-header p-0 position-relative">
                        <a href="<?php echo $AaliLINK; ?>/products-by-catalogue?cid=<?php echo $rr["catalogue_id"]; ?>" class="zoom d-block products_by_catalogue">
                            <img class="card-img-bottom d-block" src="<?php echo $AaliSYS; ?>/uploads/catalogues/<?php echo $rr["catalogue_image"]; ?>" alt="<?php echo $rr["catalogue_name"]; ?>">
                        </a>
                        <div class="post-pos">
                            <a href="<?php echo $AaliLINK; ?>/products-by-catalogue?cid=<?php echo $rr["catalogue_id"]; ?>" class="receipe blue bg-dark text-light"><i class="fa fa-eye"></i> View Products</a>
                        </div>
                    </div>
                    <div class="card-body course-details" style="margin-top: -15px;">
                        <a href="<?php echo $AaliLINK; ?>/products-by-catalogue?cid=<?php echo $rr["catalogue_id"]; ?>" class="course-desc text-center" style="margin-top: -20px; font-size: 0.8em;"><?php echo $rr["catalogue_name"]; ?></a>
                        <div class="course-meta mt-2 text-center">
                            <span class="meta-value float-left" style="font-size: 0.7em;"> Stock : <b><?php echo $ss["count_product"]; ?></b> </span>
                            <span class="meta-value float-right" style="font-size: 0.7em;"> Views: 12</span>
                        </div>
                    </div>
                </div>
            </div>
<?php   } ?><div>
<?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-lg-3 col-md-6 item float-left">
                    <div class='mb-2' id="msg"></div>
                    <div class="card shadow-lg ">
                        <div class="card-header p-0 position-relative">
                            <a href="<?php echo $AaliLINK; ?>/product-detail?pid=<?php echo $row["product_id"]; ?>" class="zoom d-block ">
                                <img class="card-img-bottom d-block" src="<?php echo $AaliSYS; ?>/uploads/products/<?php echo $row["product_image"]; ?>" alt="<?php echo $row["product_name"]; ?>">
                            </a>
                            <div class="post-pos">
                                <a href="<?php echo $AaliLINK; ?>/product-detail?pid=<?php echo $row["product_id"]; ?>" class="receipe blue bg-dark text-light "><i class="fa fa-eye"></i> View Detail</a>
                            </div>
                        </div>
                        <div class="card-body course-details" style="margin-top: -15px;">
                            <div class="price-review d-flex justify-content-between mb-1align-items-center">
                                <p style="color: #D84F00;">Price: $<?php echo $row["product_price"]; ?></p>
                                <ul class="rating-star">
                                    <li><del>$<?php echo $row["product_old_price"]; ?></del></li>
                                </ul>
                            </div>
                            <a href="<?php echo $AaliLINK; ?>/product-detail?pid=<?php echo $row["product_id"]; ?>" class="course-desc text-center "><?php echo $row["product_name"]; ?></a>
                            <div class="course-meta mt-2 text-center">
                                <div class="meta-item course-">
                                    <span class="fa fa-user-o"></span>
                                    <span class="meta-value"> : 128 </span>
                                </div>
                                <a href="#" pid="<?php echo $row["product_id"]; ?>" id="add_to_cart" class="meta-item course- products">
                                    <span class="fa fa-shopping-cart"></span>
                                    <span class="meta-value"> Add To Cart</span>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
<?php       }
        }else{echo "No Product";}
    }
    
    








  
  
  
  
  
  
    
    
    
    
    

    
    
    
//Get Products..
    if(isset($_POST["productDetail"])){
        $product_id = $_POST["proId"];
        $product_query = mysqli_query($conn, "SELECT * FROM aalierp_product WHERE product_id='".$product_id."'");
        $row = mysqli_fetch_assoc($product_query); 
        $query = mysqli_query($conn, "SELECT c.category_id,c.category_name,b.sub_category_id,b.sub_category_name,u.unit_id,u.unit_name FROM aalierp_category c, aalierp_sub_category b, aalierp_unit u WHERE c.category_id='".$row["category_id"]."' AND b.sub_category_id='".$row["sub_category_id"]."' AND u.unit_id='".$row["unit_id"]."'");    
        $rec = mysqli_fetch_assoc($query);
        ?>
            <div class="container py-lg-3 py-md-3">
                <div class="row ">
                    <div class="col-lg-1 col-md-6 item">
                        <?php $qquery = mysqli_query($conn, "SELECT * FROM aalierp_image WHERE product_id='".$product_id."'"); while($rr = mysqli_fetch_array($qquery)){ ?>
                            <img class="card-img-bottom d-block" src="<?php echo $AaliSYS; ?>/uploads/products/<?php echo $rr["product_image"]; ?>" alt="<?php echo $row["product_name"]; ?>" />
                        <?php } ?>
                    </div>
                    <div class="col-lg-4 col-md-6 item">
                        <div class="card shadow-lg">
                            <div class="card-body">
                                <p class="p-3">
                                    <img class="card-img-bottom d-block" src="<?php echo $AaliSYS; ?>/uploads/products/<?php echo $row["product_image"]; ?>" alt="<?php echo $row["product_name"]; ?>" />
                                </p>
                            </div>
                            <div class="card-header p-3 position-relative">
                                <a href="#" class="zoom d-block ">
                                    <!-- <img class="card-img-bottom d-block" src="<?php echo $AaliSYS; ?>/uploads/products/<?php echo $row["product_image"]; ?>" alt="<?php echo $row["product_name"]; ?>"> -->
                                    <video src="<?php echo $AaliSYS; ?>/uploads/products/<?php echo $row["product_video"]; ?>" autoplay loop controls class="w-100"></video>
                                </a>
                                <div class="post-pos">
                                    <a href="#"  class="receipe blue bg-dark text-light "><i class="fa fa-eye"></i> View Detail</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-7 col-md-6 item">
                        <h4 class="text-center py-3" style="background: #C44E00; color: white; border-radius: 20px 20px 0px 0px;">Product detail</h4>
                        <div id="dmsg"></div>
                        <div class="card">
                            <div class="card-body course-details">
                                <div class="row">
                                    <div class="col-md-8 mt-3">
                                        <h2 class="mb-2"><?php echo $row["product_name"]; ?></h2>
                                        <h3 class="mb-3">Price: <b class="text-primary">$<?php echo $row["product_price"]; ?></b></h3>
                                        <p class="">Category: <b><?php echo $rec["category_name"]; ?></b></p>
                                        <p class="">Sub Category: <b><?php echo $rec["sub_category_name"]; ?></b></p>
                                        <p class="">Search Keywords: <b><?php echo $row["product_keywords"]; ?></b></p>
                                    </div>
                                    <div class="col-md-4 py-3" style="text-align: right;">
                                        <p class="mb-2">Quantity: <b> 01 <?php echo $rec["unit_name"]; ?></b></p>
                                        <p class="mb-2">Total Price: <b>$<?php echo $row["product_price"]; ?></b></p>
                                        <hr>
                                        <a href="#" pid="<?php echo $row["product_id"]; ?>" id="add_detail_cart" class="btn btnn w-100 products-detail"> Add To Cart </a>
                                    </div>
                                </div>
                                <div class="course-meta mt-4">
                                    <div class="meta-item course-lesson">
                                        <span class="fa fa-clock-o"></span>
                                        <span class="meta-value"> Time: <b>4hrs</b> </span>
                                    </div>
                                    <div class="meta-item course-">
                                        <span class="fa fa-user-o"></span>
                                        <span class="meta-value"> Clients: 128 </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row ">
                    <div class="col-lg-12 col-md-6 item py-3">
                        <div class="card shadow-lg">
                            <div class="card-body course-details">
                                <h4>Product description:</h4>
                                <p class="px-5">
                                    <?php echo $row["product_desc"]; ?>
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row py-5">
                    <h4 class="col-lg-12 col-md-12 item float-left py-3">Related Other Products</h4>
                    <?php $c_query = mysqli_query($conn, "SELECT * FROM aalierp_product WHERE catalogue_id = '".$row["catalogue_id"]."' ORDER BY RAND() LIMIT 4");
                          while($ror = mysqli_fetch_array($c_query)){ ?>
                                <div class="col-lg-3 col-md-6 item float-left">
                                    <div class='mb-2' id="msg"></div>
                                    <div class="card shadow-lg">
                                        <div class="card-header p-0 position-relative">
                                            <a href="<?php echo $AaliLINK; ?>/product-detail?pid=<?php echo $row["product_id"]; ?>" class="zoom d-block ">
                                                <img class="card-img-bottom d-block" src="<?php echo $AaliSYS; ?>/uploads/products/<?php echo $ror["product_image"]; ?>" alt="<?php echo $ror["product_name"]; ?>">
                                            </a>
                                            <div class="post-pos">
                                                <a href="<?php echo $AaliLINK; ?>/product-detail?pid=<?php echo $row["product_id"]; ?>" class="receipe blue bg-dark text-light "><i class="fa fa-eye"></i> View Detail</a>
                                            </div>
                                        </div>
                                        <div class="card-body course-details" style="margin-top: -15px;">
                                            <div class="price-review d-flex justify-content-between mb-1align-items-center">
                                                <p style="color: #D84F00;">Price: $<?php echo $ror["product_price"]; ?></p>
                                                <ul class="rating-star">
                                                    <li><del>$<?php echo $ror["product_old_price"]; ?></del></li>
                                                </ul>
                                            </div>
                                            <a href="<?php echo $AaliLINK; ?>/product-detail?pid=<?php echo $row["product_id"]; ?>" class="course-desc text-center "><?php echo $ror["product_name"]; ?></a>
                                            <div class="course-meta mt-2 text-center">
                                                <div class="meta-item course-">
                                                    <span class="fa fa-user-o"></span>
                                                    <span class="meta-value"> : 128 </span>
                                                </div>
                                                <a href="#" pid="<?php echo $ror["product_id"]; ?>" id="add_to_cart" class="meta-item course- products">
                                                    <span class="fa fa-shopping-cart"></span>
                                                    <span class="meta-value"> Add To Cart</span>
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                    <?php } ?>
                </div>
            </div>
<?php   }
    
    
    
    


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

        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'>
                        <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                        <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','1')";
                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 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='$logo' style='width: 100px; height: auto;' alt='D101Sale 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 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://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($row["user_email"], $subjectt, $messag, $header);
                    
                    
                    
                    
                    echo "
                        <div class='alert alert-success alert-block'>
                            <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                            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'>
                        <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                        <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','1')";
            if (mysqli_query($conn,$sql)) {
                echo "
                    <div class='alert alert-success'>
                        <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                        <b>Product is Added Successfully..!</b>
                    </div>
                ";
                exit();
            }
            
        }
    }
   
//Add Product To Cart From Detail..
    if(isset($_POST["addDetailCart"])){
        $p_id = $_POST["proId"];

        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'>
                        <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                        <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','1')";
                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@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>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://d101sale.com'><img src='$logo' style='width: 100px; height: auto;' alt='D101Sale 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 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://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($row["user_email"], $subjectt, $messag, $header);
                    
                    
                    
                    
                    echo "
                        <div class='alert alert-success alert-block'>
                            <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                            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'>
                        <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                        <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','1')";
            if (mysqli_query($conn,$sql)) {
                echo "
                    <div class='alert alert-success alert-block'>
                        <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                        <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@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>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://d101sale.com'><img src='$logo' style='width: 100px; height: auto;' alt='D101Sale 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 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://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; 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>

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($row["user_email"], $subjectt, $messag, $header);
                    
                    
                    
                    
                    echo "
                        <div class='alert alert-success'>
                            <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                            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();
    }
    
    
    
    

//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="14%">
                            <?php if($product_type == "Tangible"){ ?>
                                <select class="form-control mail" >
                                    <?php $mail = mysqli_query($conn, "SELECT * FROM aalierp_shipment WHERE country_id='".$country."'");
                                        while($rew = mysqli_fetch_array($mail)){ ?>
                                        <option value="<?php echo $rew["ship_type"]; ?>"><?php echo $rew["ship_type"]; ?></option>
                                    <?php } ?>
                                </select>
                            <?php }else{echo "No shipment";} ?>
                        </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 update"><i class="fa fa-thumbs-up"></i></a>
                                <a href="#" remove_id="<?php echo $product_id; ?>" class="btn btn-danger 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"><b class="net_total" style="font-size: 1.3em; float: right;"></b></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"><b class="net_total" style="font-size: 1.3em; float: right;"></b></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 D101Sale Portal"; 
$fromm = "noreply@D101Sale.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://d101sale.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://d101sale.com'><img src='$logo' style='width: 100px; height: auto;' alt='D101Sale Logo' /></a><br><br>
            | <a href='https://d101sale.com/products' style='text-decoration: none; color: blue;'> Products</a> | 
            <a href='https://d101sale.com/library' style='text-decoration: none; color: blue;'> Post Material</a> | 
            <a href='https://d101sale.com/forum' style='text-decoration: none; color: blue;'> Forum</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 using D101Sale 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>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://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;'>
<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:info@D101Sale.org'>info@D101Sale.org</a> | <a href='tel:+14583425008'>+1 (458) 342 5008</a> | <br><br>
            </p>
        </div>
        <br><br><br>
        <a href='https://d101sale.com'><img src='$logo' style='width: 100px; height: auto;' alt='D101Sale logo' /></a><br><br>
        A108 Adam Street | Las Vegas, NY 535022 | United States<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/forum'>Forum</a> | <a href='https://d101sale.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'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><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'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><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();
    } 
    




    
    
    
    
    
    


?>