<?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, "cust_email", "_egpsnonce");


$page_links = "";

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

$cust_details = isset( $_GET['cust']) ? sanitize_text_field($_GET['cust']) : "";
$getinvnumber = isset($_GET['invno']) ? sanitize_text_field($_GET['invno']) : "";


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

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

                <input type="text" value="" name="cust_email" id="customer_srcfield" />
                        
                <input type="submit" id="customer_srcbutton" value="Search" name="src_email" />
        
            </form>

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


if($cust_details == ""){

    if($email == ""){

        $total = EgpsDatabase::egps_count_table_rows_distinct("orders", "customer_email");
        $limit = 50; // number of rows in page
        $offset = ( $pagenum - 1 ) * $limit;

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

        $all_customers = EgpsOrders::getOrdersByCustomers($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_customers = EgpsOrders::getOrdersByCustomersSearch($email);
    }

    echo '<table class="table table-striped">
    <thead>
        <tr>
        <th scope="col">#</th>
        <th scope="col">email</th>
        </tr>
    </thead>
    <tbody>';

    

    $in = 1; 

    foreach($all_customers as $cust){
        echo '
        <tr>
            <th scope="row">' . esc_html($in++) . '</th>
            <td><a href="' . esc_url($myurl . '&tab=customers&cust=' . $cust->customer_email) . '"> ' . esc_html($cust->customer_email) . '</a></td>
        </tr>';
    }

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

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

}else{

    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; 

    $cust_order_details = EgpsOrders::getAllOrdersByCustomerAdminpage($cust_details);

    foreach($cust_order_details as $cust){

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

        echo '
        <tr>
            <th scope="row">' . esc_html($in++) . '</th>
            <td><a href="' . esc_url($myurl . '&tab=customers&cust=' . $cust->customer_email) . '"> ' . esc_html($cust->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/' . $cust->customer_email . '/' . $cust->order_invoice_number) . '" 
                >' . esc_html($cust->order_invoice_number) . '</a></td>
            
            <td>' . esc_html($cust->purchase_currency . " " . $cust->price) .'</td>
            
            <td> ' . esc_html(explode(" ", $cust->time)[0]) . '</td>
        </tr>';
    }

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

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

