<?php
    if(!defined('ABSPATH')){
        exit; // Exit if accessed directly
    }


$album_id = sanitize_text_field($_GET['show_album_photos']);
$pagenum = isset( $_GET['pg'] ) ? absint( sanitize_text_field($_GET['pg']) ) : 1;

//pagination
$options = get_option('egps_sitedesign');
$total = EgpsDatabase::egps_count_table_rows_where("egps_photos", $album_id);

if ( isset($options['egps_photoPerPageAdmin']) && $options['egps_photoPerPageAdmin'] != ""){
    $limit = $options['egps_photoPerPageAdmin']; // number of rows in page
}else{
    $limit = 40;
}

$offset = ( $pagenum - 1 ) * $limit;

$num_of_pages = ceil( $total / $limit );

$page_links = paginate_links( array(
    'base' => add_query_arg( 'pg', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'egps-easy-sell-for-google-photo' ),
    'next_text' => __( '&raquo;', 'egps-easy-sell-for-google-photo' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );


$album_data_array = EgpsDatabase::egps_get_table_rows_where_limit(
    'egps_photos', 
    'egps_album_id', 
    'equal', 
    $album_id,
    'id',
    'ASC',
    $offset,
    $limit
);


//html
echo '<br><div class="" id="egps_admin_album_table"><h2>Album photos</h2>';

$i = 1;

echo '<div class="egps_admin_photo">';

foreach ($album_data_array as $item){

    echo '<figure>
        <img src="' . esc_html(get_site_url()) . '/wp-content/uploads/egps/photos/' . esc_html($item->egps_id) . '.jpg' . '" />';

    echo '<figcaption>
        <a href="' . esc_html(get_site_url()) . '/wp-json/egps_rest/get_photo_data/' . esc_html($item->id) . '" 
            type="button" 
            class="btn btn-info egps_edit_photo_meta" 
        >Edit meta</a>';

    if($item->egps_photo_show == 'y'){
        echo '<button 
            id="egps_show_photo_' . esc_html($item->egps_id) . '" 
            type="button" 
            class="btn btn-success egps_showhidebtn_photo" 
            value="n">Visible</button>';
    }else{
        echo '<button 
            id="egps_show_photo_' . esc_html($item->egps_id) . '" 
            type="button" 
            class="btn btn-danger egps_showhidebtn_photo" 
            value="y">Hidden</button>';
    }

    echo '</figcaption></figure>';


}

echo '</div></div>';

if ( $page_links != "" ) {
    $arr_allowed = Egps::egps_email_admin_out_allowed();

    echo '<table width="100%"><tr><th scope="row"></th><td colspan="5" align="center">' . wp_kses($page_links, $arr_allowed)  . '</td></tr></table>';
}

// modal dialog for photo meta
require_once "modal.php";