public with sharing class MassFileDownloaderController {
    @AuraEnabled
    public static ContentDocumentItem[] getFiles(Integer offset) {
        List<ContentDocumentLink> linkList = [select ContentDocumentId from ContentDocumentLink where LinkedEntityId =: System.Label.ReceivingNoteFolder];
        List<Id> idList = new List<Id>();
        for(ContentDocumentLink link: linkList){
            idList.add(link.ContentDocumentId);
        }
        List<ContentDocument> conList = [
            SELECT
            Id, Title, FileExtension, LatestPublishedVersionId,Owner.Name,OwnerId,LastModifiedDate
            FROM ContentDocument where Id in :idList order by LastModifiedDate desc limit 50 offset:offset
        ];
        List<ContentDocumentItem> itemList = new List<ContentDocumentItem>();
        for(ContentDocument con: conList){
            itemList.add(new ContentDocumentItem(con,false));
        }
        return itemList;
    }
    @AuraEnabled
    public static ContentDocumentItem[] searchFiles(String title,Integer offset){
        if(title == ''){
            List<ContentDocumentLink> linkList = [select ContentDocumentId from ContentDocumentLink where LinkedEntityId =: System.Label.ReceivingNoteFolder];
            List<Id> idList = new List<Id>();
            for(ContentDocumentLink link: linkList){
                idList.add(link.ContentDocumentId);
            }
            List<ContentDocument> conList = [
                SELECT
                Id, Title, FileExtension, LatestPublishedVersionId,Owner.Name,OwnerId,LastModifiedDate
                FROM ContentDocument where Id in :idList order by LastModifiedDate desc limit 50 offset:offset
            ];
            List<ContentDocumentItem> itemList = new List<ContentDocumentItem>();
            for(ContentDocument con: conList){
                itemList.add(new ContentDocumentItem(con,false));
            }
            return itemList;
        }else{
            title = '%' + title + '%';
            List<ContentDocumentLink> linkList = [select ContentDocumentId from ContentDocumentLink where LinkedEntityId =: System.Label.ReceivingNoteFolder];
            List<Id> idList = new List<Id>();
            for(ContentDocumentLink link: linkList){
                idList.add(link.ContentDocumentId);
            }
            List<ContentDocument> conList = [
                SELECT
                Id, Title, FileExtension, LatestPublishedVersionId,Owner.Name,OwnerId,LastModifiedDate
                FROM ContentDocument where Id in :idList and Title like: title order by LastModifiedDate desc limit 50 offset:offset
            ];
            List<ContentDocumentItem> itemList = new List<ContentDocumentItem>();
            for(ContentDocument con: conList){
                itemList.add(new ContentDocumentItem(con,false));
            }
            return itemList;
        }
        
    }
    public class ContentDocumentItem {
        @AuraEnabled
        public ContentDocument con;
        @AuraEnabled
        public boolean isSelect;
        public ContentDocumentItem(ContentDocument con,Boolean isSelect){
            this.con = con;
            this.isSelect = isSelect;
        }
    }
}