<?php defined('ABSPATH') || exit;
$plan = \DZQCOD\Core::opt('license_plan','free');
$templates = [
    ['id'=>'default','name'=>'الأساسي','plan'=>'free','desc'=>'عمودي كلاسيكي','primary'=>'#667eea','secondary'=>'#764ba2','success'=>'#27ae60','bg'=>'#ffffff','text'=>'#333333','btn_text'=>'#ffffff','radius'=>'12','layout'=>'stacked','input_style'=>'bordered','group_bg'=>'#f8fafc','group_title'=>'yes','field_bg'=>'#ffffff','field_border'=>'#e2e8f0','label_color'=>'#374151','shadow'=>'none','total_style'=>'card'],
    ['id'=>'modern-inline','name'=>'عصري متقابل','plan'=>'free','desc'=>'حقول جنب بعض','primary'=>'#6366f1','secondary'=>'#8b5cf6','success'=>'#22c55e','bg'=>'#fafafe','text'=>'#1e293b','btn_text'=>'#ffffff','radius'=>'14','layout'=>'inline','input_style'=>'bordered','group_bg'=>'#eef2ff','group_title'=>'yes','field_bg'=>'#ffffff','field_border'=>'#c7d2fe','label_color'=>'#4338ca','shadow'=>'sm','total_style'=>'gradient'],
    ['id'=>'dark-pro','name'=>'داكن احترافي','plan'=>'pro','desc'=>'خلفية داكنة فخمة','primary'=>'#8b5cf6','secondary'=>'#6d28d9','success'=>'#10b981','bg'=>'#0f172a','text'=>'#e2e8f0','btn_text'=>'#ffffff','radius'=>'16','layout'=>'stacked','input_style'=>'filled','group_bg'=>'#1e293b','group_title'=>'yes','field_bg'=>'#1e293b','field_border'=>'#334155','label_color'=>'#94a3b8','shadow'=>'lg','total_style'=>'gradient'],
    ['id'=>'ocean','name'=>'أزرق محيطي','plan'=>'pro','desc'=>'بطاقات زرقاء','primary'=>'#0ea5e9','secondary'=>'#0284c7','success'=>'#14b8a6','bg'=>'#f0f9ff','text'=>'#0c4a6e','btn_text'=>'#ffffff','radius'=>'14','layout'=>'inline','input_style'=>'bordered','group_bg'=>'#e0f2fe','group_title'=>'yes','field_bg'=>'#ffffff','field_border'=>'#7dd3fc','label_color'=>'#0369a1','shadow'=>'sm','total_style'=>'card'],
    ['id'=>'rose','name'=>'وردي أنيق','plan'=>'pro','desc'=>'دافئ مع ظلال','primary'=>'#f43f5e','secondary'=>'#e11d48','success'=>'#f59e0b','bg'=>'#fff1f2','text'=>'#4c0519','btn_text'=>'#ffffff','radius'=>'20','layout'=>'stacked','input_style'=>'bordered','group_bg'=>'#ffe4e6','group_title'=>'yes','field_bg'=>'#ffffff','field_border'=>'#fda4af','label_color'=>'#be123c','shadow'=>'sm','total_style'=>'gradient'],
    ['id'=>'minimal','name'=>'بسيط نظيف','plan'=>'enterprise','desc'=>'خطوط سفلية فقط','primary'=>'#374151','secondary'=>'#1f2937','success'=>'#059669','bg'=>'#ffffff','text'=>'#111827','btn_text'=>'#ffffff','radius'=>'4','layout'=>'stacked','input_style'=>'underline','group_bg'=>'transparent','group_title'=>'no','field_bg'=>'transparent','field_border'=>'#d1d5db','label_color'=>'#6b7280','shadow'=>'none','total_style'=>'simple'],
    ['id'=>'neon','name'=>'نيون متوهج','plan'=>'enterprise','desc'=>'داكن مع ألوان مضيئة','primary'=>'#22d3ee','secondary'=>'#06b6d4','success'=>'#34d399','bg'=>'#0f172a','text'=>'#f1f5f9','btn_text'=>'#0f172a','radius'=>'12','layout'=>'inline','input_style'=>'filled','group_bg'=>'#1e293b','group_title'=>'yes','field_bg'=>'#0f172a','field_border'=>'#22d3ee','label_color'=>'#67e8f9','shadow'=>'lg','total_style'=>'gradient'],
    ['id'=>'forest','name'=>'غابة خضراء','plan'=>'enterprise','desc'=>'ألوان طبيعية','primary'=>'#16a34a','secondary'=>'#15803d','success'=>'#22c55e','bg'=>'#f0fdf4','text'=>'#14532d','btn_text'=>'#ffffff','radius'=>'10','layout'=>'stacked','input_style'=>'bordered','group_bg'=>'#dcfce7','group_title'=>'yes','field_bg'=>'#ffffff','field_border'=>'#86efac','label_color'=>'#166534','shadow'=>'sm','total_style'=>'card'],
    ['id'=>'sunset','name'=>'غروب دافئ','plan'=>'enterprise','desc'=>'برتقالي دافئ','primary'=>'#f97316','secondary'=>'#ea580c','success'=>'#84cc16','bg'=>'#fffbeb','text'=>'#78350f','btn_text'=>'#ffffff','radius'=>'18','layout'=>'inline','input_style'=>'bordered','group_bg'=>'#fef3c7','group_title'=>'yes','field_bg'=>'#ffffff','field_border'=>'#fdba74','label_color'=>'#c2410c','shadow'=>'sm','total_style'=>'gradient'],
    ['id'=>'royal','name'=>'ملكي بنفسجي','plan'=>'enterprise','desc'=>'فخامة ملكية','primary'=>'#7c3aed','secondary'=>'#6d28d9','success'=>'#a78bfa','bg'=>'#faf5ff','text'=>'#3b0764','btn_text'=>'#ffffff','radius'=>'8','layout'=>'stacked','input_style'=>'bordered','group_bg'=>'#ede9fe','group_title'=>'yes','field_bg'=>'#ffffff','field_border'=>'#c4b5fd','label_color'=>'#5b21b6','shadow'=>'sm','total_style'=>'card'],
    ['id'=>'candy','name'=>'حلوى وردية','plan'=>'enterprise','desc'=>'مرح للمنتجات النسائية','primary'=>'#ec4899','secondary'=>'#db2777','success'=>'#f472b6','bg'=>'#fdf2f8','text'=>'#831843','btn_text'=>'#ffffff','radius'=>'22','layout'=>'inline','input_style'=>'bordered','group_bg'=>'#fce7f3','group_title'=>'yes','field_bg'=>'#ffffff','field_border'=>'#f9a8d4','label_color'=>'#9d174d','shadow'=>'sm','total_style'=>'gradient'],
    ['id'=>'arctic','name'=>'ثلجي قطبي','plan'=>'enterprise','desc'=>'أزرق بارد','primary'=>'#38bdf8','secondary'=>'#0ea5e9','success'=>'#67e8f9','bg'=>'#ecfeff','text'=>'#164e63','btn_text'=>'#ffffff','radius'=>'24','layout'=>'stacked','input_style'=>'bordered','group_bg'=>'#cffafe','group_title'=>'yes','field_bg'=>'#ffffff','field_border'=>'#7dd3fc','label_color'=>'#0e7490','shadow'=>'sm','total_style'=>'card'],
    ['id'=>'earth','name'=>'ترابي طبيعي','plan'=>'enterprise','desc'=>'ألوان أرضية','primary'=>'#a16207','secondary'=>'#92400e','success'=>'#d97706','bg'=>'#fefce8','text'=>'#422006','btn_text'=>'#ffffff','radius'=>'10','layout'=>'stacked','input_style'=>'underline','group_bg'=>'#fef9c3','group_title'=>'no','field_bg'=>'transparent','field_border'=>'#d97706','label_color'=>'#854d0e','shadow'=>'none','total_style'=>'simple'],
    ['id'=>'glass','name'=>'زجاجي شفاف','plan'=>'enterprise','desc'=>'تأثير زجاج مطفي','primary'=>'#6366f1','secondary'=>'#4f46e5','success'=>'#10b981','bg'=>'#0f172a','text'=>'#e2e8f0','btn_text'=>'#ffffff','radius'=>'16','layout'=>'inline','input_style'=>'filled','group_bg'=>'#1a2332','group_title'=>'yes','field_bg'=>'#151f2e','field_border'=>'#2d3a4d','label_color'=>'#94a3b8','shadow'=>'lg','total_style'=>'gradient'],
];
$colors=['form_primary_color'=>['اللون الرئيسي',\DZQCOD\Core::opt('form_primary_color','#667eea')],'form_secondary_color'=>['التدرج',\DZQCOD\Core::opt('form_secondary_color','#764ba2')],'form_success_color'=>['لون الزر',\DZQCOD\Core::opt('form_success_color','#27ae60')],'form_btn_text_color'=>['نص الزر',\DZQCOD\Core::opt('form_btn_text_color','#fff')],'form_text_color'=>['النص',\DZQCOD\Core::opt('form_text_color','#333')],'form_bg_color'=>['الخلفية',\DZQCOD\Core::opt('form_bg_color','#fff')]];
$toggles=['form_enabled'=>'تفعيل نموذج DZQCOD','form_replace_woo'=>'استبدال ووكومرس','show_product_image'=>'صورة المنتج','show_price'=>'عرض السعر','show_name'=>'الاسم','show_phone'=>'هاتف 1','show_phone2'=>'هاتف 2','show_delivery_type'=>'📦 نوع التوصيل (منزل/مكتب)','show_wilaya'=>'الولاية','show_commune'=>'البلدية','show_quantity'=>'الكمية','show_notes'=>'ملاحظات','show_breakdown'=>'تفصيل الأسعار','show_whatsapp_btn'=>'واتساب','show_trust_badges'=>'شارات الثقة','sticky_order_bar'=>'🔽 شريط ثابت أسفل الصفحة'];
$texts=['form_submit_text'=>'نص زر الطلب','form_success_text'=>'رسالة النجاح','form_whatsapp_text'=>'نص واتساب','form_whatsapp_number'=>'رقم واتساب','form_trust_text'=>'شارات الثقة (•)','delivery_label'=>'📦 عنوان نوع التوصيل','delivery_home_label'=>'🏠 نص خيار المنزل','delivery_home_desc'=>'وصف خيار المنزل','delivery_desk_label'=>'🏪 نص خيار المكتب','delivery_desk_desc'=>'وصف خيار المكتب','delivery_default'=>'الافتراضي (home/desk)'];
$default_order='product_header,tiers,name,phone,phone2,colors,sizes,custom_builder,delivery_type,wilaya,commune,address,quantity,custom_fields,notes,breakdown,payment,submit,whatsapp,trust';
$current_order=\DZQCOD\Core::opt('form_field_order',$default_order);
$field_labels=['product_header'=>'📦 رأس المنتج','tiers'=>'🏷️ عروض الكمية','name'=>'👤 الاسم','phone'=>'📱 الهاتف 1','phone2'=>'📱 الهاتف 2','colors'=>'🎨 الألوان','sizes'=>'📐 المقاسات','custom_builder'=>'🏭 المنتج المخصص','delivery_type'=>'📦 نوع التوصيل','wilaya'=>'📍 الولاية','commune'=>'🏘️ البلدية','address'=>'🏠 العنوان','quantity'=>'🔢 الكمية','custom_fields'=>'📋 حقول مخصصة','notes'=>'📝 ملاحظات','breakdown'=>'💰 الأسعار','payment'=>'💳 الدفع','submit'=>'🛒 الطلب','whatsapp'=>'💬 واتساب','trust'=>'✅ الثقة']; ?>
<div class="wrap dza-wrap" dir="rtl">
<div class="dza-header"><h1>🎨 تصميم النموذج</h1><button id="save-design" class="dza-btn primary">💾 حفظ التصميم</button></div>
<?php include __DIR__.'/nav.php'; ?>
<div class="dza-content">
<div class="dza-card" style="margin-bottom:16px"><h3>🎨 قوالب جاهزة <small style="color:#94a3b8;font-weight:400">— انقر لمعاينة فورية، احفظ لتفعيل</small></h3>
<p style="font-size:12px;color:#64748b;margin:0 0 12px">كل قالب يغيّر: <strong>التخطيط</strong> + <strong>الألوان</strong> + <strong>شكل الحقول</strong> + <strong>الخلفيات</strong> + <strong>الظلال</strong></p>
<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(155px,1fr));gap:10px">
<?php foreach($templates as $tpl):
$locked=($tpl['plan']==='pro'&&!in_array($plan,['pro','enterprise']))||($tpl['plan']==='enterprise'&&$plan!=='enterprise'); ?>
<div class="form-tpl<?php echo esc_html($locked?' locked':''); ?>" data-tpl='<?php echo esc_attr(wp_json_encode($tpl,JSON_UNESCAPED_UNICODE|JSON_HEX_TAG|JSON_HEX_AMP)); ?>' style="cursor:<?php echo esc_html($locked?'not-allowed':'pointer'); ?>;border:2px solid #e2e8f0;border-radius:12px;padding:10px;text-align:center;transition:.2s;opacity:<?php echo esc_html($locked?'0.5':'1'); ?>;position:relative" <?php if(!$locked):?>onclick="applyTemplate(this)"<?php endif;?>>
<?php if($locked):?><span style="position:absolute;top:4px;right:4px;font-size:9px;background:#f59e0b;color:#fff;padding:1px 6px;border-radius:4px">🔒 <?php echo esc_html(strtoupper($tpl['plan']));?></span><?php endif;?>
<div style="height:55px;border-radius:8px;background:<?php echo esc_html($tpl['bg']);?>;border:1px solid #e2e8f0;padding:5px;display:flex;flex-direction:column;gap:2px;overflow:hidden">
<div style="height:12px;border-radius:4px;background:linear-gradient(135deg,<?php echo esc_html($tpl['primary']);?>,<?php echo esc_html($tpl['secondary']);?>)"></div>
<?php if($tpl['layout']==='inline'):?><div style="display:flex;gap:2px"><div style="flex:1;height:8px;border-radius:3px;background:<?php echo esc_html($tpl['group_bg']);?>;border:1px solid <?php echo esc_html($tpl['field_border']);?>"></div><div style="flex:1;height:8px;border-radius:3px;background:<?php echo esc_html($tpl['group_bg']);?>;border:1px solid <?php echo esc_html($tpl['field_border']);?>"></div></div>
<?php else:?><div style="height:8px;border-radius:3px;background:<?php echo esc_html($tpl['group_bg']);?>;border:1px solid <?php echo esc_html($tpl['field_border']);?>"></div><?php endif;?>
<div style="height:10px;border-radius:4px;background:<?php echo esc_html($tpl['success']);?>"></div></div>
<div style="font-size:11px;font-weight:800;margin-top:5px"><?php echo esc_html($tpl['name']);?></div>
<div style="font-size:9px;color:#94a3b8"><?php echo esc_html($tpl['desc']);?></div></div>
<?php endforeach;?></div></div>

