global class NewAndEditContactController extends NewAndEditBaseController 
{
    public String unifiedIContactID{set;get;}
    public String staticResourceContactV2 {get; set;}
    
	
    public NewAndEditContactController(ApexPages.StandardController controller) {
        List<String> fieldList = new List<String>(Schema.getGlobalDescribe().get('Contact').getDescribe().fields.getMap().keyset());  
        // Add fields to controller. This is to avoid the SOQL error in visualforce page
        if (!Test.isRunningTest()) {
            controller.addFields(fieldList);
        }
        Init(controller.getRecord());
        String contactId = controller.getRecord().Id;
        if(contactId != null){
            Contact c = [select id,UnifiedI_Contact_ID__c from Contact where Id =:contactId ];
            system.debug('Contact c = '+c);
            unifiedIContactID = c.UnifiedI_Contact_ID__c;
        }else{
            Map<string,string> mso = ApexPages.currentPage().getParameters();
            system.debug('mso='+mso);
            if(mso.containsKey('con4_lkid')){
                controller.getRecord().put('AccountId',mso.get('con4_lkid'));
            }
        }
        system.debug('controller.getRecord()='+controller.getRecord());
        //zhj MEBG新方案改造 2022-11-24 start
        PIHelper.PIIntegration piIntegration = PIHelper.getPIIntegrationInfo('contactV2');
        staticResourceContactV2 = JSON.serialize(piIntegration);
        System.debug('staticResourceContactV2 = ' + staticResourceContactV2);
        //zhj MEBG新方案改造 2022-11-24 end
    }
    
    PageReference RedirectStandardPage(){
        Map<string,string> mso = ApexPages.currentPage().getParameters();
        system.debug(mso);
        PageReference pg = null;
        mso.remove('sfdc.override');
        system.debug('recordId='+recordId);
        if(string.isBlank(recordId)){
            pg = new PageReference('/003/e');
        }else{
            pg = new PageReference('/'+recordId+'/e');
        }
        //pg.getParameters().putAll(mso);
        pg.getParameters().put('RecordType',mso.get('RecordType'));
        pg.getParameters().put('accid',mso.get('accid'));
        pg.getParameters().put('nooverride','1');
        pg.setRedirect(true);
        return pg;
    }

    public PageReference PageLoad(){
        system.debug('rtTypeId='+rtTypeId);
        string s = null;
        if(!string.isBlank(rtTypeId)){
            s = Schema.SObjectType.Contact.getRecordTypeInfosById().get(rtTypeId).getDeveloperName();
            if(s == 'Agency' || s == 'Internal_staff'){
                return RedirectStandardPage();
            }
        }
        string accid = ApexPages.currentPage().getParameters().get('accid');
        if(!string.isBlank(accid)){
            List<Account> accs = [select RecordType.DeveloperName from account where id = :accid];
            if(accs.size()>0){
                s = accs[0].RecordType.DeveloperName;
                if(s == 'Office' || s == 'AgencyContact' || s == 'Agency'){
                    return RedirectStandardPage();
                }
            }
            
            
        }
        system.debug('null');
        return null;
    }
    
    @RemoteAction
    global static Response saveContact(String leadJson,String transId,Boolean isNew) {
        return save(new Contact(),leadJson,transId,isNew);
    }

    //zhj MEBG新方案改造 2022-11-24 start
    @RemoteAction
    global static ResponseV2 searchContactByAccountId(String accountId,String RecordTypeId,String Isactive) {
        System.debug('accountId = ' + accountId);
        ResponseV2 res = new ResponseV2();
        try{
            Account account = [select Id,Name from Account where Id = :accountId];
            List<Contact> contactList = [select id,AWS_Data_Id__c, CManageCode__c,Name from Contact where AccountId = :account.Id];
            Map<String,String> dataIdManageCodeMap = new Map<String,String>();
            for(Contact contact : contactList){
                dataIdManageCodeMap.put(contact.AWS_Data_Id__c,contact.CManageCode__c);
            }
            res.dataIdManageCodeMap = dataIdManageCodeMap;
            res.accountName = account.Name;
            res.contactList = contactList;

            ID DoctorRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Doctor').getRecordTypeId();
            Map<String,String> accMap= new Map<String,String>();
            List<Account> accList=[select ID,Is_Active__c,Is_Active_Formula__c from Account where ID =:accountId];
            if(accList.size()>0){
                for(Account acc:accList){
                    if (String.isNotBlank(acc.Is_Active__c)){
                        String subId = acc.Id;
                        accMap.put(subId.substring(0,15), acc.Is_Active__c);
                        continue;
                    }
                    if (String.isNotBlank(acc.Is_Active_Formula__c)){
                        String subId = acc.Id;
                        accMap.put(subId.substring(0,15), acc.Is_Active_Formula__c);
                    }
                }
            }
            System.debug('accMap= ' + accMap);
            //如果是Doctor记录类型的需要进行姓名+手机号判断
            if(DoctorRecordTypeId.equals(RecordTypeId)){
                res.isNameMobileVerif = true;
            }
            System.debug('Isactive= ' + Isactive);
            System.debug('DoctorRecordTypeId.equals(RecordTypeId) = ' + DoctorRecordTypeId.equals(RecordTypeId));
            System.debug('accMap.get(accountId) = ' + accMap.get(accountId));
            if (('有效'.equals(Isactive) || '有効'.equals(Isactive))
                && ('有効'.equals(accMap.get(accountId)) || '有效'.equals(accMap.get(accountId))) && (DoctorRecordTypeId.equals(RecordTypeId))) {
                res.isMobileVerif = true;
            }else {
                res.isMobileVerif = false;
            }
            res.status = 'success';
            return res;
        }catch(Exception e){
            res.message = e.getMessage() + '';
            res.status = 'fail';
            return res;
        }
    }

    global class ResponseV2{
        public List<Contact> contactList{set;get;}
        public String accountName{set;get;}
        public Map<String,String> dataIdManageCodeMap{set;get;}
        public Boolean isMobileVerif;
        public Boolean isNameMobileVerif;
        public String message{set;get;}
        public String status{set;get;}
    }
    //zhj MEBG新方案改造 2022-11-24 end
}