<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if (!class_exists('babAdmin')) {
    class babAdmin
    {
        public function __construct()
        {
            add_action('init', [$this, 'bab_accordion_block_post_type']);
            add_action('admin_menu', [$this, 'bab_sub_Menu']);
            add_filter('manage_accordion_block_posts_columns', [$this, 'accordionManageColumns'], 10);
            add_action('manage_accordion_block_posts_custom_column', [$this, 'accordionManageCustomColumns'], 10, 2);
        }
        public function bab_accordion_block_post_type()
        {
            register_post_type(
                'accordion_block',
                array(
                    'label' => 'Accordion',
                    'labels' => [
                        'add_new' => 'Add New', // Global page
                        'add_new_item' => 'Add New Accordion', // When click on new post
                        'edit_item' => 'Edit Accordion',
                        'not_found' => 'There is no accordion please add one',
                        'item_published' => 'Accordion Published',
                        'item_updated' => 'Accordion Updated'
                    ],
                    'public' => false,
                    'show_ui' => true,
                    'show_in_rest' => true,
                    'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 600" width="24" height="24" aria-hidden="true" focusable="false"><g transform="translate(0.000000,600.000000) scale(0.100000,-0.100000)" fill="#4527a4"><path d="M2830 5994 c-368 -31 -646 -93 -934 -206 -759 -299 -1385 -925 -1684 -1684 -89 -227 -155 -484 -188 -741 -23 -175 -23 -551 0 -726 83 -635 347 -1208 765 -1660 494 -535 1120 -858 1848 -953 175 -23 551 -23 726 0 728 95 1354 418 1848 953 418 452 682 1025 765 1660 23 175 23 551 0 726 -71 543 -261 1013 -583 1440 -488 650 -1216 1071 -2024 1172 -121 15 -450 27 -539 19z m2005 -1221 c48 -26 116 -94 141 -143 18 -34 19 -95 19 -1630 0 -1535 -1 -1596 -19 -1630 -25 -49 -93 -117 -141 -143 l-40 -22 -1695 0 c-1633 0 -1696 1 -1730 19 -49 25 -117 93 -143 141 l-22 40 0 1595 c0 1535 1 1596 19 1630 37 71 117 140 186 161 14 4 781 7 1705 6 l1680 -2 40 -22z"></path><path d="M1462 4585 c-58 -25 -62 -48 -62 -385 l0 -300 1700 0 1700 0 0 300 c0 340 -3 360 -64 385 -52 22 -3224 22 -3274 0z m300 -202 l88 -88 90 90 90 90 72 -73 73 -72 -163 -162 -162 -163 -162 162 -163 163 70 70 c38 38 72 70 75 70 2 0 44 -39 92 -87z"></path><path d="M1400 3000 l0 -700 1700 0 1700 0 0 700 0 700 -1700 0 -1700 0 0 -700z m615 380 l160 -160 -73 -72 -72 -73 -90 90 -90 90 -90 -90 -90 -90 -72 73 -73 72 160 160 c88 88 162 160 165 160 3 0 77 -72 165 -160z"></path><path d="M1400 1800 c0 -340 3 -360 64 -385 53 -22 3219 -22 3272 0 61 25 64 45 64 385 l0 300 -1700 0 -1700 0 0 -300z m362 83 l88 -88 90 90 90 90 72 -73 73 -72 -163 -162 -162 -163 -162 162 -163 163 70 70 c38 38 72 70 75 70 2 0 44 -39 92 -87z"></path></g></svg>'),
                    'template' => [['bab/accordion']],
                    'template_lock' => 'all',
                )
            );
        }
        public function bab_sub_Menu()
        {
            add_submenu_page(
                'edit.php?post_type=accordion_block',
                'Demo & Help',
                'Demo & Help',
                'manage_options',
                'bab_Dashboard',
                [$this, 'bab_Dashboard_page']
            );
        }
        public function bab_Dashboard_page()
        {

            if (!current_user_can('manage_options')) {
                return;
            }
            ?>
            <div id='vgbDashboard' data-info='<?php echo esc_attr(wp_json_encode([
                'version' => BAB_PLUGIN_VERSION,
                'isPremium' => bab_is_premium(),
                'hasPro' => BAB_HAS_PRO,
                'licenseActiveNonce' => wp_create_nonce('bab_license_active')
            ])); ?>'></div>
            <?php
        }

        public function accordionManageColumns($defaults)
        {
            unset($defaults['date']);
            $defaults['shortcode'] = 'ShortCode';
            $defaults['date'] = 'Date';
            return $defaults;
        }
        public function accordionManageCustomColumns($column_name, $post_ID)
        {
            if ($column_name === 'shortcode') {
                echo '<div class="bPlAdminShortcode" id="bPlAdminShortcode-' . esc_attr($post_ID) . '">
					<input value="[accordion id=' . esc_attr($post_ID) . ']" onclick="copyBPlAdminShortcode(\'' . esc_attr($post_ID) . '\')" readonly>
					<span class="tooltip">Copy To Clipboard</span>
				</div>';
            }
        }
    }
    new babAdmin();

}
