<?php
defined('ABSPATH') || exit;

// Danh sách các taxonomy cần áp dụng
$custom_taxonomies = ['category', 'post_tag', 'questions', 'questions_tag'];

/**
 * Hiển thị form thêm mới term (Add)
 */
function cms_add_term_custom_fields($taxonomy_slug) {
    ?>
    <div class="form-field term-title-wrap">
        <label for="term_title"><?php _e('Secondary Title', TEXT_DOMAIN); ?></label>
        <input type="text" name="term_title" id="term_title" value="" />
        <p><?php _e('Nhập tiêu đề thứ hai cho mục này.', TEXT_DOMAIN); ?></p>
    </div>

    <div class="form-field term-content-wrap">
        <label for="term_content"><?php _e('Content', TEXT_DOMAIN); ?></label>
        <?php
        wp_editor('', 'term_content_editor_' . esc_attr($taxonomy_slug), [
            'textarea_name' => 'term_content',
            'media_buttons' => true,
            'textarea_rows' => 10,
            'quicktags'     => true,
        ]);
        ?>
        <p><?php _e('Nhập nội dung cho taxonomy này.', TEXT_DOMAIN); ?></p>
    </div>
    <?php
}

/**
 * Hiển thị form chỉnh sửa term (Edit)
 */
function cms_edit_term_custom_fields($term, $taxonomy_slug) {
    $term_title = get_term_meta($term->term_id, 'term_title', true);
    $content = get_term_meta($term->term_id, 'term_content', true);
    ?>
    <tr class="form-field term-title-wrap">
        <th scope="row"><label for="term_title"><?php _e('Secondary Title', TEXT_DOMAIN); ?></label></th>
        <td><input type="text" name="term_title" id="term_title" value="<?php echo esc_attr($term_title); ?>" /></td>
    </tr>

    <tr class="form-field term-content-wrap">
        <th scope="row"><label for="term_content"><?php _e('Content', TEXT_DOMAIN); ?></label></th>
        <td>
            <?php
            wp_editor($content, 'term_content_editor_' . esc_attr($taxonomy_slug), [
                'textarea_name' => 'term_content',
                'media_buttons' => true,
                'textarea_rows' => 10,
                'quicktags'     => true,
            ]);
            ?>
            <p class="description"><?php _e('Nội dung của taxonomy.', TEXT_DOMAIN); ?></p>
        </td>
    </tr>
    <?php
}

/**
 * Lưu dữ liệu custom field khi thêm/sửa term
 */
function cms_save_term_custom_fields($term_id) {
    // Xử lý term_title
    if (isset($_POST['term_title'])) {
        $new_title = sanitize_text_field($_POST['term_title']);
        $old_title = get_term_meta($term_id, 'term_title', true);

        if ($new_title !== $old_title) {
            if ($new_title !== '') {
                update_term_meta($term_id, 'term_title', $new_title);
            } elseif ($old_title !== '') {
                delete_term_meta($term_id, 'term_title');
            }
        }
    }

    // Xử lý term_content
    if (isset($_POST['term_content'])) {
        $new_content = wp_kses_post($_POST['term_content']);
        $old_content = get_term_meta($term_id, 'term_content', true);

        if ($new_content !== $old_content) {
            if ($new_content !== '') {
                update_term_meta($term_id, 'term_content', $new_content);
            } elseif ($old_content !== '') {
                delete_term_meta($term_id, 'term_content');
            }
        }
    }
}


/**
 * Gắn hook cho tất cả taxonomy cần xử lý
 */
foreach ($custom_taxonomies as $taxonomy) {

    // Form thêm mới
    add_action("{$taxonomy}_add_form_fields", function () use ($taxonomy) {
        cms_add_term_custom_fields($taxonomy);
    });

    // Form chỉnh sửa
    add_action("{$taxonomy}_edit_form_fields", function ($term) use ($taxonomy) {
        cms_edit_term_custom_fields($term, $taxonomy);
    });

    // Hook lưu
    add_action("created_{$taxonomy}", 'cms_save_term_custom_fields');
    add_action("edited_{$taxonomy}", 'cms_save_term_custom_fields');
}