/* 
Theme Name: Hello Elementor Child
Theme URI: https://github.com/elementor/hello-theme-child/
Description: Hello Elementor Child is a child theme of Hello Elementor, created by Elementor team
Author: Elementor Team
Author URI: https://elementor.com/
Template: hello-elementor
Version: 2.0.0
Text Domain: hello-elementor-child
License: GNU General Public License v3 or later.
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Tags: flexible-header, custom-colors, custom-menu, custom-logo, editor-style, featured-images, rtl-language-support, threaded-comments, translation-ready
*/

/* Add your custom styles here */

// Register Services Post Type
function register_services_post_type() {
    $labels = array(
        'name'                  => 'Services',
        'singular_name'         => 'Service',
        'menu_name'             => 'Services',
        'name_admin_bar'        => 'Service',
        'archives'              => 'Service Arkiv',
        'all_items'             => 'Alle Services',
        'add_new_item'          => 'Tilføj Ny Service',
        'add_new'               => 'Tilføj Ny',
        'new_item'              => 'Ny Service',
        'edit_item'             => 'Rediger Service',
        'update_item'           => 'Opdater Service',
        'view_item'             => 'Se Service',
        'search_items'          => 'Søg Services',
        'featured_image'        => 'Service Billede',
        'set_featured_image'    => 'Vælg service billede',
    );

    $args = array(
        'label'                 => 'Service',
        'description'           => 'Skadeservice ydelser',
        'labels'                => $labels,
        'supports'              => array('title', 'editor', 'excerpt', 'thumbnail', 'revisions'),
        'taxonomies'            => array('service_category'),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-admin-tools',
        'has_archive'           => 'services',
        'capability_type'       => 'post',
        'show_in_rest'          => true, // Elementor support
        'rewrite'               => array('slug' => 'service'),
    );

    register_post_type('service', $args);
}
add_action('init', 'register_services_post_type', 0);

// Register Service Categories
function register_service_categories_taxonomy() {
    $labels = array(
        'name'                       => 'Service Kategorier',
        'singular_name'              => 'Service Kategori',
        'menu_name'                  => 'Kategorier',
        'all_items'                  => 'Alle Kategorier',
        'add_new_item'               => 'Tilføj Ny Kategori',
        'edit_item'                  => 'Rediger Kategori',
    );

    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_in_rest'               => true,
        'rewrite'                    => array('slug' => 'service-kategori'),
    );

    register_taxonomy('service_category', array('service'), $args);
}
add_action('init', 'register_service_categories_taxonomy', 0);

// Add Custom Meta Fields
function add_service_meta_boxes() {
    add_meta_box(
        'service-details',
        'Service Detaljer',
        'service_details_callback',
        'service'
    );
}
add_action('add_meta_boxes', 'add_service_meta_boxes');

