<?php

// Hide wc-ads-incomplete from old Orders list + order by date
add_action( 'pre_get_posts', function( $query ) {

    if ( is_admin() && $query->is_main_query() && 'shop_order' === $query->get('post_type')  && is_woocommerce_orders_page() ) {
        // Exclude the custom status
        $query->set( 'post_status', array_diff( array_keys( wc_get_order_statuses() ), [ 'wc-ads-incomplete' ] ) );

        // Force order by date (latest first)
        $query->set( 'orderby', 'date' );
        $query->set( 'order', 'DESC' );
    }
});





function is_woocommerce_orders_page() {
    // Check for HPOS orders page
    if ( isset( $_GET['page'] ) && $_GET['page'] === 'wc-orders' ) {
        return true;
    }

    // Check for legacy orders page
    global $pagenow;
    if ( $pagenow === 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] === 'shop_order' ) {
        return true;
    }


    // Additional check for WooCommerce admin pages
    if ( function_exists( 'get_current_screen' ) ) {
        $screen = get_current_screen();
        if ( $screen && in_array( $screen->id, [ 'woocommerce_page_wc-orders', 'edit-shop_order' ] ) ) {
            return true;
        }
    }

    return false;
}


function ads_get_current_admin_url() {
    $page_url = menu_page_url('incomplete-order-dashboard', false);
    $query_args = [];
    $allowed_params = ['s', 'date_filter', 'items_per_page'];
    foreach ($allowed_params as $param) {
        if (isset($_GET[$param]) && !empty($_GET[$param])) {
            $query_args[$param] = ($param === 'items_per_page') ? intval($_GET[$param]) : sanitize_text_field(wp_unslash($_GET[$param]));
        }
    }
    if (!empty($query_args)) {
        $page_url = add_query_arg($query_args, $page_url);
    }
    return $page_url;
}

add_action('admin_head', function () {
    $screen = get_current_screen();
    if ( $screen && strpos($screen->id, 'orderflow') !== false ) {
        ?>
        <style>
            .notice, div.error, div.updated {
                display: none !important;
            }
        </style>
        <?php
    }
});

// Directly print the full meta tag from option
add_action('wp_head', function () {
    $meta_tag = get_option('ads_fb_domain_verification');

    if (!empty($meta_tag)) {
        echo $meta_tag . "\n";
    }
});

function add_incomplete_order_count() {
    global $wpdb;
    global $submenu;

    // নিশ্চিত করুন যে parent menu 'orderflow' আছে
    if (isset($submenu['orderflow'])) {
        $table_name = $wpdb->prefix . 'ads_incomplete_orders_tracker';

        // ডাটাবেস থেকে ইনকমপ্লিট অর্ডারের সংখ্যা গণনা করা
        $count = $wpdb->get_var("SELECT COUNT(id) FROM {$table_name} WHERE status_to = 'ads-incomplete'");

        // যদি কোনো ইনকমপ্লিট অর্ডার থাকে, তাহলে কাউন্ট ব্যাজ তৈরি করা
        if ($count > 0) {
            $badge_html = ' <span class="awaiting-mod update-plugins count-' . esc_attr($count) . '"><span class="processing-count">' . number_format_i18n($count) . '</span></span>';

            // সাবমেনু আইটেমটি খুঁজে বের করে টাইটেলে ব্যাজ যুক্ত করা
            foreach ($submenu['orderflow'] as $key => $menu_item) {
                if ($menu_item[2] === 'orderflow-incomplete-orders') {
                    $submenu['orderflow'][$key][0] .= $badge_html;
                    break;
                }
            }
        }
    }
}

// admin_menu action-এ এই ফাংশনটি যুক্ত করা
add_action('admin_menu', 'add_incomplete_order_count');




add_filter('manage_edit-shop_order_columns', function($columns) {
    return rahim_safe_remove_total_column($columns);
}, 999);

add_filter('manage_woocommerce_page_wc-orders_columns', function($columns) {
    return rahim_safe_remove_total_column($columns);
}, 999);

function rahim_safe_remove_total_column($columns) {
    $new_columns = $columns;
    if (isset($new_columns['order_total'])) {
        unset($new_columns['order_total']);
    }
    return $new_columns; // অন্য কলামগুলো অক্ষত থাকবে
}


add_filter( 'woocommerce_available_payment_gateways', 'custom_move_cod_to_top_of_list', 999 );

function custom_move_cod_to_top_of_list( $available_gateways ) {

    if ( ! is_checkout() || is_admin() || ! is_array( $available_gateways ) ) {
        return $available_gateways;
    }

    if ( isset( $available_gateways['cod'] ) ) {
        $cod_gateway = $available_gateways['cod'];

        unset( $available_gateways['cod'] );

        $new_gateways_order = array( 'cod' => $cod_gateway ) + $available_gateways;
        return $new_gateways_order;
    }

    return $available_gateways;
}