<?php
namespace ONLINE_ADAv4;

if (!defined('ABSPATH')) exit;

$screen = get_current_screen();
if( $screen->id != "wcag_scan" ){ return; }

$cbr_report = get_option("_oadaas_cbr_paths")["pub_path"];
$pages_scanned = get_post_meta($_GET["post"], "_oadaas_pages_scanned", true);

if (empty($cbr_report)) {
    $cbr_report = false;
}

$full_url = Plugin::$instance->info["home_url"];
$trimmed = preg_replace('/^(http[s]?:[\/]{0,2})?/', "", $full_url);

$get_snapshot = false;
$snapshot = wp_upload_dir()["basedir"] . "/oadaas/snapshot.png";

if ( !file_exists($snapshot) || !is_file($snapshot) ){
    $get_snapshot = true;
}

if ( file_exists($snapshot) && is_file($snapshot) ){
    $snapshot_url = wp_upload_dir()["baseurl"] . "/oadaas/snapshot.png";
}

$sitemap = Helper::get_sitemap();
if ( empty($sitemap) || !$sitemap ){$sitemap = [];}

if( !empty($sitemap) ){
    $nq_pages = !empty($sitemap["non_query_links"]) ? count($sitemap["non_query_links"]) : 0;
    $q_pages = !empty($sitemap["query_links"]) ? count($sitemap["query_links"]) : 0;
}

$license = get_option("accessibility-suite-pro-license");

$icons = [
    "error" => Plugin::$instance->info["url"] . "/public/img/error-icon.png",
    "contrast" => Plugin::$instance->info["url"] . "/public/img/contrast-icon.png",
    "alert" => Plugin::$instance->info["url"] . "/public/img/warning-icon.png",
    "star" => Plugin::$instance->info["url"] . "/public/img/star.svg",
    "tooltip" => Plugin::$instance->info["url"] . "/public/img/tooltip_arrow.png"
];

$scan_completed = get_post_meta($_GET["post"], "_oadaas_scan-completion-date", true);
$option_cutoff = get_option("oadaas_updates_array");
$old_scan = false;
if(isset($option_cutoff["2.2.0"])){
    $cutoff_for_old_scan = $option_cutoff["2.2.0"]["scan_cutoff_date"];
    $old_scan = $scan_completed < $cutoff_for_old_scan;
}


wp_localize_script( "ada_plugin_scan_reports_script", "report_vars", [
    "wcag" => wp_json_encode(["guidelines" => Helper::getWcagArticlesModel(1, true), "articles" => Helper::getWcagArticlesModel(2)]),
    "scan_id" => $_GET["post"],
    "site_url" => Plugin::$instance->info["site_url"],
    "home_url" => Plugin::$instance->info["home_url"],
    "get_snapshot" => $get_snapshot,
    "icons" => $icons,
    "total_pages" => count($pages_scanned),
    "license" => $license,
    "items_per_page_01" => get_option("_oadaas_items_per_page_01", 20),
    "items_per_page_02" => get_option("_oadaas_items_per_page_02", 5),
    "old_scan" => $old_scan
] );

?>
<?php if($old_scan): ?>
    <div class="report-block" style="padding-bottom:95px;">
        <!-- Section 1 -->
        <div class="title-section section ll-row">
            <div class="ll-col-lg-9">
                <div class="logo-wrap">
                    <img class="logo" src="<?php echo esc_url(Plugin::$instance->info["url"]) . "/admin/assets/images/ada-logo-h-solid.svg"; ?>" alt="Ability, Inc Logo"></div>
                <div><h1 class="header">ACCESSIBILITY COMPLIANCE</h1></div>
            </div>
            <div class="date-wrap ll-col-lg-3">
            Scan completed: <?php echo wp_kses(gmdate("F jS, Y", $scan_completed),wp_kses_allowed_html()); ?>
            </div>
            <div class="new-scan-container">
                Version 2.2.0 is installed! This new update has overhauled the audit and brings exciting new capabilities! See the new audit in action by starting a <a class="new-scan-button" href=<?php echo "'" . esc_url(home_url("/wp-admin/post-new.php?post_type=wcag_scan")) . "'"; ?> role="button">new audit</a> now!
            </div>
            <div class="scan-message-container">
                <div>
                    Due to the increased complexity of the new audit features, audits created before <?php echo wp_kses(gmdate("F jS, Y g:ia e", $cutoff_for_old_scan),wp_kses_allowed_html()); ?> are no longer supported from version 2.2.0 and later. We encourage you to run a new audit and see all of the new updates and features the audit is now capable of but if you need to see these audit results you will need to install version 2.1.2 of the Accessibility Suite by Ability, Inc plugin from the wordpress repository found <a target="_blank" href="https://wordpress.org/plugins/online-accessibility/advanced/">here</a>. Version 2.1.2 can be found by going to the "Previous Versions" section at the bottom of the page, selecting 2.1.2 from the dropdown, and clicking "Download" to the right of the dropdown.
                </div>
            </div>
        </div>
    </div>
