<?php
namespace ONLINE_ADA;

$all_invalid = get_option("_oadaas_invalid_images_list");
$remaining_invalid_items = 0;

if( !empty($all_invalid) ){
    list($invalid_image_ids, $remaining_invalid_items) = paginate_items($all_invalid);
    $invalid_images = [];
    foreach ($invalid_image_ids as $id) {
        $invalid_images[$id] = $all_invalid[$id];
    }
}

$all_valid = get_option("_oadaas_valid_images_list");
$remaining_valid_items = 0;
if( !empty($all_valid) ){
    list($valid_image_ids, $remaining_valid_items) = paginate_items($all_valid);
    $valid_images = [];
    foreach ($valid_image_ids as $id) {
        $valid_images[$id] = $all_valid[$id];
    }
}

/**
 * Generates links for pagination out of an array of items.
 *
 * @param $post_id
 * @param $items
 *
 * @return array
 */
function paginate_items($items)
{
    $items = array_keys($items);
    $num_items = count($items);
    $per_page = 10;
    $num_pages = ceil($num_items / $per_page);

    $page = isset($_REQUEST['wcag_page']) ? (int)$_REQUEST['wcag_page'] : null;
    if ($page === null) $page = get_option('wcag_page');
    if ($page < 1) $page = 1;
    if ($page > $num_pages) $page = $num_pages;

    update_option('wcag_page', $page);

    $paged_items = array();
    $start_item = ($page - 1) * $per_page; // (1-1) * 20 = 0... (2-1) * 20 = 20
    $end_item = $start_item + $per_page;

    for ($i = $start_item; $i < $end_item; $i++) {
        if (!isset($items[$i])) break;
        $paged_items[] = $items[$i];
    }

    $remaining_items = $num_items - count($paged_items);

    return array(
        $paged_items,
        $remaining_items
    );
}

?>



<div class="options-wrap ll-col-12">
    <span>
        <label class="switch">
            <input id="show-valid" type="checkbox">
            <span class="slider round"></span>
            <div class="text">Show valid images</div>
        </label>
    </span>
</div>
<div class="content-wrap ll-col-12">
    <div class="title">
        <h4>Review Images</h4>
    </div>
    <div class="body">
        <!-- Invalid Images table -->
        <div class="table-header">Invalid images</div>
        <?php if( !empty($invalid_images) ): ?>
            <table class="ll-col-12 ll-d-sm-flex ll-flex-sm-column" id="invalid-images">
                <thead>
                    <tr class="ll-no-gutters ll-row">
                        <th class="preview ll-col-lg-2">Preview</th>
                        <th class="info ll-col-lg-6">Information</th>
                        <th class="alt ll-col-lg-4">Edit alt text</th>
                    </tr>
                </thead>
                <tbody>
                    <?php include("partials/review-invalid.php"); ?>
                </tbody>
            </table>

            <div class="continue ll-col-12">
                <div class="button-row">
                    <input type="button" class="button button-primary invalid-images" value="Save and continue">
                </div>
                <?php if ($remaining_invalid_items > 0) : ?>
                    <p class="remaining-count">
                        <?php echo wp_kses(sprintf(_n("There is %s more item to review.", "There are %s more items to review.", $remaining_invalid_items), $remaining_invalid_items),wp_kses_allowed_html()); ?>
                    </p>
                <?php else : ?>
                    <p class="remaining-count remaining-count-final">This is the last page of invalid images to review.</p>
                <?php endif; ?>
            </div>
        <?php else: ?>
            <p>No invalid images were found</p>
        <?php endif; ?>
        <!-- Valid images table -->
        <div class="valid-images-wrapper">
            <div class="table-header">Valid images</div>
            <?php if (!empty($valid_images)) : ?>
                <table class="ll-col-12 ll-d-sm-flex ll-flex-sm-column" id="valid-images">
                    <thead>
                        <tr class="ll-no-gutters ll-row">
                            <th class="preview ll-col-lg-2">Preview</th>
                            <th class="info ll-col-lg-6">Information</th>
                            <th class="alt ll-col-lg-4">Edit alt text</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php include("partials/review-valid.php"); ?>
                    </tbody>
                </table>

                <div class="continue ll-col-12">
                    <div class="button-row">
                        <input type="button" class="button button-primary valid-images" value="Save and continue">
                    </div>
                    <?php if ($remaining_invalid_items > 0) : ?>
                        <p class="remaining-count">
                            <?php echo wp_kses(sprintf(_n("There is %s more item to review.", "There are %s more items to review.", $remaining_invalid_items), $remaining_invalid_items),wp_kses_allowed_html()); ?>
                        </p>
                    <?php else : ?>
                        <p class="remaining-count remaining-count-final">This is the last page of invalid images to review.</p>
                    <?php endif; ?>
                </div>

            <?php else : ?>
                <p class="no-images-found">No valid images were found</p>
            <?php endif; ?>
        </div>
    </div>
</div>


<?php
