<?php
	include 'load.php';

if(isset($_POST['selectedIds'])) {
    $selectedIds = $_POST['selectedIds'];
 
        foreach($selectedIds as $id) {
            // Delete from mobileorders table
            $cartinfo = mysqli_query($conn, "SELECT * FROM mobiletemporarycart WHERE orderid='$id'");
            if ($cartinfo->num_rows > 0) {
                while($row = $cartinfo->fetch_assoc()) {
                    $pro_qty = $row['pro_qty'];
                    $proid = $row['proid'];

                    $sql = "UPDATE tbl_stock SET qty= qty + '$pro_qty' WHERE prid='$proid'";
                    $conn->query($sql);
                    
                }
            }

    
            $stmt_delete_mobileorders = $DB_con->prepare('DELETE FROM mobileorders WHERE order_id = :order_id');
            $stmt_delete_mobileorders->bindParam(':order_id', $id);
            $stmt_delete_mobileorders->execute();
            
            // // Delete from mobiletemporarycart table
            $stmt_delete_mobiletemporarycart = $DB_con->prepare('DELETE FROM mobiletemporarycart WHERE orderid = :order_id');
            $stmt_delete_mobiletemporarycart->bindParam(':order_id', $id);
            $stmt_delete_mobiletemporarycart->execute();
            
            // // Delete from invoice_other_details table
            $stmt_delete_invoice_other_details = $DB_con->prepare('DELETE FROM invoice_other_details WHERE order_id = :order_id');
            $stmt_delete_invoice_other_details->bindParam(':order_id', $id);
            $stmt_delete_invoice_other_details->execute();
        }

    echo 'Rows deleted successfully';
} else {
    echo 'Error: No rows selected for deletion';
}
?>

