Looping through FormVites

@section head {
}

@functions{ 


    EngineRecordList getFormVites(){
    
    EngineAdapter ad = EngineAdapter.Create("FormVites", "FormVites"); 
    ad.AddResultFields(new[] {"RegForms", "RegFormsOptional", "RegFormsMulti","DocumentType"});
    ad.AddCondition("<Eq FieldId=\"AddToEbridge\" Value=\"true\"/>");
    
        return ad.GetList(1, 1000);
    }


    System.Collections.Generic.List<Int32> AllFormIds(EngineRecordList formViteForms){
    
        System.Collections.Generic.List<Int32> _eventIds = new System.Collections.Generic.List<Int32>();
        
        foreach(EngineRecord fv in formViteForms){
             if(formViteForms.Count > 0){
                foreach(EngineRecord i in formViteForms){
                    if(i.GetRecordList("RegForms").Count > 0){
                        foreach(EngineRecord r in i.GetRecordList("RegForms")){
                            if(!_eventIds.Contains(r.Id.Value)){
                                _eventIds.Add(r.Id.Value);
                            }
                        }
                    }
                    
                    if(i.GetRecordList("RegFormsOptional").Count > 0){
                        foreach(EngineRecord r in i.GetRecordList("RegFormsOptional")){
                            if(!_eventIds.Contains(r.Id.Value)){
                                _eventIds.Add(r.Id.Value);
                            }
                        }
                    }
                    
                    if(i.GetRecordList("RegFormsMulti").Count > 0){
                        foreach(EngineRecord r in i.GetRecordList("RegFormsMulti")){
                            if(!_eventIds.Contains(r.Id.Value)){
                                _eventIds.Add(r.Id.Value);
                            }
                        }
                    }
                }
             }
        }
        
        return _eventIds;
    
    }
     // END ATTACHMENT STUFF

        EngineRecordList getLinks(int fvId, string lastRunDate, EngineRecordList formViteForms){

    System.Collections.Generic.List<Int32> _eventIds = new System.Collections.Generic.List<Int32>();
    string eventIdCondition = "";
    //Get RegFormIds For the FormVites
    
     if(formViteForms.Count > 0){
    
        foreach(EngineRecord i in formViteForms){
            if(i.Id == fvId){
                if(i.GetRecordList("RegForms").Count > 0){
                    foreach(EngineRecord r in i.GetRecordList("RegForms")){
                        if(!_eventIds.Contains(r.Id.Value)){
                            _eventIds.Add(r.Id.Value);
                            eventIdCondition += "<Match Value=\"" + r.Id.Value.ToString()  + "\"/>";
                        }
                    }
                }
            
                if(i.GetRecordList("RegFormsOptional").Count > 0){
                    foreach(EngineRecord r in i.GetRecordList("RegFormsOptional")){
                        if(!_eventIds.Contains(r.Id.Value)){
                            _eventIds.Add(r.Id.Value);
                            eventIdCondition += "<Match Value=\"" + r.Id.Value.ToString()  + "\"/>";
                        }
                    }
                }
            
                if(i.GetRecordList("RegFormsMulti").Count > 0){
                    foreach(EngineRecord r in i.GetRecordList("RegFormsMulti")){
                        if(!_eventIds.Contains(r.Id.Value)){
                            _eventIds.Add(r.Id.Value);
                            eventIdCondition += "<Match Value=\"" + r.Id.Value.ToString()  + "\"/>";
                        }
                    }
                }
            
             }
        }
     }

    string contactIdCondition = "";
   
    //Get Latest Approved Formvites 
    EngineAdapter ad = EngineAdapter.Create("FormInvitees"); 
    ad.AddResultFields(new[] {"ContactId" });
    ad.AddCondition("<Eq FieldId=\"FormVitesId\" Value=\"" + fvId.ToString() + "\"/>");
    ad.AddCondition("<Ge Not=\"false\" FieldId=\"SubmittedDate\" Value=\"" + lastRunDate + "\"/>");
    EngineRecordList invitees = ad.GetList(1, 2000);
      
     
     EngineRecordList churches = null;
        
     if(invitees.Count > 0){
    
        foreach(EngineRecord i in invitees){
            contactIdCondition += "<Match Value=\"" + @i.GetInteger("ContactId").ToString()  + "\"/>";
        }
      //Get church records and reg forms
        EngineAdapter ad3 = EngineAdapter.Create("Contacts","Churches"); 
        
        ad3.AddResultFields(new[] {"FirstName","District.Name","Registrations.AccessKey","Registrations.RegisterDate","Registrations.EventId","Registrations.EventName","Registrations.WhenUpdated","GCFANumber"});
        ad3.AddCondition("<And><InList FieldId=\"Id\">" + contactIdCondition + "</InList><InList FieldId=\"Registrations.EventId\">" + eventIdCondition + "</InList></And>", true);
        churches = ad3.GetList(1, 2000);  
        
     }
      
   return churches;
    
    //End getLinks    
    } 
 
 //End Functions
}  


