<?php



//////////////////////////// HTML FUNCTION ENDS (c) i2d Technologies ///////////////////////////////////////
if (!function_exists('make_form')) {
function make_form($values,$encoding,$edit_settings=0) {
if (array_key_exists('texts', $values)) {
   $text=$values['texts'];
   }
if (array_key_exists('integers', $values)) {
   $integer=$values['integers'];
   }
if (array_key_exists('dropdowns', $values)) {
   $dropdown=$values['dropdowns'];
   }
if (array_key_exists('dropdowns2', $values)) {
   $dropdown2=$values['dropdowns2'];
   }
if (array_key_exists('checkboxes', $values)) {
   $checkboxes=$values['checkboxes'];
   }
if (array_key_exists('timepicks', $values)) {
   $timepicks=$values['timepicks'];
   }

   if (array_key_exists('directfields', $values)) {
   $directfields=$values['directfields'];
   }
   
if (array_key_exists('wysiwygs', $values)) {
   $wysiwygitem=$values['wysiwygs'];
   }

if (array_key_exists('wysiwygs_choice', $values)) {
   $wysiwygitem_choice=$values['wysiwygs_choice'];
   }   
   
if (array_key_exists('disableds', $values)) {
   $disableds=$values['disableds'];
   }
if (array_key_exists('dates', $values)) {
   $dates=$values['dates'];
   }
if (array_key_exists('datedfield', $values)) {
   $datedfield=$values['datedfield'];
   }
if (array_key_exists('alias', $values)) {
   $alias=$values['alias'];
   }
   if (array_key_exists('redirect', $values)) {
   $redirect=$values['redirect'];
   }
if (array_key_exists('redirect_adv', $values)) {
   $redirect2=$values['redirect_adv'];
   }
if (array_key_exists('form_heading', $values)) {
   $form_heading=$values['form_heading'];
   }
if (array_key_exists('form_table', $values)) {
   $form_table=$values['form_table'];
   }

if (array_key_exists('edit', $edit_settings)) {
   $edit=$edit_settings['edit'];
   }
if (array_key_exists('lead_id', $edit_settings)) {
   $lead_id=$edit_settings['lead_id'];
   }
if (array_key_exists('table', $edit_settings)) {
   $etable=$edit_settings['table'];
   }
if (array_key_exists('e_ref', $edit_settings)) {
   $e_ref=$edit_settings['e_ref'];
   }
 
if (array_key_exists('update_settings', $values)) {
   $update_settings=$values['update_settings'];
   }
   
if (array_key_exists('old_deal_card', $values)) {
   $old_deal_card=$values['old_deal_card'];
   } 
?>



<script type="text/javascript">
$(document).ready(function() {
  $(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });
});
</script>



<script type="text/javascript">
 $(document).ready(function() {
//$("#subbtn").click(function(){
//$("#subbtn").text("Please Wait...");

	//  });
	  
  });
  </script>

 <div id="page-right-content">

                    <div class="container">
                        <div class="row">
                            <div class="col-sm-12">
																				<?php
																				
																				random_tip();
if(isset($edit) && $edit!="0"){
	?>
                                <h4 class="header-title m-t-0 m-b-20"><?php echo "Update ".$form_heading; ?></h4>
<?php } 
else { ?>
                                <h4 class="header-title m-t-0 m-b-20"><?php echo "Add New ".$form_heading; ?></h4>
<?php } ?>      

<?php 
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
?>
	  </div>
                        </div> <!-- end row -->


                        <div class="row">
               <div class="col-12">



<form role="form" enctype="multipart/form-data" method="post">
<div class="p-20 m-b-20">
<div class="row">


<?php
if(isset($datedfield)){
if(isset($edit) && $edit!="0"){
if(isset($values['dated_table'])){
$date_settings=array(
'table'=>$values['dated_table'],
'id'=>$lead_id,
'type'=>'0',
'to_find'=>$values['datedfield']);
$found_date=db_fetch($date_settings);
dater2($found_date);
//echo "<h4>Entry Date is ".$found_date."</h4>";
?>
<?php
if(isset($values['edit_heading']) && $values['edit_heading']!=0) {
?>
<h3 align="center"><span style="color:red;"><?php echo $values['prefix'].$lead_id; ?></span></h3>
<?php } ?>
<?php
}
}	
else{
dater2();	
}
}
?>
</div>
<?php
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($dropdown2) && !empty($dropdown2)){
foreach ($dropdown2 as $droploop2 => $valloop2){
	
if(isset($edit) && $edit!="0"){

$this_field2=db_ref($dropdown2[$droploop2]['prim_table'],id_col,$lead_id,$dropdown2[$droploop2]['this_table_field']);

	
$db_settings32=array(
'table'=>$dropdown2[$droploop2]['sub_table'],
'id'=>$this_field2,
'type'=>'0',
'to_find'=>$dropdown2[$droploop2]['remote_tbl_title_col']);

$drop32=db_fetch($db_settings32);

//helloworld($drop32);

$this_field2b=db_ref($dropdown2[$droploop2]['prim_table'],id_col,$lead_id,$dropdown2[$droploop2]['this_table_field2']);

$db_settings42=array(
'table'=>$dropdown2[$droploop2]['sub_table'],
'id'=>$this_field2b,
'type'=>'0',
'to_find'=>$dropdown2[$droploop2]['remote_tbl_title_col']);

$drop42=db_fetch($db_settings42);

////////////////////////////////////////////


}
?>
<?php
if(isset($edit) && $edit!="0"){
$editing2=array(
'editing'=>'1',
'selected_id'=>$this_field2,
'selected_text'=>$drop32,
'selected_id2'=>$this_field2b,
'selected_text2'=>$drop42
);
}
else{
$editing2=array(
'editing'=>'0');	
}
dynamicdrop_adv_multi($dropdown2[$droploop2]['label'],$dropdown2[$droploop2]['encoding'],$editing2,$dropdown2[$droploop2]['ref'],$dropdown2[$droploop2]['ref2'],$dropdown2[$droploop2]['prim_table'],$dropdown2[$droploop2]['sub_table'],$dropdown2[$droploop2]['table_title'],$dropdown2[$droploop2]['table_id_col'],$dropdown2[$droploop2]['table_parent_col'],$dropdown2[$droploop2]['parent_add'],$dropdown2[$droploop2]['child_add'],$dropdown2[$droploop2]['label2']);
?> 
<?php
if(isset($_POST['add_item'])){
$dropfound22=$dropdown2[$droploop2]['ref']."_multi_dropname";
$dropfound222=$_POST[$dropfound22];
$droparray2[$dropdown2[$droploop2]['this_table_field']] = $dropfound222;

$dropfound22_sub=$dropdown2[$droploop2]['ref2']."_multi_dropname";
$dropfound222_sub=$_POST[$dropfound22_sub];
$droparray2_sub[$dropdown2[$droploop2]['this_table_field2']] = $dropfound222_sub;



}


}
}
?>