function service_details_callback($post) {
    wp_nonce_field('save_service_details', 'service_details_nonce');
    
    $service_icon = get_post_meta($post->ID, '_service_icon', true);
    $service_price_from = get_post_meta($post->ID, '_service_price_from', true);
    $response_time = get_post_meta($post->ID, '_response_time', true);
    $availability = get_post_meta($post->ID, '_availability', true);
    $emergency_service = get_post_meta($post->ID, '_emergency_service', true);
    $service_features = get_post_meta($post->ID, '_service_features', true);
    $cta_text = get_post_meta($post->ID, '_cta_text', true);
    $cta_phone = get_post_meta($post->ID, '_cta_phone', true);
    $seo_focus_keyword = get_post_meta($post->ID, '_seo_focus_keyword', true);
    $seo_meta_description = get_post_meta($post->ID, '_seo_meta_description', true);
    ?>
    
    <table class="form-table">
        <tr>
            <th><label for="service_icon">Service Ikon</label></th>
            <td><input type="text" id="service_icon" name="service_icon" value="<?php echo esc_attr($service_icon); ?>" placeholder="🔥 eller <i class='fas fa-fire'></i>" /></td>
        </tr>
        <tr>
            <th><label for="service_price_from">Pris fra (DKK)</label></th>
            <td><input type="number" id="service_price_from" name="service_price_from" value="<?php echo esc_attr($service_price_from); ?>" /></td>
        </tr>
        <tr>
            <th><label for="response_time">Responstid</label></th>
            <td><input type="text" id="response_time" name="response_time" value="<?php echo esc_attr($response_time); ?>" placeholder="Under 1 time" /></td>
        </tr>
        <tr>
            <th><label for="availability">Tilgængelighed</label></th>
            <td>
                <select id="availability" name="availability">
                    <option value="24/7" <?php selected($availability, '24/7'); ?>>24/7 Døgnservice</option>
                    <option value="business" <?php selected($availability, 'business'); ?>>Almindelig åbningstid</option>
                    <option value="emergency" <?php selected($availability, 'emergency'); ?>>Kun akut</option>
                </select>
            </td>
        </tr>
        <tr>
            <th><label for="emergency_service">Akut service</label></th>
            <td><input type="checkbox" id="emergency_service" name="emergency_service" value="1" <?php checked($emergency_service, 1); ?> /> Tilgængelig som akut service</td>
        </tr>
        <tr>
            <th><label for="service_features">Service Features (en per linje)</label></th>
            <td><textarea id="service_features" name="service_features" rows="5" cols="50"><?php echo esc_textarea($service_features); ?></textarea></td>
        </tr>
        <tr>
            <th><label for="cta_text">Call-to-Action Tekst</label></th>
            <td><input type="text" id="cta_text" name="cta_text" value="<?php echo esc_attr($cta_text); ?>" placeholder="Få hjælp nu" /></td>
        </tr>
        <tr>
            <th><label for="cta_phone">Call-to-Action Telefon</label></th>
            <td><input type="text" id="cta_phone" name="cta_phone" value="<?php echo esc_attr($cta_phone); ?>" placeholder="+45 12 34 56 78" /></td>
        </tr>
    </table>
    
    <h3>SEO Indstillinger</h3>
    <table class="form-table">
        <tr>
            <th><label for="seo_focus_keyword">Fokus Søgeord</label></th>
            <td><input type="text" id="seo_focus_keyword" name="seo_focus_keyword" value="<?php echo esc_attr($seo_focus_keyword); ?>" /></td>
        </tr>
        <tr>
            <th><label for="seo_meta_description">Meta Beskrivelse</label></th>
            <td><textarea id="seo_meta_description" name="seo_meta_description" rows="3" cols="50" maxlength="160"><?php echo esc_textarea($seo_meta_description); ?></textarea></td>
        </tr>
    </table>
    <?php
}

function save_service_details($post_id) {
    if (!isset($_POST['service_details_nonce']) || !wp_verify_nonce($_POST['service_details_nonce'], 'save_service_details')) {
        return;
    }

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }

    $fields = array(
        'service_icon' => '_service_icon',
        'service_price_from' => '_service_price_from',
        'response_time' => '_response_time',
        'availability' => '_availability',
        'emergency_service' => '_emergency_service',
        'service_features' => '_service_features',
        'cta_text' => '_cta_text',
        'cta_phone' => '_cta_phone',
        'seo_focus_keyword' => '_seo_focus_keyword',
        'seo_meta_description' => '_seo_meta_description'
    );

    foreach ($fields as $field => $meta_key) {
        if (isset($_POST[$field])) {
            update_post_meta($post_id, $meta_key, sanitize_text_field($_POST[$field]));
        }
    }
}
add_action('save_post', 'save_service_details');

// SEO Meta Tags
function add_service_seo_meta() {
    if (is_singular('service')) {
        global $post;
        $meta_description = get_post_meta($post->ID, '_seo_meta_description', true);
        $focus_keyword = get_post_meta($post->ID, '_seo_focus_keyword', true);
        
        if ($meta_description) {
            echo '<meta name="description" content="' . esc_attr($meta_description) . '">' . "\n";
        }
        
        // Schema markup
        $schema = array(
            '@context' => 'https://schema.org',
            '@type' => 'Service',
            'name' => get_the_title(),
            'description' => $meta_description ?: get_the_excerpt(),
            'provider' => array(
                '@type' => 'Organization',
                'name' => get_bloginfo('name'),
                'telephone' => get_post_meta($post->ID, '_cta_phone', true)
            )
        );
        
        echo '<script type="application/ld+json">' . json_encode($schema) . '</script>' . "\n";
    }
}
add_action('wp_head', 'add_service_seo_meta');

// Enqueue styles
function enqueue_service_styles() {
    if (is_singular('service') || is_post_type_archive('service')) {
        wp_enqueue_style('services-style', get_template_directory_uri() . '/css/services.css');
    }
}
add_action('wp_enqueue_scripts', 'enqueue_service_styles');
update_option('skadeanmeldelse_admin_emails', 'casper@chtech.dk, lars@lyngholm.nu');