@{
    string lastRunDate = "";
    string thisRunDate = "";
}

@if(!String.IsNullOrEmpty(Request.QueryString["lrd"])){
    
    lastRunDate = Request.QueryString["lrd"];
    thisRunDate = DateTime.Now.ToString("G");
   
    <div>
    @("lrd" + @thisRunDate)
    </div>

 

   EngineRecordList FormVites = getFormVites();
     
     System.Collections.Generic.List<Int32> FormViteIds = new System.Collections.Generic.List<Int32>();
     
     foreach(EngineRecord fv in FormVites){
        FormViteIds.Add(fv.Id.Value);
     }
     
     System.Collections.Generic.List<Int32> FormIds = AllFormIds(FormVites);
     System.Collections.Generic.List<Int32> fakeIds = new System.Collections.Generic.List<Int32>(){1};
     System.Collections.Generic.List<Int32> FormsWithFiles = new System.Collections.Generic.List<Int32>();
     

     
     foreach(int f in FormIds){
        @BRT.RegData(f, pageSize:1,regIds:fakeIds,
        template:
        @<text>
            @foreach(RegDataHelperExtensions.EventRegData.Question q in item.Questions){
                if(q.Type == 12){
                    FormsWithFiles.Add(f);
                }
            }
        </text>);
     }
    
    foreach(EngineRecord fv in FormVites){
     int fvId = fv.Id.Value;
     string fvDocumentType = "None";
     if(!fv.IsDBNull("DocumentType")){
        fvDocumentType = fv.GetFirstCategoryLabel("DocumentType");
     }
    
       EngineRecordList churches = getLinks(fvId,lastRunDate,FormVites );
        
        if(churches != null){
            foreach(EngineRecord church in churches){
                if(church.GetRecordList("Registrations").Count > 0){
                    foreach(EngineRecord reg in church.GetRecordList("Registrations")){
                        <div>
                        *|https://iaumc-reg.brtapp.com/confirmcondensed/@reg.GetString("AccessKey")|@(church.GetString("FirstName").Replace("\\","").Replace("/","").Replace(" ","").Replace(",","").Replace("(","").Replace(")","") + "-" +  @reg.Id.Value.ToString() +  ".pdf")|@(church.GetRecordList("District").Count > 0? church.GetRecordList("District")[0].GetString("Name"):"")|@church.GetString("FirstName") - @(church.GetString("GCFANumber") + "CH")|@reg.GetDateTime("WhenUpdated").ToString("MM/dd/yyyy")|@(reg.GetString("EventName") + " _FV_|" + fvDocumentType)
                        </div>
                        
                        if(FormsWithFiles.Contains(reg.GetInteger("EventId"))){
                            @BRT.RegData(reg.GetInteger("EventId"), pageSize:1,accessKeys:new System.Collections.Generic.List<string>(){reg.GetString("AccessKey")},
                            template:
                            @<text>
                                @foreach(RegDataHelperExtensions.EventRegData.Registration regg  in item.Registrations){
                                    foreach(RegDataHelperExtensions.EventRegData.Registrant registrant in regg.Registrants){
                                        foreach(RegDataHelperExtensions.EventRegData.RegistrantAnswer a in registrant.Answers){
                                            if(a.AnswerFiles.Count > 0){
                                                foreach(String surl in a.AnswerFiles){
                                                    <div>
                                                        @{
                                                        string newname = @reg.Id.Value.ToString() + "-" + System.IO.Path.GetFileNameWithoutExtension(surl) + ".pdf";
                                                        string oldname = @reg.Id.Value.ToString() + "-" + System.IO.Path.GetFileName(surl);
                                                        }
                                                        @(System.IO.Path.GetExtension(surl).Trim() == ".pdf"?"==":"+++")|@surl|@newname|@(church.GetRecordList("District").Count > 0? church.GetRecordList("District")[0].GetString("Name"):"")|@church.GetString("FirstName") - @(church.GetString("GCFANumber") + "CH")|@reg.GetDateTime("WhenUpdated").ToString("MM/dd/yyyy")|@(reg.GetString("EventName") + " _FV_") |@oldname|@fvDocumentType
                                                    </div>
                                                }
                                            }
                                               
                                        }
                                        
                                    }
                                }
                            </text>)
        
        
                        }
                    }
                }
            }
        }

    }
    

}