<?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>';
}

// $cm_pack_arr = array();
// $qry_cm_packs = "SELECT * FROM " . $wpdb->prefix . "woocommerce_cm_products ORDER BY cm_region ASC, cm_destination ASC, cm_packname ASC";
// $rs_cm_packs = $wpdb->get_results($qry_cm_packs, ARRAY_A);

// if($rs_cm_packs) {
//     foreach($rs_cm_packs as $rs_cm_pack) {
//         $cm_pack_arr[$rs_cm_pack['cm_pack_id']]['cm_pack_id'] = $rs_cm_pack['cm_pack_id'];
//         $cm_pack_arr[$rs_cm_pack['cm_pack_id']]['cm_region'] = $rs_cm_pack['cm_region'];
//         $cm_pack_arr[$rs_cm_pack['cm_pack_id']]['cm_destination'] = $rs_cm_pack['cm_destination'];
//         $cm_pack_arr[$rs_cm_pack['cm_pack_id']]['cm_packname'] = $rs_cm_pack['cm_packname'];
//     }
// }

// Settings Start //////////////////////
$settings = array();

$qry_temp_settings = "SELECT * FROM " . $wpdb->prefix . "bonvoi_settings WHERE bv_setting_name 
    IN('all_table_display_num')";
$rs_temp_settings = $wpdb->get_results($qry_temp_settings, ARRAY_A);

if($rs_temp_settings) {
    foreach($rs_temp_settings as $rs_temp_setting) {
        $settings[$rs_temp_setting['bv_setting_name']] = $rs_temp_setting['bv_setting_val'];
    }
}
// Settings End //////////////////////

$qry_pagi = "SELECT COUNT(bv_shop_id) AS ttl_pagi FROM " . $wpdb->prefix . "bonvoi_shops ORDER BY bv_shop_name";
$rs_pagi = $wpdb->get_row($qry_pagi, ARRAY_A);

$pagi_ttl = (int)$rs_pagi['ttl_pagi'];

if(!empty($settings['all_table_display_num'])) {
    $pagi_limit = $settings['all_table_display_num'];
}
else {
    $pagi_limit = 10;
}

