<?php


class Razer_Gold_Pin_Store_RGPS_Admin
{
    public function __construct()
    {
        add_action('admin_menu', array($this, 'menu'));
        add_action( 'wp_ajax_razer_gold_pin_store_generate', array($this,'razer_gold_pin_store_generate_ajax'));
    }

    public function menu()
    {
        $razer_icon = razer_gold_pin_store_generate_rgps()->assets . 'img/razer-gold-logo.png';
        add_menu_page('Razer Gold Pin', 'Razer Gold Pin', 'manage_options', 'razer-gold-pin-store-generate', array($this,'razer-gold-pin-store-generate'), $razer_icon);
        add_submenu_page('razer-gold-pin-store-generate', __('Configuración'), __('Configuración', 'razer-gold-pin-store'), 'manage_options', 'config-razer-gold-pin-store-generate', array($this,'config'));
        add_submenu_page('razer-gold-pin-store-generate', __('Generar PIN'), __('Generar PIN'), 'manage_options', 'create-razer-gold-pin-store-generate', array($this,'generate'));
        //add_submenu_page('razer-gold-pin-store-generate', __('Historial'), __('Historial'), 'manage_options', 'history-razer-gold-pin-store-generate', array($this,'history'));
        remove_submenu_page('razer-gold-pin-store-generate', 'razer-gold-pin-store-generate');
    }

    public function config()
    {
        $config  = get_option('razer-gold-pin-store-generate', []);

        ?>
        <div class="wrap about-wrap">
            <h2><?php _e('Configuraciones'); ?></h2>
            <form id="razer-gold-pin-store-config">
                <table>
                    <tbody>
                    <tr>
                        <td style="padding-top: 10%;">
                            <select name="enviroment" class="select-enviroment">
                                <option value="1" <?php if(isset($config['enviroment']) && $config['enviroment']) echo 'selected'; ?>>Pruebas</option>
                                <option value="0" <?php if(isset($config['enviroment']) && !$config['enviroment']) echo 'selected'; ?>>Producción</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th><?php _e('Application Code');?></th>
                        <td>
                            <input type="number" name="application_code" value="<?php if(isset($config['application_code'])) echo esc_attr($config['application_code']); ?>" required>
                        </td>
                    </tr>
                    <tr>
                        <th><?php  _e('Secret key');?></th>
                        <td>
                            <input type="password" name="secret_key" value="<?php if(isset($config['secret_key'])) echo esc_attr($config['secret_key']); ?>" required>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label for="razer-gold-pin-store-excel">Subir excel (catálogo de productos)</label>
                        </td>
                        <td>
                            <input type="file" accept=".xlsx" id="razer-gold-pin-store-excel">
                            <?php wp_nonce_field( "razer_gold_pin_store_generate_excel", "razer_gold_pin_store_generate_excel" );  ?>
                        </td>
                    </tr>
                    </tbody>
                </table>
                <?php wp_nonce_field( "razer_gold_pin_store_generate", "razer_gold_pin_store_generate" ); ?>
                <?php submit_button(); ?>
            </form>
        </div>
        <?php
    }

    public function generate()
    {
        global $wpdb;
        $table_name = $wpdb->prefix . 'razer_gold_pin_store';
        $query = "SELECT * FROM $table_name";
        $result = $wpdb->get_results($query);

        $config  = get_option('razer-gold-pin-store-generate', []);

        if(empty($result) || empty($config)) wp_redirect( admin_url( 'admin.php?page=config-razer-gold-pin-store-generate' ) );

        ?>
        <div class="wrap about-wrap">
            <h2><?php _e('Generar Pin'); ?></h2>
            <form id="razer-gold-pin-store-generate">
                <table>
                    <tbody>
                        <tr>
                            <td>
                                <label for="razer_gold_pin_store"></label>
                                <select name="razer_gold_pin_store" id="razer_gold_pin_store">
                                    <option value="" selected>--Seleccione un código--</option>
                                    <?php foreach ($result as $item):  ?>
                                        <option value="<?php echo esc_attr($item->code); ?>"><?php echo esc_attr($item->description); ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </td>
                        </tr>
                    <tr>
                        <td>
                            <label for="razer_gold_pin_store_qty"></label>
                            <input name="razer_gold_pin_store_qty" id="razer_gold_pin_store_qty" type="number" min="1" title="Cantidad a generar" placeholder="cantidad a generar" required="">
                        </td>
                    </tr>
                    </tbody>
                </table>
                <?php submit_button('Generar'); ?>
            </form>
        </div>
        <?php
    }

    public function history()
    {
        ?>
        <table id="razer-history" class="display compact" style="width:90%">
            <thead>
            <tr>
                <th>Usuario</th>
                <th>Fecha de creación</th>
                <th>Exportar</th>
            </tr>
            </thead>
            <tfoot>
            <tr>
                <th>Usuario</th>
                <th>Fecha de creación</th>
                <th>Exportar</th>
            </tr>
            </tfoot>
        </table>
        <?php
    }

    public function razer_gold_pin_store_generate_ajax()
    {
        if ( ! wp_verify_nonce(  $_REQUEST['razer_gold_pin_store_generate'], 'razer_gold_pin_store_generate' ) )
            return;

        $config = [];
        $enviroment = sanitize_text_field($_REQUEST['enviroment']);
        $config['enviroment'] = (bool)(int)$enviroment;
        $config['application_code'] = sanitize_text_field($_REQUEST['application_code']);
        $config['secret_key'] = sanitize_text_field($_REQUEST['secret_key']);
        update_option('razer-gold-pin-store-generate', $config);
    }
}