<div style="display:flex;gap:20px;flex-wrap:wrap">
<div style="flex:1;min-width:280px;max-width:440px">
<div class="dza-card"><h3>⚡ الحقول</h3><?php foreach($toggles as $k=>$l):?><label class="dza-toggle"><input type="checkbox" class="fs" data-key="<?php echo esc_html($k);?>" <?php checked(\DZQCOD\Core::opt($k,'yes'),'yes');?>><span><?php echo esc_html($l);?></span></label><?php endforeach;?></div>
<div class="dza-card"><h3>🔀 الترتيب</h3><ul id="field-sorter" style="list-style:none;padding:0;margin:0"><?php foreach(array_map('trim',explode(',',$current_order)) as $fk):if(!isset($field_labels[$fk]))continue;?><li data-field="<?php echo esc_html($fk);?>" draggable="true" style="padding:8px 10px;margin-bottom:3px;background:#f8f9fa;border:2px solid #e0e0e0;border-radius:6px;cursor:grab;display:flex;align-items:center;gap:6px;font-size:12px;font-weight:600"><span style="color:#999">⠿</span><?php echo esc_html($field_labels[$fk]);?></li><?php endforeach;?></ul><input type="hidden" id="field-order-val" class="fs" data-key="form_field_order" value="<?php echo esc_attr($current_order);?>"><button type="button" class="button" id="reset-order" style="margin-top:6px;font-size:11px">↻ افتراضي</button></div>
<div class="dza-card"><h3>🎨 الألوان</h3><?php foreach($colors as $k=>[$l,$v]):?><div class="dza-color-row"><span><?php echo esc_html($l);?></span><input type="color" class="fs" data-key="<?php echo esc_html($k);?>" value="<?php echo esc_attr($v);?>"></div><?php endforeach;?></div>
<div class="dza-card"><h3>🎯 تصميم متقدم</h3>
<div class="dza-color-row"><span>خلفية المجموعات</span><input type="color" class="fs" data-key="form_group_bg" value="<?php echo esc_attr(\DZQCOD\Core::opt('form_group_bg','#f8fafc'));?>"></div>
<div class="dza-color-row"><span>خلفية الحقول</span><input type="color" class="fs" data-key="form_field_bg" value="<?php echo esc_attr(\DZQCOD\Core::opt('form_field_bg','#ffffff'));?>"></div>
<div class="dza-color-row"><span>حدود الحقول</span><input type="color" class="fs" data-key="form_field_border" value="<?php echo esc_attr(\DZQCOD\Core::opt('form_field_border','#e2e8f0'));?>"></div>
<div class="dza-color-row"><span>لون التسميات</span><input type="color" class="fs" data-key="form_label_color" value="<?php echo esc_attr(\DZQCOD\Core::opt('form_label_color','#374151'));?>"></div>
<label style="display:block;margin-top:6px;font-size:12px">عناوين المجموعات:<select class="fs" data-key="form_group_title" style="margin-right:6px;padding:4px;border:1px solid #ddd;border-radius:4px"><option value="yes" <?php selected(\DZQCOD\Core::opt('form_group_title','yes'),'yes');?>>✅ نعم</option><option value="no" <?php selected(\DZQCOD\Core::opt('form_group_title','yes'),'no');?>>❌ لا</option></select></label>
<label style="display:block;margin-top:6px;font-size:12px">نمط المجموع:<select class="fs" data-key="form_total_style" style="margin-right:6px;padding:4px;border:1px solid #ddd;border-radius:4px"><option value="card" <?php selected(\DZQCOD\Core::opt('form_total_style','card'),'card');?>>بطاقة</option><option value="gradient" <?php selected(\DZQCOD\Core::opt('form_total_style','card'),'gradient');?>>تدرج</option><option value="simple" <?php selected(\DZQCOD\Core::opt('form_total_style','card'),'simple');?>>بسيط</option></select></label>
<label style="display:block;margin-top:6px;font-size:12px">الظل:<select class="fs" data-key="form_shadow" style="margin-right:6px;padding:4px;border:1px solid #ddd;border-radius:4px"><option value="none" <?php selected(\DZQCOD\Core::opt('form_shadow','none'),'none');?>>بدون</option><option value="sm" <?php selected(\DZQCOD\Core::opt('form_shadow','none'),'sm');?>>خفيف</option><option value="lg" <?php selected(\DZQCOD\Core::opt('form_shadow','none'),'lg');?>>قوي</option></select></label></div>
<div class="dza-card"><h3>📐 التخطيط</h3>
<label>استدارة: <span id="rv"><?php echo intval(\DZQCOD\Core::opt('form_border_radius','12'));?></span>px<input type="range" class="fs" data-key="form_border_radius" min="0" max="24" step="2" value="<?php echo intval(\DZQCOD\Core::opt('form_border_radius','12'));?>" style="width:100%" oninput="document.getElementById('rv').textContent=this.value"></label>
<label style="display:block;margin-top:6px">عرض: <input type="number" class="fs" data-key="form_max_width" value="<?php echo intval(\DZQCOD\Core::opt('form_max_width','480'));?>" min="300" max="800" style="width:70px;padding:4px">px</label>
<label style="display:block;margin-top:6px">تخطيط:</label><select class="fs" data-key="form_layout" style="width:100%;padding:5px;border:1px solid #ddd;border-radius:6px"><option value="stacked" <?php selected(\DZQCOD\Core::opt('form_layout','stacked'),'stacked');?>>📱 عمودي</option><option value="inline" <?php selected(\DZQCOD\Core::opt('form_layout','stacked'),'inline');?>>📐 متقابل</option><option value="compact" <?php selected(\DZQCOD\Core::opt('form_layout','stacked'),'compact');?>>🗜️ مضغوط</option></select>
<label style="display:block;margin-top:6px">شكل الحقول:</label><select class="fs" data-key="form_input_style" style="width:100%;padding:5px;border:1px solid #ddd;border-radius:6px"><option value="bordered" <?php selected(\DZQCOD\Core::opt('form_input_style','bordered'),'bordered');?>>🔲 محاطة</option><option value="underline" <?php selected(\DZQCOD\Core::opt('form_input_style','bordered'),'underline');?>>➖ خط سفلي</option><option value="filled" <?php selected(\DZQCOD\Core::opt('form_input_style','bordered'),'filled');?>>🟫 ممتلئة</option></select></div>
<div class="dza-card"><h3>🔤 الخطوط</h3><select class="fs" data-key="form_font" style="width:100%;padding:5px;border:1px solid #ddd;border-radius:6px;margin-bottom:6px"><option value="">الافتراضي</option><option value="Tajawal" <?php selected(\DZQCOD\Core::opt('form_font',''),'Tajawal');?>>Tajawal</option><option value="Cairo" <?php selected(\DZQCOD\Core::opt('form_font',''),'Cairo');?>>Cairo</option><option value="Almarai" <?php selected(\DZQCOD\Core::opt('form_font',''),'Almarai');?>>Almarai</option></select><label>حجم: <input type="number" class="fs" data-key="form_font_size" value="<?php echo intval(\DZQCOD\Core::opt('form_font_size','15'))?:15;?>" min="12" max="20" style="width:50px;padding:4px">px</label></div>
<div class="dza-card"><h3>✏️ النصوص</h3><?php foreach($texts as $k=>$l):?><div style="margin-bottom:6px"><label style="font-size:11px;font-weight:600"><?php echo esc_html($l);?></label><input type="text" class="fs" data-key="<?php echo esc_html($k);?>" value="<?php echo esc_attr(\DZQCOD\Core::opt($k,''));?>" style="width:100%;padding:5px;border:1px solid #ddd;border-radius:6px;margin-top:2px"></div><?php endforeach;?></div>
<div class="dza-card"><h3>🔧 CSS مخصص</h3><textarea class="fs" data-key="form_custom_css" rows="3" style="width:100%;padding:5px;font-family:monospace;font-size:11px;border:1px solid #ddd;border-radius:6px;direction:ltr"><?php echo esc_textarea(\DZQCOD\Core::opt('form_custom_css',''));?></textarea></div>
</div>

