<?php


add_action('admin_menu', array('mt_Admin_Page', 'mt_admin_menu'));

class mt_Admin_Page
{

    public static function mt_admin_menu()
    {
        $main = add_menu_page(
            'mt',                                         // page title
            'Doctor',                                         // menu title
            'manage_options',                               // capability
            'mt_admin_page',                              // menu slug
            array(__CLASS__, 'mt_admin_main_page'),
            '',
            26                                              // callback function
        );

        $sub = add_submenu_page(
            'mt_admin_page',                         // parent slug
            'Manage Bookings',                         // page title
            'Dashboard',                               // menu title
            'manage_options',                          // capability
            'mt_admin_page',                         // menu slug
            array(__CLASS__, 'mt_admin_main_page')         // callback function, same as above
        );


        $mt_post_type = 'mt_service';
        $mt_post_title_type = 'doctorsprofile';
        /* Get CPT Object */
        $mt_post_type_obj = get_post_type_object($mt_post_type);
        $mt_post_type_objs = get_post_type_object($mt_post_title_type);
        $mt_services_menu_page = add_submenu_page(
            'mt_admin_page',                             // parent slug
            $mt_post_type_obj->labels->name,             // page title
            'Book Services',                                // menu title
            $mt_post_type_obj->cap->edit_posts,          // capability
            'edit.php?post_type=' . $mt_post_type        // menu slug
        );

        $mt_add_new_service_menu_page = add_submenu_page(
            'mt_admin_page',                             // parent slug
            $mt_post_type_obj->labels->name,             // page title
            'Add Book Services',                                 // menu title
            $mt_post_type_obj->cap->edit_posts,          // capability
            'post-new.php?post_type=' . $mt_post_type    // menu slug
        );
        $mt_add_new_service_menu_page = add_submenu_page(
            'mt_admin_page',                             // parent slug
            $mt_post_type_objs->labels->name,             // page title
            'Add Doctor Profile',                                 // menu title
            $mt_post_type_objs->cap->edit_posts,          // capability
            'post-new.php?post_type=' . $mt_post_title_type    // menu slug
        );
        $mt_add_new_service_menu_page = add_submenu_page(
            'mt_admin_page',                             // parent slug
            $mt_post_type_objs->labels->name,             // page title
            'All Doctor',                                 // menu title
            $mt_post_type_objs->cap->edit_posts,          // capability
            'edit.php?post_type=' . $mt_post_title_type    // menu slug
        );
        $mt_bookings = add_submenu_page(
            'mt_admin_page',                         // parent slug
            'mt bookings',                           // page title
            'All Bookings',                            // menu title
            'manage_options',                          // capability
            'mt_admin_bookings_page',                // menu slug
            array(__CLASS__, 'mt_admin_bookings_page_func')         // callback function, same as above
        );

        $mt_settings = add_submenu_page(
            'mt_admin_page',                         // parent slug
            'mt bookings',                           // page title
            'Settings',                                // menu title
            'manage_options',                          // capability
            'mt_admin_settings_page',                // menu slug
            array(__CLASS__, 'mt_admin_settings_page_func')         // callback function, same as above
        );

        foreach (array($main, $sub, $mt_bookings, $mt_settings, $mt_services_menu_page) as $slug) {
            // make sure the style callback is used on our page only
            add_action(
                "admin_print_styles-$slug",
                array(__CLASS__, 'enqueue_style')
            );
            // make sure the script callback is used on our page only
            add_action(
                "admin_print_scripts-$slug",
                array(__CLASS__, 'enqueue_script')
            );


        }

        add_action('admin_enqueue_scripts', array(__CLASS__, 'mt_edit_services'));
        add_action('admin_notices', array(__CLASS__, 'mt_services_header_html'));

        // Add custom column to service post type edit list
        add_filter('manage_mt_service_posts_columns', array(__CLASS__, 'mt_columns_head_only_mt_services'), 10);
        add_action('manage_mt_service_posts_custom_column', array(__CLASS__, 'mt_columns_content_only_mt_services'), 10, 2);


    }


    // CREATE TWO FUNCTIONS TO HANDLE THE COLUMN
    public static function mt_columns_head_only_mt_services($mt_featured_image_column)
    {
        unset($mt_featured_image_column['taxonomy-mt_service_type']);
        unset($mt_featured_image_column['title']);
        unset($mt_featured_image_column['date']);

        $mt_featured_image_column['esmb_service_featured_image'] = 'Photo';
        $mt_featured_image_column['title'] = 'Name';
        $mt_featured_image_column['taxonomy-mt_service_type'] = 'Service Type';
        $mt_featured_image_column['mt_availability'] = 'Available Till';
        $mt_featured_image_column['date'] = 'Date';
        return $mt_featured_image_column;
    }

    public static function mt_columns_content_only_mt_services($column_name, $post_ID)
    {

        $today = date("Y-m-d");
        $today_time = strtotime($today);

        add_image_size('mt_service-admin-post-featured-image', 60, 60, false);
        switch ($column_name) {
            case 'esmb_service_featured_image':
                if (function_exists('the_post_thumbnail')) {
                    echo the_post_thumbnail('mt_service-admin-post-featured-image');
                }
                break;

            case 'mt_availability':
                $mt_service_ending_date = get_post_meta($post_ID, 'mt_service_availability_ends_at', TRUE);
                $expire_time = strtotime($mt_service_ending_date);
                if ($expire_time > $today_time) {
                    echo $mt_service_ending_date;
                } else { ?>
                    <div class='mt-service-not-available'> <?php _e('Availability ended. Update availability to get booking orders.', 'mt-service-booking'); ?> </div>
                    <?php
                }

                break;
        }

    }

