<?php
/**
 * Plugin Name: מחשבון פרגולה MOMENT — sade-il.com
 * Description: מוסיף את הקוד המקוצר [sade_pergola_widget] שמציג את מחשבון הפרגולה MOMENT. בדיקה עצמית, לא אישור מהנדס.
 * Version: 1.1.1
 * Author: א. סדצקי הנדסה וייעוץ
 * Author URI: https://sade-il.com
 * License: Free to use. Keep the credit line; you may add rel="nofollow" or rel="sponsored" to its link.
 */

if (!defined('ABSPATH')) {
    exit;
}

function sade_pergola_widget_shortcode($atts)
{
    $atts = shortcode_atts(array('height' => '300'), $atts, 'sade_pergola_widget');
    $height = absint($atts['height']);
    if ($height < 200) {
        $height = 300;
    }
    static $count = 0;
    $count++;
    $id = 'sade-pergola-widget-' . $count;

    $html  = '<iframe src="https://sade-il.com/embed/pergola-card" title="מחשבון פרגולה MOMENT" width="100%" height="' . $height . '" loading="lazy" style="border:0;max-width:100%" id="' . esc_attr($id) . '"></iframe>';
    $html .= '<p style="font-size:14px">מחשבון פרגולה מאת <a href="https://sade-il.com/pergola-calculator">א. סדצקי הנדסה וייעוץ — sade-il.com</a></p>';
    if ($count === 1) {
        $html .= '<script>(function(){if(window.sadeWidget)return;window.sadeWidget=1;var O="https://sade-il.com",m;function close(){if(m){m.remove();m=null;document.documentElement.style.overflow=""}}function open(){if(m)return;m=document.createElement("div");m.setAttribute("style","position:fixed;inset:0;z-index:2147483000;background:rgba(20,15,10,.6);display:flex;align-items:center;justify-content:center;padding:12px");m.innerHTML=\'<div style="position:relative;width:100%;max-width:640px;height:92vh;background:#fff;border-radius:16px;overflow:hidden;box-shadow:0 20px 60px rgba(0,0,0,.35)"><button type="button" aria-label="סגירה" style="position:absolute;top:8px;left:8px;z-index:1;width:36px;height:36px;border:0;border-radius:50%;background:#f3ede4;font-size:20px;cursor:pointer">×</button><iframe src="\'+O+\'/embed/pergola-quick" title="מחשבון פרגולה MOMENT" style="border:0;width:100%;height:100%"></iframe></div>\';m.addEventListener("click",function(e){if(e.target===m||e.target.tagName==="BUTTON")close()});document.body.appendChild(m);document.documentElement.style.overflow="hidden"}function ping(){var f=document.querySelectorAll(\'iframe[src^="\'+O+\'/embed/pergola-card"]\');for(var i=0;i<f.length;i++)if(f[i].contentWindow)f[i].contentWindow.postMessage({type:"sade-host-ready"},O)}document.addEventListener("load",function(e){if(e.target&&e.target.tagName==="IFRAME")ping()},true);setTimeout(ping,1000);setTimeout(ping,3000);addEventListener("keydown",function(e){if(e.key==="Escape")close()});addEventListener("message",function(e){if(e.origin!==O||!e.data)return;var d=e.data;if(d.type==="sade-widget-open"){open();return}if(d.type==="sade-widget-close"){close();return}if(d.type!=="sade-widget-height")return;var f=document.querySelectorAll(\'iframe[src^="\'+O+\'/embed/pergola-card"]\');for(var i=0;i<f.length;i++){if(f[i].contentWindow===e.source){f[i].style.height=d.height+"px";e.source.postMessage({type:"sade-host-ready"},O)}}})})();</script>';
    }
    return $html;
}
add_shortcode('sade_pergola_widget', 'sade_pergola_widget_shortcode');
