<?php
namespace NewsletterBridge\Admin\Tabs;

use NewsletterBridge\Config\MailupConfig;

/**
 * Tab MailUp per l'admin
 */
class MailupTab {
    
    public static function render() {
        // Usa la stessa logica di MailupConfig per leggere i valori
        $config = MailupConfig::getConfig();
        ?>
        <h2>Configurazione MailUp</h2>
        <table class="form-table">
            <tr>
                <th scope="row">Client ID</th>
                <td>
                    <input type="text" name="MAILUP_CLIENT_ID" value="<?php echo esc_attr($config->clientId); ?>" class="regular-text" />
                    <p class="description">Inserire il client id</p>
                </td>
            </tr>
            <tr>
                <th scope="row">Client Secret</th>
                <td>
                    <input type="text" name="MAILUP_CLIENT_SECRET" value="<?php echo esc_attr($config->clientSecret); ?>" class="regular-text" />
                    <p class="description">Inserire il client secret id</p>
                </td>
            </tr>
            <tr>
                <th scope="row">Client Username</th>
                <td>
                    <input type="text" name="MAILUP_CLIENT_USER" value="<?php echo esc_attr($config->user); ?>" class="regular-text" />
                    <p class="description">Inserire il client username</p>
                </td>
            </tr>
            <tr>
                <th scope="row">Client Password</th>
                <td>
                    <input type="password" name="MAILUP_CLIENT_PASS" value="<?php echo esc_attr($config->password); ?>" class="regular-text" />
                    <p class="description">Inserire il client password</p>
                </td>
            </tr>
            <tr>
                <th scope="row">ID Lista</th>
                <td>
                    <input type="number" name="MAILUP_CLIENT_LIST" value="<?php echo esc_attr($config->list); ?>" class="small-text" />
                    <p class="description">Inserire l'ID della lista al quale iscrivere l'utente</p>
                </td>
            </tr>
            <tr>
                <th scope="row">ID Gruppo</th>
                <td>
                    <input type="number" name="MAILUP_CLIENT_GROUP" value="<?php echo esc_attr($config->group); ?>" class="small-text" />
                    <p class="description">Inserire l'ID del gruppo al quale iscrivere l'utente</p>
                </td>
            </tr>
            <tr>
                <th scope="row">ID Gruppo DEM</th>
                <td>
                    <input type="number" name="MAILUP_CLIENT_GROUP_DEM" value="<?php echo esc_attr($config->groupDem); ?>" class="small-text" />
                    <p class="description">Inserire l'ID del gruppo DEM per le iscrizioni con marketing attivato</p>
                </td>
            </tr>
            <tr>
                <th scope="row">ID Messaggio</th>
                <td>
                    <input type="number" name="MAILUP_ID_MESSAGE" value="<?php echo esc_attr($config->emailTemplateID); ?>" class="small-text" />
                    <p class="description">Inserire l'ID del messaggio da clonare per l'invio della newsletter</p>
                </td>
            </tr>
            <tr>
                <th scope="row">Callback URI</th>
                <td>
                    <input type="text" name="MAILUP_CALLBACK_URI" value="<?php echo esc_attr($config->callbackUri); ?>" class="regular-text" />
                    <p class="description">Callback URI</p>
                </td>
            </tr>
            <tr>
                <th scope="row">Optin su registrazione</th>
                <td>
                    <label>
                        <input type="radio" name="MAILUP_OPTIN" value="true" <?php checked($config->optin, true); ?> />
                        Sì
                    </label>
                    <label>
                        <input type="radio" name="MAILUP_OPTIN" value="false" <?php checked($config->optin, false); ?> />
                        No
                    </label>
                    <p class="description">Optin su registrazione?</p>
                </td>
            </tr>
        </table>
        <?php
    }
    
    public static function save($data) {
        MailupConfig::saveConfig($data);
    }
}
