<?php
/**
 * 联系地图区块模板
 * 两栏布局：左边联系方式，右边Google地图
 */

// 防止直接访问
if (!defined('ABSPATH')) {
    exit;
}

// 获取短代码参数（如果通过短代码调用）
global $contact_map_shortcode_atts;
$shortcode_atts = isset($contact_map_shortcode_atts) ? $contact_map_shortcode_atts : array();

// 获取自定义器设置，短代码参数优先
$title = !empty($shortcode_atts['title']) ? $shortcode_atts['title'] : get_theme_mod('contact_map_title', '联系我们');
$office_address = !empty($shortcode_atts['office_address']) ? $shortcode_atts['office_address'] : get_theme_mod('contact_office_address', 'i288 Tongxie Road Shanghai China 200335');
$phone = !empty($shortcode_atts['phone']) ? $shortcode_atts['phone'] : get_theme_mod('contact_phone', '+86-21-50896255');
$email = !empty($shortcode_atts['email']) ? $shortcode_atts['email'] : get_theme_mod('contact_email', 'info@nhet.energy');
$map_embed_code = !empty($shortcode_atts['map_embed_code']) ? $shortcode_atts['map_embed_code'] : get_theme_mod('contact_map_embed_code', '');
$map_height = !empty($shortcode_atts['map_height']) ? $shortcode_atts['map_height'] : get_theme_mod('contact_map_height', '400px');

// 样式设置
$bg_color = get_theme_mod('contact_bg_color', '#f8f9fa');
$text_color = get_theme_mod('contact_text_color', '#333333');
$icon_color = get_theme_mod('contact_icon_color', '#007cba');

// 容器ID和类
$container_id = !empty($shortcode_atts['id']) ? $shortcode_atts['id'] : 'nenghui-contact-map';
$container_class = 'nenghui-contact-map';
if (!empty($shortcode_atts['class'])) {
    $container_class .= ' ' . $shortcode_atts['class'];
}

// 如果没有设置地图嵌入代码，使用默认的Google地图代码
if (empty($map_embed_code)) {
    $default_map_file = get_template_directory() . '/docs/Google-maps.txt';
    if (file_exists($default_map_file)) {
        $map_embed_code = file_get_contents($default_map_file);
    }
}

// 处理地图高度
if (!preg_match('/^\d+(\.\d+)?(px|vh|%)$/', $map_height)) {
    $map_height = '400px';
}
?>

<section id="<?php echo esc_attr($container_id); ?>" class="<?php echo esc_attr($container_class); ?>" style="background-color: <?php echo esc_attr($bg_color); ?>; color: <?php echo esc_attr($text_color); ?>;">
    <div class="container">
        <?php if (!empty($title)): ?>
        <div class="contact-map-header">
            <h2 class="contact-map-title"><?php echo esc_html($title); ?></h2>
        </div>
        <?php endif; ?>
        
        <div class="contact-map-content">
            <!-- 左栏：联系方式 -->
            <div class="contact-info">
                <div class="contact-info-inner">
                    <?php if (!empty($office_address)): ?>
                    <div class="contact-item">
                        <div class="contact-icon" style="color: <?php echo esc_attr($icon_color); ?>;">
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
                                <path d="M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z"/>
                            </svg>
                        </div>
                        <div class="contact-details">
                            <h3 class="contact-label">OFFICE ADDRESS:</h3>
                            <p class="contact-value"><?php echo nl2br(esc_html($office_address)); ?></p>
                        </div>
                    </div>
                    <?php endif; ?>
                    
                    <?php if (!empty($phone)): ?>
                    <div class="contact-item">
                        <div class="contact-icon" style="color: <?php echo esc_attr($icon_color); ?>;">
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
                                <path d="M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z"/>
                            </svg>
                        </div>
                        <div class="contact-details">
                            <h3 class="contact-label">PHONE / MOBILE:</h3>
                            <p class="contact-value">
                                <a href="tel:<?php echo esc_attr(str_replace(array(' ', '-', '(', ')'), '', $phone)); ?>" style="color: inherit; text-decoration: none;">
                                    <?php echo esc_html($phone); ?>
                                </a>
                            </p>
                        </div>
                    </div>
                    <?php endif; ?>
                    
                    <?php if (!empty($email)): ?>
                    <div class="contact-item">
                        <div class="contact-icon" style="color: <?php echo esc_attr($icon_color); ?>;">
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
                                <path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4l-8 5-8-5V6l8 5 8-5v2z"/>
                            </svg>
                        </div>
                        <div class="contact-details">
                            <h3 class="contact-label">MAILBOX:</h3>
                            <p class="contact-value">
                                <a href="mailto:<?php echo esc_attr($email); ?>" style="color: inherit; text-decoration: none;">
                                    <?php echo esc_html($email); ?>
                                </a>
                            </p>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
            
            <!-- 右栏：Google地图 -->
            <div class="contact-map">
                <div class="contact-map-container" style="height: <?php echo esc_attr($map_height); ?>; min-height: 300px;">
                    <?php if (!empty($map_embed_code)): ?>
                        <?php echo $map_embed_code; ?>
                    <?php else: ?>
                        <div class="map-placeholder" style="height: 100%; background-color: #e9ecef; display: flex; align-items: center; justify-content: center; border-radius: 8px;">
                            <p style="color: #6c757d; text-align: center; margin: 0;">
                                请在自定义器中设置地图嵌入代码<br>
                                <small>联系地图设置 → 地图设置 → 地图嵌入代码</small>
                            </p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</section>