    public static function mt_services_header_html()
    {
        global $pagenow, $post;
        global $post_type;
        $mt_plugin_path = plugin_dir_url(__FILE__);
        $mt_icon_url = $mt_plugin_path . 'assets/img/service-booking.png';

        if ($post_type == 'mt_service' && ($pagenow == 'edit.php' || $pagenow == 'post.php')) {
            ?>
            <div class="mt-container">
                <header class="mt-admin-main-page-header-wrapper">
                    <div class="jumbotron text-center">
                        <div class="mt-admin-plugin-title">
                            <img src="<?php echo $mt_icon_url; ?>" alt="Service Booking Icon">
                            <h2 class="display-5"> <?php _e('Doctor Appointment', 'mt-service-booking'); ?></h2>
                        </div>
                    </div>
                </header>

                <main class="mt-admin-main-page-wrapper">
                    <div class="tabs">
                        <ul>
                            <li>
                                <a href="admin.php?page=mt_admin_page"> <?php _e('Dashboard ', 'mt-service-booking'); ?>  </a>
                            </li>
                            <li><a href="edit.php?post_type=mt_service"
                                   class="active"> <?php _e('Book Services ', 'mt-service-booking'); ?></a></li>
                            <li>
                                <a href="post-new.php?post_type=mt_service"> <?php _e('Add Book Services ', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="post-new.php?post_type=doctorsprofile"><?php _e('Add Doctor Profile', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="edit.php?post_type=doctorsprofile"><?php _e('All Doctor', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="admin.php?page=mt_admin_bookings_page"> <?php _e('All Bookings ', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="admin.php?page=mt_admin_settings_page"><?php _e('Settings  ', 'mt-service-booking'); ?></a>
                            </li>
                        </ul>
                    </div>

                </main>

            </div>
            <?php
        }

        if ($post_type == 'mt_service' && $pagenow == 'post-new.php') {
            ?>
            <div class="mt-container">
                <header class="mt-admin-main-page-header-wrapper">
                    <div class="jumbotron text-center">
                        <div class="mt-admin-plugin-title">
                            <img src="<?php echo $mt_icon_url; ?>" alt="Service Booking Icon">
                            <h2 class="display-5"> <?php _e('Doctor Appointment ', 'mt-service-booking'); ?></h2>
                        </div>
                    </div>
                </header>

                <main class="mt-admin-main-page-wrapper">
                    <div class="tabs">
                        <ul>
                            <li>
                                <a href="admin.php?page=mt_admin_page"> <?php _e('Dashboard ', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="edit.php?post_type=mt_service"> <?php _e('Book Services ', 'mt-service-booking'); ?></a>
                            </li>
                            <li><a href="post-new.php?post_type=mt_service"
                                   class="active"> <?php _e('Add Book Services ', 'mt-service-booking'); ?></a></li>
                            <li>
                                <a href="post-new.php?post_type=doctorsprofile"><?php _e('Add Doctor Profile', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="edit.php?post_type=doctorsprofile"><?php _e('All Doctor', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="admin.php?page=mt_admin_bookings_page"> <?php _e('All Bookings ', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="admin.php?page=mt_admin_settings_page"><?php _e('Settings  ', 'mt-service-booking'); ?></a>
                            </li>
                        </ul>
                    </div>

                </main>

            </div>
            <?php
        }

        if ($post_type == 'doctorsprofile' && ($pagenow == 'edit.php' || $pagenow == 'post.php')) {
            ?>
            <div class="mt-container">
                <header class="mt-admin-main-page-header-wrapper">
                    <div class="jumbotron text-center">
                        <div class="mt-admin-plugin-title">
                            <img src="<?php echo $mt_icon_url; ?>" alt="Service Booking Icon">
                            <h2 class="display-5"> <?php _e('Doctor Appointment ', 'mt-service-booking'); ?></h2>
                        </div>
                    </div>
                </header>

                <main class="mt-admin-main-page-wrapper">
                    <div class="tabs">
                        <ul>
                            <li>
                                <a href="admin.php?page=mt_admin_page"> <?php _e('Dashboard ', 'mt-service-booking'); ?>  </a>
                            </li>
                            <li><a href="edit.php?post_type=mt_service"> <?php _e('Book Services ', 'mt-service-booking'); ?></a></li>
                            <li>
                                <a href="post-new.php?post_type=mt_service"> <?php _e('Add Book Services ', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="post-new.php?post_type=doctorsprofile" ><?php _e('Add Doctor Profile', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="edit.php?post_type=doctorsprofile" class="active"><?php _e('All Doctor', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="admin.php?page=mt_admin_bookings_page"> <?php _e('All Bookings ', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="admin.php?page=mt_admin_settings_page"><?php _e('Settings  ', 'mt-service-booking'); ?></a>
                            </li>
                        </ul>
                    </div>

                </main>

            </div>
            <?php
        }

        if ($post_type == 'doctorsprofile' && $pagenow == 'post-new.php') {
            ?>
            <div class="mt-container">
                <header class="mt-admin-main-page-header-wrapper">
                    <div class="jumbotron text-center">
                        <div class="mt-admin-plugin-title">
                            <img src="<?php echo $mt_icon_url; ?>" alt="Service Booking Icon">
                            <h2 class="display-5"> <?php _e('Doctor Appointment ', 'mt-service-booking'); ?></h2>
                        </div>
                    </div>
                </header>

                <main class="mt-admin-main-page-wrapper">
                    <div class="tabs">
                        <ul>
                            <li>
                                <a href="admin.php?page=mt_admin_page"> <?php _e('Dashboard ', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="edit.php?post_type=mt_service"> <?php _e('Book Services ', 'mt-service-booking'); ?></a>
                            </li>
                            <li><a href="post-new.php?post_type=mt_service"> <?php _e('Add Book Services ', 'mt-service-booking'); ?></a></li>
                            <li>
                                <a href="post-new.php?post_type=doctorsprofile" class="active"><?php _e(' Add Doctor Profile', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="edit.php?post_type=doctorsprofile" ><?php _e('All Doctor', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="admin.php?page=mt_admin_bookings_page"> <?php _e('All Bookings ', 'mt-service-booking'); ?></a>
                            </li>
                            <li>
                                <a href="admin.php?page=mt_admin_settings_page"><?php _e('Settings  ', 'mt-service-booking'); ?></a>
                            </li>
                        </ul>
                    </div>

                </main>

            </div>
            <?php
        }
    }

    public static function mt_edit_services($hook)
    {

        global $post;
        global $post_type;

        wp_enqueue_style('mt-admin-css', plugin_dir_url(__FILE__) . 'assets/private/css/mt-admin.css', array(), '1.1', false);

        wp_enqueue_script('jquery');

        wp_register_script('mt-admin-scripts', plugins_url('assets/private/js/mt-admin-scripts.js', __FILE__), array(), FALSE, TRUE);
        wp_enqueue_script('mt-admin-scripts');
        wp_localize_script('mt-admin-scripts', 'backend_ajax_object',
            array(
                'ajaxurl' => admin_url('admin-ajax.php'),
                'pluginsUrl' => plugins_url()

            )
        );

        if ($hook == 'post-new.php' || $hook == 'post.php' || $hook == 'edit.php') {
            if ('mt_service' === $post_type) {
                wp_enqueue_style('bootstrap-css', plugin_dir_url(__FILE__) . 'assets/css/bootstrap-v4.3.1.min.css', array(), '1.1', false);
                wp_enqueue_style('style-css', plugin_dir_url(__FILE__) . 'assets/private/css/mt-admin-only.css', array(), '1.1', false);

                wp_enqueue_script('popper-js', plugin_dir_url(__FILE__) . 'assets/js/popper.min.js', array(), '1.1', true);
                wp_enqueue_script('bootstrap-js', plugin_dir_url(__FILE__) . 'assets/js/bootstrap-v4.3.1.min.js', array(), '1.1', true);


            }
        }
        if ($hook == 'post-new.php' || $hook == 'post.php' || $hook == 'edit.php') {
            if ('doctorsprofile' === $post_type) {
                wp_enqueue_style('bootstrap-css', plugin_dir_url(__FILE__) . 'assets/css/bootstrap-v4.3.1.min.css', array(), '1.1', false);
                wp_enqueue_style('style-css', plugin_dir_url(__FILE__) . 'assets/private/css/mt-admin-only.css', array(), '1.1', false);

                wp_enqueue_script('popper-js', plugin_dir_url(__FILE__) . 'assets/js/popper.min.js', array(), '1.1', true);
                wp_enqueue_script('bootstrap-js', plugin_dir_url(__FILE__) . 'assets/js/bootstrap-v4.3.1.min.js', array(), '1.1', true);


            }
        }

    }

    /**
     * Load stylesheet on our admin page only.
     *
     * @return void
     */
    public static function enqueue_style()
    {
        wp_register_style('mt_bootstrap_css', plugins_url('assets/css/bootstrap-v4.3.1.min.css', __FILE__));
        wp_enqueue_style('mt_bootstrap_css');

        wp_enqueue_style('style-css', plugin_dir_url(__FILE__) . 'assets/private/css/mt-admin-only.css', array(), '1.1', false);

    }

    /**
     * Load JavaScript on our admin page only.
     *
     * @return void
     */
    public static function enqueue_script()
    {
        wp_enqueue_script('jquery');


        wp_register_script('popper', plugins_url('assets/js/popper.min.js', __FILE__), array(), FALSE, TRUE);
        wp_enqueue_script('popper');

        wp_register_script('bootstrap', plugins_url('assets/js/bootstrap-v4.3.1.min.js', __FILE__), array(), FALSE, TRUE);
        wp_enqueue_script('bootstrap');

        wp_register_script('mt-bookings', plugins_url('assets/private/js/mt-bookings-table-scripts.js', __FILE__), array(), FALSE, TRUE);
        wp_enqueue_script('mt-bookings');

        wp_register_script('mt-admin-scripts', plugins_url('assets/private/js/mt-admin-scripts.js', __FILE__), array(), FALSE, TRUE);
        wp_enqueue_script('mt-admin-scripts');

        wp_localize_script('mt-admin-scripts', 'backend_ajax_object',
            array(
                'ajaxurl' => admin_url('admin-ajax.php'),
                'pluginsUrl' => plugins_url()
            )
        );


    }


    public static function mt_admin_main_page()
    {
        global $wpdb;
        $mt_settings_data = $wpdb->prefix . 'mt_settings';

        $mt_plugin_path = plugin_dir_url(__FILE__);
        $mt_icon_url = $mt_plugin_path . 'assets/img/service-booking.png';

        if (isset($_POST['mt_save_admin_email_data'])) {
            $admin_mail_subject = stripslashes_deep($_POST['mt_admin_email_subject']);
            $admin_mail_body = stripslashes_deep($_POST['mt_admin_email_body']);
            $mt_customer_pending_email_subject = stripslashes_deep($_POST['mt_customer_pending_email_subject']);
            $mt_customer_pending_email_body = stripslashes_deep($_POST['mt_customer_pending_email_body']);
            $mt_customer_confirmed_email_subject = stripslashes_deep($_POST['mt_customer_confirmed_email_subject']);
            $mt_customer_confirmed_email_body = stripslashes_deep($_POST['mt_customer_confirmed_email_body']);
            $mt_customer_cancelled_email_subject = stripslashes_deep($_POST['mt_customer_cancelled_email_subject']);
            $mt_customer_cancelled_email_body = stripslashes_deep($_POST['mt_customer_cancelled_email_body']);

            $customer_cookie_duration = stripslashes_deep($_POST['mt_customer_cookie_duration']);
            // Securly insert data with $wpdb->inert method preventing the sql injection and also escaping strings
            $wpdb->insert($mt_settings_data, array(
                'admin_mail_subject' => $admin_mail_subject,
                'admin_mail_body' => $admin_mail_body,
                'customer_mail_pending_subject' => $mt_customer_pending_email_subject,
                'customer_mail_pending_body' => $mt_customer_pending_email_body,
                'customer_mail_confirmed_subject' => $mt_customer_confirmed_email_subject,
                'customer_mail_confirmed_body' => $mt_customer_confirmed_email_body,
                'customer_mail_cancel_subject' => $mt_customer_cancelled_email_subject,
                'customer_mail_cancel_body' => $mt_customer_cancelled_email_subject,
                'customer_cookie_duration' => $customer_cookie_duration
            ));

        };

        // When the page loads fetch data from database
        $mt_settings_data_fetch = $wpdb->get_row("SELECT * FROM $mt_settings_data ORDER BY id DESC LIMIT 1");

        // When settings data is changed fetch new data from database
        $mt_check_changes = isset($_POST['mt_save_admin_email_data']);

        if ($mt_check_changes) {
            $mt_settings_data_fetch = $wpdb->get_row("SELECT * FROM $mt_settings_data ORDER BY id DESC LIMIT 1");
        }

        $fetch_admin_mail_subject = $mt_settings_data_fetch->admin_mail_subject;
        $fetch_admin_mail_body = $mt_settings_data_fetch->admin_mail_body;
        $fetch_mt_customer_pending_email_subject = $mt_settings_data_fetch->customer_mail_pending_subject;
        $fetch_mt_customer_pending_email_body = $mt_settings_data_fetch->customer_mail_pending_body;
        $fetch_mt_customer_confirmed_email_subject = $mt_settings_data_fetch->customer_mail_confirmed_subject;
        $fetch_mt_customer_confirmed_email_body = $mt_settings_data_fetch->customer_mail_confirmed_body;
        $fetch_mt_customer_cancelled_email_subject = $mt_settings_data_fetch->customer_mail_cancel_subject;
        $fetch_mt_customer_cancelled_email_body = $mt_settings_data_fetch->customer_mail_cancel_body;
        $fetch_customer_cookie_duration = $mt_settings_data_fetch->customer_cookie_duration;


        ?>
        <div class="mt-container">
            <header class="mt-admin-main-page-header-wrapper">
                <div class="jumbotron text-center">
                    <div class="mt-admin-plugin-title">
                        <img src="<?php echo $mt_icon_url; ?>" alt="Service Booking Icon">
                        <h2 class="display-5"> <?php _e('Doctor Appointment ', 'mt-service-booking'); ?></h2>
                    </div>
                </div>
            </header>
            <main class="mt-admin-main-page-wrapper">
                <div class="tabs">
                    <ul>
                        <li><a href="admin.php?page=mt_admin_page"
                               class="active"> <?php _e('Dashboard ', 'mt-service-booking'); ?></a></li>
                        <li>
                            <a href="edit.php?post_type=mt_service"><?php _e('Book Services  ', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="post-new.php?post_type=mt_service"><?php _e('Add Book Services  ', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="post-new.php?post_type=doctorsprofile"><?php _e('Add Doctor Profile  ', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="edit.php?post_type=doctorsprofile"><?php _e('Doctor All  ', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="admin.php?page=mt_admin_bookings_page"><?php _e('All Bookings  ', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="admin.php?page=mt_admin_settings_page"><?php _e('Settings  ', 'mt-service-booking'); ?></a>
                        </li>
                    </ul>
                </div>

                <div class="mt-table-wrapper container text-center">
                    <div class="mt-container">
                        <div class="header_wrap">
                            <div class="mt-approval-nonce-wrapper">
                                <input type="hidden" name="mt_booking_approval_nonce" id="mt_booking_approval_nonce"
                                       value="<?php echo wp_create_nonce("mt_booking_approval_nonce"); ?>">
                            </div>
                        </div>
                        <form class="mt-pending-bookings-container">
                            <table class="table table-striped table-class" id="table-id">
                                <thead>
                                <tr>
                                    <th><?php _e('Select ', 'mt-service-booking'); ?></th>
                                    <th><?php _e('ID', 'mt-service-booking'); ?></th>
                                    <th><?php _e('Service', 'mt-service-booking'); ?></th>
                                    <th><?php _e('Customer', 'mt-service-booking'); ?></th>
                                    <th><?php _e('Phone', 'mt-service-booking'); ?></th>
                                    <th><?php _e('Email', 'mt-service-booking'); ?></th>
                                    <th><?php _e('Date ', 'mt-service-booking'); ?></th>
                                    <th><?php _e('Time Slot', 'mt-service-booking'); ?></th>
                                    <th><?php _e('Status', 'mt-service-booking'); ?></th>
                                </tr>
                                </thead>
                                <tbody id="mtPendingBookings">

                                </tbody>
                            </table>
                            <div class="mt-admin-loading-gif">
                                <img src="<?php echo plugin_dir_url(__FILE__) . 'assets/img/loading.gif'; ?>">
                            </div>
                            <footer class="blockquote-footer mt-pending-table-footer">  <?php _e('10 Pending Booking Orders only for upcoming slots from the current time( Policy: Come first servied first )', 'mt-service-booking'); ?> </footer>
                        </form>

                    </div> <!-- End of Container -->

                    <!-- Doc button  -->
                    <div class="my-4 mt-doc-button">
                        <button type="button" class="btn btn-primary ">
                            <a class="text-light" target="_blank"
                               href="https://itindeed.com/mt-service-booking-wordpress-plugin/"> <?php _e('Documentation', 'mt-service-booking'); ?> </a>
                        </button>
                    </div>

                </div>

            </main>
        </div>


        <?php

    }


    public static function mt_admin_bookings_page_func()
    {
        global $title;
        global $wpdb;
        $mt_bookings = $wpdb->prefix . 'mt_bookings';
        $mt_all_bookings_from_database = "SELECT * FROM $mt_bookings WHERE approve_booking = '1' ORDER BY id DESC";
        $mt_order_list = $wpdb->get_results($mt_all_bookings_from_database, ARRAY_A);

        $mt_plugin_path = plugin_dir_url(__FILE__);
        $mt_icon_url = $mt_plugin_path . 'assets/img/service-booking.png';
        ?>
        <div class="mt-container">
            <header class="mt-admin-main-page-header-wrapper">
                <div class="jumbotron text-center">
                    <div class="mt-admin-plugin-title">
                        <img src="<?php echo $mt_icon_url; ?>" alt="Service Booking Icon">
                        <h2 class="display-5"> <?php _e('Doctor Appointment ', 'mt-service-booking'); ?></h2>
                    </div>
                </div>
            </header>

            <main class="mt-admin-main-page-wrapper">
                <div class="tabs">
                    <ul>
                        <li><a href="admin.php?page=mt_admin_page"> <?php _e('Dashboard', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="edit.php?post_type=mt_service"> <?php _e('Book Services', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="post-new.php?post_type=mt_service"><?php _e('Add Book Services  ', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="post-new.php?post_type=doctorsprofile"><?php _e('Add Doctor Profile  ', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="edit.php?post_type=doctorsprofile"><?php _e('Doctor All  ', 'mt-service-booking'); ?></a>
                        </li>
                        <li><a href="admin.php?page=mt_admin_bookings_page"
                               class="active"><?php _e('All Bookings ', 'mt-service-booking'); ?></a></li>
                        <li>
                            <a href="admin.php?page=mt_admin_settings_page"><?php _e('Settings  ', 'mt-service-booking'); ?></a>
                        </li>
                    </ul>
                </div>
                <div class="mt-table-wrapper container text-center">
                    <div class="mt-container">
                        <div class="header_wrap">
                            <div class="num_rows">
                                <div class="form-group">
                                    <!--		Show Numbers Of Rows 		-->
                                    <select class="form-control" name="state" id="maxRows">
                                        <option value="15"><?php _e('15', 'mt-service-booking'); ?></option>
                                        <option value="20"><?php _e('20', 'mt-service-booking'); ?></option>
                                        <option value="50"><?php _e('50', 'mt-service-booking'); ?></option>
                                        <option value="70"><?php _e('70', 'mt-service-booking'); ?></option>
                                        <option value="100"><?php _e('100', 'mt-service-booking'); ?></option>
                                        <option value="5000000"><?php _e('Show ALL Rows', 'mt-service-booking'); ?></option>
                                    </select>
                                </div>
                            </div>
                            <div class="tb_search">
                                <input type="text" id="search_input_all" onkeyup="FilterkeyWord_all_table()"
                                       placeholder="Search.." class="form-control">
                            </div>
                            <div class="mt-approval-nonce-wrapper">
                                <input type="hidden" name="mt_booking_approval_nonce" id="mt_booking_approval_nonce"
                                       value="<?php echo wp_create_nonce("mt_booking_approval_nonce"); ?>">
                            </div>
                        </div>
                        <form>
                            <table class="table table-striped table-class" id="table-id">
                                <thead>
                                <tr>
                                    <th><?php _e('No. ', 'mt-service-booking'); ?></th>
                                    <th><?php _e('Booking ID ', 'mt-service-booking'); ?></th>
                                    <th><?php _e('Service Name ', 'mt-service-booking'); ?></th>
                                    <th><?php _e('Customer Name ', 'mt-service-booking'); ?></th>
                                    <th> <?php _e('Phone', 'mt-service-booking'); ?></th>
                                    <th> <?php _e('Email', 'mt-service-booking'); ?></th>
                                    <th><?php _e('Booked Date ', 'mt-service-booking'); ?></th>
                                    <th> <?php _e('Booked Time Slot', 'mt-service-booking'); ?></th>
                                    <th> <?php _e('Status', 'mt-service-booking'); ?></th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php
                                $index = 0;
                                foreach ($mt_order_list as $mt_order_list) :
                                    $index = $index + 1;
                                    ?>
                                    <tr>
                                        <td><?php echo $index; ?></td>
                                        <td><?php echo $mt_order_list['id']; ?></td>
                                        <td><?php echo $mt_order_list['service_name']; ?></td>
                                        <td><?php echo $mt_order_list['customer_name']; ?></td>
                                        <td><?php echo $mt_order_list['customer_phone']; ?></td>
                                        <td><?php echo $mt_order_list['customer_email']; ?></td>
                                        <td><?php echo $mt_order_list['booked_date']; ?></td>
                                        <td><?php echo $mt_order_list['booked_time_slot']; ?></td>
                                        <td><span> Confirmed </span></td>
                                    </tr>
                                <?php endforeach; ?>

                                <tbody>
                            </table>
                        </form>
                        <!--    Start Pagination -->
                        <div class='pagination-container'>
                            <nav aria-label="Page navigation example">
                                <ul class="pagination">
                                    <!--	Here the JS Function Will Add the Rows -->
                                </ul>
                            </nav>
                        </div>
                        <div class="rows_count"><?php _e('Showing 11 to 20 of 91 entries ', 'mt-service-booking'); ?></div>

                    </div> <!-- End of Container -->

                </div>


            </main>
        </div>
        <?php
    }


    public static function mt_admin_settings_page_func()
    {
        global $wpdb;
        $mt_settings_data = $wpdb->prefix . 'mt_settings';

        $mt_plugin_path = plugin_dir_url(__FILE__);
        $mt_icon_url = $mt_plugin_path . 'assets/img/service-booking.png';

        if (isset($_POST['mt_save_admin_email_data'])) {
            $admin_mail_subject = stripslashes_deep($_POST['mt_admin_email_subject']);
            $admin_mail_body = stripslashes_deep($_POST['mt_admin_email_body']);
            $mt_customer_pending_email_subject = stripslashes_deep($_POST['mt_customer_pending_email_subject']);
            $mt_customer_pending_email_body = stripslashes_deep($_POST['mt_customer_pending_email_body']);
            $mt_customer_confirmed_email_subject = stripslashes_deep($_POST['mt_customer_confirmed_email_subject']);
            $mt_customer_confirmed_email_body = stripslashes_deep($_POST['mt_customer_confirmed_email_body']);
            $mt_customer_cancelled_email_subject = stripslashes_deep($_POST['mt_customer_cancelled_email_subject']);
            $mt_customer_cancelled_email_body = stripslashes_deep($_POST['mt_customer_cancelled_email_body']);

            $customer_cookie_duration = stripslashes_deep($_POST['mt_customer_cookie_duration']);
            // Securly insert data with $wpdb->inert method preventing the sql injection and also escaping strings
            $wpdb->insert($mt_settings_data, array(
                'admin_mail_subject' => $admin_mail_subject,
                'admin_mail_body' => $admin_mail_body,
                'customer_mail_pending_subject' => $mt_customer_pending_email_subject,
                'customer_mail_pending_body' => $mt_customer_pending_email_body,
                'customer_mail_confirmed_subject' => $mt_customer_confirmed_email_subject,
                'customer_mail_confirmed_body' => $mt_customer_confirmed_email_body,
                'customer_mail_cancel_subject' => $mt_customer_cancelled_email_subject,
                'customer_mail_cancel_body' => $mt_customer_cancelled_email_subject,
                'customer_cookie_duration' => $customer_cookie_duration
            ));

        };

        // When the page loads fetch data from database
        $mt_settings_data_fetch = $wpdb->get_row("SELECT * FROM $mt_settings_data ORDER BY id DESC LIMIT 1");

        // When settings data is changed fetch new data from database
        $mt_check_changes = isset($_POST['mt_save_admin_email_data']);

        if ($mt_check_changes) {
            $mt_settings_data_fetch = $wpdb->get_row("SELECT * FROM $mt_settings_data ORDER BY id DESC LIMIT 1");
        }

        $fetch_admin_mail_subject = $mt_settings_data_fetch->admin_mail_subject;
        $fetch_admin_mail_body = $mt_settings_data_fetch->admin_mail_body;
        $fetch_mt_customer_pending_email_subject = $mt_settings_data_fetch->customer_mail_pending_subject;
        $fetch_mt_customer_pending_email_body = $mt_settings_data_fetch->customer_mail_pending_body;
        $fetch_mt_customer_confirmed_email_subject = $mt_settings_data_fetch->customer_mail_confirmed_subject;
        $fetch_mt_customer_confirmed_email_body = $mt_settings_data_fetch->customer_mail_confirmed_body;
        $fetch_mt_customer_cancelled_email_subject = $mt_settings_data_fetch->customer_mail_cancel_subject;
        $fetch_mt_customer_cancelled_email_body = $mt_settings_data_fetch->customer_mail_cancel_body;
        $fetch_customer_cookie_duration = $mt_settings_data_fetch->customer_cookie_duration;


        ?>
        <div class="mt-container">
            <header class="mt-admin-main-page-header-wrapper">
                <div class="jumbotron text-center">
                    <div class="mt-admin-plugin-title">
                        <img src="<?php echo $mt_icon_url; ?>" alt="Service Booking Icon">
                        <h2 class="display-5"> <?php _e('Doctor Appointment ', 'mt-service-booking'); ?></h2>
                    </div>
                </div>
            </header>
            <main class="mt-admin-main-page-wrapper">
                <div class="tabs">
                    <ul>
                        <li><a href="admin.php?page=mt_admin_page"> <?php _e('Dashboard ', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="edit.php?post_type=mt_service"><?php _e('Book Services  ', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="post-new.php?post_type=mt_service"><?php _e('Add Book Services  ', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="post-new.php?post_type=doctorsprofile"><?php _e('Add Doctor Profile  ', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="edit.php?post_type=doctorsprofile"><?php _e('Doctor All  ', 'mt-service-booking'); ?></a>
                        </li>
                        <li>
                            <a href="admin.php?page=mt_admin_bookings_page"><?php _e('All Bookings  ', 'mt-service-booking'); ?></a>
                        </li>
                        <li><a href="admin.php?page=mt_admin_settings_page"
                               class="active"><?php _e('Settings  ', 'mt-service-booking'); ?></a></li>
                    </ul>
                </div>
                <form method="post">
                    <div class="mt-email-notification-data-wrapper container">
                        <!-- Admin Email Notification data starts-->
                        <div class="mt-admin-email-data-form">
                            <div class="card">
                                <div class="card-header">
                                    <?php _e('Service Provider\'s Email Notification', 'mt-service-booking'); ?>
                                </div>
                                <div class="card-body">
                                    <div class="form-group">
                                        <label for="mt_admin_email_subject"><?php _e('Admin Email Subject  ', 'mt-service-booking'); ?></label>
                                        <input type="text" name="mt_admin_email_subject" class="form-control"
                                               id="mt_admin_email_subject"
                                               value="<?php echo $fetch_admin_mail_subject; ?>"
                                               placeholder="Example: A Booking has been placed">
                                    </div>
                                    <div class="form-group">
                                        <label for="mt_admin_email_body"><?php _e('Admin Email Body  ', 'mt-service-booking'); ?></label>
                                        <textarea class="form-control" name="mt_admin_email_body"
                                                  id="mt_admin_email_body" rows="5"
                                                  placeholder="Your message body"><?php echo $fetch_admin_mail_body; ?></textarea>
                                    </div>
                                    <footer class="blockquote-footer"><?php _e('Service provider will receive this message when a booking will be placed. Booking info will be added automatically. ', 'mt-service-booking'); ?> </footer>
                                </div>
                            </div>
                        </div>
                        <!-- Admin Email Notification ends -->

                        <!-- Customer Email Notification data starts -->
                        <div class="mt-customer-email-data-form mt-5">
                            <div class="card">
                                <div class="card-header">
                                    <?php _e('Customer\'s Pending Booking Notification ', 'mt-service-booking'); ?>
                                </div>
                                <div class="card-body">
                                    <div class="form-group">
                                        <label for="mt_customer_pending_email_subject"><?php _e('Customer Email Subject  ', 'mt-service-booking'); ?></label>
                                        <input type="text" name="mt_customer_pending_email_subject" class="form-control"
                                               id="mt_customer_pending_email_subject"
                                               value="<?php echo $fetch_mt_customer_pending_email_subject; ?>"
                                               placeholder="Example: Your Booking is Pending">
                                    </div>
                                    <div class="form-group">
                                        <label for="mt_customer_pending_email_body"><?php _e('Customer Email Body  ', 'mt-service-booking'); ?></label>
                                        <textarea class="form-control" name="mt_customer_pending_email_body"
                                                  id="mt_customer_pending_email_body" rows="5"
                                                  placeholder="Your pending message body"><?php echo $fetch_mt_customer_pending_email_body; ?></textarea>
                                    </div>
                                    <footer class="blockquote-footer"> <?php _e('Customers will receive this message on placing a booking. Booking info will be added automatically. ', 'mt-service-booking'); ?></footer>
                                </div>
                            </div>
                        </div>
                        <!-- Customer Email Notification data ends -->

                        <!-- Customer Email Notification on booking confirmation -->
                        <div class="mt-customer-email-data-form mt-5">
                            <div class="card">
                                <div class="card-header">
                                    <?php _e('Customer\'s Booking Confirmation Notification ', 'mt-service-booking'); ?>
                                </div>
                                <div class="card-body">
                                    <div class="form-group">
                                        <label for="mt_customer_confirmed_email_subject"><?php _e('Confirmed Email Subject  ', 'mt-service-booking'); ?></label>
                                        <input type="text" name="mt_customer_confirmed_email_subject"
                                               class="form-control" id="mt_customer_confirmed_email_subject"
                                               value="<?php echo $fetch_mt_customer_confirmed_email_subject; ?>"
                                               placeholder="Example: Your Booking is Confirmed">
                                    </div>
                                    <div class="form-group">
                                        <label for="mt_customer_confirmed_email_body"><?php _e('Confirmed Email Body  ', 'mt-service-booking'); ?></label>
                                        <textarea class="form-control" name="mt_customer_confirmed_email_body"
                                                  id="mt_customer_confirmed_email_body" rows="5"
                                                  placeholder="Your confirmed message body"><?php echo $fetch_mt_customer_confirmed_email_body; ?></textarea>
                                    </div>
                                    <footer class="blockquote-footer"> <?php _e('Customers will receive this message when you confirmed a booking from the admin dashboard', 'mt-service-booking'); ?></footer>
                                </div>
                            </div>
                        </div>
                        <!-- Customer Email Notification on booking confirmation ends -->

                        <!-- Customer Email Notification on booking Cancellation -->
                        <div class="mt-customer-email-data-form mt-5">
                            <div class="card">
                                <div class="card-header">
                                    <?php _e('Customer\'s Booking Cancellation Notification ', 'mt-service-booking'); ?>
                                </div>
                                <div class="card-body">
                                    <div class="form-group">
                                        <label for="mt_customer_cancelled_email_subject"><?php _e('Cancelled Booking Email Subject  ', 'mt-service-booking'); ?></label>
                                        <input type="text" name="mt_customer_cancelled_email_subject"
                                               class="form-control" id="mt_customer_cancelled_email_subject"
                                               value="<?php echo $fetch_mt_customer_cancelled_email_subject; ?>"
                                               placeholder="Example: Your Booking is Cancelled Booking">
                                    </div>
                                    <div class="form-group">
                                        <label for="mt_customer_cancelled_email_body"><?php _e('Cancelled Booking Email Body  ', 'mt-service-booking'); ?></label>
                                        <textarea class="form-control" name="mt_customer_cancelled_email_body"
                                                  id="mt_customer_cancelled_email_body" rows="5"
                                                  placeholder="Your Cancelled Booking message body"><?php echo $fetch_mt_customer_cancelled_email_body; ?></textarea>
                                    </div>
                                    <footer class="blockquote-footer"> <?php _e('Customers will receive this message when you cancel a booking from the admin dashboard', 'mt-service-booking'); ?></footer>
                                </div>
                            </div>
                        </div>
                        <!-- Customer Email Notification on booking Cancellation ends -->

                        <!-- User Cookie -->
                        <div class="mt-customer-email-data-form mt-5">
                            <div class="card">
                                <div class="card-header">
                                    <?php _e('User Cookie  ', 'mt-service-booking'); ?>
                                </div>
                                <div class="card-body">
                                    <div class="form-group">
                                        <label for="mt_customer_cookie_duration"><?php _e('How many days do you want to save the customer info on their browser cookie?  ', 'mt-service-booking'); ?></label>
                                        <input type="number" name="mt_customer_cookie_duration"
                                               id="mt_customer_cookie_duration"
                                               value="<?php echo $fetch_customer_cookie_duration; ?>"
                                               class="form-control" placeholder="30">
                                    </div>
                                    <footer class="blockquote-footer">  <?php _e('Don\'t change it freequently ', 'mt-service-booking'); ?> </footer>
                                </div>
                            </div>
                        </div>
                        <!-- User Cookie data ends -->
                        <button name="mt_save_admin_email_data" type="submit"
                                class="btn btn-primary mt-3"> <?php _e('Save Changes ', 'mt-service-booking'); ?></button>
                    </div>

                </form>
            </main>
        </div>


        <?php

    }


    protected static function list_globals()
    {
        print '<h2>Global variables</h2><table class="code">';
        ksort($GLOBALS);
        foreach ($GLOBALS as $key => $value) {
            print '<tr><td>$' . esc_html($key) . '</td><td>';
            if (!is_scalar($value)) {
                print '<var>' . gettype($value) . '</var>';
            } else {
                if (FALSE === $value)
                    $show = '<var>FALSE</var>';
                elseif ('' === $value)
                    $show = '<var>""</var>';
                else
                    $show = esc_html($value);
                print $show;
            }
            print '</td></tr>';
        }
        print '</table>';
    }

    protected static function list_backtrace($backtrace)
    {
        print '<h2>debug_backtrace()</h2><ol class="code">';
        foreach ($backtrace as $item) {
            print '<li>';
            if (isset ($item['class']))
                print $item['class'] . $item['type'];
            print $item['function'];
            if (isset ($item['args']))
                print '<pre>args = ' . print_r($item['args'], TRUE) . '</pre>';
            if (isset ($item['file']))
                print '<br>' . $item['file'] . ' line: ' . $item['line'];
            print "\n";
        }
        print '</ol>';
    }
}


// Add meta box for selecting Add Doctor Profile
function add_doctor_meta_box()
{
    add_meta_box(
        'doctor_meta_box',
        'Select Add Doctor Profile',
        'render_doctor_meta_box',
        'mt_service', // Post type to add meta box to
        'normal',
        'high' //priority
    );
}

add_action('add_meta_boxes', 'add_doctor_meta_box');



// Render meta box content
function render_doctor_meta_box($post)
{
    // Retrieve saved doctor selection
    $selected_doctor = get_post_meta($post->ID, 'selected_doctor', true);

    // Get doctor posts
    $doctor_posts = get_posts(array(
        'post_type' => 'doctorsprofile',
        'posts_per_page' => -1,
    ));

    // Render dropdown
    echo '<label for="selected_doctor">Select Doctor:</label>';
    echo '<select id="selected_doctor" name="selected_doctor">';
    echo '<option value="">Select a Doctor</option>';
    foreach ($doctor_posts as $doctor_post) {
        $doctor_title = $doctor_post->post_title;
        $doctor_permalink = get_permalink($doctor_post->ID);
        $selected = ($selected_doctor == $doctor_post->ID) ? 'selected="selected"' : '';
        echo '<option value="' . esc_attr($doctor_post->ID) . '" ' . $selected . '>' . esc_html($doctor_title) . '</option>';
    }
    echo '</select>';

    // Display selected doctor's title and permalink if already selected
    if (!empty($selected_doctor)) {
        echo '<p><strong>Selected Doctor Title:</strong> ' . get_the_title($selected_doctor) . '</p>';
        echo '<p><strong>Selected Doctor Permalink:</strong> <a href="' . get_permalink($selected_doctor) . '">' . get_permalink($selected_doctor) . '</a></p>';
    }
}

// Save selected doctor
function save_doctor_meta_box($post_id)
{
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    if (!current_user_can('edit_post', $post_id)) return;
    if (isset($_POST['selected_doctor'])) {
        update_post_meta($post_id, 'selected_doctor', sanitize_text_field($_POST['selected_doctor']));
    }
}

add_action('save_post', 'save_doctor_meta_box' , 100);

// Modify the permalink displayed in the list of 'mt_service' posts
function modify_mt_service_permalink($permalink, $post)
{
    if ($post->post_type == 'mt_service') {
        $selected_doctor_id = get_post_meta($post->ID, 'selected_doctor', true);
        if ($selected_doctor_id) {
            $doctor_permalink = get_permalink($selected_doctor_id);
            if ($doctor_permalink) {
                $permalink = esc_url($doctor_permalink);
            }
        }
    }
    return $permalink;
}

add_filter('post_type_link', 'modify_mt_service_permalink', 10, 2);


// Customize the post title column for 'mt_service' post type
function custom_mt_service_title_column($title, $post_id)
{
    if (get_post_type($post_id) === 'mt_service') {
        $selected_doctor_id = get_post_meta($post_id, 'selected_doctor', true);
        if ($selected_doctor_id) {
            $doctor_name = get_post_field('post_title', $selected_doctor_id); // Retrieve the doctor's name
            if ($doctor_name) {
                return $doctor_name;
            }
        }
    }
    return $title;
}

add_filter('the_title', 'custom_mt_service_title_column', 10, 2);

// Modify the "Featured image" option for 'mt_service' post type
// Modify the "Featured image" option for 'mt_service' post type and label it as "Photo"
function custom_mt_service_featured_image($content, $post_id)
{
    if (get_post_type($post_id) === 'mt_service') {
        $selected_doctor_id = get_post_meta($post_id, 'selected_doctor', true);
        if ($selected_doctor_id) {
            $doctor_thumbnail = get_the_post_thumbnail($selected_doctor_id);
            if ($doctor_thumbnail) {
                // Customize label from "Featured image" to "Photo"
                $content = str_replace('Featured image', 'Photo', $content);
                // Replace the content with doctor's featured image
                $content = $doctor_thumbnail;
            }
        }
    }
    return $content;
}

add_filter('admin_post_thumbnail_html', 'custom_mt_service_featured_image', 10, 2);


// Add custom column for displaying doctor's thumbnail image in the list of 'mt_service' posts
function add_photo_column($columns)
{
    // Add custom "Photo" column
    $columns['photo'] = 'Photo';
    return $columns;
}

add_filter('manage_mt_service_posts_columns', 'add_photo_column');

// Display doctor's thumbnail image in the custom column
function display_photo_column($column, $post_id)
{
    if ($column === 'photo') {
        $selected_doctor_id = get_post_meta($post_id, 'selected_doctor', true);
        if ($selected_doctor_id) {
            $doctor_thumbnail = get_the_post_thumbnail($selected_doctor_id, 'thumbnail');
            if ($doctor_thumbnail) {
                echo $doctor_thumbnail;
            } else {
                // If no photo available for the doctor, show 'No photo available'
                echo 'No photo available';
            }
        } else {
            // If no doctor selected, show 'No doctor selected'
            echo 'No doctor selected';
        }
    }
}

add_action('manage_mt_service_posts_custom_column', 'display_photo_column', 10, 2);

// Enqueue CSS to hide the default thumbnail column
function hide_default_thumbnail_column_css()
{
    global $post_type;
    if ('mt_service' === $post_type) {
        ?>
        <style type="text/css">
            /* Hide default thumbnail column header */
            .column-post-thumbnail {
                display: none !important;
            }

            /* Hide default thumbnail column content */
            .column-post-thumbnail + .column-photo {
                display: none !important;
            }
        </style>
        <?php
    }
}

add_action('admin_head', 'hide_default_thumbnail_column_css');



















