<?php
defined('ABSPATH') or die('No script kiddies please!!');
?>
<div class="wrap std-wrap">
    <div class="std-header std-clearfix">
        <h1 class="std-floatLeft">
            <img src="<?php echo STD_URL . 'images/logo.png' ?>" class="std-plugin-logo" />
            <span class="std-sub-header"><?php esc_html_e('Subscribers', 'subscribe-to-download'); ?></span>
        </h1>

        <div class="std-add-wrap">
            <?php
            $form_alias = (!empty($_GET['form_alias'])) ? sanitize_text_field($_GET['form_alias']) : '';
            $keyword = (!empty($_GET['keyword'])) ? sanitize_text_field($_GET['keyword']) : '';
            $export_url = admin_url('admin-post.php?action=std_export_csv&form_alias=' . $form_alias . '&keyword=' . $keyword . '&_wpnonce=' . wp_create_nonce('std_export_csv_nonce'));
            ?>
            <a href="<?php echo esc_url($export_url); ?>"><input type="button" class="std-button-orange" value="<?php esc_html_e('Export to CSV', 'subscribe-to-download'); ?>"></a>

        </div>
    </div>
    <div class="std-form-wrap">
        <div class="std-export-wrap std-clearfix">
            <form method="get" action="<?php echo admin_url('admin.php'); ?>" class="std-subscriber-export-form">
                <input type="hidden" name="page" value="std-subscribers" />
                <select name="form_alias" class="std-export-alias-trigger">
                    <option value=""><?php esc_html_e('All', 'subscribe-to-download'); ?></option>
                    <?php
                    global $wpdb;
                    $form_table = STD_FORM_TABLE;
                    $form_rows = $wpdb->get_results("select * from $form_table order by form_title asc");
                    $selected_form_alias = (!empty($_GET['form_alias'])) ? sanitize_text_field($_GET['form_alias']) : '';
                    if (!empty($form_rows)) {
                        foreach ($form_rows as $form_row) {
                    ?>
                            <option value="<?php echo esc_attr($form_row->form_alias); ?>" <?php selected($selected_form_alias, $form_row->form_alias); ?>><?php echo esc_attr($form_row->form_title); ?></option>
                    <?php
                        }
                    }
                    ?>
                </select>
                <div class="std-subscriber-search-wrap">
                    <input type="search" name="keyword" placeholder="<?php esc_html_e('Keyword', 'subscribe-to-download'); ?>" value="<?php echo (!empty($_GET['keyword'])) ? esc_attr($_GET['keyword']) : ''; ?>" />
                    <span class="dashicons dashicons-search"></span>
                </div>
                <p class="description"><?php esc_html_e('Please select a form and filter subscribers before exporting to CSV if you want all the custom fields information in the CSV file', 'subscribe-to-download') ?></p>
            </form>

            <?php
            global $wpdb;
            $subscriber_table = STD_SUBSCRIBERS_TABLE;
            $form_table = STD_FORM_TABLE;
            $current_page = (!empty($_GET['paged'])) ? intval($_GET['paged']) : 1;
            $limit = 20;
            $offset = ($current_page - 1) * $limit;
            if (!empty($_GET['form_alias'])) {
                $form_alias = sanitize_text_field($_GET['form_alias']);
                if (!empty($_GET['keyword'])) {
                    $keyword = sanitize_text_field($_GET['keyword']);
                    $subscriber_query = $wpdb->prepare("select * from $subscriber_table inner join $form_table on $subscriber_table.subscriber_form_alias = $form_table.form_alias where subscriber_form_alias = %s and (subscriber_name like %s or subscriber_email like %s) order by subscriber_id desc limit $offset,$limit", $form_alias, '%' . $keyword . '%', '%' . $keyword . '%');
                    $total_subscriber_query = $wpdb->prepare("select count(*) from $subscriber_table inner join $form_table on $subscriber_table.subscriber_form_alias = $form_table.form_alias where subscriber_form_alias = %s and (subscriber_name like %s or subscriber_email like %s) order by subscriber_id desc", $form_alias, '%' . $keyword . '%', '%' . $keyword . '%');
                } else {
                    $subscriber_query = $wpdb->prepare("select * from $subscriber_table inner join $form_table on $subscriber_table.subscriber_form_alias = $form_table.form_alias where subscriber_form_alias = %s order by subscriber_id desc limit $offset,$limit", $form_alias);
                    $total_subscriber_query = $wpdb->prepare("select count(*) from $subscriber_table inner join $form_table on $subscriber_table.subscriber_form_alias = $form_table.form_alias where subscriber_form_alias = %s order by subscriber_id desc", $form_alias);
                }
            } else {
                if (!empty($_GET['keyword'])) {
                    $keyword = sanitize_text_field($_GET['keyword']);
                    $subscriber_query = $wpdb->prepare("select * from $subscriber_table inner join $form_table on $subscriber_table.subscriber_form_alias = $form_table.form_alias where (subscriber_name like %s or subscriber_email like %s) limit $offset,$limit", '%' . $keyword . '%', '%' . $keyword . '%');
                    $total_subscriber_query = $wpdb->prepare("select count(*) from $subscriber_table inner join $form_table on $subscriber_table.subscriber_form_alias = $form_table.form_alias where (subscriber_name like %s or subscriber_email like %s)", '%' . $keyword . '%', '%' . $keyword . '%');
                } else {
                    $subscriber_query = "select * from $subscriber_table inner join $form_table on $subscriber_table.subscriber_form_alias = $form_table.form_alias order by subscriber_id desc limit $offset,$limit";
                    $total_subscriber_query = "select count(*) from $subscriber_table inner join $form_table on $subscriber_table.subscriber_form_alias = $form_table.form_alias";
                }
            }
            $subscriber_rows = $wpdb->get_results($subscriber_query);
            $subscriber_total_rows = $wpdb->get_var($total_subscriber_query);
            if (!empty($subscriber_rows)) {
                $total_pages = ceil($subscriber_total_rows / $limit);

                $big = 999999999; // need an unlikely integer
                $keyword = (!empty($_GET['keyword'])) ? sanitize_text_field($_GET['keyword']) : '';
                $form_alias = (!empty($_GET['form_alias'])) ? sanitize_text_field($_GET['form_alias']) : '';
                echo '<div class="std-subscriber-pagination-wrap">';
                echo paginate_links(array(
                    'base' => str_replace($big, '%#%', get_pagenum_link($big, false)),
                    'format' => '?paged=%#%',
                    'current' => $current_page,
                    'total' => $total_pages,
                    'next_text' => esc_html__('>>', 'subscribe-to-download'),
                    'prev_text' => esc_html__('<<', 'subscribe-to-download'),
                    'add_args' => array('page' => 'std-subscribers', 'keyword' => $keyword, 'form_alias' => $form_alias)
                ));
                echo '</div>';
            }
            ?>
        </div>
        <table class="wp-list-table widefat fixed std-form-lists-table">
            <thead>
                <tr>
                    <th><?php esc_html_e('Subscriber Name', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('Subscriber Email', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('GDPR Agreement', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('Form', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('Download Status', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('User IP', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('Subscribed Date', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('Action', 'subscribe-to-download'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php
                if (!empty($subscriber_rows)) {
                    foreach ($subscriber_rows as $subscriber_row) {
                        switch ($subscriber_row->subscriber_gdpr_agreement) {
                            case 0:
                                $std_gdpr_agreement = __('No', 'subscribe-to-download');
                                break;
                            case 1:
                                $std_gdpr_agreement = __('Yes', 'subscribe-to-download');
                                break;
                            case 2:
                                $std_gdpr_agreement = __('Unavailable', 'subscribe-to-download');
                                break;
                            default:
                                $std_gdpr_agreement = __('Unavailable', 'subscribe-to-download');

                                break;
                        }
                ?>
                        <tr>
                            <td><?php echo esc_html($subscriber_row->subscriber_name); ?></td>
                            <td><?php echo esc_html($subscriber_row->subscriber_email); ?></td>
                            <td><?php echo esc_html($std_gdpr_agreement); ?></td>
                            <td><a href="<?php echo admin_url('admin.php?page=subscribe-to-download&form_id=' . $subscriber_row->form_id . '&action=edit_form'); ?>" target="_blank"><?php echo esc_html($subscriber_row->form_title); ?></a></td>
                            <td><?php echo (!empty($subscriber_row->subscriber_download_status)) ? esc_html__('Yes', 'subscribe-to-download') : esc_html__('No', 'subscribe-to-download'); ?></td>
                            <td><?php echo (!empty($subscriber_row->subscribed_user_ip)) ? esc_html($subscriber_row->subscribed_user_ip) : ''; ?></td>
                            <td><?php echo (!empty($subscriber_row->subscription_date)) ? esc_html($subscriber_row->subscription_date) : ''; ?></td>
                            <td>
                                <a href="<?php echo admin_url('admin.php?page=std-subscribers&action=view_subscriber&subscriber_id=' . $subscriber_row->subscriber_id); ?>" class="std-view-subscriber" title="<?php echo esc_html__('View Subscriber', 'subscribe-to-download'); ?>"><span class="dashicons dashicons-visibility"></span></a>
                                <a class="std-delete std-subscriber-delete" href="javascript:void(0)" data-subscriber-id="<?php echo intval($subscriber_row->subscriber_id); ?>" title="<?php esc_html_e('Delete Subscriber', 'subscribe-to-download'); ?>"><?php esc_html_e('Delete', 'subscribe-to-download'); ?></a>
                                <span class="std-unsubscribe-link-wrap">
                                    <a class="std-unsubscribe-link" href="javascript:void(0)" title="<?php esc_html_e('Copy unsubscribe link', 'subscribe-to-download'); ?>"><span class="dashicons dashicons-database-remove"></span></a>
                                    <span class="std-unsubscribe-link-ref std-display-none" style="display:none;"><?php echo home_url() . '/?std_action=unsubscribe&unsubscribe_key=' . $subscriber_row->subscriber_encryption_key; ?></span>
                                </span>
                            </td>

                        </tr>
                    <?php
                    }
                } else {
                    ?>
                    <tr>
                        <td colspan="6"><?php esc_html_e('No subscribers found.', 'subscribe-to-download'); ?></td>
                    </tr>
                <?php
                }
                ?>
            </tbody>
            <tfoot>
                <tr>
                    <th><?php esc_html_e('Subscriber Name', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('Subscriber Email', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('GDPR Agreement', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('Form', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('Download Status', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('User IP', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('Subscribed Date', 'subscribe-to-download'); ?></th>
                    <th><?php esc_html_e('Action', 'subscribe-to-download'); ?></th>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
<div class="std-form-message"></div>