<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WPCSR_Pickup {
    function __construct(){
        add_action( 'admin_menu', array( $this, 'admin_menu_callback') );
        add_action( 'admin_init', array( $this, 'register_setting_callback' ) );
        // Pickup frontend hook
    }
    function admin_menu_callback(){
        add_submenu_page(
			'wpcsr-rate',
			__('Warehouses', 'wpcargo-shipment-rate'),
			__('Warehouses', 'wpcargo-shipment-rate'),
			'manage_options',
			'wpcsr-warehouse',
			array( $this, 'warehouse_page_callback' )
        );
        add_submenu_page(
			'wpcsr-rate',
			__('Pickup Rates', 'wpcargo-shipment-rate'),
			__('Pickup Rates', 'wpcargo-shipment-rate'),
			'manage_options',
			'wpcsr-pickup-rate',
			array( $this, 'pickup_rate_page_callback' )
		);
    }
    function register_setting_callback(){
        register_setting( 'wpcsr_warehouses_option', 'wpcsr_warehouses' );
    }
    function pickup_rate_page_callback(){
        global $wpdb;
        $warehouses     = get_option('wpcsr_warehouses');
        $locations      = get_wpcsr_location();
        $pagelink 		= admin_url( 'admin.php?page=wpcsr-pickup-rate' );
        $per_page 		= WPCSR_PER_PAGE;
		$page 			= ( isset( $_GET['wpcsr_page'] ) ) ? (int)$_GET['wpcsr_page'] : 1 ;
		$offset 		= ($page - 1) * $per_page;
		$total_row 		= count( get_wpcsr_pickup_rates() );
        $page_count 	= (int)ceil($total_row / $per_page);
        $sql            = "SELECT * FROM `".$wpdb->prefix.WPCSR_DB_PICKUP ."` ORDER BY `name` ASC LIMIT {$offset}, {$per_page}";
		$pickup_rates 	= $wpdb->get_results( $sql );
        if( empty( $warehouses ) ){
            ?>
            <div class="notice notice-warning">
                <p><?php esc_html_e('No warehouse found, please add warehouse to set up pickup rates.', 'wpcargo-shipment-rate' ); ?> <a class="button button-primary button-small" href="<?php echo admin_url('admin.php?page=wpcsr-warehouse'); ?>"><?php esc_html_e('Add Warehouse', 'wpcargo-shipment-rate' ); ?></a></p>
            </div>
            <?php
            return;
        }
        ?>
        <div class="wrap">
            <h1 class="wp-heading-inline"><?php esc_html_e('Pickup Rates', 'wpcargo-shipment-rate' ); ?></h1>
            <div class="postbox">
                <div class="inside">
                    <?php require_once( WPCSR_PATH.'admin/templates/pickup-rate.tpl.php'); ?>
                </div>
            </div>
        </div> 
        <?php
    }
    function warehouse_page_callback(){
        $warehouses = get_option('wpcsr_warehouses');
        ?>
        <div class="wrap">
            <h1 class="wp-heading-inline"><?php esc_html_e('Warehouses', 'wpcargo-shipment-rate' ); ?></h1>
            <div class="postbox">
                <div class="inside">
                <form id="warehouse-form"class="form-table" method="post" action="options.php">
                    <?php
                        settings_fields( 'wpcsr_warehouses_option' );
                        do_settings_sections( 'wpcsr_warehouses_option' );
                        require_once( WPCSR_PATH.'admin/templates/warehouse.tpl.php');
                    ?>
                </form>
                </div>
            </div>
        </div>  
        <?php
    }
}
new WPCSR_Pickup;