<?php
defined('ABSPATH') or die('No script kiddies please!!');
?>
<div class="std-settings-each-section" data-tab="convertkit"  <?php echo ($active_tab != 'convertkit') ? 'style="display:none;"' : ''; ?>>
    <div class="std-field-wrap">
        <label><?php esc_html_e('Enable convertkit', 'subscribe-to-download'); ?></label>
        <div class="std-field">
            <input type="checkbox" name="std_settings[convertkit][enable]" value="1" <?php echo (!empty($std_settings['convertkit']['enable'])) ? 'checked="checked"' : ''; ?> class="std-checkbox-toggle-trigger" data-toggle-class="std-sib-enabled" />
        </div>
    </div>
    <?php $enable_convertkit = (!empty($std_settings['convertkit']['enable'])) ? 1 : 0; ?>
    <div class="std-sib-enabled" <?php $this->display_none($enable_convertkit, 1); ?>>
        <div class="std-field-wrap">
            <label><?php esc_html_e('Convertkit API Key', 'subscribe-to-download'); ?></label>
            <div class="std-field">
                <input type="text" name="std_settings[convertkit][api_key]" value="<?php echo (!empty($std_settings['convertkit']['api_key'])) ? esc_attr($std_settings['convertkit']['api_key']) : ''; ?>" class="std-convertkit-api-key" />
                <p class="description"><?php echo sprintf(esc_html__('Please go %s here %s to generate your api key. Please note that you should be logged in to generate the API key.', 'subscribe-to-download'), '<a href="https://app.convertkit.com/account_settings/advanced_settings" target="_blank">', '</a>'); ?></p>
            </div>
        </div>
        <div class="std-field-wrap">
            <label><?php esc_html_e('Status', 'subscribe-to-download'); ?></label>
            <div class="std-field">
                <?php
                $convertkit_status = (!empty($std_settings['convertkit']['status'])) ? 1 : 0;
                $convertkit_status_class = ($convertkit_status == 1) ? 'std-connected' : 'std-disconnected';
                ?>
                <input type="hidden" name="std_settings[convertkit][status]" value="<?php echo intval($convertkit_status); ?>" class="std-convertkit-status-flag" />

                <span class="std-convertkit-status std-connection-status <?php echo esc_attr($convertkit_status_class); ?>"><?php echo ($convertkit_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($convertkit_status)) ? esc_html__('Renew List', 'subscribe-to-download') : esc_html__('Connect', 'subscribe-to-download');
                ?>
                <input type="button" class="std-convertkit-connect button-secondary" value="<?php echo esc_attr($connect_label); ?>">
                <?php if ($convertkit_status == 1) {
                ?>
                    <input type="button" class="std-convertkit-reset button-secondary" value="<?php esc_html_e("Reset", 'subscribe-to-download'); ?>">
                <?php
                }
                ?>
                <div class="std-convertkit-api-response" style="display:none;"></div>
            </div>
        </div>


        <div class="std-field-wrap">
            <label><?php esc_html_e('Convertkit Forms', 'subscribe-to-download'); ?></label>
            <div class="std-field">
                <div class="std-convertkit-lists-wrap">
                    <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('Form Name', 'subscribe-to-download'); ?></span>
                        </div>
                        <?php
                        if (!empty($std_settings['convertkit']['convertkit_lists'])) {
                            $convertkit_lists = json_decode(rtrim($std_settings['convertkit']['convertkit_lists']));

                        ?>
                            <?php
                            foreach ($convertkit_lists->forms as $convertkit_list) {
                            ?>
                                <div class="std-each-list">
                                    <span class="std-list-col"><?php echo esc_attr($convertkit_list->id); ?></span>
                                    <span class="std-list-col"><?php echo esc_attr($convertkit_list->name); ?></span>
                                </div>
                            <?php } ?>
                        <?php
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>


        <textarea name="std_settings[convertkit][convertkit_lists]" class="std-convertkit-lists-input" style="display:none;"><?php echo (!empty($std_settings['convertkit']['convertkit_lists'])) ? rtrim($std_settings['convertkit']['convertkit_lists']) : ''; ?></textarea>
        <div class="std-field-wrap">
            <label><?php esc_html_e('Last Log', 'subscribe-to-download'); ?></label>
            <div class="std-field">
                <textarea name="std_settings[convertkit][ck_log]"><?php echo (!empty($std_settings['convertkit']['ck_log'])) ? $this->sanitize_html($std_settings['convertkit']['ck_log']) : ''; ?></textarea>
                <input type="button" class="std-clear-log-trigger button-secondary" value="<?php esc_html_e('Clear Log', 'subscribe-to-download'); ?>">
            </div>
        </div>
    </div>
</div>