<?php
namespace EasyRestaurantManager\Hooks\Handler;

class AdminMenuHandler {
    public function add_admin_menu() {
        global $submenu;
        add_menu_page(
            'easy-restaurant-manager',
            'Easy Restaurant Manager',
            'manage_options',
            'easy-restaurant-manager.php',
            array($this, 'render_admin_page'),
            $this->getMenuIcon(),
            25
        );

        $submenu['easy-restaurant-manager.php']['dashboard'] = array(
            'Dashboard',
            'manage_options',
            'admin.php?page=easy-restaurant-manager.php#/',
        );

        $submenu['easy-restaurant-manager.php']['menus'] = array(
            'Menus',
            'manage_options',
            'admin.php?page=easy-restaurant-manager.php#/menus',
        );

        $submenu['easy-restaurant-manager.php']['category'] = array(
            'Category',
            'manage_options',
            'admin.php?page=easy-restaurant-manager.php#/category',
        );
        $submenu['easy-restaurant-manager.php']['tables'] = array(
            'Tables',
            'manage_options',
            'admin.php?page=easy-restaurant-manager.php#/tables',
        );
        $submenu['easy-restaurant-manager.php']['reservation_table'] = array(
            'Reservation Table',
            'manage_options',
            'admin.php?page=easy-restaurant-manager.php#/reservation_table',
        );
        $submenu['easy-restaurant-manager.php']['guide'] = array(
            'Usage Guide',
            'manage_options',
            'admin.php?page=easy-restaurant-manager.php#/guide',
        );

        $this->enqueue_admin_assets();
    }

    public function getMenuIcon()
    {
        return EASY_RESTAURANT_MANAGER_PLUGIN_URL . 'assets/images/iconAdmin.png';
    }

    public function render_admin_page() {
        echo '<div id="restaurant-menu-mange-and-order-app"></div>';
    }

    public function enqueue_admin_assets() {
        // if (strpos($hook, 'restaurant-menu-mange-and-order') === false) {
        //     return;
        // }

        wp_enqueue_style(
            'easyRestaurantManager-admin',
            EASY_RESTAURANT_MANAGER_PLUGIN_URL . 'assets/css/admin.css',
            [],
            EASY_RESTAURANT_MANAGER_VERSION
        );
        wp_enqueue_media();
        wp_enqueue_script('wp-tinymce');
        wp_enqueue_editor();
        wp_enqueue_script(
            'easyRestaurantManager-js',
            EASY_RESTAURANT_MANAGER_PLUGIN_URL . 'assets/js/admin.js',
            ['jquery','wp-api-fetch', 'wp-blocks', 'wp-editor', 'wp-components', 'wp-element', 'wp-data'],
            EASY_RESTAURANT_MANAGER_VERSION,
            true
        );

        wp_localize_script('easyRestaurantManager-js', 'RestaurantMenuMangeData', [
            'rest_api' => rest_url('easy-restaurant-manage/v1'),
            'assets_url' => EASY_RESTAURANT_MANAGER_PLUGIN_URL . 'assets/',
            'nonce' => wp_create_nonce('wp_rest'),
            'branch_id' => 1,
        ]);
    }
}