<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
//$w2pshop_action  = isset( $_REQUEST['w2pshop_action'] ) ? sanitize_text_field(wp_unslash( $_REQUEST['w2pshop_action'] )) : '';
$w2pshop_post_id = get_the_ID();

// General Setting Options
$is_product_private = W2PShopSettings::get_product_option( $w2pshop_post_id, 'w2pshop_is_private_product' );
$list_of_customers = get_users( array( 'role__in' => array('customer'), 'number' => 25, 'paged' => 1 ) );
$preselected_customers = W2PShopSettings::get_product_option( $w2pshop_post_id, 'w2pshop_private_users', array() );
$bypass_design_upload = W2PShopSettings::get_product_option( $w2pshop_post_id, 'w2pshop_bypass_design_and_upload' );

$tabs = [];
$tabs = apply_filters( 'w2pshop_woo_product_panel_tabs', $tabs );
?>
<div id="w2pshop_product_data" class="panel woocommerce_options_panel">
	<div id="w2pshop_product_data_tabs">
		<ul>
            <li><a href="#w2pshop_general"><?php esc_attr_e( 'General', 'web-to-print-shop-core' ); ?></a></li>
			<?php
			foreach ( $tabs as $name => $label ) {
                echo '<li><a href="#' . esc_attr( $name ) . '">' . esc_html( $label ) . '</a></li>';
			}
			?>
		</ul>
        <div id="w2pshop_general">
            <div class="general_options_group" id="w2pshop_general_id_form_group">
				<?php
				W2PShopSettings::generate_p_options_checkbox_html(
					'Is Product Private?',
					'w2pshop_is_private_product',
					'Is this product private/ only available for a specific set of user(s)?',
					! empty( $is_product_private )
				);
				?>
                <p class="form-field">
                    <input type="hidden" name="w2pshop_private_users" id="w2pshop_private_users" value="" />
                    <label for="w2pshop_private_users">List of Customers with Access to Private Product</label>
                    <select id="w2pshop_private_users" name="w2pshop_private_users[]" multiple="multiple" data-placeholder="List of Customers with Access to Private Product">
                        <?php
                        foreach ( $list_of_customers as $customer ) {
                            $_selected = ( (is_array ($preselected_customers) && in_array( $customer->ID, $preselected_customers ) ) )? ' selected="selected"' : '';
                            echo '<option value="' . esc_attr( $customer->ID ) . '"' . esc_attr( $_selected ) . '>';
                            echo esc_html( esc_attr( $customer->user_email ) );
                            echo '</option>';
                        }
                        ?>
                    </select>
                    <span class="woocommerce-help-tip" data-tip="Select all customers who will have access to this product."></span>
                </p>
				<?php 
                W2PShopSettings::generate_p_options_checkbox_html(
					'Bypass Design and Upload Artwork',
					'w2pshop_bypass_design_and_upload',
					'Bypass Design and Upload Artwork Processes and directly Add to Cart.',
					! empty( $bypass_design_upload )
				);
                ?>
            </div>
        </div>
		<?php do_action( 'w2pshop_woo_product_panel_content' );
        wp_nonce_field( 'save_w2pshop_product_settings', 'w2pshop_product_settings_nonce' );?>
	</div>
</div>

<?php
wp_enqueue_style('w2pshop_woo_product_panel_css', W2PSHOP_PLUGIN_URL . 'assets/css/w2pshop-woo-product-panel.css', array(), W2PSHOPCORE_VERSION);
wp_enqueue_script('w2pshop_woo_product_panel_js', W2PSHOP_PLUGIN_URL . 'assets/js/w2pshop-woo-product-panel.js', array('jquery'), W2PSHOPCORE_VERSION, true);
wp_localize_script( 'w2pshop_woo_product_panel_js', 'w2pshopPanelData', array(
	'ajaxurl' => admin_url( 'admin-ajax.php' ),
	'nonce'   => wp_create_nonce( 'w2pshop_search_nonce' )
));
?>