<?php
session_start();
require_once("class/connectdb.php");
$conn = new DbConnect;
$con = $conn->connect();

//Delete Blog post
if(isset($_POST['query_trash'])){
	$blog_id = $_POST['blog_id'];
	$sql = mysqli_query($con, "DELETE FROM blog WHERE blog_id = $blog_id ");
	
}

//Delete a product
if(isset($_POST['delete_product'])){
	$product_id = $_POST['product_id'];
	$sql = mysqli_query($con, "DELETE FROM product WHERE product_id = '$product_id' ");
	
}

//Delete an order
if(isset($_POST['query_order'])){
	$order_id = $_POST['order_id'];
	$prd_name = $_POST['prd_name'];
	$sql = mysqli_query($con, "DELETE FROM shipping_info WHERE order_id = '$order_id' AND item_list = '$prd_name' ");
	
}

//Update an order
if(isset($_POST['query_update_order'])){
	$qty = $_POST['quantity'];
	$address = $_POST['address'];
	$order_id = $_POST['order_id'];
	$prd_name = $_POST['prd_name'];
	$sql = mysqli_query($con, "UPDATE shipping_info SET quantity = '$qty', address = '$address' WHERE order_id = '$order_id' AND item_list = '$prd_name' ");
	
}

//Update an stock
if(isset($_POST['query_stock'])){
	$product_id = $_POST['product_id'];
	$minStockLevel = $_POST['minStockLevel'];
	$maxStockLevel = $_POST['maxStockLevel'];
	$qty_to_add = $_POST['qtyToAdd'];
	
	$query = mysqli_query($con, "SELECT * FROM product WHERE product_id = '$product_id' ");
	$row = mysqli_fetch_array($query);
	$current_stock = $row['current_stock_level'];
	$new_stock = $current_stock + $qty_to_add;
	
	$sql = mysqli_query($con, "UPDATE product SET min_stock_level = '$minStockLevel', max_stock_level = '$maxStockLevel' , current_stock_level = '$new_stock' 
	WHERE product_id = '$product_id'");
	
	return $sql;
	
}

//Update an stock
if(isset($_POST['query_price'])){
	$product_id = $_POST['product_id'];
	$costPrice = $_POST['costPrice'];
	$sellingPrice = $_POST['sellingPrice'];
	
	$sql = mysqli_query($con, "UPDATE product SET cost = '$costPrice', price = '$sellingPrice' WHERE product_id = '$product_id'");
	
	return $sql;
	
}

//Update Delivery status
if(isset($_POST['delivery_status'])){
	$cart_id = $_POST['cart_id'];
	$status = $_POST['status'];
	
	$sql = mysqli_query($con, "UPDATE cart SET delivery_status = '$status' WHERE cart_id = $cart_id ");
	echo $sql;
}

if(isset($_POST["category_id"]) && !empty($_POST["category_id"])){
	
	$category_id = $_POST['category_id'];
	
    //Get all state data
    $query = mysqli_query($con, "SELECT * FROM sub_category WHERE category_id = '$category_id' AND display_status = 1 ");
    
    //Count total number of rows
    $rowCount = $query->num_rows;
    
    //Display states list
    if($rowCount > 0){
     echo '<option value="">Select Subcategory</option>';
       
        while($row = $query->fetch_assoc()){ 
            echo '<option value="'.$row['sub_category_id'].'">'.$row['sub_category'].'</option>';
        }
    }else{
        echo '<option value="">Subcategory not available</option>';
    }
}

?>