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

global $wp;
$myurl = admin_url( "admin.php?page=easy-photo-sell_orders");
$nonceurl = wp_nonce_url($myurl, 'inv_search', '_egpsnonce');

$page_links = "";

if( isset($_GET['_egpsnonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_egpsnonce'])), 'inv_search')){
    $inv_search = isset( $_POST['inv_search']) ? sanitize_text_field($_POST['inv_search']) : "";
}else{
    $inv_search = "";
}

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

echo '<table class="table">
    <thead>
        <tr>
        <th scope="col">Search: </th>
        <th scope="col" style="text-align: left; float: left;">
            
            <form method="post" id="invoice_srcform" role="search" action="'.esc_html($nonceurl).'&tab=last_ordered">

                <input type="text" value="" name="inv_search" id="inv_srcfield" />
                        
                <input type="submit" id="invoice_srcbutton" value="Search invoice" name="src_invoice" />
        
            </form>

        </th>
        </tr>
    </thead>
    <tbody></tbody></table>';

if($inv_search == ""){

    $total = EgpsDatabase::egps_count_table_rows("orders");
    $limit = 100; // number of rows in page
    $offset = ( $pagenum - 1 ) * $limit;

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

    $all_orders = EgpsOrders::getAllOrdersLast($offset, $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
    ) );

}else{
    $all_orders = EgpsOrders::searchInvoiceNumber($inv_search);

    $page_links = "";
}

    echo '<table class="table table-striped">
    <thead>
        <tr>
        <th scope="col">#</th>
        <th scope="col">email</th>
        <th scope="col">items</th>
        <th scope="col">invoice number</th>
        <th scope="col">price paid</th>
        <th scope="col">date</th>
        </tr>
    </thead>
    <tbody>';

    

    $in = 1; 

    foreach($all_orders as $ord){

        $items = EgpsOrders::getOrderItemsByInvoice($ord->order_invoice_number);

        echo '
        <tr>
            <th scope="row">' . esc_html($in++) . '</th>
            <td><a href="' . esc_url($myurl . '&tab=customers&cust=' . $ord->customer_email) . '"> ' . esc_html($ord->customer_email) . '</a></td>
            <td>';
            
                foreach($items as $item){
                    echo '<img src="' . esc_url(get_site_url() . '/wp-content/uploads/egps/photos/' . $item->order_items . '.jpg') . '" width="50" style="margin-right: 10px;" />';
                }

            echo '</td>
            <td> <a 
                class="egps-modal-opener"
                href="'. esc_url(get_site_url() . '/wp-json/egps_rest/get_order_details/' . $ord->customer_email . '/' . $ord->order_invoice_number) . '">' . esc_html($ord->order_invoice_number) . '</a></td>
            
            <td>' . esc_html($ord->purchase_currency . " " . $ord->price) .'</td>
            
            <td> ' . esc_html(explode(" ", $ord->time)[0]) . '</td>
        </tr>';
    }

    if ( $page_links != "" ) {
        echo '<tr><th scope="row"></th><td colspan="5" align="center">' . esc_html($page_links) . '</td></tr>';
    }

    echo '
    </tbody>
    </table>';

    // modal dialog for invoice details
    require_once "modal.php";


