<?php
include "includes/application_top.php";
validateAdmin();
$pgname="manage_contacts";

$status_arr = getStatuses();
$staffs_arr = getStaffs(); 
 $country_arr  = getCountry(); 
$getFormFields = "SELECT * FROM " . TBL_FORM_FIELDS . " WHERE cf_status='Y' ";
$execFormFields = $dbh->prepare($getFormFields)or die(print_r($dbh->errorInfo, true));
$execFormFields->execute();

$formFields = array();
while ($fetchFormFields = $execFormFields->fetch(PDO::FETCH_ASSOC)) {
    $formFields[] = $fetchFormFields;
}
$op_data = array();

 foreach ($formFields as $fkey => $fvalue) {
            $slug = generateSlug($fvalue['cf_name']);
            $ftype = $fvalue['cf_type'];

            $getDynamicData = "SELECT * FROM ".TBL_CONTACT_DYNAMIC_VALUES." WHERE cd_cf_id=:cd_cf_id AND cd_status = 'Y'";
            $recgetDynamicData = $dbh->prepare($getDynamicData);
            // $recgetDynamicData->bindParam(':cd_ct_id', $fetchContacts['ct_id']);
            $recgetDynamicData->bindParam(':cd_cf_id', $fvalue['cf_id']);
            $recgetDynamicData->execute();
            
            while($fecgetDynamicData = $recgetDynamicData->fetch(PDO::FETCH_ASSOC)){
                $op_data[$fecgetDynamicData['cd_ct_id']][$fecgetDynamicData['cd_cf_id']][$fecgetDynamicData['cd_cfo_id']] = $fecgetDynamicData['cd_cfo_value'];
            }

}


$getContacts = "SELECT * FROM soc_data WHERE ct_archived = 'N' AND ct_merged = 'N' ORDER BY ct_id DESC " ;
$execContacts = $dbh->prepare($getContacts);
$execContacts->execute();

