/*
Theme Name: Flatsome Child
Description: This is a child theme for Flatsome Theme
Author: UX Themes
Template: flatsome
Version: 3.0
*/

/*************** ADD CUSTOM CSS HERE.   ***************/


@media only screen and (max-width: 48em) {
/*************** ADD MOBILE ONLY CSS HERE  ***************/


}
function sago_ts_ux_builder_element() {
    add_ux_builder_shortcode('sago_ts_product', array(
        'name'      => __('Sago thông số kỹ thuật'),
        'category'  => __('Product Page'),
        'priority'  => 1,
        'options'   => array(
            'class' => array(
                'type'       => 'textfield',
                'heading'    => 'Class',
                'param_name' => 'class',
                'default'    => '',
            ),
            'advanced_options' => array(
                'type'       => 'select',
                'heading'    => 'Visibility',
                'param_name' => 'advanced_options',
                'default'    => '',
                'options'    => array(
                    ''                          => 'Visible',
                    'hidden'                    => 'Hidden',
                    'hide-for-medium'           => 'Only for Desktop',
                    'show-for-small'            => 'Only for Mobile',
                    'show-for-medium hide-for-small' => 'Only for Tablet',
                    'show-for-medium'           => 'Hide for Desktop',
                    'hide-for-small'            => 'Hide for Mobile',
                ),
            ),
        ),
    ));
}
add_action('ux_builder_setup', 'sago_ts_ux_builder_element');

function sago_cung_ts_func($atts) {
    // Extract attributes
    $atts = shortcode_atts(array(
        'class'            => '',
        'advanced_options' => '',
    ), $atts, 'sago_ts_product');

    ob_start();

    global $product;

    // Kiểm tra nếu không phải là sản phẩm WooCommerce
    if (!is_a($product, 'WC_Product') || !$product) {
        return '<p>' . __('Không tìm thấy sản phẩm.', 'textdomain') . '</p>';
    }

    $product_id = $product->get_id();

    // Lấy các taxonomy (terms) của sản phẩm
    $thuong_hieu  = get_the_terms($product_id, 'thuong-hieu');
    $dung_tich    = get_the_terms($product_id, 'dung-tich');
    $nong_do_con  = get_the_terms($product_id, 'nong-do-con');
    $loai_vang    = get_the_terms($product_id, 'loai-vang');
    $vung         = get_the_terms($product_id, 'vung');
    $xuat_xu      = get_the_terms($product_id, 'xuat-xu');
    $phan_hang    = get_the_terms($product_id, 'phan-hang');
    $giong_nho    = get_the_terms($product_id, 'giong-nho');
    $nien_vu      = get_the_terms($product_id, 'nien-vu');
    $quy_cach     = get_the_terms($product_id, 'quy-cach');

    // Bắt đầu output
    echo '<div class="sago-sp-ts box-group-product ' . esc_attr($atts['class']) . ' ' . esc_attr($atts['advanced_options']) . '">';
    echo '<table class="sago_table" style="border-collapse: collapse; width: 100%;">';

    $fields = array(
        'thuong-hieu'  => array('label' => 'Thương hiệu',   'terms' => $thuong_hieu),
        'dung-tich'    => array('label' => 'Dung tích',     'terms' => $dung_tich),
        'nong-do-con'  => array('label' => 'Nồng độ cồn',   'terms' => $nong_do_con),
        'loai-vang'    => array('label' => 'Loại vang',     'terms' => $loai_vang),
        'vung'         => array('label' => 'Vùng',          'terms' => $vung),
        'xuat-xu'      => array('label' => 'Xuất xứ',       'terms' => $xuat_xu),
        'phan-hang'    => array('label' => 'Phân hạng',     'terms' => $phan_hang),
        'giong-nho'    => array('label' => 'Giống nho',     'terms' => $giong_nho),
        'nien-vu'      => array('label' => 'Niên vụ',       'terms' => $nien_vu),
        'quy-cach'     => array('label' => 'Quy cách',      'terms' => $quy_cach),
    );

    foreach ($fields as $taxonomy => $data) {
        if ($data['terms'] && !is_wp_error($data['terms'])) {
            $term_names = wp_list_pluck($data['terms'], 'name');
            $term_list  = implode(', ', $term_names);
            if ($term_list) {
                echo '<tr>';
                echo '<td style="padding: 8px; border: 1px solid #ddd; font-weight: bold;">' . esc_html($data['label']) . '</td>';
                echo '<td style="padding: 8px; border: 1px solid #ddd;">' . esc_html($term_list) . '</td>';
                echo '</tr>';
            }
        }
    }

    echo '</table>';
    echo '</div>';

    return ob_get_clean();
}
add_shortcode('sago_ts_product', 'sago_cung_ts_func');