<?php

// Retrieve all car listings created by the current user
$args = array(
    'post_type' => 'gocars_shortcode',
    'posts_per_page' => -1,
    'orderby' => isset($_GET['orderby']) ? $_GET['orderby'] : 'title',
    'order' => isset($_GET['order']) ? $_GET['order'] : 'ASC',
);
$listings = new WP_Query( $args );
?>
<div class="wrap">
    <h1 class="wp-heading-inline">Gocars Online Shortcodes</h1>
    <a href="<?php echo esc_url( admin_url( 'post-new.php?post_type=gocars_shortcode' ) ); ?>" class="page-title-action">Add New</a>
    <hr class="wp-header-end gc-header-end">

    <form method="post" action="<?php echo esc_url( admin_url( 'admin.php?page=gocars_online' ) ); ?>">
        <?php //wp_nonce_field( 'gocars_listings_page', 'gocars_listings_page_nonce' ); ?>
        <table id="gc-list-table" class="wp-list-table widefat striped">
            <thead>
                <tr>
                    <th><input type="checkbox" id="check-all"></th>
                    <th class="manage-column column-title column-primary sorted<?php echo ( isset($_GET['orderby']) && isset($_GET['order']) && $_GET['orderby'] == 'title' && $_GET['order'] == 'ASC') ? ' asc' : ' desc'?>">
                        <a href="<?php echo esc_url( add_query_arg( array('orderby' => 'title', 'order' => (isset($_GET['orderby']) && $_GET['orderby'] == 'title' && $_GET['order'] == 'ASC') ? 'DESC' : 'ASC'), admin_url( 'admin.php?page=gocars_online' ) ) ); ?>">
                            <span>Title</span><span class="sorting-indicator"></span>
                        </a>
                    </th>
                    <th class="manage-column column-date column-primary sorted<?php echo (isset($_GET['orderby']) && isset($_GET['order']) && $_GET['orderby'] == 'date' && $_GET['order'] == 'ASC') ? ' asc' : ' desc'?>">
                        <a href="<?php echo esc_url( add_query_arg( array('orderby' => 'date', 'order' => (isset($_GET['orderby']) && $_GET['orderby'] == 'date' && $_GET['order'] == 'ASC') ? 'DESC' : 'ASC'), admin_url( 'admin.php?page=gocars_online' ) ) ); ?>">
                            <span>Date</span><span class="sorting-indicator"></span>
                        </a>
                    </th>
                    <th>Shortcode</th>
                    <th>Actions</th>
                </tr>
            </thead>
    
            <tbody id="the-list">
                <?php if ( $listings->have_posts() ) : ?>
                    <?php while ( $listings->have_posts() ) : $listings->the_post(); ?>
                        <tr>
                            <th>
                                <input type="checkbox" name="listing[]" value="<?php echo esc_attr( get_the_ID() ); ?>">
                            </th>
                            <td>
                                <a href="<?php echo esc_url( get_edit_post_link() ); ?>"><?php the_title(); ?></a>
                            </td>
                            <td><?php echo esc_html( get_the_date() ); ?></td>
                            <td class="shortcode column-shortcode" data-colname="Shortcode">
                                <span class="shortcode">
                                    <input type="text" onfocus="this.select();" readonly="readonly" value='<?php echo esc_html( get_post_meta( get_the_ID(), 'shortcode', true ) ); ?>' class="large-text code">
                                </span>
                            </td>
                            <td>
                                <a href="<?php echo esc_url( get_edit_post_link() ); ?>">Edit</a>
                                |
                                <a href="<?php echo esc_url( get_delete_post_link() ); ?>" onclick="return confirm('Are you sure you want to delete this listing?')">Delete</a>
                            </td>

                        </tr>
                    <?php endwhile; wp_reset_postdata(); ?>
                <?php else: ?>

                    <tr>
                        <th>
                        </th>
                        <td>
                            No Shortcodes
                        </td>
                        <td></td>
                        <td></td>
                        <td></td>

                    </tr>

                <?php endif; ?>
            </tbody>
            <tfoot>
                <tr>
                    <th><input type="checkbox" id="check-all"></th>
                    <th class="manage-column column-title column-primary sorted<?php echo (isset($_GET['orderby']) && isset($_GET['order']) && $_GET['orderby'] == 'title' && $_GET['order'] == 'ASC') ? ' asc' : ' desc'?>">
                        <a href="<?php echo esc_url( add_query_arg( array('orderby' => 'title', 'order' => (isset($_GET['orderby']) && $_GET['orderby'] == 'title' && $_GET['order'] == 'ASC') ? 'DESC' : 'ASC'), admin_url( 'admin.php?page=gocars_online' ) ) ); ?>">
                            <span>Title</span><span class="sorting-indicator"></span>
                        </a>
                    </th>
                    <th class="manage-column column-date column-primary sorted<?php echo (isset($_GET['orderby']) && isset($_GET['order']) && $_GET['orderby'] == 'date' && $_GET['order'] == 'ASC') ? ' asc' : ' desc'?>">
                        <a href="<?php echo esc_url( add_query_arg( array('orderby' => 'date', 'order' => (isset($_GET['orderby']) && $_GET['orderby'] == 'date' && $_GET['order'] == 'ASC') ? 'DESC' : 'ASC'), admin_url( 'admin.php?page=gocars_online' ) ) ); ?>">
                            <span>Date</span><span class="sorting-indicator"></span>
                        </a>
                    </th>
                    <th>Shortcode</th>
                    <th>Actions</th>
                </tr>
            </tfoot>
        </table>
        <!-- <div class="tablenav bottom">
            <div class="alignleft actions bulkactions">
                <label for="bulk-action-selector-bottom" class="screen-reader-text">Επιλέξτε μαζική ενέργεια</label>
                <select name="action2" id="bulk-action-selector-bottom">
                    <option value="-1">Μαζικές ενέργειες</option>
                    <option value="delete">Διαγραφή</option>
                </select>
                <input type="submit" id="doaction2" class="button action" value="Εφαρμογή">
            </div>
            <div class="tablenav-pages one-page"><span class="displaying-num">1 στοιχείο</span>
                <span class="pagination-links"><span class="tablenav-pages-navspan button disabled" aria-hidden="true">«</span>
                <span class="tablenav-pages-navspan button disabled" aria-hidden="true">‹</span>
                <span class="screen-reader-text">Τρέχουσα σελίδα</span><span id="table-paging" class="paging-input"><span class="tablenav-paging-text">1 από <span class="total-pages">1</span></span></span>
                <span class="tablenav-pages-navspan button disabled" aria-hidden="true">›</span>
                <span class="tablenav-pages-navspan button disabled" aria-hidden="true">»</span></span>
            </div>
            <br class="clear">
        </div> 
        <button type="submit" class="button button-secondary">Delete Selected Listings</button> -->
    </form>
</div>