<div class="row">
<p>&nbsp;</p>
<p>&nbsp;</p>
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($dropdown) && !empty($dropdown)){
foreach ($dropdown as $droploop => $valloop){
	
if(isset($edit) && $edit!="0"){	

$this_field1=db_ref($form_table,id_col,$lead_id,$dropdown[$droploop]['this_table_field']);

//helloworld($this_field1);

$db_settings3=array(
'table'=>$dropdown[$droploop]['table'],
'id'=>$this_field1,
'type'=>'0',
'to_find'=>$dropdown[$droploop]['remote_tbl_title_col']);


$drop3=db_fetch($db_settings3);

}
?>
<?php
if(isset($edit) && $edit!="0"){
$editing=array(
'editing'=>'1',
'selected_id'=>$this_field1,
'selected_text'=>$drop3);
}
else{
$editing=array(
'editing'=>'1');	
}
dynamicdrop_adv($dropdown[$droploop]['label'],$dropdown[$droploop]['encoding'],$editing,$dropdown[$droploop]['ref'],$dropdown[$droploop]['table'],$dropdown[$droploop]['table_title'],$dropdown[$droploop]['table_id_col'],$dropdown[$droploop]['add_new'],$dropdown[$droploop]['other_id_col']);
?> 
<?php
if(isset($_POST['add_item'])){
$dropfound=$dropdown[$droploop]['ref']."_dropname";
$dropfound2=$_POST[$dropfound];
$droparray[$dropdown[$droploop]['this_table_field']] = $dropfound2;
}


}
}
?>
</div>
<!------i2d Technologies v3.0 ----     ---------------->
<!------i2d Technologies v3.0 ----     ---------------->
<!------i2d Technologies v3.0 ----     ---------------->
<div class="row">
<?php
if(isset($text) && !empty($text)){
$parameters=array(
'text'=>$text,
'form_name'=>'fname',
'form_submit_id'=>'add_item',
'table'=>$etable,
'table_ref'=>$e_ref,
'edit'=>$edit,
'id'=>$lead_id
);

$text_fields=form_control($parameters);
}
?>
</div>
<div class="row">
<?php
///////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($integer) && !empty($integer)){
$parameters2=array(
'text'=>$integer,
'form_name'=>'fname',
'form_submit_id'=>'add_item',
'edit'=>$edit,
'id'=>$lead_id
);
$integer_fields=form_control($parameters2);

}
?>
</div>
<div class="row">
<?php
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($disableds) && !empty($disableds)){
$parametersd=array(
'text'=>$disableds,
'form_name'=>'fname',
'form_submit_id'=>'add_item',
'edit'=>$edit,
'id_col'=>$id_col,
'id'=>$lead_id,
'table'=>$table
);
$disableds_fields=form_control($parametersd);
}
?>
</div>
<div class="row">
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($checkboxes) && !empty($checkboxes)){
$parametersch=array(
'text'=>$checkboxes,
'form_name'=>'fname',
'form_submit_id'=>'add_item',
'edit'=>$edit,
'id'=>$lead_id
);
$checkbox_fields=form_check_box($parametersch);
}
?>
</div>
<div class="row">
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($timepicks) && !empty($timepicks)){
$parameterstm=array(
'text'=>$timepicks,
'form_name'=>'fname',
'form_submit_id'=>'add_item',
'edit'=>$edit,
'id'=>$lead_id
);
$timepick_fields=form_time_pick($parameterstm);
}
?>
</div>
<div class="row">
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($wysiwygitem) && !empty($wysiwygitem)){
foreach ($wysiwygitem as $wysiwyg1 => $wysiwygv){
$wysiwygarray1[] = $wysiwygitem[$wysiwyg1]['fname'];
?>
<script type="text/javascript">
  $(document).ready(function() {
$('#<?php echo $wysiwygitem[$wysiwyg1]['fname']."_div"; ?>').summernote({
  height: <?php echo $wysiwygitem[$wysiwyg1]['height']; ?>,                 // set editor height
  minHeight: null,             // set minimum height of editor
  maxHeight: null,             // set maximum height of editor
  
  <?php if(isset($wysiwygitem[$wysiwyg1]['toolbar']) && $wysiwygitem[$wysiwyg1]['toolbar']=="all") {  ?> 
  
  toolbar: [
            [ 'style', [ 'style' ] ],
            [ 'font', [ 'bold', 'italic', 'underline', 'strikethrough', 'superscript', 'subscript', 'clear'] ],
            [ 'fontname', [ 'fontname' ] ],
            [ 'fontsize', [ 'fontsize' ] ],
            [ 'color', [ 'color' ] ],
            [ 'para', [ 'ol', 'ul', 'paragraph', 'height' ] ],
            [ 'table', [ 'table' ] ],
            ['insert', ['link', 'picture', 'video']],
            [ 'view', [ 'undo', 'redo', 'fullscreen', 'codeview', 'help' ] ]
        ] 
		
  <?php }  else { ?>		
		
		toolbar: [
            [ 'font', [ 'bold', 'italic', 'underline', 'strikethrough','clear'] ],
            [ 'fontsize', [ 'fontsize' ] ],
            [ 'view', [ 'undo', 'redo', 'fullscreen'] ]
        ]
		
  <?php } ?>
		
  //focus: true                  // set focus to editable area after initializing summernote
});
  });
  </script>
  



              <h4 class="m-b-30 m-t-0 header-title"><strong><?php echo $wysiwygitem[$wysiwyg1]['label']; ?></strong></h4>
            
                 
				 

<div class="col-md-12">
<?php
if(isset($edit) && $edit!="0"){
if(isset($values['form_table'])){
$wys_settings=array(
'table'=>$values['form_table'],
'id'=>$lead_id,
'type'=>'0',
'echo'=>'0',
'to_find'=>$wysiwygitem[$wysiwyg1]['fname']);
$found_wys=db_fetch($wys_settings);
}
}
?>

      
<textarea name="<?php echo $wysiwygitem[$wysiwyg1]['fname']."_name"; ?>" id="<?php echo $wysiwygitem[$wysiwyg1]['fname']."_div"; ?>">
<?php 
if(isset($edit) && $edit!="0"){
echo $found_wys;
}
else{
	if(isset($wysiwygitem[$wysiwyg1]['placeholder'])){
echo $wysiwygitem[$wysiwyg1]['placeholder']; 
}} ?>	
</textarea>
          </div>


    
		
		

<?php
	  
}
}
?>
</div>
<div class="row">
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($wysiwygitem_choice) && !empty($wysiwygitem_choice)){
foreach ($wysiwygitem_choice as $wysiwyg1_choice => $wysiwygv_choice){
$wysiwygarray1_choice[] = $wysiwygitem_choice[$wysiwyg1_choice]['fname'];

$select_name=$wysiwygitem_choice[$wysiwyg1_choice]['fname']."_sel";
$text_area_name=$wysiwygitem_choice[$wysiwyg1_choice]['fname']."_text_area";
$tags_div_name=$wysiwygitem_choice[$wysiwyg1_choice]['fname']."_tags_div";


if(isset($wysiwygitem_choice[$wysiwyg1_choice]['drop_down']) && $wysiwygitem_choice[$wysiwyg1_choice]['drop_down']!=0 && $edit==0) {



//helloworld($syg_val_choice);

$select_update_cols=array($wysiwygitem_choice[$wysiwyg1_choice]['drop_table_title'],$wysiwygitem_choice[$wysiwyg1_choice]['drop_table_value'],$wysiwygitem_choice[$wysiwyg1_choice]['drop_table_status'],$wysiwygitem_choice[$wysiwyg1_choice]['drop_table_ref'],sub_table_url);
	
	$updatesettings=array(
	'table'=>$wysiwygitem_choice[$wysiwyg1_choice]['drop_table'],
	'id_col'=>$wysiwygitem_choice[$wysiwyg1_choice]['drop_table_id'],
	'ref_value'=>$wysiwygitem_choice[$wysiwyg1_choice]['select_alias'],
	'ref_col'=>$wysiwygitem_choice[$wysiwyg1_choice]['drop_table_ref'],
	'fields_array'=>$select_update_cols,
	'status_value'=>'Active',
	'status_col'=>$wysiwygitem_choice[$wysiwyg1_choice]['drop_table_status'],
	'errors'=>"0",
	'echo'=>"0",
	'type'=>'status_ref');

	$updatesettings_load=db_fetch($updatesettings);
	
	//var_dump($updatesettings_load);

}

if(isset($edit) && !empty($edit)){
$syg_val_choice=db_ref($wysiwygitem_choice[$wysiwyg1_choice]['table'],$wysiwygitem_choice[$wysiwyg1_choice]['table_id'],$lead_id,$wysiwygitem_choice[$wysiwyg1_choice]['fname']);	
}


?>


<!-- PAGE FUNCTION STARTS (c) i2d Technologies Website Engine -->
   
<?php
if(isset($wysiwygitem_choice[$wysiwyg1_choice]['drop_down']) 
&& $wysiwygitem_choice[$wysiwyg1_choice]['drop_down']!=0 && $edit==0) {
?>
      
   <script type="text/javascript">
  $(document).ready(function() {
$('#<?php echo $text_area_name ?>').summernote();
var $option1 = $(<?php echo $select_name; ?>).find('option:selected');
var rt1 = $option1.val();//to get content of "value" attrib
$('#<?php echo $text_area_name; ?>').summernote ('code', rt1);
$('#<?php echo $select_name; ?>').on('change', function(){
var $option = $(<?php echo $select_name; ?>).find('option:selected');
var rt = $option.val();//to get content of "value" attrib
//alert(rt);
/*
var rtarray = rt.split(',');
$('#<?php echo $text_area_name; ?>').tagsinput('removeAll');
$.each(rtarray, function(rtindex, rtvalue) {
console.log(rtvalue);
$('#<?php echo $text_area_name; ?>').tagsinput('add', rtvalue);
});
*/
//$('#<?php echo $text_area_name; ?>').val(rt);
//var markupStr = '<table class="table"><tbody><tr><td><p><b>BRAND DESIGN / IDENTITY DESIGN</b></p><ul><li>LOGO / INSIGNIA DESIGN</li><li>COLOR THEME</li><li>3 Concepts , 3 Revisions&nbsp;<br></li></ul></td><td style="text-align: center; "><p><br></p><p>PKR 24,000*</p></td></tr><tr><td><p>1 Month Validity</p></td><td style="text-align: center; "><p>.</p></td></tr></tbody></table>';
//$('#<?php echo $text_area_name; ?>').summernote('code', markupStr);
$('#<?php echo $text_area_name; ?>').summernote ('code', rt);

});

    
});
	  
  </script>

<?php } ?>
<?php
if(isset($edit) && !empty($edit)){ ?>


   <script type="text/javascript">
  $(document).ready(function() {

//$('#<?php echo $text_area_name; ?>').tagsinput('add', <?php echo $syg_val_choice; ?>);
    
});
	  
  </script>

<?php } ?>

              <h4 class="m-b-30 m-t-30 header-title"><strong><?php echo $wysiwygitem_choice[$wysiwyg1_choice]['text_area_label']; ?></strong></h4>  
              
  
  <div class="form-group">
                        <label class="control-label"><h5><?php echo $wysiwygitem_choice[$wysiwyg1_choice]['select_label']; ?></h5></label>
	
 <?php
if(isset($wysiwygitem_choice[$wysiwyg1_choice]['drop_down']) && $wysiwygitem_choice[$wysiwyg1_choice]['drop_down']!=0 && $edit==0) {
?> 
  <select class="form-control" id="<?php echo $select_name; ?>">
<?php 
foreach ($updatesettings_load as $updateitem){
?>
<option value='<?php echo $updateitem[select_value]; ?>'><?php echo $updateitem[select_title]; ?></option>
<?php } ?>    
	</select>
															<p>&nbsp;</p>
<?php } ?>

				<div class="col-md-12">




<div class="tags-default">

<?php
if(isset($edit) && !empty($edit)){ ?>
  <textarea value="<?php echo $syg_val_choice; ?>" id="<?php echo $text_area_name; ?>" name="<?php echo $text_area_name; ?>"></textarea>
<?php 
}
else {
?>
  <textarea placeholder="add more tags" id="<?php echo $text_area_name; ?>" name="<?php echo $text_area_name; ?>"></textarea>
<?php } ?>

                                    </div>      



          </div>
          
          </div>


    
		
		

<?php
	  
}
}
?>
</div>
<div class="row">
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($dates) && !empty($dates)){
foreach ($dates as $dates1 => $datesval){
$datesarray1[] = $dates[$dates1]['fname'];
dater3($dates[$dates1]['label'],$dates[$dates1]['fname'],$dates[$dates1]['date']);
}
}
?>
</div>
<div class="row">
<p>&nbsp;</p>
<div class="col-md-12 text-center gap-items-5">
                                                    <button type="reset" class="btn btn-lg btn-danger waves-effect m-l-5"
													onclick="history.go(-1);">
                                                        Cancel<label><i class="fa fa-tick"></i></label>
                                                    </button>
													<?php