$i=0;
$records = array();
while($fetchContacts = $execContacts->fetch(PDO::FETCH_ASSOC)){
  
      $records["data"][$i]['checkbox'] = '<input type="checkbox" name="mass_id" value="'.$fetchContacts["ct_ct_id"].'" class="ct_id" id="tr_'.$fetchContacts["ct_ct_id"].'"><br/><a  href="show_contact_details.php?id='.$fetchContacts['ct_ct_id'].'" title="View contact" ><i class="fa fa-eye"></i></a>
                  <a  onclick="deleteRecord('.$fetchContacts['ct_ct_id'].');" title="Delete contact"><i class="fa fa-trash"></i></a>
                <a  class="mag-popup" ct_id="'.$fetchContacts['ct_ct_id'].'" href="getTags.php?ct_id='.$fetchContacts['ct_ct_id'].'" title="Show tags"><i class="fa fa-tags"></i></a>
                 <a  class="mag-popup-1" ct_id="'.$fetchContacts['ct_ct_id'].'" href="getRelatedContacts.php?ct_id='.$fetchContacts['ct_ct_id'].'" title="Related contacts"><i class="fa fa-users"></i></a>';

        $name= $fetchContacts['ct_name'];
                        if($fetchContacts['ct_lname']!=''){ $name = $fetchContacts['ct_name']." ".$fetchContacts['ct_lname'];
                         }

        $records["data"][$i]['name'] = '<span class="td_div" static_slug="name"><span class="td_edit"><input type="text" class="td_edit_input" input_type="text" col_name="ct_name" col_tbl="'.TBL_CONTACT.'" value="'.$name.'"></span><span class="td_show td_div_sel" static_slug="name">'.$name.'</span></span>';


        

        if($fetchContacts['ct_followup_date'] == "" || $fetchContacts['ct_followup_date'] == '0000-00-00'  || $fetchContacts['ct_followup_date'] == '1970-01-01' || $fetchContacts['ct_followup_date'] == '1969-12-31'){
            $followup_date = '-';
            $followup_date12 ='-';
        }else{
            $followup_date = date('Y-m-d', strtotime($fetchContacts['ct_followup_date']));
            $followup_date12 = date('m-d-Y', strtotime($fetchContacts['ct_followup_date']));
        }

        $records["data"][$i]['followup_date'] = '<span class="td_div" static_slug="follow_up_date"><span class="td_edit"><input type="text" class="td_edit_input td_edit_input_datepicker" input_type="datepicker" col_name="ct_followup_date" col_tbl="'.TBL_CONTACT.'" value="'.$followup_date .'"></span>
                        <span id="follo'.$fetchContacts['ct_ct_id'].'" class="td_show">'.$followup_date12 .'</span></span>';

        if($fetchContacts['ct_email'] != ""){
            $email = $fetchContacts['ct_email'];
        }else{
            $email = '-';
        }

         $records["data"][$i]['email'] = ' <span class="td_div" static_slug="email"><span class="td_edit"><textarea cols="40" rows="3" class="td_edit_input" input_type="text" col_name="ct_email" col_tbl="'.TBL_CONTACT_BY.'">'.$fetchContacts['ct_email'].'</textarea></span>
                        <span class="td_show">'.$email.'
                        </span></span>';

        if($fetchContacts['ct_phone'] != ""){
            $phone = $fetchContacts['ct_phone'];
        }else{
            $phone = '-';
        }

         $records["data"][$i]['phone'] = ' <span class="td_div" static_slug="phone"><span class="td_edit"><textarea cols="30" rows="3" class="td_edit_input"  input_type="text" col_name="ct_phone" col_tbl="'.TBL_CONTACT_BY.'">'.$fetchContacts['ct_phone'].'</textarea></span>
                        <span class="td_show">'.$phone.'</span></span>';

        if($fetchContacts['ct_description'] != ""){
            $project_desc = $fetchContacts['ct_description'];
        }else{
            $project_desc = '-';
        }

         $records["data"][$i]['project_description'] = '<span class="td_div" static_slug="project_description"><span class="td_edit">
                        <textarea cols="30" rows="3" class="td_edit_input" input_type="text" col_name="ct_description" col_tbl="'.TBL_CONTACT.'">'.$fetchContacts['ct_description'].'</textarea></span>
                       
                        <span class="td_show item more">'.$project_desc.'</span></span>';

        


         if($fetchContacts['ct_import_date'] == "" || $fetchContacts['ct_import_date'] == '0000-00-00 00:00:00' || $fetchContacts['ct_import_date'] == '01-01-1970 00:00:00' || $fetchContacts['ct_import_date'] == '1969-12-31 00:00:00') {
                          $import_date = '-';
                          $import_date1 ='-';
                        }else{
                            // $followup_date = convertToDate($fetchContacts['ct_followup_date']);
                            $import_date = date('Y-m-d H:i:s', strtotime($fetchContacts['ct_import_date']));
                            $import_date1 = date('m-d-Y H:i:s', strtotime($fetchContacts['ct_import_date']));
                        }
                        //echo $import_date1; die;

        $records["data"][$i]['import_date'] = '<span class="td_div" static_slug="import_date"><span class="td_edit"><input type="text" class="td_edit_input td_edit_input_datepicker" input_type="datepicker" col_name="ct_import_date" col_tbl="'.TBL_CONTACT.'" value="'.$import_date.'"></span><span class="td_show">'.$import_date1.'</span></span>';

       
        if($fetchContacts['ct_source'] != ""){
            $source = $fetchContacts['ct_source'];
        }else{
            $source = '-';
        }


        $records["data"][$i]['source'] = ' <span class="td_div" static_slug="source"><span class="td_edit"><input type="text" class="td_edit_input" input_type="text" col_name="ct_source" col_tbl="'.TBL_CONTACT.'" value="'.$fetchContacts['ct_source'].'"></span><span class="td_show">'.$source.'</span></span>';


       $records["data"][$i]['rating'] = ' <span class="td_div " static_slug="star_rating"><span class="td_div_sel_star" static_slug="star_rating_'.$fetchContacts['ct_ct_id'].'" id="td_star_'.$fetchContacts['ct_ct_id'].'">
                           <img src="dist/img/'.$fetchContacts['ct_star_rating'].'_star.png'.'">
                            <Span style="display: none;">'.$fetchContacts['ct_star_rating'].'</Span>
                        </span></span>';
      
         if($fetchContacts['ct_deleted'] > 0){ $status = $status_arr[$fetchContacts['ct_deleted']]; }else{ $status = '-';}
         $records["data"][$i]['status'] = ' <span class="td_div_slug" static_slug="status"><span class="td_edit">
                        <select id="editstatus" class="td_edit_input"  input_type="select"  col_tbl="'. TBL_CONTACT.'" col_name="ct_deleted" >
                            

                        </select>
                        </span>

                        <span class="td_show td_div_sel" static_slug="status">'.$status.'</span></span>';

      
                       // die;
        foreach ($formFields as $fkey => $fvalue) {
            $slug = generateSlug($fvalue['cf_name']);
            $ftype = $fvalue['cf_type'];

            switch ($ftype) {
                case 'text':
                    
                    
                   $records["data"][$i][$slug] = '<span class="td_div" static_slug="'.$slug.'"><span class="td_edit">
                        <input type="text" cols="20" rows="3" class="td_edit_input"  input_type="text"  col_tbl="'.TBL_CONTACT_DYNAMIC_VALUES.'" col_name="cd_cfo_value" cf_id = "'.$fvalue['cf_id'].'" value="'.$op_data[$fetchContacts['ct_ct_id']][$fvalue['cf_id']][0].'" />
                    </span>
                    <span class="td_show">'.$op_data[$fetchContacts['ct_ct_id']][$fvalue['cf_id']][0].'
                    </span></span>';
                    
                   

                    break;


                case 'textarea':

                    if($op_data[$fetchContacts['ct_ct_id']][$fvalue['cf_id']][0] != ""){
                        $reason = $op_data[$fetchContacts['ct_ct_id']][$fvalue['cf_id']][0];
                    }else{
                        $reason = '-';
                    }
                   

                   $records["data"][$i][$slug] = '<span class="td_div" static_slug="'.$slug.'"><span class="td_edit">
                        
                        <textarea cols="20" rows="3" class="td_edit_input"  input_type="textarea"  col_tbl="'.TBL_CONTACT_DYNAMIC_VALUES.'" col_name="cd_cfo_value" cf_id = "'.$fvalue['cf_id'].'">'.$op_data[$fetchContacts['ct_ct_id']][$fvalue['cf_id']][0].'</textarea>
                    </span>
                    <span class="td_show">'.$reason.'
                    </span></span>';
                   
                   
                    break;


                case 'radio':
                    
                    $getOptions = "SELECT * FROM " . TBL_FORM_FIELD_OPTIONS . " WHERE cfo_cf_id = :cfo_cf_id AND cfo_status='Y'";
                    $execOptions = $dbh->prepare($getOptions)or die(print_r($dbh->errorInfo, true));
                    $execOptions->bindParam(':cfo_cf_id', $fvalue['cf_id']);
                    $execOptions->execute();
                    
                    while ($fetchOptions = $execOptions->fetch(PDO::FETCH_ASSOC)) {

                        if(is_array($op_data[$fetchContacts['ct_ct_id']][$fvalue['cf_id']]) && array_key_exists($fetchOptions['cfo_id'], $op_data[$fetchContacts['ct_ct_id']][$fvalue['cf_id']])){ 
                            $final_value = $fetchOptions['cfo_name'];
                            break;
                            }
                        else{
                            $final_value = '-';
                        }
                    }

                     $records["data"][$i][$slug] = '  <span class="td_div" static_slug="'.$slug.'"><span class="td_show td_div_sel" id="td_'.$fvalue['cf_id'].'_'.$fetchContacts['ct_ct_id'].'" static_slug="edit_'.$fvalue['cf_id'].'_'.$fetchContacts['ct_ct_id'].'">'.$final_value.'</span></span>';
                            
                            
                    

                    break;


                case 'select':
                   
                    $getOptions = "SELECT * FROM " . TBL_FORM_FIELD_OPTIONS . " WHERE cfo_cf_id = :cfo_cf_id AND cfo_status='Y'";
                    $execOptions = $dbh->prepare($getOptions)or die(print_r($dbh->errorInfo, true));
                    $execOptions->bindParam(':cfo_cf_id', $fvalue['cf_id']);
                    $execOptions->execute();

                    while ($fetchOptions = $execOptions->fetch(PDO::FETCH_ASSOC)) {
                        if(is_array($op_data[$fetchContacts['ct_ct_id']][$fvalue['cf_id']]) && array_key_exists($fetchOptions['cfo_id'], $op_data[$fetchContacts['ct_ct_id']][$fvalue['cf_id']])){
                         $final_value = $fetchOptions['cfo_name'];
                            break; 
                        
                        }else{
                            $final_value = '-';
                        }
                    }
                   
                           
                          $records["data"][$i][$slug] = '<span class="td_div_slug" static_slug="'.$slug.'"><span class="td_show td_div_sel" id="td_'.$fvalue['cf_id'].'_'.$fetchContacts['ct_ct_id'].'" static_slug="edit_'.$fvalue['cf_id'].'_'.$fetchContacts['ct_ct_id'].'">'.$final_value.'</span></span>';

                           
                        break;

                case 'checkbox':
                   
                    $getOptions = "SELECT * FROM " . TBL_FORM_FIELD_OPTIONS . " WHERE cfo_cf_id = :cfo_cf_id AND cfo_status='Y'";
                    $execOptions = $dbh->prepare($getOptions)or die(print_r($dbh->errorInfo, true));
                    $execOptions->bindParam(':cfo_cf_id', $fvalue['cf_id']);
                    $execOptions->execute();
                    $checked_values = "";
                    while ($fetchOptions = $execOptions->fetch(PDO::FETCH_ASSOC)) {
                        
                        if(array_key_exists($fetchOptions['cfo_id'], $op_data[$fetchContacts['ct_ct_id']][$fvalue['cf_id']])){ 
                            $checked_values .= $fetchOptions['cfo_name'].", ";
                        }else{
                            $checked_values .= '';
                        }
                        
                    }            
                    
                    
                   $records["data"][$i][$slug]  .= '<span class="td_div" static_slug="'.$slug.'"><span class="td_edit">
                        <select id="edit'.$fvalue['cf_id'].'" class="td_edit_input_multiple"  input_type="checkbox"  col_tbl="'.TBL_CONTACT_DYNAMIC_VALUES.'" col_name="cd_cfo_id" cf_id = "'.$fvalue['cf_id'].'" multiple>
                            <option value="">Please select</option>';
                            
                            $getOptions = "SELECT * FROM " . TBL_FORM_FIELD_OPTIONS . " WHERE cfo_cf_id = :cfo_cf_id AND cfo_status='Y'";
                            $execOptions = $dbh->prepare($getOptions)or die(print_r($dbh->errorInfo, true));
                            $execOptions->bindParam(':cfo_cf_id', $fvalue['cf_id']);
                            $execOptions->execute();
                            while($fecexecOptions1 = $execOptions->fetch(PDO::FETCH_ASSOC)){
                                $records["data"][$i][$slug] .='<option value="'.$fecexecOptions1['cfo_id'].'">'.$fecexecOptions1['cfo_name'].'</option>';
                            }
                            
                       $records["data"][$i][$slug] .= '</select>
                    </span>';


                    
                     
                    if($checked_values == ''){
                        $val = '-';
                    }
                    else{
                        $val = substr(trim($checked_values), 0, -1);
                    } 

                    $records["data"][$i][$slug] .= '<span class="td_show td_div_sel">'.$val.'</span></span>';
                    
                   //$records["data"][$i][$slug] = $html;
                    break; 
                case 'datepicker':
                    if($fieldCols[$fvalue['cf_id']][0]['mcl_field_status'] == 'N'){
                        $hideCol =  ' hideCol';
                    }
                    else{
                        $hideCol = ' ';
                    }
                    if($op_data[$fetchContacts['ct_ct_id']][$fvalue['cf_id']][0] != '')
                    {
                      $date = date('Y-m-d', strtotime($op_data[$fetchContacts['ct_ct_id']][$fvalue['cf_id']][0]));
                    }
                    else{
                      $date = '-';
                    }

                     
                    $records["data"][$i][$slug] = '<span class="td_div_slug" static_slug="'.$slug.'"><span class="td_edit">
                        <input type="text" cols="20" rows="3" class="td_edit_input td_edit_input_datepicker"  input_type="datepicker"  col_tbl="'.TBL_CONTACT_DYNAMIC_VALUES.'" col_name="cd_cfo_value" cf_id = "'.$fvalue['cf_id'].'" value="'.$date.'" />
                    </span>
                    <span class="td_show">'.$date.'
                    </span></span>';
                    
                    break;   
                                                           
                default:
                    # code...
                    break;
            }
        }

        $records["data"][$i]['action'] = '<a  target="_blank" href="show_contact_details.php?id='.$fetchContacts['ct_ct_id'].'" title="View contact" ><i class="fa fa-eye"></i></a>
                  <a  onclick="deleteRecord('.$fetchContacts['ct_ct_id'].');" title="Delete contact"><i class="fa fa-trash"></i></a>
                <a  class="mag-popup" ct_id="'.$fetchContacts['ct_ct_id'].'" href="getTags.php?ct_id='.$fetchContacts['ct_ct_id'].'" title="Show tags"><i class="fa fa-tags"></i></a>
                 <a  class="mag-popup-1" ct_id="'.$fetchContacts['ct_ct_id'].'" href="getRelatedContacts.php?ct_id='.$fetchContacts['ct_ct_id'].'" title="Related contacts"><i class="fa fa-users"></i></a>';
          $records["data"][$i]['no_followup'] = ' <span class="td_div" static_slug="no_followup"><span class="td_edit"><input type="text" class="td_edit_input"  col_name="ct_no_followup" col_tbl="'.TBL_CONTACT.'" value="'.$fetchContacts['ct_no_followup_check_flag'].'"></span>
                        <span class="td_show">'.$fetchContacts['ct_no_followup_check_flag'].'</span></span>';        
        
        $i++;
}
echo json_encode($records);