<?php
defined('ABSPATH') or die('No script kiddies please!!');
global $wpdb;
$subscriber_id = intval($_REQUEST['subscriber_id']);
$subscriber_table = STD_SUBSCRIBERS_TABLE;
$form_table = STD_FORM_TABLE;
$subscriber_row = $wpdb->get_row($wpdb->prepare("select * from $subscriber_table inner join $form_table on $subscriber_table.subscriber_form_alias = $form_table.form_alias where subscriber_id = %d", $subscriber_id));
if (empty($subscriber_row)) {
    die(esc_html__('Invalid subscriber ID', 'subscribe-to-download'));
}
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;
}
$subscriber_custom_fields = maybe_unserialize($subscriber_row->subscriber_custom_fields);
$form_details = maybe_unserialize($subscriber_row->form_details);

?>
<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('Subscriber Detail', 'subscribe-to-download'); ?></span>
        </h1>
    </div>
    <div class="std-subscriber-detail-table">

        <table class="widefat">
            <tr>
                <td class="std-table-label"><?php esc_html_e('Subscriber Name', 'subscribe-to-download'); ?></td>
                <td><?php echo esc_html($subscriber_row->subscriber_name); ?></td>
            </tr>
            <tr>
                <td class="std-table-label"><?php esc_html_e('Subscriber Email', 'subscribe-to-download'); ?></td>

                <td><?php echo esc_html($subscriber_row->subscriber_email); ?></td>
            </tr>
            <?php
            if (!empty($form_details['form']['custom_fields'])) {
                foreach ($form_details['form']['custom_fields'] as $custom_field_key => $custom_field_details) {
                    if (!empty($custom_field_details['show'])) {
            ?>
                        <tr>
                            <td class="std-table-label"><?php echo esc_html($custom_field_details['label']); ?></td>
                            <td>
                                <?php
                                if (!empty($subscriber_custom_fields[$custom_field_key])) {

                                    if (is_array($subscriber_custom_fields[$custom_field_key])) {
                                        $custom_field_value = implode(', ', $subscriber_custom_fields[$custom_field_key]);
                                    } else {
                                        $custom_field_value = $subscriber_custom_fields[$custom_field_key];
                                    }
                                    echo esc_html($custom_field_value);
                                }
                                ?>
                            </td>
                        </tr>
            <?php
                    }
                }
            }
            ?>
            <tr>
                <td class="std-table-label"><?php esc_html_e('GDPR Agreement', 'subscribe-to-download'); ?></td>

                <td><?php echo esc_html($std_gdpr_agreement); ?></td>
            </tr>
            <tr>
                <td class="std-table-label"><?php esc_html_e('Form', 'subscribe-to-download'); ?></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>
            </tr>
            <tr>
                <td class="std-table-label"><?php esc_html_e('Download Status', 'subscribe-to-download'); ?></td>

                <td><?php echo (!empty($subscriber_row->subscriber_download_status)) ? esc_html__('Yes', 'subscribe-to-download') : esc_html__('No', 'subscribe-to-download'); ?></td>
            </tr>
            <tr>
                <td class="std-table-label"><?php esc_html_e('User IP', 'subscribe-to-download'); ?></td>
                <td><?php echo (!empty($subscriber_row->subscribed_user_ip)) ? esc_html($subscriber_row->subscribed_user_ip) : ''; ?></td>
            </tr>
            <tr>
                <td class="std-table-label"><?php esc_html_e('Subscribed Date', 'subscribe-to-download'); ?></td>
                <td><?php echo (!empty($subscriber_row->subscription_date)) ? esc_html($subscriber_row->subscription_date) : ''; ?></td>
            </tr>



        </table>
    </div>

</div>