<?php

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

?>
<div class="wrap">
    <h1>Manage your sitemap</h1>
    <br>
    <div class="options-wrap">
        <button data-target="tab-1">Re-Create sitemap</button><button data-target="tab-2">Upload a sitemap</button><button data-target="tab-3">Review sitemap</button>
    </div>
    <div class="content-wrap disclaimer">
        <p>Note: Urls to media files will be omitted</p>
        <p>When regenerating the sitemap has finished, click "Review sitemap". Once you're happy with it, click save.</p>
    </div>
    <div class="content-wrap">
        <div class="tab active" id="tab-1">
            <button id="regenerate-sitemap">Regenerate</button>
            <span class="loader"></span>
            <span class="msg"></span>
        </div>
        <div class="tab" id="tab-2">
            <p style="font-size:16px;">Accepted file formats include .xml, .csv, .xls, .xlsx</p>
			<input aria-label="Upload File" type="file" name="fileUpload" id="fileUpload" accept=".xml,.csv" />
            <label for="fileUpload">Choose a file</label>
            <div class="output-wrapper">
                <div>Selected file: <span class="file"></span></div>
            </div>
            <br>
            <div style="display:flex;align-items:center;">
                <button class="save-upload">Save</button>
                <span class="loader"></span>
                <span class="msg"></span>
            </div>
        </div>
        <div class="tab" id="tab-3">
            <div class="bar">
                <label for="add">Add a page to audit:</label>
                <span><?php echo wp_kses(home_url() . "/",wp_kses_allowed_html()); ?></span>
                <input id="input-add" type="text" name="add" /><button id="button-add">Add</button>
            </div>
            <ul class="sitemap-list">
                <?php foreach( $sitemap AS $type => $val_arr ): ?>
                    <li class="query-type <?php echo esc_attr($type) ?>"><span><?php echo $type == "query_links"? "Query Links" : "Non Query Links"; ?></span></li>
                    <?php foreach($val_arr AS $link): ?>
                        <?php $error = false; ?>
                        <?php
                            if( ( strpos($link, "http://") === FALSE || strpos($link, "http://" ) !== 0 )
                                && ( strpos($link, "https://") === FALSE || strpos($link, "https://" ) !== 0) )
                            {
                                //Link is relative or anomaly.
                                $error = "Domain does not start with a protocol of http or https. This link will not be used.";
                            }else if( strpos($link, home_url()) === FALSE ){
                                //Link has protocol so it should contain the site domain. If it doesn't, then it is not for this domain
                                $error = "Domain does not match this site's domain. This link will not be used";
                            }
                        ?>

                        <li>
                            <div>
                                <?php if($error !== false){echo "<i class='fa fa-exclamation e_i'></i>";} ?>
                                <span><?php echo wp_kses(wp_strip_all_tags($link, true),wp_kses_allowed_html()); ?></span>
                                <button class="remove">remove</button>
                                <?php if($error !== false){echo "<span class='e_m'>".wp_kses($error,wp_kses_allowed_html())."</span>";} ?>
                            </div>
                        </li>
                    <?php endforeach; ?>
                <?php endforeach; ?>
            </ul>
            <div class="bar">
                <button id="save-sitemap" type="text" name="save-sitemap">Save sitemap</button>
                <span class="loader"></span>
                <span class="msg"></span>
            </div>
        </div>
    </div>
</div>