<style>
.nenghui-contact-map {
    padding: 80px 0;
    position: relative;
}

.nenghui-contact-map .container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
}

.contact-map-header {
    text-align: center;
    margin-bottom: 60px;
}

.contact-map-title {
    font-size: 2.5rem;
    font-weight: 700;
    margin: 0;
    letter-spacing: 2px;
}

.contact-map-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 60px;
    align-items: start;
}

.contact-info {
    padding: 40px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.contact-info-inner {
    display: flex;
    flex-direction: column;
    gap: 40px;
}

.contact-item {
    display: flex;
    align-items: flex-start;
    gap: 20px;
}

.contact-icon {
    flex-shrink: 0;
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    border: 2px solid currentColor;
}

.contact-icon svg {
    width: 24px;
    height: 24px;
}

.contact-details {
    flex: 1;
}

.contact-label {
    font-size: 0.9rem;
    font-weight: 600;
    letter-spacing: 1px;
    margin: 0 0 8px 0;
    opacity: 0.8;
}

.contact-value {
    font-size: 1.1rem;
    line-height: 1.6;
    margin: 0;
    font-weight: 500;
}

.contact-value a:hover {
    color: #005580;
    text-decoration: underline;
    transition: all 0.3s ease;
}

.contact-map {
    position: relative;
}

.contact-map-container {
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    position: relative;
}

.contact-map-container iframe {
    width: 100% !important;
    height: 100% !important;
    border: none !important;
    display: block;
    border-radius: 8px;
    position: relative !important;
}

/* 针对Google地图嵌入代码的样式 */
.contact-map-container .embed-map-responsive,
.contact-map-container > div {
    width: 100% !important;
    height: 100% !important;
    position: relative !important;
    padding-bottom: 0 !important;
    padding: 0 !important;
}   

.contact-map-container .embed-map-container {
    width: 100% !important;
    height: 100% !important;
    position: relative !important;
    overflow: hidden !important;
    background: none !important;
}

.contact-map-container .embed-map-frame {
    width: 100% !important;
    height: 100% !important;
    position: relative !important;
    border-radius: 8px;
    border: none !important;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .nenghui-contact-map {
        padding: 60px 0;
    }
    
    .contact-map-title {
        font-size: 2rem;
    }
    
    .contact-map-content {
        grid-template-columns: 1fr;
        gap: 40px;
    }
    
    .contact-info {
        padding: 30px;
    }
    
    .contact-info-inner {
        gap: 30px;
    }
    
    .contact-item {
        gap: 15px;
    }
    
    .contact-icon {
        width: 40px;
        height: 40px;
    }
    
    .contact-icon svg {
        width: 20px;
        height: 20px;
    }
    
    .contact-label {
        font-size: 0.8rem;
    }
    
    .contact-value {
        font-size: 1rem;
    }
}

@media (max-width: 480px) {
    .nenghui-contact-map {
        padding: 40px 0;
    }
    
    .contact-map-header {
        margin-bottom: 40px;
    }
    
    .contact-map-title {
        font-size: 1.8rem;
    }
    
    .contact-info {
        padding: 20px;
    }
    
    .contact-map-container {
        height: 300px !important;
    }
}

/* 动画效果 */
.nenghui-contact-map .contact-item {
    opacity: 0;
    transform: translateY(20px);
    animation: fadeInUp 0.6s ease forwards;
}

.nenghui-contact-map .contact-item:nth-child(1) {
    animation-delay: 0.1s;
}

.nenghui-contact-map .contact-item:nth-child(2) {
    animation-delay: 0.2s;
}

.nenghui-contact-map .contact-item:nth-child(3) {
    animation-delay: 0.3s;
}

.contact-map-container {
    opacity: 0;
    transform: translateX(20px);
    animation: fadeInRight 0.8s ease 0.4s forwards;
}

@keyframes fadeInUp {
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes fadeInRight {
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

/* 地图响应式处理 - 完全覆盖Google-maps.txt中的样式 */
.nenghui-contact-map .embed-map-responsive {
    position: relative !important;
    text-align: left !important;
    width: 100% !important;
    height: 100% !important;
    padding-bottom: 0 !important;
    padding: 0 !important;
}

.nenghui-contact-map .embed-map-container {
    overflow: hidden !important;
    background: none !important;
    width: 100% !important;
    height: 100% !important;
    position: relative !important;
    top: 0 !important;
    left: 0 !important;
}

.nenghui-contact-map .embed-map-frame {
    width: 100% !important;
    height: 100% !important;
    position: relative !important;
    top: 0 !important;
    left: 0 !important;
    border-radius: 12px;
    border: none !important;
}

/* 强制覆盖内联样式 */
.nenghui-contact-map .map-embed-wrapper style {
    display: none !important;
}

.nenghui-contact-map .map-embed-wrapper > div {
    position: relative !important;
    width: 100% !important;
    height: 100% !important;
    padding-bottom: 0 !important;
}
</style>