$pagi_start = ((int)$num - 1) * $pagi_limit;
$pagi_loop = ceil($pagi_ttl/$pagi_limit);

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

    echo '<h1>Retail Shops</h1><br/>';

    echo '<div class="wpcompleteautomatc_top_panel">';
        // echo '<a href="#" class="wpcompleteautomatc_func_btn delete_btn">Delete Selected Items</a>';
        echo '<a href="'.admin_url('admin.php?page=wpautomationbonvoi_retail_shops&process=new_template').'" class="wpcompleteautomatc_btn">Add New Shop</a>';
        //echo '<a href="#" class="wpcompleteautomatc_btn">Sync Shopee Products</a>';
    echo '</div><br/>';

    echo '<div class="wpcompleteautomatc_table_panel">';
        echo '<table class="wpcompleteautomatc_table">';
            echo '<tr class="header">';
                echo '<td>';
                    echo 'Shop';
                echo '</td>';
                echo '<td>';
                    echo 'Email';
                echo '</td>';
                echo '<td>';
                    echo 'Phone';
                echo '</td>';
                echo '<td>';
                   echo 'Actions';
                echo '</td>';
            echo '</tr>';

            $qry_tempts = "SELECT * FROM " . $wpdb->prefix . "bonvoi_shops ORDER BY bv_shop_name ASC LIMIT " . $pagi_start . ", " . $pagi_limit;
            $rs_tempts = $wpdb->get_results($qry_tempts, ARRAY_A);

            if($rs_tempts) {
                $css_cls = 'odd';

                foreach($rs_tempts as $rs_tempt) {
                    if($css_cls == 'even') {
                        $css_cls = 'odd';
                    }
                    else {
                        $css_cls = 'even';
                    }

                    $edit_url = admin_url('admin.php?page=wpautomationbonvoi_retail_shops&process=edit_template&id='.$rs_tempt['bv_shop_id']);
                    // $delete_url = admin_url('admin.php?page=wpautomationbonvoi_retail_shops&process=edit_template&id='.$rs_tempt['bv_shop_id']);

                    echo '<tr class="'.$css_cls.'">';
                        echo '<td>';
                            echo $rs_tempt['bv_shop_name'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_tempt['bv_shop_email'];
                        echo '</td>';
                        echo '<td>';
                            echo $rs_tempt['bv_shop_phone'];
                        echo '</td>';
                        echo '<td>';
                            echo '<a class="wpcompleteautomatc_actions" href="'.$edit_url.'">Edit</a>';
                            echo '<a class="wpcompleteautomatc_actions del_btn" delid="'.$rs_tempt['bv_shop_id'].'" href="#">Delete</a>';
                        echo '</td>';
                    echo '</tr>';
                }
            }
        
        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=wpautomationbonvoi_retail_stuff&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>';
        // }

        
        // New Pagination Bar ////////////////
        if($pagi_loop > 1) {
            $pagi_endpoint_url = 'wpautomationbonvoi_retail_shops';

            echo '<br/>';
            echo '<div class="wpcompleteautomatc_pagi">';
            
            $current_page = (int)$num;
            $total_pages = $pagi_loop;
            $max_visible_pages = 5;
            
            // Calculate the start and end of visible page range
            if($total_pages <= $max_visible_pages) {
                // If total pages is 5 or less, show all pages
                $start_page = 1;
                $end_page = $total_pages;
            } else {
                // Calculate sliding window
                if($current_page <= 3) {
                    // Near the beginning: show 1,2,3,4,5
                    $start_page = 1;
                    $end_page = $max_visible_pages;
                } elseif($current_page > $total_pages - 2) {
                    // Near the end: show last 5 pages
                    $start_page = $total_pages - $max_visible_pages + 1;
                    $end_page = $total_pages;
                } else {
                    // In the middle: show current_page - 2 to current_page + 2
                    $start_page = $current_page - 2;
                    $end_page = $current_page + 2;
                }
            }
            
            // Previous button
            if($current_page > 1) {
                $prev_url = admin_url('admin.php?page='.$pagi_endpoint_url.'&num='.($current_page - 1));
                echo '<a href="'.$prev_url.'" class="wpcompleteautomatc_pages wpcompleteautomatc_prev">Previous</a>';
            } else {
                echo '<span class="wpcompleteautomatc_pages wpcompleteautomatc_disabled">Previous</span>';
            }
            
            // First page + ellipsis (if needed)
            if($start_page > 1) {
                $first_url = admin_url('admin.php?page='.$pagi_endpoint_url.'&num=1');
                echo '<a href="'.$first_url.'" class="wpcompleteautomatc_pages">1</a>';
                
                if($start_page > 2) {
                    echo '<span class="wpcompleteautomatc_ellipsis_start">...</span>';
                }
            }
            
            // Page numbers
            for($i = $start_page; $i <= $end_page; $i++) {
                $actv_cls = '';
                $loop_url = admin_url('admin.php?page='.$pagi_endpoint_url.'&num='.$i);
                
                if($current_page == $i) {
                    $actv_cls = ' wpcompleteautomatc_actv';
                }
                
                echo '<a href="'.$loop_url.'" class="wpcompleteautomatc_pages'.$actv_cls.'">'.$i.'</a>';
            }
            
            // Last page + ellipsis (if needed)
            if($end_page < $total_pages) {
                if($end_page < $total_pages - 1) {
                    echo '<span class="wpcompleteautomatc_ellipsis_end">...</span>';
                }
                
                $last_url = admin_url('admin.php?page='.$pagi_endpoint_url.'&num='.$total_pages);
                echo '<a href="'.$last_url.'" class="wpcompleteautomatc_pages">'.$total_pages.'</a>';
            }
            
            // Next button
            if($current_page < $total_pages) {
                $next_url = admin_url('admin.php?page='.$pagi_endpoint_url.'&num='.($current_page + 1));
                echo '<a href="'.$next_url.'" class="wpcompleteautomatc_pages wpcompleteautomatc_next">Next</a>';
            } else {
                echo '<span class="wpcompleteautomatc_pages wpcompleteautomatc_disabled">Next</span>';
            }
            
            echo '</div>';
        }
        /////////////////
        
    echo '</div>';

    //Hidden form
    // wpautomationbonvoi_bv_new_retail_shops_form - wpautomationbonvoi_bv_new_retail_shop_nonce
    // wpautomationbonvoi_bv_update_retail_shops_form - wpautomationbonvoi_bv_update_retail_shop_nonce
    // wpautomationbonvoi_bv_delete_retail_shops_form - wpautomationbonvoi_bv_delete_retail_shop_nonce
    
    $del_form_nonce = wp_create_nonce( 'wpautomationbonvoi_bv_delete_retail_shop_nonce' );

    echo '<form class="delete_form" method="POST" action="'.admin_url('admin-post.php').'">';
        echo '<input type="hidden" name="action" value="wpautomationbonvoi_bv_delete_retail_shop_nonce" />';
        echo '<input type="hidden" name="formnonce" class="formnonce" value="'.$del_form_nonce.'"/>';
        echo '<input type="hidden" name="del_ids" class="delids" value=""/>';
    echo '</form>';
echo '</div>';
?>

<script>
    jQuery(document).ready(function($) {
        jQuery(".del_btn").click(function(e) {
            e.preventDefault();
            var del_ids = jQuery(this).attr("delid");

            if(confirm("Are You Sure You Want To Proceed?")) {
                jQuery(".delids").val(del_ids);
                jQuery(".delete_form").submit();
            }

            return false;
        });

        // jQuery(".prolistall").click(function(e) {
        //     jQuery(".prolist").prop("checked", this.checked);
        // });

        // jQuery(".modeloptform").click(function(e) {
        //     e.preventDefault();

        //     var cur_id = jQuery(this).attr("iddata");
        //     var opt_val_cls = ".optval_" + cur_id;
        //     var result_cls = ".optresult_" + cur_id;

        //     var nonce = jQuery(".ajaxpromoduptnonce").val();
        //     var opt_val = jQuery(opt_val_cls).val();

        //     jQuery.ajax({
        //         type : "post",
        //         url : "<?php echo admin_url('admin-ajax.php'); ?>",
        //         data : {
        //             action: "ajaxpromodeluptfunc", 
        //             opt_val : opt_val, 
        //             cur_id: cur_id,
        //             nonce: nonce
        //         },
        //         success: function(response) {
        //             var json = jQuery.parseJSON(response);

        //             jQuery(result_cls).html(json.html);

        //             setTimeout(function() {
        //                 jQuery(result_cls).html("");
        //             }, 5000);
        //         }
        //     });
        // });
    });
</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_ellipsis_start {
    padding: 8px 10px 8px 4px;
    color: #666;
    font-weight: bold;
}

.wpcompleteautomatc_ellipsis_end {
    padding: 8px 10px 8px 4px;
    color: #666;
    font-weight: bold;
}

.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;
}

.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>