/*
 * Author: Yanan Chen
 * Created Date: 02/28/2022
 * Purpose: Utility class for describe layouts
 * Test Class: NewAndEditUserFaultInfoController 
 * History: 
 *      02/28/2022 - Yanan Chen - Initial Code.
 * 
 * */
global class NewAndEditUserFaultInfoController extends NewAndEditBaseController {
    public String PIPL_Input_Account_Error_Msg{set;get;}
    public String contactId{set;get;}//For Lookup field
    public String staticResourceContact {get; set;}
    public String contactAWSDataId{set;get;}
    public String contactName{set;get;}
    public String endUserDAWSDataId{set;get;}
    public String endUserDName{set;get;}
    public NewAndEditUserFaultInfoController(ApexPages.StandardController controller){
        List<String> fieldList = new List<String>(Schema.getGlobalDescribe().get('User_FaultInfo__c').getDescribe().fields.getMap().keyset());  
        // Add fields to controller. This is to avoid the SOQL error in visualforce page
        if(!Test.isRunningTest()){
            controller.addFields(fieldList); // contact lookup
            LookUpOverrideFields.add('UFContact__c');
            LookUpOverrideFields.add('CONTACT__c');
        }
        Init(controller.getRecord());
        //添加项
        PIPL_Input_Account_Error_Msg = label.PIPL_Input_Account_Error_Msg;
        //contact信息（搜索查询query url用）
        staticResourceContact = JSON.serialize(PIHelper.getPIIntegrationInfo('Contact'));
        SObject obj = controller.getRecord();
        system.debug('obj.id=='+obj.id);
        if(obj.Id == null){
            //初始化加载值
            obj.put('OwnerId', UserInfo.getUserId());
        } else {
            User_FaultInfo__c userFaultInfo = [select CONTACT__c, UFContact__c from User_FaultInfo__c where id=:obj.Id];
            System.debug('userFaultInfo: ' + userFaultInfo);
            if (userFaultInfo.CONTACT__c != null) {
                List<Contact> contact = [select AWS_Data_Id__c,Name from Contact where id=:userFaultInfo.CONTACT__c];
                System.debug('contact: ' + contact);
                if(contact.size()>0){
                    if (contact[0].AWS_Data_Id__c != null && contact[0].AWS_Data_Id__c != '') {
                        contactAWSDataId = contact[0].AWS_Data_Id__c;
                    }else {
                        contactName = contact[0].Name;
                    }
                }
            }else {
                contactAWSDataId = '无';
                contactName = '无';
            }
            if (userFaultInfo.UFContact__c != null) {
                List<Contact> contact = [select AWS_Data_Id__c,Name from Contact where id=:userFaultInfo.UFContact__c];
                System.debug('contacts: ' + contact);
                if(contact.size()>0){
                    if (contact[0].AWS_Data_Id__c != null && contact[0].AWS_Data_Id__c != '') {
                        endUserDAWSDataId = contact[0].AWS_Data_Id__c;
                    }else {
                        endUserDName = contact[0].Name;
                    }
                }
            }else {
                endUserDAWSDataId = '无';
                endUserDName = '无';
            }
        }
    }

    
    @RemoteAction
    global static Response saveUserFaultInfo(String sobJson, String transId, Boolean isNew){
        system.debug('JSON Payload:'+sobJson);
        return save(new User_FaultInfo__c(), sobJson, transId, isNew);
    }

}