<?php
if($redirectstatus == "success") {
    echo '<div class="wpcompleteautomatc_msgbox_success">';
        echo $redirectmsg;
    echo '</div>';
}
else if ($redirectstatus == "failed") {
    echo '<div class="wpcompleteautomatc_msgbox_error">';
        echo $redirectmsg;
    echo '</div>';
}

$filter_sql = "";

// if(!empty($filter_orders)) {
//     $filter_orders_arr = explode(",", $filter_orders);

//     if(count($filter_orders_arr) > 0) {
//         for($i=0; $i<count($filter_orders_arr); $i++) {
//             $filter_sql .= "'" . trim($filter_orders_arr[$i]) . "',";
//         }

//         $filter_sql = " WHERE order_sn IN(" . substr($filter_sql, 0, -1) . ")";
//     }
// }
// else {
//     $filter_sql = " WHERE order_process_status='Pending' AND order_active='1'";
// }

$qry_pagi = "SELECT COUNT(*) AS ttl_pagi FROM " . $wpdb->prefix . "autoesim_order" . $filter_sql;
$rs_pagi = $wpdb->get_row($qry_pagi, ARRAY_A);

$pagi_ttl = (int)$rs_pagi['ttl_pagi'];
$pagi_limit = 10;
$pagi_start = ((int)$num - 1) * $pagi_limit;
$pagi_loop = ceil($pagi_ttl/$pagi_limit);


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

    echo '<h3>Orders</h3>';

    echo '<div class="wpcompleteautomatc_top_panel">';
        //echo '<a href="'.admin_url('admin.php?page=wpcompleteautomatc_email_templates').'" class="wpcompleteautomatc_func_btn">Back To Dashboard</a>';
        //echo '<a href="#" class="wpcompleteautomatc_btn">Sync Shopee Products</a>';
    echo '</div>';

    echo '<div class="wpcompleteautomatc_form_panel">';
    echo '</div>';

    echo '<div class="wpcompleteautomatc_table_panel">';
        echo '<table class="wpcompleteautomatc_table">';
            echo '<tr class="header">';
                // echo '<td>';
                //     echo '<input type="checkbox" class="prolistall" value="all"/>';
                // echo '</td>';
                echo '<td>';
                    echo 'Order SN';
                echo '</td>';
                // echo '<td>';
                //     echo 'Username';
                // echo '</td>';
                echo '<td>';
                    echo 'Email';
                echo '</td>';
                echo '<td>';
                    echo 'Order Type';
                echo '</td>';
                echo '<td>';
                    echo 'Convert Status';
                echo '</td>';
                echo '<td>';
                    echo 'Qty';
                echo '</td>';
                echo '<td>';
                    echo 'ICCID';
                echo '</td>';
                echo '<td>';
                    echo 'Total Price';
                echo '</td>';
                echo '<td>';
                    echo 'Destination';
                echo '</td>';
                echo '<td>';
                    echo 'Plan';
                echo '</td>';
                echo '<td>';
                    echo 'Order Datetime';
                echo '</td>';
                echo '<td>';
                    echo 'Status';
                echo '</td>';
                echo '<td>';
                    echo 'Voucher';
                echo '</td>';
                echo '<td>';
                    echo 'Stripe Status';
                echo '</td>';
                echo '<td>';
                    echo 'Stripe Datetime';
                echo '</td>';
                echo '<td>';
                    echo 'Post To Automation';
                echo '</td>';
                //echo '<td>';
                //    echo 'Actions';
                //echo '</td>';
            echo '</tr>';

            $qry_orders = "SELECT * FROM " . $wpdb->prefix . "autoesim_order".$filter_sql." ORDER BY auesim_cdate DESC LIMIT " . $pagi_start . ", " . $pagi_limit;
            $rs_orders = $wpdb->get_results($qry_orders, ARRAY_A);

            $lines = 0;
            if($rs_orders) {
                 foreach($rs_orders as $rs_order) {
                    //odd
                    if ($lines % 2 == 0) {
                        $line_cls = "even";
                    }
                    else {
                        $line_cls = "odd";
                    }

                    echo '<tr class="'.$line_cls.'">';
                        // echo '<td>';
                        //     echo '<input type="checkbox" class="prolist" value="'.$rs_order['order_id'].'"/>';
                        // echo '</td>';

                        echo '<td>';
                            echo $rs_order['auesim_order_sn'];
                        echo '</td>';
                        // echo '<td>';
                        //     echo $rs_order['auesim_buyer_username'];
                        // echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_email'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_item_type'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_item_convert_status'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_qty'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_esim_iccid'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_currency'] . $rs_order['auesim_ttl_price'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_destination'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_plan'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_cdate'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_status'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_voucher'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_stripe_status'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_stripe_cdate'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_order['auesim_posttoserver_datetime'];
                        echo '</td>';
                    echo '</tr>';

                    $lines++;
                }
            }
        
        echo '</table>';
        
        //$nonce_promodupt = wp_create_nonce("ajax_wpcompleteautomatc_productmodelupdate_nonce");
        //echo '<input type="hidden" class="ajaxpromoduptnonce" value="'.$nonce_promodupt.'"/>';

        if($pagi_loop > 1) {
            echo '<div class="wpcompleteautomatc_pagi">';

            for($g=0; $g<$pagi_loop; $g++) {
                $loop_num = $g + 1;
                $actv_cls = '';
                
                $loop_url = admin_url('admin.php?page=wpautomationesim&num='.$loop_num);

                if($num == $loop_num) {
                    $actv_cls = ' wpcompleteautomatc_actv';
                }
                echo '<a href="'.$loop_url.'" class="wpcompleteautomatc_pages'.$actv_cls.'">'.$loop_num.'</a>';
            }

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

echo '</div>';
?>

<script>
    jQuery(document).ready(function($) {});
</script>

<style>
@media (min-width: 1025px) {
    .wpcompleteautomatc_msgbox_success {
        padding:30px 30px;
        margin:20px 20px;
    }

    .wpcompleteautomatc_msgbox_error {
        padding:30px 30px;
        margin:20px 20px;
    }

    .wpcompleteautomatc_body {
        margin:20px 20px;
    }
}

@media (min-width: 768px) and (max-width: 1024px) {
    .wpcompleteautomatc_msgbox_success {
        padding:10px 10px;
        margin:10px 10px;
    }

    .wpcompleteautomatc_msgbox_error {
        padding:10px 10px;
        margin:10px 10px;
    }

    .wpcompleteautomatc_body {
        margin:20px 10px;
    }
}

@media (max-width: 767px) {
    .wpcompleteautomatc_msgbox_success {
        padding:10px 10px;
        margin:10px 10px;
    }

    .wpcompleteautomatc_msgbox_error {
        padding:10px 10px;
        margin:10px 10px;
    }

    .wpcompleteautomatc_body {
        margin:20px 10px;
    }
}

.wpcompleteautomatc_form_panel {

}

.general_form_css {
    
}

.form_fields {
    padding-bottom:15px;
}

.form_lbl {
    font-weight: bold;
    padding-bottom:5px;
}

.form_text {
    
}

.form_text input.fullwidth, .form_text textarea.fullwidth, .form_text select.fullwidth {
    width:100%;
    padding:10px 15px;
}

.form_text select.fullwidth {
    height: auto !important;
}

.form_text textarea.fullwidth {
    height:200px;
}

.form_text input.fullwidth[type="file"] {
    padding:10px 15px !important;
    border: 1px solid #c3c3c4;
    background-color: rgba(255,255,255,1.0);
    width:auto;
}

.wpcompleteautomatc_msgbox_success {
    background:#f4fff2;
    border:1px solid #dbe8cf;
}

.wpcompleteautomatc_msgbox_error {
    background:#fff2f2;
    border:1px solid #f4dbdb;
}

.wpcompleteautomatc_body {
    
}

.wpcompleteautomatc_top_panel {
    margin-bottom: 10px;
}

.wpcompleteautomatc_btn {
    padding:10px 20px;
    color:#ffffff !important;
    background:#786890;
    text-decoration: none;
    margin-right:10px;
    margin-bottom:10px;
    border: none;
    cursor: pointer;
}

.wpcompleteautomatc_btn:hover {
    background:#665580;
}

.wpcompleteautomatc_func_btn {
    padding:10px 20px;
    color:#ffffff !important;
    background:#6a937c;
    text-decoration: none;
    margin-right:10px;
    margin-bottom:10px;
}

.wpcompleteautomatc_func_btn:hover {
    background:#3f7d5b;
}

.wpcompleteautomatc_actions {
    margin-right:10px;
    margin-bottom:5px;
    margin-top:5px;
    text-decoration: none;
}

.wpcompleteautomatc_table_panel {
    overflow: auto;
}

.wpcompleteautomatc_table {
    border-collapse: collapse;
}

.wpcompleteautomatc_table tr {
    border-collapse: collapse;
}

.wpcompleteautomatc_table td {
    border-collapse: collapse;
    border:1px solid #c9cdd2;
    padding:10px 20px;
}

.wpcompleteautomatc_table tr.even td {
    background:#f5f7fa;
}

.wpcompleteautomatc_table tr.odd td {
    background:#e8ebf1;
}

.wpcompleteautomatc_table tr.title td {
    background:#afbaca;
    color:#fff;
}

.wpcompleteautomatc_table td.actions {
    
}

.wpcompleteautomatc_table tr.header {

}

.wpcompleteautomatc_table tr.header td {
    border:1px solid #c2ccd9;
    background: #e0e5ec;
    font-weight: bold;
}

.wpcompleteautomatc_pagi {
    margin-top:20px;
    margin-bottom: 10px;
}

.wpcompleteautomatc_pages {
    padding:10px 20px;
    color:#ffffff !important;
    background:#bcbcbc;
    text-decoration: none;
    margin-right:10px;
    margin-bottom:10px;
}

.wpcompleteautomatc_pages:hover {
    background:#a0a0a0;
}

.wpcompleteautomatc_actv {
    background:#786890;
}

.wpcompleteautomatc_actv:hover {
    background:#665580;
}
</style>