if(isset($edit) && $edit!="0"){
	?>
													
													<button type="submit" name="add_itembtn" id="subbtn" 
													class="btn btn-lg btn-primary waves-effect waves-light">
                                                        Update Item<label><i class="fa fa-tick"></i></label>
                                                    </button>
<?php } 
else { 
?>
													<button type="submit" name="add_itembtn" id="subbtn" 
													class="btn btn-lg btn-primary waves-effect waves-light">
                                                        Insert Item<label><i class="fa fa-tick"></i></label>
                                                    </button>

<?php } ?>
                                                    <input type="hidden" name="add_item" value="add_item">
                    <hr class="d-lg-none">
                    
                                                </div>


			
				  </div>
				  
				  
				  
				  
				  
				  
				  
				  
				  
				  </div>
				  <input type='submit' style="display: none;">
				  </form>
				  
				  
				  
				  
				  
				  </div>
				  </div>
				  
				  

				  <?php
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
if(isset($_POST['add_item'])){
    
   // var_dump($_POST); // Keep for debugging
    
    // Initialize arrays
    $array_one = array();
    $array_two = array();
    
    // Process text fields
    if(isset($text_fields['fields']) && isset($text_fields['posts'])) {
        $text1 = $text_fields['fields'];
        $text2 = $text_fields['posts'];
        
        if(count($text1) === count($text2)) {
            $text3 = array_combine($text1, $text2);
        } else {
            $text3 = array();
        }
    } else {
        $text3 = array();
    }
    
    // Process integer fields
    $integer3 = array();
    if(isset($integer_fields) && !empty($integer_fields)) {
        $integer1 = $integer_fields['fields'];
        $integer2 = $integer_fields['posts'];
        if(count($integer1) === count($integer2)) {
            $integer3 = array_combine($integer1, $integer2);
        }
    }
    
    // Process WYSIWYG fields
    $wysiwyg3 = array();
    if(isset($wysiwygitem) && !empty($wysiwygitem)) {
        $wysiwygarray2 = array();
        foreach ($wysiwygitem as $wysiwyg1 => $wysiwygv) {
            $wysiwygfound = $wysiwygitem[$wysiwyg1]['fname']."_name";
            if(isset($_POST[$wysiwygfound])) {
                $wysiwygarray2[] = $_POST[$wysiwygfound];
            }
        }
        if(isset($wysiwygarray1) && count($wysiwygarray1) === count($wysiwygarray2)) {
            $wysiwyg3 = array_combine($wysiwygarray1, $wysiwygarray2);
        }
    }
    
    // Process WYSIWYG choice fields
    $wysiwyg3_choice = array();
    if(isset($wysiwygitem_choice) && !empty($wysiwygitem_choice)) {
        $wysiwygarray2_choice = array();
        foreach ($wysiwygitem_choice as $wysiwyg1_choice => $wysiwygv_choice) {
            $wysiwygfound_choice = $wysiwygitem_choice[$wysiwyg1_choice]['fname']."_name";
            if(isset($_POST[$wysiwygfound_choice])) {
                $wysiwygarray2_choice[] = $_POST[$wysiwygfound_choice];
            }
        }
        if(isset($wysiwygarray1_choice) && count($wysiwygarray1_choice) === count($wysiwygarray2_choice)) {
            $wysiwyg3_choice = array_combine($wysiwygarray1_choice, $wysiwygarray2_choice);
        }
    }
    
    // Process date fields
    $dates3 = array();
    if(isset($dates) && !empty($dates)) {
        $datesarray2 = array();
        foreach ($dates as $dates1 => $datesval) {
            $datefound = $dates[$dates1]['fname'];
            if(isset($_POST[$datefound])) {
                $datefound2 = $_POST[$datefound];
                $datefoundnewdate = strtotime($datefound2);
                $datefoundnewdate = date("Y-m-d", $datefoundnewdate);
                $datesarray2[] = $datefoundnewdate;
            }
        }
        if(isset($datesarray1) && count($datesarray1) === count($datesarray2)) {
            $dates3 = array_combine($datesarray1, $datesarray2);
        }
    }
    
    // Process insertion date
    $array_date = array();
    if(isset($datedfield) && isset($_POST['insertion_date'])) {
        $datedfield1 = $_POST['insertion_date'];
        $newdate2 = strtotime($datedfield1);
        $newdate = date("Y-m-d", $newdate2);
        $array_date = array('insertion_date' => $newdate);
    }
    
    // Process direct fields
    $direct3 = array();
    if(isset($directfields) && !empty($directfields)) {
        foreach ($directfields as $d1 => $dvalue) {
            $field_name = $directfields[$d1]['fname'];
            $field_value = $directfields[$d1]['value'];
            $direct3[$field_name] = $field_value;
        }
    }
    
    // Build array_one (metadata/system fields)
    $extra = array('status' => 'Active', 'alias' => $alias);
    $array_one = $extra;
    
    // Merge all system/metadata fields into array_one
    if(!empty($integer3)) {
        $array_one = array_merge($array_one, $integer3);
    }
    if(!empty($array_date)) {
        $array_one = array_merge($array_one, $array_date);
    }
    if(isset($droparray) && !empty($droparray)) {
        $array_one = array_merge($array_one, $droparray);
    }
    if(isset($droparray2) && !empty($droparray2)) {
        $array_one = array_merge($array_one, $droparray2);
    }
    if(isset($droparray2_sub) && !empty($droparray2_sub)) {
        $array_one = array_merge($array_one, $droparray2_sub);
    }
    if(!empty($dates3)) {
        $array_one = array_merge($array_one, $dates3);
    }
    if(isset($checkboxes) && !empty($checkboxes)) {
        $array_one = array_merge($array_one, $checkboxes);
    }
    if(isset($timepicks) && !empty($timepicks)) {
        $array_one = array_merge($array_one, $timepicks);
    }
    
    // Build array_two (content/user input fields)
    $array_two = array();
    
    // First, capture direct form fields that match patterns like col1_name, col2_name, etc.
    $form_fields = array();
    foreach($_POST as $key => $value) {
        // Skip system fields and empty values
        if($key !== 'add_item' && $key !== 'add_itembtn' && $value !== '') {
            // First, capture direct form fields that match patterns like col1_name, col2_name, etc.
    $form_fields = array();
    foreach($_POST as $key => $value) {
        // Skip system fields and empty values
        if($key !== 'add_item' && $key !== 'add_itembtn' && $value !== '') {
            // Check if it's a column field (col1_name, col2_name, etc.) or other form field
            if(preg_match('/^col\d+_name$/', $key)) {
                // Remove '_name' suffix from column fields
                $clean_key = str_replace('_name', '', $key);
                $form_fields[$clean_key] = $value;
            }
            elseif(preg_match('/^[a-zA-Z_]+_name$/', $key)) {
                // Remove '_name' suffix from other fields ending with '_name'
                $clean_key = str_replace('_name', '', $key);
                $form_fields[$clean_key] = $value;
            }
            elseif(in_array($key, array('dated', 'name', 'email', 'phone', 'address'))) {
                // Keep these fields as-is
                $form_fields[$key] = $value;
            }
        }
    }
        }
    }
    
    // Start with form fields
    if(!empty($form_fields)) {
        $array_two = array_merge($array_two, $form_fields);
    }
    
    // Merge all other content fields into array_two
    if(!empty($text3)) {
        $array_two = array_merge($array_two, $text3);
    }
    if(!empty($wysiwyg3)) {
        $array_two = array_merge($array_two, $wysiwyg3);
    }
    if(!empty($wysiwyg3_choice)) {
        $array_two = array_merge($array_two, $wysiwyg3_choice);
    }
    if(!empty($direct3)) {
        $array_two = array_merge($array_two, $direct3);
    }
    
    // Debug: Show what we're about to insert
    //echo "<pre>DEBUG - Final Array One (metadata):\n";
    //print_r($array_one);
    //echo "\nDEBUG - Final Array Two (content):\n";
    //print_r($array_two);
    //echo "</pre>";
    
    // Handle edit vs insert
    if(isset($edit) && $edit != "0") {
        // Edit mode
        $array_three = array_merge($array_one, $array_two);
        var_dump($array_three);
        $editdata = db_editdata($form_table,'id', $array_three, $lead_id, $alias,null);
        if(isset($update_settings) && !empty($update_settings)) {
         /*   add_updatev2($update_settings['update_flag'], $lead_id, $_SESSION['user'], 
                        $update_settings['update_ref'], $update_settings['update_email']);
    */
        }
        
        $img_id = $lead_id;
    } else {
        // Insert mode
        $editdata = db_insertdata($form_table, $array_two, $array_one, 0, null);
        $img_id = $editdata;
    }
    
    // Handle result
    if($editdata) {
        if(isset($redirect) && !empty($redirect)) {
            echo "Inserted / updated ! ";
            app_redirect_adv($redirect);
        } else {
            echo "error";
            if(isset($redirect2)) {
                $redirect2 = $redirect2."/".$img_id;
                app_redirect_adv($redirect2);
            }
        }
    } else {
        return;
    }
}
?>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div>
<?php
   
} 
}
/////////////////////////// function ends /////////////////////////////////



























?>