<?php
$pageview_totals = array(
    'filter'     => array('allowed' => 0, 'blocked' => 0, 'total' => 0),
    'detail'     => array('allowed' => 0, 'blocked' => 0, 'total' => 0),
    'detail_pdf' => array('allowed' => 0, 'blocked' => 0, 'total' => 0),
);
$contactform_offerte = 0;
$price_structure_requests_count = 0;

if (isset($pageviews)) {
    foreach ($pageviews as $pageview) {
        $action = isset($pageview->user_action) ? $pageview->user_action : '';
        if (!isset($pageview_totals[$action])) {
            continue;
        }

        $status_key = (isset($pageview->blocked) && (int) $pageview->blocked === 1) ? 'blocked' : 'allowed';
        $pageview_totals[$action][$status_key] = intval($pageview->visits);
        $pageview_totals[$action]['total'] = (int) $pageview_totals[$action]['allowed'] + (int) $pageview_totals[$action]['blocked'];
    }
}

if (isset($contact_submissions)) {
    foreach ($contact_submissions as $contact_submission) {
        $contactform_offerte = intval($contact_submission->visits);
    }
}

if (isset($price_structure_requests)) {
    foreach ($price_structure_requests as $price_structure_request) {
        $price_structure_requests_count += intval($price_structure_request->visits);
    }
}

$total_pageviews = 0;
$total_allowed_pageviews = 0;
$total_blocked_pageviews = 0;
foreach ($pageview_totals as $pageview_total) {
    $total_pageviews += (int) $pageview_total['total'];
    $total_allowed_pageviews += (int) $pageview_total['allowed'];
    $total_blocked_pageviews += (int) $pageview_total['blocked'];
}

$export_url = wp_nonce_url(
    add_query_arg(
        array(
            'action' => 'eurostocks_export_analytics',
            'analytics_period' => $analytics_period,
        ),
        admin_url('admin-post.php')
    ),
    'eurostocks_export_analytics'
);

?>

<div id="eurostocks-analytics">
    <div class="euro-flex euro-justify-between">
        <div>
            <h1><?php esc_html_e("Plugin statistics", "eurostocks"); ?></h1>
            <p><?php esc_html_e("This page provides insight into the use of the import module. Due to our smart caching technique, not every view will count towards the budget limit.", "eurostocks"); ?></p>
        </div>
        <div>
            <select id="analytics_period" name="analytics_period">
                <option disabled><?php esc_html_e("Select a period"); ?></option>
                <option <?php echo ($analytics_period == 'today')?"selected ":""; ?> value="today"><?php esc_html_e("Today", "eurostocks"); ?></option>
                <option <?php echo ($analytics_period == 'yesterday')?"selected ":""; ?>value="yesterday"><?php esc_html_e("Yesterday", "eurostocks"); ?></option>
                <option <?php echo ($analytics_period == 'week')?"selected ":""; ?>value="week"><?php esc_html_e("Past 7 days", "eurostocks"); ?></option>
                <option <?php echo ($analytics_period == 'month')?"selected ":""; ?>value="month"><?php esc_html_e("Past 30 days", "eurostocks"); ?></option>
                <option <?php echo ($analytics_period == 'year')?"selected ":""; ?>value="year"><?php esc_html_e("Past Year", "eurostocks"); ?></option>
                <option <?php echo ($analytics_period == 'all')?"selected ":""; ?>value="all"><?php esc_html_e("All data", "eurostocks"); ?></option>
            </select>
            <a class="button button-primary" href="<?php echo esc_url($export_url); ?>">
                <?php esc_html_e("Export CSV", "eurostocks"); ?>
            </a>
        </div>
    </div>
    <div id="eurostocks-analytics-header">
        <div class="euro-flex">
            <div>
                <h2><?php echo esc_html($total_pageviews); ?></h2>
                <span><?php esc_html_e("Pageviews", "eurostocks"); ?></span>
                <p class="description"><?php printf(esc_html__('Allowed: %1$d / Blocked: %2$d', 'eurostocks'), $total_allowed_pageviews, $total_blocked_pageviews); ?></p>
            </div>

            <div>
                <h2><?php echo esc_html($contactform_offerte); ?></h2>
                <span><?php esc_html_e("Contact form submissions", "eurostocks"); ?></span>
            </div>

            <div>
                <h2><?php echo esc_html($price_structure_requests_count); ?></h2>
                <span><?php esc_html_e("Price breakdown requests", "eurostocks"); ?></span>
            </div>
        </div>
       
    </div>

    <div>
        <h2><?php esc_html_e("Views"); ?></h2>
        <table class="wp-list-table widefat fixed striped">
            <thead>
                <tr>
                    <th><?php esc_html_e("Page", "eurostocks"); ?></th>
                    <th><?php esc_html_e("Allowed", "eurostocks"); ?></th>
                    <th><?php esc_html_e("Blocked", "eurostocks"); ?></th>
                    <th><?php esc_html_e("Total", "eurostocks"); ?></th>
                </tr>
            </thead>
            <tbody>
               <tr>
                <td><?php esc_html_e("Filter page", "eurostocks"); ?></td>
                <td><?php echo esc_html($pageview_totals['filter']['allowed']); ?></td>
                <td><?php echo esc_html($pageview_totals['filter']['blocked']); ?></td>
                <td><?php echo esc_html($pageview_totals['filter']['total']); ?></td>
               </tr>
               <tr>
                <td><?php esc_html_e("Detail page", "eurostocks"); ?></td>
                <td><?php echo esc_html($pageview_totals['detail']['allowed']); ?></td>
                <td><?php echo esc_html($pageview_totals['detail']['blocked']); ?></td>
                <td><?php echo esc_html($pageview_totals['detail']['total']); ?></td>
               </tr>
               <tr>
                <td><?php esc_html_e("PDF detail page", "eurostocks"); ?></td>
                <td><?php echo esc_html($pageview_totals['detail_pdf']['allowed']); ?></td>
                <td><?php echo esc_html($pageview_totals['detail_pdf']['blocked']); ?></td>
                <td><?php echo esc_html($pageview_totals['detail_pdf']['total']); ?></td>
               </tr>
            </tbody>
        </table>
    </div>


    <div>
        <h2><?php esc_html_e("Contact form submissions", "eurostocks"); ?></h2>
        <table class="wp-list-table widefat fixed striped">
            <thead>
                <tr>
                    <th><?php esc_html_e("Form", "eurostocks"); ?></th>
                    <th><?php esc_html_e("Submissions", "eurostocks"); ?></th>
                </tr>
            </thead>
            <tbody>
               <tr>
                <td><?php esc_html_e("Quote requests", "eurostocks"); ?></td>
                <td><?php echo esc_html($contactform_offerte); ?></td>
               </tr>
            </tbody>
        </table>
    </div>

    <div>
        <h2><?php esc_html_e("Price breakdown requests", "eurostocks"); ?></h2>
        <table class="wp-list-table widefat fixed striped">
            <thead>
                <tr>
                    <th><?php esc_html_e("Request type", "eurostocks"); ?></th>
                    <th><?php esc_html_e("Requests", "eurostocks"); ?></th>
                </tr>
            </thead>
            <tbody>
               <tr>
                <td><?php esc_html_e("Price breakdown requests", "eurostocks"); ?></td>
                <td><?php echo esc_html($price_structure_requests_count); ?></td>
               </tr>
            </tbody>
        </table>
    </div>

</div>
