/**
* Theme Name: Doclik Child
* Description: This is a child theme of Doclik, generated by Merlin WP.
* Author: <a href="https://themeforest.net/user/template_path/portfolio">template_path</a>
* Template: doclik
* Version: 1.0.1
*/

function show_doctors_for_hospital_department($atts) {
    /* Get current hospital and department slugs from URL
    // You may need to adjust this for your permalink structure*/
    global $post;
    $hospital_slug = '';
    $department_slug = '';

    /* Detect if we're on a hospital or department page*/
    if (get_post_type($post) == 'gd_hospital') {
        $hospital_slug = $post->post_name;
        /* Optionally, get department from query var or custom logic*/
        $department_slug = isset($_GET['department']) ? sanitize_title($_GET['department']) : '';
    } elseif (get_post_type($post) == 'gd_department') {
        $department_slug = $post->post_name;
        /* Optionally, get hospital from query var or custom logic*/
        $hospital_slug = isset($_GET['hospital']) ? sanitize_title($_GET['hospital']) : '';
    }

    /* Get hospital and department IDs by slug*/
    $hospital_id = $hospital_slug ? get_page_by_path($hospital_slug, OBJECT, 'gd_hospital')->ID : false;
    $department_id = $department_slug ? get_page_by_path($department_slug, OBJECT, 'gd_department')->ID : false;

    if (!$hospital_id || !$department_id) {
        return '<p>Please select both a hospital and department.</p>';
    }

    /*- Query doctors linked to both hospital and department*/
    $args = [
        'post_type' => 'gd_doctor',
        'posts_per_page' => -1,
        'meta_query' => [
            [
                'key' => 'doctor_hospital', /* ACF field for hospital link*/
                'value' => '"' . $hospital_id . '"',
                'compare' => 'LIKE'
            ],
            [
                'key' => 'doctor_department', /* ACF field for department link*/
                'value' => '"' . $department_id . '"',
                'compare' => 'LIKE'
            ],
        ]
    ];

    $doctors = new WP_Query($args);

    ob_start();
    if ($doctors->have_posts()) {
        echo '<ul class="doctors-list">';
        while ($doctors->have_posts()) {
            $doctors->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        }
        echo '</ul>';
    } else {
        echo '<p>No doctors found for this hospital and department.</p>';
    }
    wp_reset_postdata();

    return ob_get_clean();
}
add_shortcode('context_aware_doctors', 'show_doctors_for_hospital_department');

function doctor_fluent_booking_calendar() {
    global $post;
    $event_id = get_field('bookingeventid', $post->ID); // Replace with your ACF field name
    if ($event_id) {
        return do_shortcode('[fluent_booking id="' . esc_attr($event_id) . '"]');
    }
    return '';
}
add_shortcode('doctor_fluent_booking', 'doctor_fluent_booking_calendar');



