<div class="std-settings-each-section" data-tab="constant_contact_v3" <?php echo ($active_tab != 'constant_contact_v3') ? 'style="display:none;"' : ''; ?>>
    <div class="std-field-wrap">
        <label><?php esc_html_e('Enable Constant Contact', 'subscribe-to-download'); ?></label>
        <div class="std-field">
            <input type="checkbox" name="std_settings[constant_contact_v3][enable]" value="1" <?php echo (!empty($std_settings['constant_contact_v3']['enable'])) ? 'checked="checked"' : ''; ?> class="std-checkbox-toggle-trigger" data-toggle-class="std-cc-enabled" />
        </div>
    </div>
    <?php $enable_constant_contact = (!empty($std_settings['constant_contact_v3']['enable'])) ? 1 : 0; ?>
    <div class="std-cc-enabled" <?php $this->display_none($enable_constant_contact, 1); ?>>
        <div class="std-field-wrap">
            <label><?php esc_html_e('Constant Contact Client ID', 'subscribe-to-download'); ?></label>
            <div class="std-field">
                <?php $redirect_uri = site_url() . '?std_constant_contact_authorize'; ?>
                <input type="text" name="std_settings[constant_contact_v3][client_id]" value="<?php echo (!empty($std_settings['constant_contact_v3']['client_id'])) ? esc_attr($std_settings['constant_contact_v3']['client_id']) : ''; ?>" class="std-constant_contact-api-key" />
                <p class="description"><?php echo sprintf(esc_html__('Please go %s here %s to generate your client id', 'subscribe-to-download'), '<a href="https://app.constantcontact.com/pages/dma/portal/" target="_blank">', '</a>'); ?></p>
                <p class="description"><?php echo sprintf(esc_html__('Please go %s here %s to know how to generate necessary keys', 'subscribe-to-download'), '<a href="https://developer.constantcontact.com/api_guide/getting_started.html#step-2-register-your-first-application" target="_blank">', '</a>'); ?></p>
                <p class="description"><?php echo sprintf(esc_html__('Please add %s in the authorize url while creating the app', 'subscribe-to-download'), $redirect_uri); ?></p>
            </div>
        </div>
        <div class="std-field-wrap">
            <label><?php esc_html_e('Constant Contact Client Secret', 'subscribe-to-download'); ?></label>
            <div class="std-field">
                <input type="text" name="std_settings[constant_contact_v3][client_secret]" value="<?php echo (!empty($std_settings['constant_contact_v3']['client_secret'])) ? esc_attr($std_settings['constant_contact_v3']['client_secret']) : ''; ?>" class="std-constant_contact-access-token" />
                <p class="description"><?php echo sprintf(esc_html__('Please go %s here %s to generate your client secret.', 'subscribe-to-download'), '<a href="https://app.constantcontact.com/pages/dma/portal/" target="_blank">', '</a>'); ?></p>
                <p class="description"><?php echo sprintf(esc_html__('Please go %s here %s to know how to generate necessary keys', 'subscribe-to-download'), '<a href="https://developer.constantcontact.com/api_guide/getting_started.html#step-2-register-your-first-application" target="_blank">', '</a>'); ?></p>


            </div>
        </div>
        <?php if (!empty($std_settings['constant_contact_v3']['client_id']) && !empty($std_settings['constant_contact_v3']['client_secret'])) { ?>
            <div class="std-field-wrap">
                <label><?php esc_html_e('Status', 'subscribe-to-download'); ?></label>
                <div class="std-field">
                    <?php
                    $constant_contact_status = (!empty($std_settings['constant_contact_v3']['access_token'])) ? 1 : 0;
                    $constant_contact_status_class = ($constant_contact_status == 1) ? 'std-cc-connected' : 'std-cc-disconnected';
                    ?>


                    <span class="std-constant_contact-status <?php echo esc_attr($constant_contact_status_class); ?>"><?php echo ($constant_contact_status == 1) ? esc_html__('Connected', 'subscribe-to-download') : esc_html__('Disconnected', 'subscribe-to-download'); ?></span>
                </div>
            </div>

            <div class="std-field-wrap">
                <label></label>
                <div class="std-field">
                    <?php
                    $connect_label = (!empty($constant_contact_status)) ? esc_html__('Reauthorize and Renew List', 'subscribe-to-download') : esc_html__('Authorize', 'subscribe-to-download');
                    ?>
                    <a href="<?php echo admin_url('admin-post.php?action=constant_contact_authorize&authorize_nonce=' . wp_create_nonce('std_cc_nonce')); ?>"><input type="button" class="std-constant_contact_v3-connect button-secondary" value="<?php echo esc_attr($connect_label); ?>"></a>
                    <?php if ($constant_contact_status == 1) {
                    ?>
                        <input type="button" class="std-constant_contact_v3-reset button-secondary" value="<?php esc_html_e("Reset", 'subscribe-to-download'); ?>">
                    <?php
                    }
                    ?>
                    <div class="std-constant_contact-api-response" style="display:none;"></div>
                </div>
            </div>
        <?php } ?>
        <?php
        if (!empty($std_settings['constant_contact_v3']['access_token'])) {

        ?>

            <div class="std-constant_contact-lists-wrap">
                <?php
                if (!empty($std_settings['constant_contact_v3']['lists'])) {
                    $constant_contact_lists = $std_settings['constant_contact_v3']['lists'];
                ?>
                    <div class="std-field-wrap std-custom-table-v3">
                        <input type="hidden" name="std_settings[constant_contact_v3][access_token]" value="<?php echo (!empty($std_settings['constant_contact_v3']['access_token'])) ? esc_attr($std_settings['constant_contact_v3']['access_token']) : ''; ?>" class="std-constant_contact-access-token" />
                        <input type="hidden" name="std_settings[constant_contact_v3][refresh_token]" value="<?php echo (!empty($std_settings['constant_contact_v3']['refresh_token'])) ? esc_attr($std_settings['constant_contact_v3']['refresh_token']) : ''; ?>" class="std-constant_contact-refresh-token" />
                        <input type="hidden" name="std_settings[constant_contact_v3][expires_in]" value="<?php echo (!empty($std_settings['constant_contact_v3']['expires_in'])) ? esc_attr($std_settings['constant_contact_v3']['expires_in']) : ''; ?>" class="std-constant_contact-expires-in" />
                        <label><?php esc_html_e('Constant Contact Lists', 'subscribe-to-download'); ?></label>
                        <div class="std-field">
                            <div class="std-custom-table">
                                <div class="std-each-list std-list-head">
                                    <span class="std-list-col"><?php esc_html_e('ID', 'subscribe-to-download'); ?></span>
                                    <span class="std-list-col"><?php esc_html_e('List Name', 'subscribe-to-download'); ?></span>
                                </div>
                                <?php
                                $key = 0;
                                foreach ($constant_contact_lists as $constant_contact_list) {
                                ?>
                                    <div class="std-each-list">
                                        <span class="std-list-col">
                                            <?php echo esc_attr($constant_contact_list['list_id']); ?>
                                            <input type="hidden" name="std_settings[constant_contact_v3][lists][<?php echo esc_attr($key); ?>][list_id]" value="<?php echo esc_attr($constant_contact_list['list_id']); ?>" />
                                        </span>
                                        <span class="std-list-col">
                                            <?php echo esc_attr($constant_contact_list['name']); ?>
                                            <input type="hidden" name="std_settings[constant_contact_v3][lists][<?php echo esc_attr($key); ?>][name]" value="<?php echo esc_attr($constant_contact_list['name']); ?>" />
                                        </span>
                                    </div>
                                <?php
                                    $key++;
                                } ?>
                            </div>
                        </div>
                    </div>
                <?php
                }
                ?>
            </div>
            <div class="std-field-wrap">
                <label><?php esc_html_e('Last Log', 'subscribe-to-download'); ?></label>
                <div class="std-field">
                    <textarea name="std_settings[constant_contact_v3][cc_log]"><?php echo (!empty($std_settings['constant_contact_v3']['cc_log'])) ? $this->sanitize_html($std_settings['constant_contact_v3']['cc_log']) : ''; ?></textarea>
                    <input type="button" class="std-clear-log-trigger button-secondary" value="<?php esc_html_e('Clear Log', 'subscribe-to-download'); ?>">
                </div>
            </div>
        <?php
        } else {
        ?>
            <div class="std-field-wrap">
                <label for=""></label>
                <div class="std-field">
                    <p class="description"><?php esc_html_e('Please enter client id and client secret, then save the settings and refresh the page to authorize in Constant Contact and fetch the lists', 'subscribe-to-download'); ?></p>
                </div>
            </div>

        <?php
        }
        ?>
    </div>
</div>