<?php

class Zbr_Admin
{
    public function __construct()
    {
        add_action('admin_init', [$this, 'init']);
        add_action('admin_menu', [$this, 'setup_menu']);
    }

    public function init()
    {
        $this->includes();

        register_setting('zbr_settings', 'zbr_api_key');
        register_setting('zbr_settings', 'zbr_api_host');

        add_settings_section(
            'zbr_settings_section',
            esc_attr__('Ziber Settings', 'zbr-api'),
            array($this, 'html_settings_section'),
            'zbr-settings'
        );

        add_settings_field(
            'zbr_api_key_settings_field',
            esc_attr__('API Key', 'zbr-api'),
            array($this, 'html_api_key_setting'),
            'zbr-settings',
            'zbr_settings_section',
            array(
                'label_for' => 'zbr_api_key',
            )
        );

        add_settings_field(
            'zbr_api_host_settings_field',
            esc_attr__('API host', 'zbr-api'),
            array($this, 'html_api_host_setting'),
            'zbr-settings',
            'zbr_settings_section',
            array(
                'label_for' => 'zbr_api_host',
            )
        );

        // meta boxes
        add_meta_box(
            'zbr_meta_box',
            'Metadata',
            [$this, 'zbr_meta_box_callback'],
            ['zbr_news', 'zbr_zone', 'zbr_zone_team_member']
        );
    }

    public function zbr_meta_box_callback($post)
    {
        $meta = get_post_meta($post->ID);
?>
        <p>
            <label>ID</label>
            <input type="text" class="widefat" readonly value="<?= $meta['_zbr_native_id'][0] ?>">
            <?php if ($post->post_type == 'zbr_zone_team_member') : ?>
                <label>Zone ID</label>
                <input type="text" class="widefat" readonly value="<?= $meta['_zbr_zone_id'][0] ?>">

                <label>Roles</label>
                <input type="text" class="widefat" readonly value="<?= $meta['_roles'][0] ?>">
            <?php endif; ?>

        </p>
    <?php
    }

    public function create_new_import()
    {

        global $wpdb;

        $tableName = $wpdb->prefix . 'zbr_loading_logs';

        $now = date("Y-m-d H:i:s");

        $wpdb->insert($tableName, array(
            'createdAt' => $now,
            'status' => 0,
        ));
        $id = $wpdb->insert_id;

        return $id;
    }

    public function run_import()
    {
        Zbr_Plugin_Core::instance()->load_api_data();
    }

    public function includes()
    {
        include_once __DIR__ . '/dashboard-functions.php';
    }

    public function html_settings_section()
    {
        echo '';
    }

    public function html_api_key_setting()
    {
        // get the value of the setting we've registered with register_setting()
        $setting = get_option('zbr_api_key');
        // output the field
    ?>
        <input type="text" class="regular-text" name="zbr_api_key" id="zbr_api_key" value="<?php echo esc_attr($setting) ?>">
    <?php
    }

    public function html_api_host_setting()
    {
        // get the value of the setting we've registered with register_setting()
        $setting = get_option('zbr_api_host');
        // output the field
    ?>
        <input type="text" class="regular-text" name="zbr_api_host" id="zbr_api_host" value="<?php echo esc_attr($setting) ?>">
        <p><small><?php echo __('Leave empty for production', 'zbr-api') ?></small></p>
    <?php
    }

    public function html_page_field()
    {
    ?>
        <div class="wrap">
            <form method="post" action="options.php">
                <?php
                settings_fields('zbr_settings'); // settings group name
                do_settings_sections('zbr-settings'); // just a page slug
                submit_button();
                ?>
            </form>
        </div>
    <?php
    }

    public function html_imports_page_field()
    {
    ?>
        <div class="wrap">
            <h1>Imports</h1>
            <div x-data="widget">
                <a class="button" @click.prevent="activate()" :class="active ? 'disabled':''">
                    <span class="spinner" :class="active ? 'is-active':''"></span>
                    <?php _e('Start import', 'zbr-api'); ?>
                </a>
            </div>
        </div>
<?php

    }

    public function setup_menu()
    {
        $logo = plugins_url('assets/images/ziber_logo.png', ZBR_PLUGIN);

        add_menu_page(
            esc_html__('Ziber settings', 'zbr-api'), // page <title>Title</title>
            esc_attr__('Ziber', 'zbr-api'), // menu link text
            'manage_options', // capability to access the page
            'zbr-settings', // page URL slug
            [$this, 'html_page_field'], // callback function /w content
            $logo,
            5 // priority
        );

        add_submenu_page(
            'zbr-settings',
            'Imports',
            'Imports',
            'manage_options',
            'zbr-imports',
            [$this, 'html_imports_page_field']
        );
    }
}

new Zbr_Admin();
