/**
 * Enqueue Postal Code Autocomplete Scripts
 */
function enqueue_postal_autocomplete() {
    if (is_front_page() || is_page()) {
        $theme_dir = get_template_directory_uri();
        
        wp_enqueue_style(
            'postal-autocomplete-css', 
            $theme_dir . '/postal-autocomplete.css', 
            array(), 
            '1.0.0'
        );
        
        wp_enqueue_script(
            'postal-autocomplete-js', 
            $theme_dir . '/postal-autocomplete.js', 
            array('jquery'), 
            '1.0.0', 
            true
        );
        
        wp_localize_script('postal-autocomplete-js', 'postalData', array(
            'ajaxurl' => admin_url('admin-ajax.php'),
            'nonce'   => wp_create_nonce('postal_nonce')
        ));
    }
}
add_action('wp_enqueue_scripts', 'enqueue_postal_autocomplete');

/**
 * AJAX Handler: Search Postal Codes (ตำบล/อำเภอ/จังหวัด)
 */
function search_postal_codes_ajax() {
    // ตรวจสอบ nonce
    if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'postal_nonce')) {
        wp_send_json_error('Invalid nonce', 403);
    }
    
    $search = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '';
    
    if (strlen($search) < 2) {
        wp_send_json_success(array());
    }
    
    $results = array();
    
    // ค้นหาจาก taxonomy 'destinations' (ตำบล/อำเภอ/จังหวัด)
    $terms = get_terms(array(
        'taxonomy'   => 'destinations',
        'hide_empty' => false,
        'number'     => 10,
        'name__like' => $search,
    ));
    
    if (!is_wp_error($terms) && !empty($terms)) {
        foreach ($terms as $term) {
            $full_address = $term->name;
            
            // ถ้ามี parent (อำเภอ/จังหวัด) ให้เอามาต่อ
            if ($term->parent > 0) {
                $parent = get_term($term->parent, 'destinations');
                if (!is_wp_error($parent)) {
                    $full_address = $term->name . ', ' . $parent->name;
                    
                    // ถ้า parent ยังมี parent อีก (จังหวัด)
                    if ($parent->parent > 0) {
                        $grandparent = get_term($parent->parent, 'destinations');
                        if (!is_wp_error($grandparent)) {
                            $full_address = $term->name . ', ' . $parent->name . ', ' . $grandparent->name;
                        }
                    }
                }
            }
            
            $results[] = array(
                'id'           => $term->term_id,
                'name'         => $term->name,
                'full_address' => $full_address,
                'slug'         => $term->slug,
            );
        }
    }
    
    wp_send_json_success($results);
}
add_action('wp_ajax_search_postal_codes', 'search_postal_codes_ajax');
add_action('wp_ajax_nopriv_search_postal_codes', 'search_postal_codes_ajax');{"id":6358,"count":0,"description":"","link":"https:\/\/npx78.com\/?tambon=fangdaeng-1","name":"\u0e1d\u0e31\u0e48\u0e07\u0e41\u0e14\u0e07","slug":"fangdaeng-1","taxonomy":"tambon","parent":1876,"meta":[],"acf":[],"_links":{"self":[{"href":"https:\/\/npx78.com\/index.php?rest_route=\/wp\/v2\/tambon\/6358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/npx78.com\/index.php?rest_route=\/wp\/v2\/tambon"}],"about":[{"href":"https:\/\/npx78.com\/index.php?rest_route=\/wp\/v2\/taxonomies\/tambon"}],"wp:post_type":[{"href":"https:\/\/npx78.com\/index.php?rest_route=%2Fwp%2Fv2%2Fdestinations&tambon=6358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}