<?php else: ?>
<div class="report-block" style="padding-bottom:95px;">
    <!-- Section 1 -->
    <div class="title-section section ll-row">
        <div class="ll-col-lg-9">
            <div class="logo-wrap">
                <img class="logo" src="<?php echo esc_url(Plugin::$instance->info["url"]) . "/admin/assets/images/ada-logo-h-solid.svg"; ?>" alt="Ability, Inc Logo"></div>
            <div><h1 class="header">ACCESSIBILITY COMPLIANCE</h1></div>
        </div>
        <div class="date-wrap ll-col-lg-3">
        Audit completed: <?php echo wp_kses(gmdate("F jS, Y", $scan_completed),wp_kses_allowed_html()); ?>
        </div>
    </div>
    <div class="section-divider summary">
        <span class="left">Summary</span>
        <span class="right"></span>
    </div>
    <!-- Section 2 -->
    <div class="summary-section section ll-row">
        <div class="left ll-col-lg-4 ll-col-xl-3">
            <img class="js-summary-img"
                src="<?php echo !empty($snapshot_url) ? esc_attr($snapshot_url) : esc_attr(Plugin::$instance->info["url"] . "/public/img/placeholder-600x400.png"); ?>"
                alt="<?php echo esc_attr($trimmed) . ' homepage' ?>"
            >
        </div>
        <div class="right ll-col-lg-8 ll-col-xl-9">
            <div class="star-row">

            </div>
            <div class="score-row">
                Scored <span class="global"></span> out of 5 stars for General Compliance
            </div>
            <div class="description-row">
                Congratulations on taking the necessary steps to make your website accessibility compliant and make the internet a better place for everyone.
            </div>
            <div class="domain-row">
                <?php echo wp_kses($trimmed,wp_kses_allowed_html()); ?>

            </div>
        </div>
    </div>
    <div class="spacer-50"></div>
    <div class="ll-row section">
        <div class="ll-col-xl-3 ll-col-lg-4">
            <div class="pages-scanned-block">
                <div class="header"></div>
                <div class="sub-header">NONCOMPLIANT PAGES:</div>

            </div>
        </div>
        <div class="ll-col-xl-9 ll-col-lg-8">
            <div class="ll-row scores-row">

            </div>
        </div>
    </div>
    <div class="section-divider reports">
        <span class="left">Detailed Reports</span>
        <span class="right"></span>
    </div>
    <div class="section-divider errors-found">
        <div class="left">Errors Found</div>
        <div class="right">
            <div class="csv-wrap" style="text-align:center;">
                <div class="top">Download Your Report</div>
                <div class="bottom ll-d-flex ll-justify-content-center">
                    <button class="ll-d-flex ll-align-items-center csv">
                        <span class="wcag-icon">
                            <img alt="" src="<?php echo esc_url(Plugin::$instance->info["url"]) . "/public/img/download-icon-white.png"; ?>" >
                        </span>
                        <span class="text">CSV REPORT</span>
                    </button>
                </div>
            </div>
            <?php if ( $cbr_report ): ?>
            <div class="text-center">
                <div class="img-wrap">
                    <img src="<?php echo esc_url(Plugin::$instance->info["url"]) . "/public/img/eye-con.png" ?>" alt="" >
                </div>
                <div class="sub-header">Color Blindness</div>
                <a style="display:block" target="blank" href="<?php echo wp_kses($cbr_report,wp_kses_allowed_html()); ?>">
                    <div class="pdf-viewer">
                        <span class="left"><img src="<?php echo esc_url(Plugin::$instance->info["url"]) . "/public/img/download-icon-white.png" ?>" alt="" ></span>
                        <span class="right">PDF Report</span>
                    </div>
                </a>
            </div>
            <?php endif; ?>
        </div>
    </div>
    <div class="section reports">
        <div class="tabs">
            <div data-tab-controls="1" class="tab tab-1 active">VIEW BY PAGE</div>
            <div data-tab-controls="2" class="tab tab-2">VIEW BY ISSUE</div>
            <div data-tab-controls="3" class="tab tab-3">FALSE POSITIVES</div>
        </div>
        <!-- FILTERS -->
        <div class="filter-wrap">
            <div class="filters-row ll-row ll-no-gutters">
                <div class="ll-flex-grow-1">
                    <div class="filter">
                        <label>
                            <input type="checkbox" class="invisible" @click="toggle_flag('a')"
                            data-filter="flag-a"
                            checked="checked">
                            <div class="checkbox checked"></div>
                            <span class="wcag-icon">A</span>
                            <span>WCAG Level A</span>
                        </label>
                    </div>
                </div>
                <div class="ll-flex-grow-1">
                    <div class="filter">
                        <label>
                            <input type="checkbox" class="invisible" @click="toggle_flag('aa')" 
                            data-filter="flag-aa"
                            checked="checked">
                            <div class="checkbox checked"></div>
                            <span class="wcag-icon">AA</span>
                            <span>WCAG Level AA</span>
                        </label>
                    </div>
                </div>
                <div class="ll-flex-grow-1">
                    <div class="filter">
                        <label>
                            <input type="checkbox" class="invisible" @click="toggle_flag('s508')" 
                            data-filter="flag-s508"
                            checked="checked">
                            <div class="checkbox checked"></div>
                            <span class="wcag-icon">508</span>
                            <span>Section 508</span>
                        </label>
                    </div>
                </div>
                <div class="ll-flex-grow-1">
                    <div class="filter">
                        <label>
                            <input type="checkbox" class="invisible" data-filter="flag-bp" @click="toggle_flag('bp')" checked="checked">
                            <div class="checkbox checked"></div>
                            <span class="wcag-icon">BP</span>
                            <span>Best Practice</span>
                        </label>
                    </div>
                </div>
                <div class="ll-flex-grow-1">
                    <div class="filter">
                        <label>
                            <input type="checkbox" class="invisible" @click="toggle_flag('contrast')" 
                            data-filter="flag-contrast"
                            checked="checked">
                            <div class="checkbox checked"></div>
                            <span class="wcag-icon no-bg">
                                <img alt="" role="presentation" src="<?php echo esc_attr(Plugin::$instance->info["url"]) . "/public/img/contrast-icon.png"; ?>" >
                            </span>
                            <span>Contrast</span>
                        </label>
                    </div>
                </div>
                <div class="ll-flex-grow-1">
                    <div class="filter darker">
                        <label>
                            <input type="checkbox" class="invisible" @click="toggle_flag('error')"
                            data-filter="flag-error" checked="checked">
                            <div class="checkbox checked"></div>
                            <span class="wcag-icon no-bg">
                                <img alt="" role="presentation" src="<?php echo esc_attr(Plugin::$instance->info["url"]) . "/public/img/error-icon.png"; ?>" >
                            </span>
                            <span>Errors</span>
                        </label>
                    </div>
                </div>
                <div class="ll-flex-grow-1">
                    <div class="filter darker">
                        <label>
                            <input type="checkbox" class="invisible" checked="checked" data-filter="flag-alert" @click="toggle_flag('alert')">
                            <div class="checkbox checked"></div>
                            <span class="wcag-icon no-bg">
                                <img alt="" role="presentation" src="<?php echo esc_attr(Plugin::$instance->info["url"]) . "/public/img/warning-icon.png"; ?>" >
                            </span>
                            <span>Alerts</span>
                        </label>
                    </div>
                </div>
            </div>
        </div>
        <!-- END FILTERS -->
        <div class="loader-container ll-d-flex ll-align-items-center ll-flex-column">
            <div style="font-size:30px;">Please wait while we retrieve your results</div>
            <div style="width:100px;margin-top:20px;">
                <li class="three-bounce">
                    <div class="one"></div>
                    <div class="two"></div>
                    <div class="three"></div>
                </li>
            </div>
        </div>
        <!-- REPORT BY PAGE-->
        <div data-tab="1" class="ll-tab-content-pl tab-1 active">
            <!-- Send tab1 to pagination -->
            <v-pagination :domain="domain" :parent_class="''" class="body ll-row ll-no-gutters" :pagelength="20" :pages="tab1" >
                <!-- Get back the working active page with title and "contents" as "slotProps" -->
                <template v-slot:default="paginationSlotProps">
                    <v-accordion :tab="1" :tab_title="paginationSlotProps.data.title" :tab_link="paginationSlotProps.link">
                        <template v-slot:body>
                            <v-accordion-body-tab1 :content="paginationSlotProps.data"></v-accordion-body-tab1>
                        </template>
                    </v-accordion>
                </template>
            </v-pagination>
        </div>

        <div data-tab="2" class="ll-tab-content-pl tab-2">
            <div class="body ll-row ll-no-gutters">
                <template v-for="( stuff, key1 ) in tab2filtered">
                    <v-accordion :tab="2" :tab_title="tab2_title(stuff)" :key="key1">
                        <template v-slot:body>
                            <v-accordion-body-tab2 :content="stuff.issue"></v-accordion-body-tab2>
                        </template>
                    </v-accordion>
                </template>
            </div>
        </div>

        <div data-tab="3" class="ll-tab-content-pl tab-3">
            <v-pagination :domain="report_data.domain" :parent_class="''" class="body ll-row ll-no-gutters" :pagelength="20" :pages="falsePosFiltered">
                <template v-slot:default="paginationSlotProps">
                    <v-accordion :tab="3" :tab_title="paginationSlotProps.data.title" :tab_link="paginationSlotProps.link">
                        <template v-slot:body>
                            <v-accordion-body-tab3 :content="paginationSlotProps.data"></v-accordion-body-tab3>
                        </template>
                    </v-accordion>
                </template>
            </v-pagination>
        </div>
    </div>
</div>

<?php endif; ?>