<div style="flex:1;min-width:300px;max-width:520px;position:sticky;top:40px;align-self:flex-start">
<div class="dza-card" style="background:#e5e7eb;padding:16px"><h3 style="text-align:center;color:#666;margin:0 0 10px;font-size:14px">👁 معاينة حية</h3><div id="preview" style="transform:scale(0.82);transform-origin:top center"></div></div></div>
</div></div></div>
<style>#field-sorter li:hover{border-color:#667eea;background:#f0f4ff}#field-sorter li.dragging{opacity:.4}#field-sorter li.drag-over{border-top:3px solid #667eea}.form-tpl:hover:not(.locked){border-color:#667eea!important;transform:translateY(-2px);box-shadow:0 4px 12px rgba(0,0,0,.1)}</style>
<script>
jQuery(function($){
var $s=$('#field-sorter'),dragEl=null;
$s.on('dragstart','li',function(e){dragEl=this;$(this).addClass('dragging');e.originalEvent.dataTransfer.effectAllowed='move'});
$s.on('dragend','li',function(){$(this).removeClass('dragging');$s.find('.drag-over').removeClass('drag-over')});
$s.on('dragover','li',function(e){e.preventDefault();$s.find('.drag-over').removeClass('drag-over');$(this).addClass('drag-over')});
$s.on('drop','li',function(e){e.preventDefault();$(this).removeClass('drag-over');if(dragEl&&dragEl!==this)$(this).before(dragEl);uo()});
function uo(){var o=[];$s.find('li').each(function(){o.push($(this).data('field'))});$('#field-order-val').val(o.join(','));pv()}
$('#reset-order').on('click',function(){var d='<?php echo esc_html($default_order);?>'.split(','),$i=$s.find('li').detach();d.forEach(function(k){$i.filter('[data-field="'+k+'"]').appendTo($s)});uo()});
function g(){var s={};$('.fs').each(function(){var k=$(this).data('key');s[k]=$(this).is(':checkbox')?$(this).is(':checked')?'yes':'no':$(this).val()});return s}
function pv(){var s=g(),r=s.form_border_radius||12,pc=s.form_primary_color||'#667eea',sc=s.form_secondary_color||'#764ba2',gc=s.form_success_color||'#27ae60',bt=s.form_btn_text_color||'#fff',bg=s.form_bg_color||'#fff',tc=s.form_text_color||'#333',gbg=s.form_group_bg||'#f8fafc',fbg=s.form_field_bg||'#fff',fbd=s.form_field_border||'#e2e8f0',lc=s.form_label_color||'#374151',ts=s.form_total_style||'card',shw=s.form_shadow||'none',gt=s.form_group_title||'yes',ist=s.form_input_style||'bordered',isIn=s.form_layout==='inline',order=(s.form_field_order||'<?php echo esc_html($default_order);?>').split(','),P={};
var shS=shw==='lg'?'0 12px 40px rgba(0,0,0,.15)':shw==='sm'?'0 4px 16px rgba(0,0,0,.08)':'none';
var iS=ist==='underline'?'border:none;border-bottom:2px solid '+fbd+';border-radius:0;background:transparent':ist==='filled'?'border:none;background:'+fbd+';border-radius:'+r+'px':'border:2px solid '+fbd+';border-radius:'+r+'px;background:'+fbg;
var F=function(l){return '<div style="margin-bottom:8px"><label style="font-weight:700;font-size:12px;color:'+lc+';display:block;margin-bottom:3px">'+l+'</label><input style="width:100%;padding:10px;box-sizing:border-box;font-size:13px;color:'+tc+';'+iS+'" disabled></div>'};
var GT=function(t){return gt==='yes'&&t?'<div style="font-size:11px;font-weight:800;color:'+pc+';margin-bottom:6px;padding-bottom:4px;border-bottom:1px solid '+fbd+'">'+t+'</div>':''};
var BX=function(c){return '<div style="background:'+gbg+';border-radius:'+r+'px;padding:12px;margin-bottom:8px">'+c+'</div>'};
if(s.show_price==='yes')P.product_header='<div style="text-align:center;padding:16px;background:linear-gradient(135deg,'+pc+','+sc+');border-radius:'+r+'px;color:#fff;margin-bottom:10px">'+(s.show_product_image==='yes'?'<div style="width:50px;height:50px;background:rgba(255,255,255,.2);border-radius:10px;margin:0 auto 6px;display:flex;align-items:center;justify-content:center;font-size:22px">📦</div>':'')+'<div style="font-size:15px;font-weight:700">اسم المنتج</div><div style="font-size:24px;font-weight:900;margin-top:4px">3,500 دج</div></div>';
P.tiers='<div style="text-align:center;padding:6px;font-size:11px;color:'+pc+';font-weight:700;margin-bottom:6px">🏷️ عروض الكمية</div>';
var nF=s.show_name==='yes'?F('👤 الاسم الكامل *'):'',pF=s.show_phone==='yes'?F('📱 الهاتف 1 *'):'';
if(isIn&&nF&&pF){P.name=BX(GT('📋 المعلومات')+'<div style="display:grid;grid-template-columns:1fr 1fr;gap:8px">'+nF+pF+'</div>');P.phone=''}else{if(nF)P.name=BX(nF);if(pF)P.phone=BX(pF)}
if(s.show_phone2==='yes')P.phone2=isIn?'<div style="padding:0 12px;margin-top:-4px;margin-bottom:8px">'+F('📱 الهاتف 2')+'</div>':BX(F('📱 الهاتف 2'));
P.colors='<div style="display:flex;gap:4px;margin-bottom:8px"><span style="padding:5px 10px;border:2px solid #ddd;border-radius:'+r+'px;font-size:11px">🎨 أحمر</span><span style="padding:5px 10px;border:2px solid '+pc+';border-radius:'+r+'px;font-size:11px;background:'+pc+'15">أزرق ✓</span></div>';
P.sizes=P.colors.replace('أحمر','S').replace('أزرق ✓','M ✓').replace('🎨','📐');
P.custom_builder=BX(GT('🏭 المنتج المخصص')+F('الطول (سم)')+F('العرض (سم)'));
if(s.show_delivery_type==='yes'){P.delivery_type=BX('<div style="margin-bottom:8px"><label style="font-weight:700;font-size:12px;color:'+lc+';display:block;margin-bottom:6px">📦 نوع التوصيل *</label><div style="display:flex;gap:8px"><div style="flex:1;padding:10px;border:2px solid '+pc+';border-radius:'+r+'px;text-align:center;background:'+pc+'10;cursor:pointer"><span style="font-size:18px">🏠</span><div style="font-weight:700;font-size:12px;margin-top:4px">توصيل للمنزل</div><div style="font-size:10px;color:#888">حتى باب منزلك</div></div><div style="flex:1;padding:10px;border:2px solid #ddd;border-radius:'+r+'px;text-align:center;cursor:pointer"><span style="font-size:18px">🏪</span><div style="font-weight:700;font-size:12px;margin-top:4px">مكتب الاستلام</div><div style="font-size:10px;color:#888">Stop Desk</div></div></div></div>')}
if(s.show_wilaya==='yes'){var wF='<div style="margin-bottom:8px"><label style="font-weight:700;font-size:12px;color:'+lc+';display:block;margin-bottom:3px">📍 الولاية *</label><select style="width:100%;padding:10px;'+iS+'" disabled><option>اختر الولاية</option></select></div>',cF=s.show_commune==='yes'?wF.replace('الولاية *','البلدية *').replace('الولاية','أولاً'):'';
if(isIn&&cF)P.wilaya=BX(GT('📍 التوصيل')+'<div style="display:grid;grid-template-columns:1fr 1fr;gap:8px">'+wF+cF+'</div>');else{P.wilaya=BX(GT('📍 التوصيل')+wF);if(cF)P.commune=BX(cF)}}
P.address=F('🏠 العنوان');
if(s.show_quantity==='yes')P.quantity='<div style="margin-bottom:8px"><label style="font-weight:700;font-size:12px;color:'+lc+'">🔢 الكمية</label><div style="display:flex;max-width:120px;margin-top:4px"><span style="width:30px;height:30px;border:2px solid '+fbd+';border-radius:'+r+'px;display:flex;align-items:center;justify-content:center;font-size:15px;cursor:pointer">−</span><span style="width:36px;text-align:center;border-top:2px solid '+fbd+';border-bottom:2px solid '+fbd+';line-height:30px;font-weight:800">1</span><span style="width:30px;height:30px;border:2px solid '+fbd+';border-radius:'+r+'px;display:flex;align-items:center;justify-content:center;font-size:15px;cursor:pointer">+</span></div></div>';
P.custom_fields=F('📋 حقل مخصص');
if(s.show_notes==='yes')P.notes=F('📝 ملاحظات');
if(s.show_breakdown==='yes'){var tBg,tSt;if(ts==='gradient'){tBg='background:linear-gradient(135deg,'+pc+','+sc+');color:#fff';tSt='border-top:2px solid rgba(255,255,255,.3)'}else if(ts==='card'){tBg='background:'+gbg;tSt='border-top:2px solid '+fbd}else{tBg='background:transparent';tSt='border-top:2px solid '+fbd}
P.breakdown='<div style="'+tBg+';border-radius:'+r+'px;padding:14px;margin:10px 0"><div style="display:flex;justify-content:space-between;font-size:12px;margin-bottom:3px"><span>📦 المنتج</span><span>3,500 دج</span></div><div style="display:flex;justify-content:space-between;font-size:12px;margin-bottom:8px"><span>🚚 الشحن</span><span>400 دج</span></div><div style="display:flex;justify-content:space-between;font-size:22px;font-weight:900;padding-top:8px;'+tSt+'"><span>💰 المجموع</span><span>3,900 دج</span></div></div>'}
P.payment='<div style="display:flex;gap:5px;margin-bottom:8px"><div style="flex:1;padding:8px;border:2px solid '+pc+';border-radius:'+r+'px;text-align:center;font-size:11px;font-weight:700;background:'+pc+'15">💵 COD</div><div style="flex:1;padding:8px;border:2px solid '+fbd+';border-radius:'+r+'px;text-align:center;font-size:11px">💳 CIB</div></div>';
P.submit='<button style="display:block;width:100%;padding:14px;background:linear-gradient(135deg,'+gc+','+pc+');color:'+bt+';border:none;border-radius:'+r+'px;font-size:15px;font-weight:900;cursor:pointer;box-shadow:0 4px 14px '+gc+'44;box-sizing:border-box">'+(s.form_submit_text||'🛒 اطلب الآن')+'</button>';
if(s.show_whatsapp_btn==='yes')P.whatsapp='<a style="display:block;text-align:center;padding:10px;background:#25D366;color:#fff;border-radius:'+r+'px;font-size:12px;font-weight:800;margin-top:6px;text-decoration:none">💬 '+(s.form_whatsapp_text||'واتساب')+'</a>';
if(s.show_trust_badges==='yes'){var tb='<div style="display:flex;flex-wrap:wrap;gap:3px;justify-content:center;margin-top:6px">';(s.form_trust_text||'').split('•').forEach(function(t){t=t.trim();if(t)tb+='<span style="background:'+gc+'15;padding:3px 8px;border-radius:12px;font-size:10px;color:'+gc+';font-weight:700">'+t+'</span>'});tb+='</div>';P.trust=tb}
var sticky='';if(s.sticky_order_bar==='yes')sticky='<div style="position:sticky;bottom:0;background:'+bg+';border-top:2px solid '+fbd+';padding:10px;display:flex;align-items:center;justify-content:space-between;border-radius:0 0 '+r+'px '+r+'px"><div><div style="font-size:9px;color:'+lc+'">المجموع</div><div style="font-size:20px;font-weight:900;color:'+pc+'">3,900 دج</div></div><button style="padding:10px 20px;background:linear-gradient(135deg,'+gc+','+pc+');color:'+bt+';border:none;border-radius:'+r+'px;font-size:13px;font-weight:800;cursor:pointer">🛒 اطلب الآن</button></div>';
var h='<div style="max-width:'+(s.form_max_width||480)+'px;margin:0 auto;padding:14px;background:'+bg+';color:'+tc+';border-radius:'+r+'px;box-shadow:'+shS+'">';
order.forEach(function(k){if(P[k])h+=P[k]});h+=sticky+'</div>';$('#preview').html(h)}
pv();$(document).on('change input','.fs',pv);
$('#save-design').on('click',function(){var b=$(this);b.prop('disabled',true).text('⏳');$.post(dza.ajax,{action:'dzqcod_save_form_settings',nonce:dza.nonce,settings:g()},function(r){b.prop('disabled',false);b.text(r.success?'✅ تم!':'❌');setTimeout(function(){b.text('💾 حفظ التصميم')},2e3)})})
});
window.applyTemplate=function(el){var t=JSON.parse(el.dataset.tpl),m={'form_primary_color':'primary','form_secondary_color':'secondary','form_success_color':'success','form_bg_color':'bg','form_text_color':'text','form_btn_text_color':'btn_text','form_border_radius':'radius','form_layout':'layout','form_input_style':'input_style','form_group_bg':'group_bg','form_group_title':'group_title','form_field_bg':'field_bg','form_field_border':'field_border','form_label_color':'label_color','form_shadow':'shadow','form_total_style':'total_style'};for(var k in m)if(t[m[k]]!==undefined)jQuery('[data-key="'+k+'"]').val(t[m[k]]).trigger('input');document.getElementById('rv').textContent=t.radius||12;document.querySelectorAll('.form-tpl').forEach(function(e){e.style.borderColor='#e2e8f0'});el.style.borderColor=t.primary};
</script>
</div></div>
