global without sharing class eSignFormFileUploadController {
    //public String staticResource {get; set;}   //deloitte-zhj 20231213 PIPL还原
    public static String sobjectTypeValue = 'Document';
    public String PIPL_Input_PDF_Error_Msg{set;get;}
    public List<ESignFileAddress>  fileList{set;get;}
    public Document documentData{set;get;}
    public String parentId{set;get;}
    public eSignFormFileUploadController(ApexPages.StandardController controller) {
        //staticResource = JSON.serialize(PIHelper.getPIIntegrationInfo('Document'));
        SObject obj = controller.getRecord();     
        parentId = obj.Id;
        system.debug('Parent Id:'+parentId); 
        fileList=getFileds(parentId);
    }

    global class Response{
        public String recordId{set;get;}
        public String message{set;get;}
        public String status{set;get;}
    }

    //deloitte-zhj 20231213 PIPL还原 start
    // @RemoteAction
    // global static Response saveFile(String fileName,String key,String transId,String parentId){
    //     FileAddress__c file = new FileAddress__c();
    //     PIHelper.PIIntegration pI=PIHelper.getPIIntegrationInfo('Document');
    //     // 去除filename里得“&” bysushanhu 20220414
    //     fileName = fileName.remove('&');
    //     file.DownloadLink__c =pI.undeleteUrl+key+'&fileName='+fileName;
    //     file.FileName__c =fileName;
    //     file.ViewLink__c =pI.queryUrl+key;
    //     file.ParentRecordId__c =parentId;
    //     file.AWS_File_Key__c = key;
    //     Response response =new Response();
    //     Savepoint sp = Database.setSavepoint();
    //     try {
    //         insert file;
    //         //4. 插入日志 
    //         //update 20220218 加入新的日志方式
    //         PIHelper.saveTransLog(sobjectTypeValue,key,transId,file.Id,JSON.serialize(file),'success','');
    //         response.recordId=file.Id;
    //         response.status='success';
    //         return response;
    //     } catch (Exception e) {
    //         System.debug('into catch'+e.getMessage());
    //         PIHelper.saveTransLog(sobjectTypeValue,key,transId,file.Id,JSON.serialize(file),'fail',e.getMessage());
    //         Database.rollback(sp);
    //         response.message=e.getMessage();
    //         response.status='fail';
    //         return response;
    //     }
    // }
    //deloitte-zhj 20231213 PIPL还原 end

    public PageReference refreshFiles() {
        system.debug('refresh files');
        fileList=getFileds(parentId);
        system.debug('fileList size:'+String.valueOf(fileList.size()));
        system.debug(JSON.serialize(fileList));
        return null;
    }
    
    public static List<ESignFileAddress> getFileds(String parentId){
        List<ESignFileAddress>  fList = new List<ESignFileAddress>();
        List<FileAddress__c> faList = new List<FileAddress__c>();
        // SWAG-C9S9P6 2022-05-25 ssm start
        // 文件地址里增加招标项目专用字段（信息Id、项目阶段、更新日期）
        if(String.isNotBlank(parentId)){
            faList = [SELECT Id,ParentRecordId__c, FileName__c,DownloadLink__c,FileAddress__c.ViewLink__c,AWS_File_Key__c  //zhj 预览/下载 加token 2022/12/01
                    // SWAG-C9S9P6 新增字段 start
                    , InfoId__c, InfoType__c, subInfoType__c, UpdateDate__c 
                    // SWAG-C9S9P6 新增字段 end
                    FROM FileAddress__c where ParentRecordId__c=:parentId order by FileName__c asc,createddate desc];
        }else{
            faList = [SELECT Id, ParentRecordId__c,FileName__c,DownloadLink__c,FileAddress__c.ViewLink__c,AWS_File_Key__c  //zhj 预览/下载 加token 2022/12/01
                // SWAG-C9S9P6 新增字段 start
                , InfoId__c, InfoType__c, subInfoType__c, UpdateDate__c 
                // SWAG-C9S9P6 新增字段 end
                FROM FileAddress__c order by FileName__c asc,createddate desc limit 100];
        }
        for(FileAddress__c fa : faList){
            ESignFileAddress esfa = new ESignFileAddress();
            esfa.ESignFileAddressId = fa.Id;
            esfa.ParentRecordId = fa.ParentRecordId__c;
            esfa.FileName = fa.FileName__c;
            esfa.DownloadLink = fa.DownloadLink__c;
            esfa.ViewLink = fa.ViewLink__c;
            esfa.AWS_File_Key = fa.AWS_File_Key__c;
            esfa.InfoId = fa.InfoId__c;
            esfa.InfoType = fa.InfoType__c;
            esfa.subInfoType = fa.subInfoType__c;
            esfa.UpdateDate = fa.UpdateDate__c;
            if(esfa.FileName.substring(0,1) == 'A'){
                esfa.ContentType = '经销商';
            }else if(esfa.FileName.substring(0,1) == 'H'){
                esfa.ContentType = '医院';
            }else {
                esfa.ContentType = '';
            }
            //2023-03-13 zhj 是否是图片 start
            String fileType = fa.FileName__c.substring(fa.FileName__c.lastIndexOf('.') + 1).toLowerCase();
            if(fileType == 'png' || fileType=='jpg' || fileType == 'jpeg'){
                esfa.IsImg = true;
            }else {
                esfa.IsImg = false;
            }
            //2023-03-13 zhj 是否是图片 end
            fList.add(esfa);
        }
        return fList;
        // SWAG-C9S9P6 2022-05-25 ssm end
    }

    global class ESignFileAddress{
        public String ESignFileAddressId{set;get;}
        public String ParentRecordId{set;get;}
        public String FileName{set;get;}
        public String DownloadLink{set;get;}
        public String ViewLink{set;get;}
        public String AWS_File_Key{set;get;}
        public String InfoId{set;get;}
        public String InfoType{set;get;}
        public String subInfoType{set;get;}
        public Date UpdateDate{set;get;}
        public String ContentType{set;get;}
        public Boolean IsImg{set;get;} //2023-03-13 zhj 是否是图片
    }
}