<?php

/**
 * Admin Product Tabs Handler
 * 
 * @package cwpt
 * @since 1.0.0
 */

defined('ABSPATH') or die;

class CWPT_Admin_Tabs
{
    /**
     * Add product tab in admin
     * 
     * @param array $tabs
     * @return array
     */
    public static function add_tab($tabs)
    {
        $cwpt_tabs = CWPT_Settings::get('custom_tab');
        if (!empty($cwpt_tabs) && is_array($cwpt_tabs)) {
            foreach ($cwpt_tabs as $tab => $label) {
                $tabs[$tab] = array(
                    'label'  => $label['text'],
                    'target' => $tab . '_type_product_options',
                    'class'  => 'show_if_' . $tab,
                );
?>
                <style>
                    #woocommerce-product-data ul.wc-tabs li.<?php echo $tab; ?>_options a::before {
                        font-family: Dashicons;
                        content: '\f513';
                    }
                </style>
            <?php
            }
        }
        return $tabs;
    }

    /**
     * Add content to admin product tab
     * 
     * @return void
     */
    public static function add_tab_content()
    {
        global $product_object;

        $cwpt_tabs = CWPT_Settings::get('custom_tab');
        if (!empty($cwpt_tabs) && is_array($cwpt_tabs)) {
            $cwpt_available_tabs = array_keys(CWPT_Settings::get('custom_tab'));
            foreach ($cwpt_available_tabs as $tab_name) {
            ?>
                <div id='<?php echo $tab_name; ?>_type_product_options' class='panel woocommerce_options_panel hidden'>
                    <div class='options_group'>
                        <?php
                        $product_id = $product_object->get_id();
                        foreach ($cwpt_tabs[$tab_name]['fields'] as $key => $field) {
                            $field_label = empty($field['label']) ? '' : $field['label'];
                            $field_type = $field['type'];

                            if (!empty($field['url'])) {
                                $field_url = $field['url'];
                            }
                            $options = [];
                            if (!empty($field['options']) && in_array($field_type, ['select', 'radio'])) {
                                $field_type_options = explode('|', $field['options']);
                                foreach ($field_type_options as $option) {
                                    $options[trim($option)] = $option;
                                }
                            }
                            $field_placeholder = empty($field['description']) ? '' : $field['description'];
                            $field_id = $tab_name . 'field_' . $key;
                            $field_url_value = $product_object->get_meta($field_id . '_url', true);
                            $field_value = $product_object->get_meta($field_id, true);

                            switch ($field_type) {
                                case 'text':
                                    woocommerce_wp_text_input(array(
                                        'id'          => $field_id,
                                        'label'       => $field_label,
                                        'value'       => $field_value,
                                        'default'     => '',
                                        'placeholder' => $field_placeholder,
                                    ));
                                    break;
                                case 'number':
                                    woocommerce_wp_text_input(array(
                                        'id'                =>  $field_id,
                                        'label'             => $field_label,
                                        'description'       => $field_placeholder,
                                        'type'              => 'number', // Essential for number fields
                                    ));

                                    break;
                                case 'select':
                                    woocommerce_wp_select(array(
                                        'id'          => $field_id,
                                        'label'       => $field_label,
                                        'options'     => $options,
                                        'description' => $field_placeholder,
                                    ));
                                    break;
                                case 'radio':
                                    woocommerce_wp_radio(array(
                                        'id'          => $field_id,
                                        'label'       => $field_label,
                                        'options'     => $options,
                                        'description' => $field_placeholder,
                                    ));
                                    break;
                                case 'textarea':
                                    woocommerce_wp_textarea_input(array(
                                        'id'          => $field_id,
                                        'label'       => $field_label,
                                        'value'       => $field_value,
                                        'default'     => '',
                                        'placeholder' => $field_placeholder,
                                    ));
                                    break;

                                case 'checkbox':
                                    woocommerce_wp_checkbox(array(
                                        'id'          => $field_id,
                                        'label'       => $field_label,
                                        'value'       => $field_value,
                                        'default'     => '',
                                        'placeholder' => $field_placeholder,
                                    ));
                                    break;
                            }
                        }
                        ?>
                    </div>
                </div>
<?php
            }
        }
    }

    /**
     * Save admin product settings
     * 
     * @param int $post_id
     * @return void
     */
    public static function save_settings($post_id)
    {
        $cwpt_tabs = CWPT_Settings::get('custom_tab');
        if (!empty($cwpt_tabs)) {
            foreach ($cwpt_tabs as $tab => $fields) {
                foreach ($cwpt_tabs[$tab]['fields'] as $key => $data) {
                    $field_url = $cwpt_tabs[$tab]['fields'][$key]['url'];
                    $field_id = $tab . 'field_' . $key;
                    if (isset($_POST[$field_id])) {
                        update_post_meta($post_id, $field_id, sanitize_text_field($_POST[$field_id]));
                    }
                    if (isset($_POST[$field_id . '_url']) && $field_url == 'show') {
                        update_post_meta($post_id, $field_id . '_url', esc_url_raw($_POST[$field_id . '_url']));
                    }
                }
            }
        }
    }

    /**
     * Register CMB2 meta boxes
     * 
     * @return void
     */
    public static function register_meta_boxes()
    {
        if (!function_exists('register_cwpt_cmb2_meta_boxes')) {
            require_once dirname(__FILE__) . '/cwpt_fields.php';
        }
    }
}
