<?php
namespace Datacake\Core\Admin\Meta;


use Datacake\Core\Wda\ProductTypeApi;

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Datacake_core
 * @subpackage Datacake_core/admin
 * @author     Datacake <ajuda@datacake.com.br>
 */
class IspMetaBox {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;
	//private $global_settings = array();
	
	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;
			

	}
    
 
    /**
     * Set up and add the meta box.
     */
    public static function add_meta_boxes() {
        add_meta_box(
            'isp_planos_data',          // Unique ID
            'Dados do Plano', // Box title
            [ self::class, 'p_html_data' ],   // Content callback, must be of type callable
            'isp_planos'
        );
        add_meta_box(
            'isp_planos_extra_data',          // Unique ID
            'Extra dados do Plano', // Box title
            [ self::class, 'p_html_extra_data' ],   // Content callback, must be of type callable
            'isp_planos'
        );
        add_meta_box(
            'isp_canal_data',          // Unique ID
            'Informações do Canal', // Box title
            [ self::class, 'c_html_data' ],   // Content callback, must be of type callable
            'isp_canais'
        );
    }
 
 
    /**
     * Save the meta box selections.
     *
     * @param int $post_id  The post ID.
     */
    public function save( int $post_id ) {
        if ( array_key_exists( 'wporg_field', $_POST ) ) {
            update_post_meta(
                $post_id,
                '_wporg_meta_key',
                $_POST['wporg_field']
            );
        }
    }
 
    public static function get_meta($id, $meta){
        $meta = get_post_meta($id, $meta);
       
        if($meta && $meta[0]){
            return htmlentities($meta[0]);
        }
        return '';

    }
    /**
     * Mostrar Metabox para Html Data
     *
     * @param \WP_Post $post   Post object.
     */
    public static function p_html_data( $post ) {
        ?>
        <div class="wrap">
            <div class="form-wrap">
                <!-- Starting isp_remote_id -->
                <label for="isp_remote_id" id="isp_remote_id_label">Id Remoto Datacake -<code>isp_remote_id</code></label>
                <input type="text" name="isp_remote_id" id="isp_remote_id" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_remote_id');?>" readonly>
                <!-- Starting isp_product_type_id -->
                <label for="isp_product_type_id" id="isp_product_type_id_label">Id Tipo de Produto -<code>isp_product_type_id</code></label>
                <input type="text" name="isp_product_type_id" id="isp_product_type_id" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_product_type_id');?>" readonly>
                <!-- Starting isp_product_class -->
                <label for="isp_product_class" id="isp_product_class_label">Classe de Produto -<code>isp_product_class</code></label>
                <input type="text" name="isp_product_class" id="isp_product_class" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_product_class');?>" readonly>
                <!-- Starting isp_is_active -->
                <label for="isp_is_active" id="isp_is_active_label">Esta Ativo? -<code>isp_is_active</code></label>
                <input type="text" name="isp_is_active" id="isp_is_active" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_is_active');?>" readonly>
                <!-- Starting isp_sku -->
                <label for="isp_sku" id="isp_sku_label">Sku -<code>isp_sku</code></label>
                <input type="text" name="isp_sku" id="isp_sku" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_sku');?>" readonly>
                <!-- Starting isp_visibility -->
                <label for="isp_visibility" id="isp_visibility_label">É Visivel? -<code>isp_visibility</code></label>
                <input type="text" name="isp_visibility" id="isp_visibility" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_visibility');?>" readonly>
                <!-- Starting isp_pricing_list -->
                <label for="isp_pricing_list" id="isp_pricing_list_label">Preço de Listagem -<code>isp_pricing_list</code></label>
                <input type="text" name="isp_pricing_list" id="isp_pricing_list" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_pricing_list');?>" readonly>
                <!-- Starting isp_pricing_sale -->
                <label for="isp_pricing_sale" id="isp_pricing_sale_label">Preço Promocional -<code>isp_pricing_sale</code></label>
                <input type="text" name="isp_pricing_sale" id="isp_pricing_sale" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_pricing_sale');?>" readonly>
                <!-- Starting isp_track_inventory -->
                <label for="isp_track_inventory" id="isp_track_inventory_label">Gerenciar Inventorio -<code>isp_track_inventory</code></label>
                <input type="text" name="isp_track_inventory" id="isp_track_inventory" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_track_inventory');?>" readonly>
                <!-- Starting isp_quantity -->
                <label for="isp_quantity" id="isp_quantity_label">Quantidade em estoque-<code>isp_quantity</code></label>
                <input type="text" name="isp_quantity" id="isp_quantity" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_quantity');?>" readonly>
                <!-- Starting isp_shippable -->
                <label for="isp_shippable" id="isp_shippable_label">É enviavel? -<code>isp_shippable</code></label>
                <input type="text" name="isp_shippable" id="isp_shippable" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_shippable');?>" readonly>
                <!-- Starting isp_weight -->
                <label for="isp_weight" id="isp_weight_label">Peso -<code>isp_weight</code></label>
                <input type="text" name="isp_weight" id="isp_weight" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_weight');?>" readonly>
                <!-- Starting isp_shipping_width -->
                <label for="isp_shipping_width" id="isp_shipping_width_label">Largura -<code>isp_shipping_width</code></label>
                <input type="text" name="isp_shipping_width" id="isp_shipping_width" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_shipping_width');?>" readonly>
                <!-- Starting isp_shipping_height -->
                <label for="isp_shipping_height" id="isp_shipping_height_label">Altura -<code>isp_shipping_height</code></label>
                <input type="text" name="isp_shipping_height" id="isp_shipping_height" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_shipping_height');?>" readonly>
                <!-- Starting isp_shipping_depth -->
                <label for="isp_shipping_depth" id="isp_shipping_depth_label">Profundidade -<code>isp_shipping_depth</code></label>
                <input type="text" name="isp_shipping_depth" id="isp_shipping_depth" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_shipping_depth');?>" readonly>
                <!-- Starting isp_quantity_allocated -->
                <label for="isp_quantity_allocated" id="isp_quantity_allocated_label">Quantidade Alocavel -<code>isp_quantity_allocated</code></label>
                <input type="text" name="isp_quantity_allocated" id="isp_quantity_allocated" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_quantity_allocated');?>" readonly>
                <!-- Starting isp_cost -->
                <label for="isp_cost" id="isp_cost_label">Custo do produto -<code>isp_cost</code></label>
                <input type="text" name="isp_cost" id="isp_cost" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_cost');?>" readonly>
                <!-- Starting isp_internal_product_code -->
                <label for="isp_internal_product_code" id="isp_internal_product_code_label">Código Interno -<code>isp_internal_product_code</code></label>
                <input type="text" name="isp_internal_product_code" id="isp_internal_product_code" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_internal_product_code');?>" readonly>
                <!-- Starting isp_option_required -->
                <label for="isp_option_required" id="isp_option_required_label">Requer Opção -<code>isp_option_required</code></label>
                <input type="text" name="isp_option_required" id="isp_option_required" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_option_required');?>" readonly>
                <!-- Starting isp_min_stock -->
                <label for="isp_min_stock" id="isp_min_stock_label">Estoque Minimo -<code>isp_min_stock</code></label>
                <input type="text" name="isp_min_stock" id="isp_min_stock" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_min_stock');?>" readonly>
                <!-- Starting isp_variable_weight -->
                <label for="isp_variable_weight" id="isp_variable_weight_label">Tem peso variavel? -<code>isp_variable_weight</code></label>
                <input type="text" name="isp_variable_weight" id="isp_variable_weight" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_variable_weight');?>" readonly>
                <!-- Starting isp_unit_type -->
                <label for="isp_unit_type" id="isp_unit_type_label">Tipo de Unidade -<code>isp_unit_type</code></label>
                <input type="text" name="isp_unit_type" id="isp_unit_type" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_unit_type');?>" readonly>
                <!-- Starting isp_hide_customer_notes -->
                <label for="isp_hide_customer_notes" id="isp_hide_customer_notes_label">Não mostrar nostas do cliente -<code>isp_hide_customer_notes</code></label>
                <input type="text" name="isp_hide_customer_notes" id="isp_hide_customer_notes" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_hide_customer_notes');?>" readonly>
                <!-- Starting isp_restricted_customers -->
                <label for="isp_restricted_customers" id="isp_restricted_customers_label">Restricted Customers -<code>isp_restricted_customers</code></label>
                <input type="text" name="isp_restricted_customers" id="isp_restricted_customers" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_restricted_customers');?>" readonly>
            </div>
        </div>
        <?php
    }
    /**
     * Mostrar Metabox para Extra data dos planos
     *
     * @param \WP_Post $post   Post object.
     */
    public static function p_html_extra_data( $post ) {
        $product_type_id = get_post_meta($post->ID, 'isp_product_type_id');
        $productTypeApi = new ProductTypeApi();
        $response = $productTypeApi
            ->setJsonApiPage()
            ->setJsonApiFilter([
                'id__in'=>$product_type_id
            ])
			->setJsonApiFields([ 
                'id', 'name', 'code', 'product_class', 'data',
			])
            ->setJsonApiInclude([
                'data'
            ])
			->list();
        if($response->isSuccessful()){
            if($response->hasDocument()){
                $type = $response->document()->primaryResources();
                
                if($type && $type[0]){
                    $type = $type[0];
                    ?>
                    <div class="wrap">
                        <p>Tipo de Produto: <?php echo $type->attribute('name')." - #",$type->id() ?>
                        <div class="form-wrap">
                        <?php
                            foreach ($type->relationship('data')->resources() as $k => $attr) {
                                $key = 'isp_data_'.$attr->attribute('code');
                                ?>
                                <!-- Starting isp_restricted_customers -->
                                <label for="<?php echo $key; ?>" id="<?php echo $key; ?>_label"><?php echo $attr->attribute('name'); ?> - <code><?php echo $key; ?></code></label>
                                <input type="text" name="<?php echo $key; ?>" id="<?php echo $key; ?>" class="postbox full-width" value="<?php echo self::get_meta($post->ID, $key);?>" readonly>
                                <?php
                            }
                        ?>
                        </div>
                    </div>
                    <?php
                }
            }
        }
    }
     /**
     * Mostrar Metabox do Canal
     *
     * @param \WP_Post $post   Post object.
     */
    public static function c_html_data( $post ) {
        
       
        ?>
        <div class="wrap">
            <div class="form-wrap">
                <!-- starting isp_remote_id -->    
                <label for="isp_remote_id" id="isp_remote_id_label">Id Datacake Remoto - <code>isp_remote_id</code></label>
                <input type="text" name="isp_remote_id" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_remote_id'); ?>" readonly>
                <!-- starting isp_logo -->
                <label for="isp_logo" id="isp_logo_label">Logo/Imagem do Canal - <code>isp_logo</code></label>
                <input type="text" name="isp_logo" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_logo'); ?>" readonly>
                <!-- starting isp_number -->
                <label for="isp_number" id="isp_number_label">Numero do Canal - <code>isp_number</code></label>
                <input type="text" name="isp_number" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_number'); ?>" readonly>
                <!-- starting isp_is_hd -->
                <label for="isp_is_hd" id="isp_is_hd_label">É HD? - <code>isp_is_hd</code></label>
                <input type="text" name="isp_is_hd" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_is_hd'); ?>" readonly>
                <!-- starting isp_is_audio -->
                <label for="isp_is_audio" id="isp_is_audio_label">É Canal de Audio? - <code>isp_is_audio</code></label>
                <input type="text" name="isp_is_audio" class="postbox full-width" value="<?php echo self::get_meta($post->ID, 'isp_is_audio'); ?>" readonly>
            </div>
        </div>
    
        <?php
    }

    
    

	
}
